CSS 中的曲线时间轴

在本文中,我们将为您的网站构建一个弯曲的时间线,您稍后可以在您的作品集中使用它来显示您的工作历史或您想要的其他内容。让我们首先看看我们正在构什么

CSS 中的曲线时间轴

现在您已经看到了我们要做的事情,所以让我们开始编写代码 

HTML

<div class="timeline">
  <div class="outer">
    <!-- .... card before this -->

    <div class="card">
      <div class="info">
        <h3 class="title">Title 1</h3>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
          eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
          minim veniam, quis nostrud exercitation ullamco laboris nisi ut
          aliquip ex ea commodo consequat.
        </p>
      </div>
    </div>
    <!-- ..... you can add more div with "card" class -->
  </div>
</div>

在 HTML 代码中,timeline类是主容器,outer类是所有卡片的包装器。然后我们card在其中添加数据。

现在让我们看看 CSS

/* Timeline Container */
.timeline {
  background: #212121;
  margin: 20px auto;
  padding: 20px;
}

/* Card container */
.card {
  position: relative;
  max-width: 400px;
}

/* setting padding based on even or odd */
.card:nth-child(odd) {
  padding: 30px 0 30px 30px;
}
.card:nth-child(even) {
  padding: 30px 30px 30px 0;
}
/* Global ::before */
.card::before {
  content: "";
  position: absolute;
  width: 50%;
  border: solid orangered;
}

/* Setting the border of top, bottom, left */
.card:nth-child(odd)::before {
  left: 0px;
  top: -4.5px;
  bottom: -4.5px;
  border-width: 5px 0 5px 5px;
  border-radius: 50px 0 0 50px;
}

/* Setting the top and bottom to "-5px" because earlier it was out of a pixel in mobile devices */
@media only screen and (max-width: 400px) {
  .card:nth-child(odd)::before {
    top: -5px;
    bottom: -5px;
  }
}

/* Setting the border of top, bottom, right */
.card:nth-child(even)::before {
  right: 0;
  top: 0;
  bottom: 0;
  border-width: 5px 5px 5px 0;
  border-radius: 0 50px 50px 0;
}

/* Removing the border if it is the first card */
.card:first-child::before {
  border-top: 0;
  border-top-left-radius: 0;
}

/* Removing the border if it is the last card  and it's odd */
.card:last-child:nth-child(odd)::before {
  border-bottom: 0;
  border-bottom-left-radius: 0;
}

/* Removing the border if it is the last card  and it's even */
.card:last-child:nth-child(even)::before {
  border-bottom: 0;
  border-bottom-right-radius: 0;
}

/* Information about the timeline */
.info {
  display: flex;
  flex-direction: column;
  background: #333;
  color: gray;
  border-radius: 10px;
  padding: 10px;
}

/* Title of the card */
.title {
  color: orangered;
  position: relative;
}

/* Timeline dot  */
.title::before {
  content: "";
  position: absolute;
  width: 10px;
  height: 10px;
  background: white;
  border-radius: 999px;
  border: 3px solid orangered;
}

/* text right if the card is even  */
.card:nth-child(even) > .info > .title {
  text-align: right;
}

/* setting dot to the left if the card is odd */
.card:nth-child(odd) > .info > .title::before {
  left: -45px;
}

/* setting dot to the right if the card is odd */
.card:nth-child(even) > .info > .title::before {
  right: -45px;
}

