Boris Kozarac Boris Kozarac - 5 months ago 13
HTML Question

Align center on flex item without messing :after

How can I vertically align

p
to center without messing
:after
border?
Using
align-items: center;
on
p
doesn't work, and using it on
.row-flex
messes border

https://jsfiddle.net/rg7rm47w/

<div class="row-flex">

<img src="http://placehold.it/100x100">
<p>
Title
<span class="tag">Short tag</span>
</p>

</div>

.row-flex {
display: flex;
}

img {
flex: 1;
}

p {
flex: 2;
padding: 20px;
position: relative;
border-bottom: 1px solid #ccc;
font-size: 11px;
margin: 0;
}

p:after {
width: 50%;
background: #333;
position: absolute;
content: '';
height: 2px;
bottom: 0;
left: 0;
}

span {
display: block;
}

Answer

Make the p tag ` flex-container column and center as usual.

JSFiddle demo

.row-flex {
  display: flex;
}
img {
  flex: 1;
}
p {
  flex: 2;
  padding: 20px;
  position: relative;
  border-bottom: 1px solid #ccc;
  font-size: 11px;
  margin: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
p:after {
  width: 50%;
  background: #333;
  position: absolute;
  content: '';
  height: 2px;
  bottom: 0;
  left: 0;
}
span {
  display: block;
}
<div class="row-flex">

  <img src="http://placehold.it/100x100">
  <p>
    Title
    <span class="tag">Short tag</span>
  </p>

  <img src="http://placehold.it/100x100">
  <p>
    Title
    <span class="tag">Short tag</span>
  </p>

</div>

<div class="row-flex">

  <img src="http://placehold.it/100x100">
  <p>
    Title
    <span class="parts">Short tag</span>
  </p>

  <img src="http://placehold.it/100x100">
  <p>
    Title
    <span class="parts">Short tag</span>
  </p>

</div>

Comments