Jump to content

Why we use overflow: hidden; and why background-color don't work without it?


Nastusya

Recommended Posts

Hello.

I don't understand, why ul element has a style overflow: hidden? And if I delete it, then background-color won't work, why? Could you tell please how does it work?

Thank you in advance for your help.

This example is from here:
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_lists_menu

<head>
<style>
ul {
  list-style-type: none;
  overflow: hidden;
  background-color: #333333;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  text-decoration: none;
}
</style>
</head>
<body>

<ul>
  <li><a href="#home">Home</a></li>
  <li><a href="#news">News</a></li>
  <li><a href="#contact">Contact</a></li>
  <li><a href="#about">About</a></li>
</ul>

</body>

 

Edited by Nastusya
Link to comment
Share on other sites

Elements with "float" are ignored when calculating the height of their parent. The parent will not expand to contain these elements. Overflow forces the parent to take the floated elements into account.

Other solutions involve adding "clear:both" to an element beneath the floated elements. This is sometimes done with the ::after pseudo-element like this:

ul::after {
  content: "";
  display: table;
  clear: both;
}

 

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...