完整的示例

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS时间轴</title>
  <style>
    :root {
      --primary-color: #212121;
      --background-color: #111;
      --font: sans-serif;
    }

    * {
      margin: 0;
      padding: 0;
    }

    body {
      background: var(--background-color);
      font-family: var(--font);
      display: flex;
      justify-content: center;
    }

    /* Timeline Container */
    .timeline {
      background: var(--primary-color);
      margin: 20px auto;
      padding: 20px;
    }

    /* Card container */
    .card {
      position: relative;
      max-width: 400px;
    }

    /* setting padding based on even or odd */
    .card:nth-child(odd) {
      padding: 30px 0 30px 30px;
    }

    .card:nth-child(even) {
      padding: 30px 30px 30px 0;
    }

    /* Global ::before */
    .card::before {
      content: "";
      position: absolute;
      width: 50%;
      border: solid orangered;
    }

    /* Setting the border of top, bottom, left */
    .card:nth-child(odd)::before {
      left: 0px;
      top: -4.5px;
      bottom: -4.5px;
      border-width: 5px 0 5px 5px;
      border-radius: 50px 0 0 50px;
    }

    /* Setting the top and bottom to "-5px" because earlier it was out of a pixel in mobile devices */
    @media only screen and (max-width: 400px) {
      .card:nth-child(odd)::before {
        top: -5px;
        bottom: -5px;
      }
    }

    /* Setting the border of top, bottom, right */
    .card:nth-child(even)::before {
      right: 0;
      top: 0;
      bottom: 0;
      border-width: 5px 5px 5px 0;
      border-radius: 0 50px 50px 0;
    }

    /* Removing the border if it is the first card */
    .card:first-child::before {
      border-top: 0;
      border-top-left-radius: 0;
    }

    /* Removing the border if it is the last card  and it's odd */
    .card:last-child:nth-child(odd)::before {
      border-bottom: 0;
      border-bottom-left-radius: 0;
    }

    /* Removing the border if it is the last card  and it's even */
    .card:last-child:nth-child(even)::before {
      border-bottom: 0;
      border-bottom-right-radius: 0;
    }

    /* Information about the timeline */
    .info {
      display: flex;
      flex-direction: column;
      background: #333;
      color: gray;
      border-radius: 10px;
      padding: 10px;
    }

    /* Title of the card */
    .title {
      color: orangered;
      position: relative;
    }

    /* Timeline dot  */
    .title::before {
      content: "";
      position: absolute;
      width: 10px;
      height: 10px;
      background: white;
      border-radius: 999px;
      border: 3px solid orangered;
    }

    /* text right if the card is even  */
    .card:nth-child(even)>.info>.title {
      text-align: right;
    }

    /* setting dot to the left if the card is odd */
    .card:nth-child(odd)>.info>.title::before {
      left: -45px;
    }

    /* setting dot to the right if the card is odd */
    .card:nth-child(even)>.info>.title::before {
      right: -45px;
    }
  </style>
</head>

<body>
  <div class="timeline">
    <div class="outer">
      <div class="card">
        <div class="info">
          <h3 class="title">Title 1</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
            dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
            ea commodo consequat. </p>
        </div>
      </div>
      <div class="card">
        <div class="info">
          <h3 class="title">Title 2</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
            dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
            ea commodo consequat. </p>
        </div>
      </div>
      <div class="card">
        <div class="info">
          <h3 class="title">Title 3</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
            dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
            ea commodo consequat. </p>
        </div>
      </div>
      <div class="card">
        <div class="info">
          <h3 class="title">Title 4</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
            dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
            ea commodo consequat. </p>
        </div>
      </div>
      <div class="card">
        <div class="info">
          <h3 class="title">Title 5</h3>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
            dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
            ea commodo consequat. </p>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

结论

这只是开始,您可以将其自定义到下一个级别,也许我会在以后的文章中继续关注,如果您有任何建议,请在下面评论。


文章来源地址https://www.toymoban.com/diary/web/308.html

到此这篇关于CSS 中的曲线时间轴的文章就介绍到这了,更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

原文地址:https://www.toymoban.com/diary/web/308.html

如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用
上一篇 2023年08月28日 17:04
使用WeasyPrint将HTML转换为Python PDF生成
下一篇 2023年08月30日 09:42

