I am learning CSS through the lessons on the w3schools.com website.
The example below works properly. There is a div element with a class of "navbar"
The overflow property has a value of "hidden". But if I remove the property, the navbar
element seems to disappear, although the elements inside it are seen if you hover over
them.
My question is: Why is "overflow:hidden" needed for div.navbar? It seems that all its
content fits inside the div and I don’t see any overflow.
Thank you.
Example
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
#div2 img {
width: 100%;
display: block;
}
</style>
</head>
<body style="background-color:white;">
<div id="n1" class="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<div class="dropdown">
<button class="dropbtn">Dropdown
</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
<div id="div2">
<img src="BNB-Stonehenge.jpg" alt="Descriptive Image Alt Text">
</div>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</body>
</html>
3
Answers
overflow: hidden
is needed because the menu is formatted using floats. When you removeoverflow: hidden
, the menu collapses to zero height because the floats don’t count as flow content. I don’t understand all the technicalities, but basicallyoverflow: hidden
causes a new block formatting context to be created. Refer to this and this.This is not a great way to build a navigation menu. A flexbox is cleaner and more robust. A quick change to
display: flex
on the navbar means you can remove the floats and theoverflow: hidden
.Fortunately, the parts of this example which relate to the drop-down sections of the menu are still valid and clever, so this is a worthwhile tutorial. But they really do need to update it to remove those floats.
everywhere you use float, you should clear that after it.
here you can add this css:
overflow: hidden; is a CSS property and value combination that is used for a specific purpose in web design and layout. It is primarily used to control how overflowing content within an element is displayed. Here are some common use cases for overflow: hidden;
Hope it’s helpful to you. For more you can visit this link https://www.w3schools.com/css/css_overflow.asp