3

I am trying to display navigation items (horizontally) in a blue colored ribbon. Somehow, the background-color property is not getting applied to the ul element. I tried to put it inside a div element with background as blue. Still, it doesn't work

Html snippet as,

<div style="background-color:blue;">
  <ul style="list-style-type:none;background-color:blue;">
    <li style="float:left;margin-right:10px;">cassandra</li>
    <li style="float:left;margin-right:10px;">mongodb</li>
    <li style="float:left;">couchdb</li>
  </ul>
</div>

4 Answers4

5

Why is my background color not showing if I have display: inline?

This is the same issue as this. The div is coming out at height 0, same as the list as the float doesn't take up any space.

If you specify the height or tell them to display:inline-block it'll work.

Fiddle: http://jsfiddle.net/7vp4vz6f/

Community
  • 1
  • 1
Patrick Eaton
  • 706
  • 3
  • 11
3

You are using float property for the li elements, so you need to apply some sort of clearfix for container to adjust it's size according to the content size. You can try with the overflow CSS property:

body > div { overflow: auto}

JSFiddle

potashin
  • 44,205
  • 11
  • 83
  • 107
1
<div style="background-color: blue; overflow: hidden;">
  <ul style="list-style-type:none;background-color:blue;">
    <li style="float:left;margin-right:10px;">cassandra</li>
    <li style="float:left;margin-right:10px;">mongodb</li>
    <li style="float:left;">couchdb</li>
  </ul>
</div>
  • 1
    Thank you for posting an answer - please try to add a short explanation of what you have done and why so users can learn from your contribution. – fraggley May 13 '20 at 09:52
0

Your elements have no width and height, that's why. Also, consider using a stylesheet, one of the many advantages is that you don't run into such issues very often.

lucian
  • 623
  • 10
  • 21