0

Why does long text inside a inline-block shift alignment of the block before?

This is my code:

ul.card { display: block }
li.card { 
          display: inline-block; 
          width: calc(12em - 12px); 
          height: calc(12em - 12px); 
          margin: 0px;
          padding: 6px; 
          background-color: maroon; color: white 
}
<ul class="card">
   <li class="card test"><a href="#">Short text</a></li>
   <li class="card test">Longer text shifts alignment</li>
</ul>

And this is the way it shows up:

enter image description here

which becomes ok with short text:

enter image description here

Codepen here

Why is that? how can I fix this?

Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
simone
  • 4,667
  • 4
  • 25
  • 47

4 Answers4

1

display: inline-block is to make your element inline with others and respect height/width, margin, and padding, but the problem with your case is you set limited width and now the line-height of your content changed to align with your text

If you remove the height, you can observe it like this

enter image description here

For a quick fix, you can set vertical-align: middle;

li.card {
  vertical-align: middle;
  display: inline-block;
  width: calc(12em - 12px);
  height: calc(12em - 12px);
  margin: 0px;
  padding: 6px;
  background-color: maroon;
  color: white
}

a {
  text-decoration: none;
  color: white;
  font-family: sans
}
<ul class="card">
  <li class="card test"><a href="#">Short text</a></li>
  <li class="card test"><a href="#">Short text ok da  da das dasdas das </a></li>
</ul>
Nick Vu
  • 14,512
  • 4
  • 21
  • 31
0
ul.card {
  display: flex;
  gap: 10px;
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Nikhil
  • 60
  • 8
  • Please read "[answer]" and "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man Mar 20 '22 at 23:03
0

It's a vertical aligning issue, caused by the default value. You would need vertical-align: middle; on li.card, like this:

ul.card {
  display: block;
}
li.card {
  vertical-align: middle;
  display: inline-block;
  width: calc(12em - 12px);
  height: calc(12em - 12px);
  margin: 0px;
  padding: 6px;
  background-color: maroon;
  color: white;
}
<ul class="card">
  <li class="card test"><a href="#">Short text</a></li>
  <li class="card test">Longer text shifts alignment</li>
</ul>
Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
0

Try below code:

ul.card { display: flex }
li.card { 
          display: inline-block; 
          width: calc(12em - 12px); 
          height: calc(12em - 12px); 
          margin: 0px;
          padding: 6px; 
          background-color: maroon; color: white 
}
li.card:not(:last-child){
  margin-right:5px;
}
<ul class="card">
   <li class="card test"><a href="#">Short text</a></li>
   <li class="card test">Longer text shifts alignment</li>
</ul>
Yash porwal
  • 301
  • 3
  • 9