html - Css 时间轴文本对齐

标签 html css

friend 们,

我正在尝试使用 CSS 构建时间轴,但我对那些红色圆圈有疑问。我想添加一些信息,这些信息应该是在红色圆圈内垂直和水平居中的文本(最多 1 或 2 个字),请问您可以帮忙吗?

.timeline-image {
  text-align: center;
  margin: 0 auto;
  width: 390px;
  height: 390px;
  background: rgba(203, 190, 181, 0.3);
  border-radius: 50%;
}

.timeline-image img {
  position: relative;
  z-index: 99999;
  top: -30px;
}

.timeline ul li {
  list-style: none;
  position: relative;
  width: 1px;
  margin: 0 auto;
  padding-top: 10px;
  background: #b2b2b2;
}

.timeline ul li:after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: 87px;
  height: 87px;
  border-radius: 50%;
  background: red;
  top: 76%;
}

.timeline ul li div {
  position: relative;
  bottom: 0;
  left: 100px;
  top: 120px;
  width: 400px;
  padding: 15px;
  background: transparent;
}

.timeline ul li:nth-child(even) div {
  left: -439px;
}

.timeline ul li:nth-child(even) div:before {
  right: -15px;
  border-width: 8px 0px 8px 16px;
  border-color: transparent transparent transparent #F45B69;
}

.timeline h3 {
  font-family: "Open-Sans", sans-serif;
  font-size: 34px;
  margin-bottom: 20px;
}

.timeline p {
  font-family: "Open-Sans", sans-serif;
  font-size: 16px;
  color: #555;
  margin-bottom: 32px;
}

.timeline ul li a {
  font-family: "Open-Sans-Bold", sans-serif;
  font-size: 16px;
  color: #000;
  text-decoration: none;
}

.timeline ul li a:hover {
  opacity: 0.7;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<section class="timeline">
  <div class="container">

  </div>
  <ul>
    <li>
      <div>
        <img src="images/timeline1.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p>
        <a href="#" class="read-more">lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>

      </div>
    </li>
    <li>
      <div>
        <img src="images/timeline2.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
        <a href="#" class="read-more">Lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>

      </div>
    </li>
    <li>
      <div>
        <img src="images/timeline3.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
        <a href="#" class="read-more">lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>

      </div>
    </li>
    <li>
      <div>
        <img src="images/timeline4.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
        <a href="#" class="read-more">lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>

      </div>
    </li>
  </ul>
</section>
<!-- timeline -->

最佳答案

使用 伪选择器 :before 然后使用内容添加文本,或者您甚至可以添加一个新的 div 然后使用位置 absolute 将其对齐到已设置样式的伪选择器的中心 :after

.timeline ul li:before {
  content: "Hey";
  position: absolute;
  width: 30px;
  height: 30px;
  top: 85%;
  left: -18px;
  z-index: 9;
  color: white;
  font-size: 24px;
}

.timeline-image {
  text-align: center;
  margin: 0 auto;
  width: 390px;
  height: 390px;
  background: rgba(203, 190, 181, 0.3);
  border-radius: 50%;
  background: blue;
}

.timeline-image img {
  position: relative;
  z-index: 99999;
  top: -30px;
}

.timeline ul li {
  list-style: none;
  position: relative;
  width: 1px;
  margin: 0 auto;
  padding-top: 10px;
  background: #b2b2b2;
  z-index: -1;
}

.timeline ul li:after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: 87px;
  height: 87px;
  border-radius: 50%;
  background: red;
  z-index: 8;
}

.timeline ul li:before {
  content: "Hey";
  position: absolute;
  width: 30px;
  height: 30px;
  top: 85%;
  left: -18px;
  z-index: 9;
  color: white;
  font-size: 24px;
}

.timeline ul li div {
  position: relative;
  bottom: 0;
  left: 100px;
  top: 120px;
  width: 400px;
  padding: 15px;
}

.timeline ul li:nth-child(even) div {
  left: -439px;
}

.timeline ul li:nth-child(even) div:before {
  right: -15px;
  border-width: 8px 0px 8px 16px;
  border-color: transparent transparent transparent #F45B69;
}

.timeline h3 {
  font-family: "Open-Sans", sans-serif;
  font-size: 34px;
  margin-bottom: 20px;
}

.timeline p {
  font-family: "Open-Sans", sans-serif;
  font-size: 16px;
  color: #555;
  margin-bottom: 32px;
}

.timeline ul li a {
  font-family: "Open-Sans-Bold", sans-serif;
  font-size: 16px;
  color: #000;
  text-decoration: none;
}

.timeline ul li a:hover {
  opacity: 0.7;
}
<section class="timeline">
  <div class="container">

  </div>
  <ul>
    <li>
      <div>
        <img src="images/timeline1.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p>
        <a href="#" class="read-more">lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>
      </div>
    </li>
    <li>
      <div>
        <img src="images/timeline2.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
        <a href="#" class="read-more">Lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>

      </div>
    </li>
    <li>
      <div>
        <img src="images/timeline3.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
        <a href="#" class="read-more">lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>

      </div>
    </li>
    <li>
      <div>
        <img src="images/timeline4.jpg" width="324" height="125" title="someText" alt="someText">
        <h3>Lorem</h3>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
        <a href="#" class="read-more">lorem
								<img src="images/example-arrow.png" title="someText" alt="someText">
							</a>

      </div>
    </li>
  </ul>
</section>
<!-- timeline -->

关于html - Css 时间轴文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44871828/

相关文章:

html - 以缩放为中心的 Bootstrap

javascript - Jquery 脚本与按钮 div 冲突

html - 更改 PHP Echo 输出的字体大小

javascript - 为什么渲染树不包含绝对定位的元素?

JavaScript - 在 Canvas 上绘图时鼠标位置错误

php - 关于 CSSTidy 的问题

html - 当父元素具有负 z-index 时标签不可点击

javascript - 输入类型文件大小在 Linux/Windows 中有所不同

html - CSS重叠问题

javascript - HTML5必需的只读输入不发布