1

I want the dash to be vertically centered with the text on the right and I want them to be 10px apart. But when I'm using flex, the text on the right wraps normally, but takes more than its content's actual size, and now the gap between the two spans is too wide. Is there an elegant solution to this ?

ul {
  width: 300px;
  list-style: none;
  text-align: center;
  font-size: 20px;
}

li {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

span.dash {
  margin-right: 10px;
}
<ul>
  <li><span class="dash">-</span><span>+ 10€ pour séance à domicile (déplacement inclus)</span></li>
</ul>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
The Draft
  • 37
  • 8
  • 1
    *"Is there an elegant solution to this?"* Not with CSS alone. In CSS / HTML a container has no idea when its content wraps. So it doesn't adjust its size accordingly. https://stackoverflow.com/q/37406353/3597276 – Michael Benjamin Aug 01 '19 at 12:33
  • That might be it, I managed to do what I want with absolute positioning but it's not super elegant – The Draft Aug 01 '19 at 13:06
  • Right. Not elegant. But hacks exist for a reason. +1 for your answer. – Michael Benjamin Aug 01 '19 at 13:08

3 Answers3

1

I found a way to do it, but I'm not too happy with it. Here it is :

ul {
  width: 300px;
  list-style: none;
  text-align: center;
  font-size: 20px;
}

li {
  position: relative;
}

span.dash {
  position: absolute;
  top: 50%;
  transform: translateX(-300%) translateY(-50%);
}
<ul>
  <li><span class="dash">-</span><span>+ 10€ pour séance à domicile (déplacement inclus)</span></li>
</ul>
The Draft
  • 37
  • 8
0

Use word-break: break-all; working fine

ul {
  width: 300px;
  list-style: none;
  text-align: center;
  font-size: 20px;
}

li {
  position: relative;
  padding-left:15px;
}
span{
  display: block;
  word-break: break-all;
}
span.dash {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translateY(-50%);
}
<ul>
  <li><span class="dash">-</span><span>+ 10€ pour séance à domicile (déplacement inclus) 10€ pour séance à domicile (déplacement inclus) </span></li>
</ul>
Lalji Tadhani
  • 14,041
  • 3
  • 23
  • 40
0

To align to the right you need to put the display: flex property on the parent element.

If I understand your question, the solution is: jsfiddle.net/gabrielpito/y6jog29x/