相关文章

  • CSS动画中的贝塞尔曲线

    最近在学习CSS动画,其中动画时间函数的部分涉及到了 贝塞尔曲线 的相关知识。对于这部分知识,之前一直没有好好学习过,正好借着这个机会学习下。 首先简单介绍下贝塞尔曲线。 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲

    2024年02月09日
    浏览(60)
  • 【CSS3】CSS3 动画 ⑤ ( 动画速度曲线 | 设置动画步长 | 动画匀速执行 | 动画分 2 步执行 | 使用动画步长实现打字机效果 )

    CSS3 样式中 , 设置 动画速度曲线 的属性是 animation-timing-function 属性 ; animation-timing-function 属性定义了动画从 初始 CSS 样式 变为 结束状态 时 所消耗的时间 ; animation-timing-function 属性常用 属性值 如下 : linear : 动画在整个执行过程中速度都是匀速的 ; ease : 默认属性值 , 动画首先

    2024年02月13日
    浏览(35)
  • 前端食堂技术周刊第 96 期:2023 CSS 状态、Nuxt 3.7、TypeScript 5.2、eBay 性能优化、贝塞尔曲线

    美味值:🌟🌟🌟🌟🌟 口味:冰镇黑乌龙 食堂技术周刊仓库地址:https://github.com/Geekhyt/weekly 大家好,我是童欧巴。欢迎来到前端食堂技术周刊,我们先来看下上周的技术资讯。 Nuxt 3.7 发布,新版 CLI、原生 Web Stream 和 Response、HTML 渲染优化、实验性支持 async context。 pnpm v

    2024年02月11日
    浏览(29)
  • CSS的强大之CSS中的变量

    自定义属性的通俗叫法,就是根据我们的需求定义属性的名称和属性值,CSS一开始就不支持原生变量。所以大家开始选择SCSS,LESS等兼容的 CSS 扩展语言。不过庆幸的是CSS目前也已经支持变量。 属性名 必须使用俩个减号(- -)开头 ,数字、字母、下划线、中划线都是可以的。但是

    2024年02月05日
    浏览(26)
  • 前端练手小项目--自定义时间(html+css+js)

    关于要写这篇文章的原因 是记录在工作上遇到的困难需求, 是希望能给大家提供一些解决问题的思路 接下来我描述这个需求的多样性,难点在哪。 勾选勾选框开始时间与结束时间默认显示昨天与今天。 取消勾选框开始时间与结束时间清空。 选择开始时间,勾选框勾选,结

    2024年02月12日
    浏览(33)
  • 〖大前端 - 基础入门三大核心之CSS篇⑱〗- CSS中的背景

    说明:该文属于 大前端全栈架构白宝书专栏, 目前阶段免费开放 , 购买任意白宝书体系化专栏可加入 TFS-CLUB 私域社区。 福利:除了通过订阅\\\"白宝书系列专栏\\\"加入社区获取 所有 付费专栏的内容之外, 还可以通过加入 星荐官共赢计划 加入私域社区。 当前子专栏 基础入门

    2024年02月01日
    浏览(30)
  • CSS中的栅格布局

    在写前端项目的时候,我之前一直习惯使用flex布局,flex布局写起来比较随心,几乎可以实现任意形式的页面布局。不过自从B占看到某位大佬的grid布局后,发现布局居然还可以这么玩,正好自己在写一个vue3的项目,需要写几个大屏展示方案,用栅格布局实现了一下,发现真

    2024年02月07日
    浏览(29)
  • CSS中的变量

    1.1 变量的声明 声明变量的时候,变量名前面要加两根连词线( -- )。变量名大小写敏感, --header-color 和 --Header-Color 是两个不同变量。 上面代码中, body 选择器里面声明了两个变量: --foo 和 --bar 。使用变量用var()函数。 它们与 color 、 font-size 等正式属性没有什么不同,只

    2024年02月08日
    浏览(28)
  • css中的 :root

    :root 是一个伪类,表示文档根元素,所有主流浏览器均支持 :root 选择器,除了 IE8 及更早的版本。 在:root中声明相当于全局属性,只要当前页面引用了:root segment所在文件,都可以使用var()来引用。 用 – 这样写法加上样式名称 例如:–background 引用:var(–background) var() var()函

    2024年02月16日
    浏览(14)
  • CSS中的定位

    CSS 中的 position 属性用于控制元素在页面中的定位方式,有四个主要的取值,每个取值都会影响元素的布局方式,它们是: static (默认值): 这是所有元素的初始定位方式。在静态定位下,元素会按照它们在文档流中的顺序依次排列,不受 top、right、bottom、left 等属性的影响

    2024年02月07日
    浏览(15)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包