when i remove overflow hidden css property from ul then why does ul tag stops showing?

<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
}

li {
float: left;
}

li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}

li a:hover {
background-color: #111111;
}
</style>
</head>
<body>

<h2>Navigation Menu</h2>
<p>In this example, we use CSS to style the list horizontally, to create a navigation menu:</p>

<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>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
}

li {
float: left;
}

li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}

li a:hover {
background-color: #111111;
}
</style>
</head>
<body>

<h2>Navigation Menu</h2>
<p>In this example, we use CSS to style the list horizontally, to create a navigation menu:</p>

<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>
</html>
11 Replies
Jochem
Jochem16mo ago
it's a weird interaction with float: left on the LI's you shouldn't use float for this purpose, it's only valid use is to get text to flow around images in a paragraph the li's are still "visible" on the page btw, they're just white text on a white background. For some reason I don't really care to delve into, overflow: hidden seems to restore the height of the UL, which it doesn't have normally because all it's children are floated this is a prime use case for display: flex instead of float
ul {
list-style-type: none;
margin: 0;
padding: 0;
/* overflow: hidden; */
display: flex;
background-color: #333333;
}

li {
/* float: left; */
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
/* overflow: hidden; */
display: flex;
background-color: #333333;
}

li {
/* float: left; */
}
produces the exact same result as your float
Anonymous DEV
Anonymous DEVOP16mo ago
ik that and i know about flexbox as well but i wana know how this is working
Jochem
Jochem16mo ago
I wouldn't rely on this behavior at all, it doesn't feel like this is necessarily intended
Anonymous DEV
Anonymous DEVOP16mo ago
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
Jochem
Jochem16mo ago
w3schools is terrible
Anonymous DEV
Anonymous DEVOP16mo ago
what do u recomend
Jochem
Jochem16mo ago
it's outdated and gives incorrect and incomplete information mdn just never do this float thing, you basically only need it when you have an article with a small inset like an image in a magazine
get out...!
get out...!16mo ago
reason is simple you are using float, I suggest to use clearfix or display flow-root here is the code with flow root <!DOCTYPE html> <html> <head> <style> ul { display: flow-root; list-style-type: none; margin: 0; padding: 0; background-color: #333333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 16px; text-decoration: none; } li a:hover { background-color: #111111; } </style> </head> <body> <h2>Navigation Menu</h2> <p>In this example, we use CSS to style the list horizontally, to create a navigation menu:</p> <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> </html> would we debate no that ?
Jochem
Jochem16mo ago
What?
get out...!
get out...!16mo ago
I mean, we should have some conversion why not use the float at first place. So it will be helpful for everyone
Jochem
Jochem16mo ago
there's honestly not that much to discuss about it. The use case for float is to cause text to flow around a floated element: https://developer.mozilla.org/en-US/docs/Web/CSS/float It used to be abused to create certain layouts, but it's not necessary to misuse it anymore because display: flex has excellent support basically, it's the same as table layouts. Used to be necessary, but now it's an anti-pattern
Want results from more Discord servers?
Add your server