4

I have an unordered list where the list items are displayed horizontally. Each list item should contain some text and images. When I add content to them the list item is no longer aligned.

ul {
  list-style-type: none;
  width: 500px;
  height: 500px;
  border: 1px solid black;
}
li {
  border: 1px solid blue;
  display: inline-block;
  height: 100px;
  width: 100px;
}
<ul>
  <li>222</li>
  <li></li>
  <li></li>
</ul>

Jsbin: https://jsbin.com/fobopayaco/edit?html,css,output

Is there som css I can add to fix this? Or should I change my markup?

6 Answers 6

7

You can just set vertical-align to top for example

ul {
  list-style-type: none;
  width: 500px;
  height: 500px;
  border: 1px solid black;
}
li {
  border: 1px solid blue;
  display: inline-block;
  vertical-align: top;
  height: 100px;
  width: 100px;
}
<ul>
  <li>222</li>
  <li></li>
  <li></li>
</ul>

Sign up to request clarification or add additional context in comments.

Comments

3

By adding text, you're seeing the effects of the default vertical-align property: your list-items are being aligned to the baseline.

If you want them all to be aligned in the same spot, specify a vertical-align property for all of them:

li{
  border:1px solid blue;
  display:inline-block;
  height:100px;
  vertical-align: top;
  width:100px;
}

Comments

1

The list just doesn't like empty items. If you need an item to be empty for some reason use <li>&nbsp;</li>, otherwise once you add content to all items normalcy will return.

Comments

0

Try float:left on li instead of display:inline-block

Comments

0

Or you can use float to left for every <li>

ul {
  list-style-type: none;
  width: 500px;
  height: 500px;
  border: 1px solid black;
}
li {
  border: 1px solid blue;
  display: inline-block;
  height: 100px;
  width: 100px;
  float: left;
}
<ul>
  <li>222</li>
  <li></li>
  <li></li>
</ul>

Comments

0

you can add float :left or display:inline in your css

    li{
      border:1px solid blue;
      display:inline-block;
      height:100px;
      width:100px;  
     display:inline;
    }

and here is working fiddle

https://jsfiddle.net/5dh2ak3a/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.