Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings
I want to make a navigation menu. Using the code below:
CSS
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #a1a1a1;
}

everything works fine, but if I remove:
CSS
overflow: hidden;

the menu background disapears. How
CSS
overflow: hidden;
logically works in this code segment?

Here is the fiddle address:
JSFiddle

What I have tried:

I can't understand the logic behind
CSS
overflow: hidden;
.
Posted
Updated 14-Feb-19 3:31am
Comments
enhzflep 13-Feb-19 15:39pm    
Mate, it's impossible to give you a definitive answer in this case to the question "why does my code produce this result?" when you don't provide the code...

Basically, the UL element has a size and position calculated by the browser. The children of that UL element have combined sizes that exceed that of the UL. As a result, they can be said to overflow their parent container. When you tell the parent to hide any content that exceeds its boundaries (overflow: hidden) the result is that stuff disappears. simples.
Ali Majed HA 14-Feb-19 0:53am    
Hey my friend, I have provided the full code in jsfiddle website with the link in question.
Thanks for your comment and explanation.

1 solution

It's an old technique to ensure that the parent element's height expands to include the floated children. Without it, the <ul>'s height collapses to zero, so the background colour has no effect.

Techniques for Clearing Floats | All About Floats | CSS-Tricks[^]
CSS: Clearing Floats with Overflow - Web Designer Wall - Design Trends and Tutorials[^]

For modern browsers, you could use Flexbox as an alternative. Remove the li { float: left; } rule, and replace overflow:hidden; with display:flex; on the ul rule:
Updated Fiddle[^]
A Complete Guide to Flexbox | CSS-Tricks[^]
Can I use... Flexbox[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900