Peter Solvien Peter Solvien - 1 month ago 8
CSS Question

Css Timeline text alignment

folks,

I am trying to build a timeline with css and I have issue with those red circles. I would like to add some information which should be centered text vertically and horizontally inside the red circles (one or 2 words maximum) , may I ask for your help ?



.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 -->




Answer Source

Use pseudo selector :before then add text using content or you could even add a new div and then align that using position absolute to center of already styled pseudo selector :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 -->