w3schools example for overflow:hidden [duplicate] - css

This question already has answers here:
CSS overflow:hidden with floats
(5 answers)
Closed 6 years ago.
I already know about overflow:hidden that It hide text when text can't be filled in border. but I don't know why the following coding need it.
This code creates navigation bar but if I erase overflow:hidden, It doesn't work. I want to know about this happening. hidden works for what?
please help.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

That is because you are not clearing float left.Please remove overflow:hidden; from code and add
ul:after{
Content:"";
display:block;
clear:both;
}
This will work properly.

its about float left when child has this property then parent leave child's height property so then ul tag invisible becouse that have no height
so us like this
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
Hope this helps you
THANKS

Actually it works well if you give ul a height.
Here the overflow is well explained: W3C
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
height:46px;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

Related

Navbar padding issue

So I have this issue where my Navbar splits itself based on the specified padding. Easier to see what I mean below.
As you can see I have an extra blank menu item which after some time I was able to narrow down to it being caused by the padding.
Above is with 0 padding. How can I keep the Navbar height along with fixing the menu?
li a {
display: block;
text-align: center;
font: Verdana;
font-size: 16px;
color: #EAE0D2;
text-decoration: none;
padding: 0;
}
Specifically:
padding: 20px; (image1) padding: 0px; (image 2)
Snippet
#menu {
display: flex;
margin: 0;
width: 1080px;
margin-top: 5%;
list-style-type: none;
background: linear-gradient(#3E3E3E, #2B2B2B);
overflow: hidden;
}
li {
flex: 1;
border-right: 1px solid #232323;
}
li:last-child {
border: none;
}
li a {
display: block;
text-align: center;
font: Verdana;
font-size: 16px;
color: #EAE0D2;
text-decoration: none;
padding: 0;
}
li a:hover {
background: linear-gradient(#404040, #3E3E3E);
}
<nav>
<ul id="menu">
<li class="active">HOME</li>
<li>GALLERY</li>
<li>ART</li>
<li>CONTACT</li>
</ul>
</nav>
Want was your problem? (My assumption)
First check this CODEPEN
You were assigning padding property to li a:hover, instead of li a.
As it's possible to achieve the desired result by adding padding to li a and other way is by assigning line-height to either li/#menu.
#menu {
display: flex;
margin: 0;
width: 1080px;
margin-top: 5%;
list-style-type: none;
background: linear-gradient(#3E3E3E, #2B2B2B);
overflow: hidden;
}
li {
flex: 1;
border-right: 1px solid #232323;
}
li:last-child {
border: none;
}
li a {
display: block;
text-align: center;
font: Verdana;
font-size: 16px;
color: #EAE0D2;
text-decoration: none;
padding: 20px;
}
li a:hover {
background: linear-gradient(#404040, #3E3E3E);
}
<nav>
<ul id="menu">
<li class="active">HOME</li>
<li>GALLERY</li>
<li>ART</li>
<li>CONTACT</li>
</ul>
</nav>

Why do I have to add "overflow:hidden" to make the navigation bar visible on the page? [duplicate]

This question already has answers here:
Why doesn't the height of a container element increase if it contains floated elements?
(7 answers)
Closed 5 years ago.
I'm a newbie to css and have been struggling with the following problem of my code for the whole morning. I would really appreciate it if someone can help me find out the reason.
Why does the navigation bar totally disappear from the page if I don't set the "overflow" of "ul.navBar" to "hidden"?
<html>
<head>
<style>
ul.navBar {
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
background-color: #4277f4;
cursor: pointer;
}
li {
float: left;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 20px;
text-decoration: none;
}
li:hover {
background-color: #A2AEB3;
}
.dropDownContent {
display: none;
position: absolute;
background-color: #7DC9E3;
width: 150px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
text-decoration: none;
}
.dropDownContent a {
color: white;
display: block;
}
.dropDownContent a:hover{
background-color: #4A96B0;
}
li.dropDownBtn:hover .dropDownContent{
display: block;
}
</style>
</head>
<body>
<ul class="navBar">
<li>Home</li>
<li class="dropDownBtn">Products
<div class="dropDownContent">
Product1
Product2
Product3
</div>
</li>
<li>Service</li>
<li>Contact</li>
</body>
</html>
Here's the code page for this navigation bar.
Why does the navigation bar totally disappear from the page if I don't set the overflow of ul.navBar to hidden?
This is happening because the child elements of .navBar are being floated. Floated elements are taken out of the normal document flow and do not take up space. Because the children take up no space .navBar has no height .
Adding overflow: hidden; triggers a new block formatting context that prevents .navBar from "collapsing" when it has floated children.
Some people will suggest using display: inline-block;. Use with caution as each element will have white space around it that will make them larger than you think. Especially when using percentage widths.
Example:
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
li {
width: 33.3333%;
}
.inline li {
display: inline-block;
background-color: gold;
}
.float li {
float: left;
background-color: indianred;
}
.flex {
clear: left;
display: flex;
background-color: skyblue;
}
<ul class="inline">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<ul class="float">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<ul class="flex">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
Here's some options on how to handle the white space if you chose the inline-block route.
Floated elements (your li in this case) have a height of 0. So, essentially, your ul element is 0 pixels tall.
Adding display: inline-block to your li elements allow for this to be corrected. Therefore, the overflow style for your ul is not required.
ul.navBar {
list-style-type: none;
margin: 0px;
padding:0px;
background-color: #4277f4;
cursor: pointer;
}
li {
display:inline-block;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 20px;
text-decoration: none;
}
li:hover {
background-color: #A2AEB3;
}
.dropDownContent {
display: none;
position: absolute;
background-color: #7DC9E3;
width: 150px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
text-decoration: none;
}
.dropDownContent a {
color: white;
display: block;
}
.dropDownContent a:hover{
background-color: #4A96B0;
}
li.dropDownBtn:hover .dropDownContent{
display: block;
}
<body>
<ul class="navBar">
<li>Home</li>
<li class="dropDownBtn">Products
<div class="dropDownContent">
Product1
Product2
Product3
</div>
</li>
<li>Service</li>
<li>Contact</li>
</ul>
</body>
</html>
The issue is that you are floating your li elements which causes the ul to not have a height. Try using display:inline-block; instead.
ul.navBar {
list-style-type: none;
margin: 0px;
padding: 0px;
background-color: #4277f4;
cursor: pointer;
}
li {
display:inline-block;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 20px;
text-decoration: none;
}
li:hover {
background-color: #A2AEB3;
}
.dropDownContent {
display: none;
position: absolute;
background-color: #7DC9E3;
width: 150px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
text-decoration: none;
}
.dropDownContent a {
color: white;
display: block;
}
.dropDownContent a:hover{
background-color: #4A96B0;
}
li.dropDownBtn:hover .dropDownContent{
display: block;
}
<ul class="navBar">
<li>Home</li>
<li class="dropDownBtn">Products
<div class="dropDownContent">
Product1
Product2
Product3
</div>
</li>
<li>Service</li>
<li>Contact</li>

Center the navi bar items while keeping the background color full-width

I'm new to CSS and working on a navi bar for my page but I can't center all the items of the bar while keeping the bar full-width. Did I miss something?
ul {
list-style-type: none;
text-transform: uppercase;
text-align: center;
background-color: #333;
overflow: hidden;
width: 100%;
z-index: 1;
}
.naviMenu ul li {
letter-spacing: 0.05em;
color: white;
float: left;
padding: 14px 16px;
}
.naviMenu ul li a {
color: white;
text-decoration: none;
}
.naviMenu ul li:hover {
background-color: #111;
}
<div class="naviMenu">
<ul>
<li><div id="homePage">Home</div></li>
<li>About</li>
<li>Text</li>
<li><a href="photo.html">Photo</li>
<li><a href="special.html">Special Project</li>
<li><a href="contact.html">Contact</li>
</ul>
Screenshot
Thank you.
Your markup is a bit broken with missing closing tags and divs instead of links. But what you're doing is fairly straight forward - you just need to tweak things a bit.
Mark up:
<div class="naviMenu">
<ul>
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
The main problem is the float left you have on the li element currently.
CSS:
.naviMenu ul {
width: 100%; /* make it full width */
background: #333;
text-align: center; /* align it center */
list-style: none;
}
.naviMenu ul li {
display: inline-block; /* inline-block respects align center. allows padding */
letter-spacing: 0.05em;
padding: 14px 16px;
}
.naviMenu ul li a {
color: #fff;
text-decoration: none;
text-transform: uppercase;
}
Codepen: http://codepen.io/anon/pen/ygLZXK
Here you go.
* {
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
text-transform: uppercase;
text-align: center;
background-color: #333;
overflow: hidden;
width: 100%;
z-index: 1;
}
.naviMenu ul li {
letter-spacing: 0.05em;
color: white;
padding: 14px 16px;
display: inline-block;
font-size: 14px;
}
.naviMenu ul li a {
color: white;
text-decoration: none;
}
.naviMenu ul li:hover {
background-color: #111;
}
<div class="naviMenu">
<ul>
<li>
<div id="homePage">Home</div>
</li>
<li>About
</li>
<li>Text</li>
<li>Photo
</li>
<li>Special Project
</li>
<li>Contact
</li>
</ul>
</div>

horizontal menu with auto width and same dimension of the tabs

I'd like to create a horizontal menu with auto(100%) width.
MY CSS
.horizontal {
width: 100%;
}
.horizontal ul {
display: table;
width: 100%
}
.horizontal li {
display: table-cell;
}
.horizontal li a {
height: 30px;
display: block;
border: 1px solid #aaa;
text-align: center;
padding: 4px;
margin: 0 2px;
background: #ccc;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
MY HTML
<div class="horizontal">
<ul>
<li>Long Item</li>
<li>Short Item</li>
<li>Really Long Item</li>
<li>Nav Item</li>
<li>Some Other Link</li>
</ul>
</div>
My problem is that I would like that the "tabs" (a elements) have the same size maintaining 100% of width
It would be possible to do so? How could I do this? Thanks
Give li width in percentage.
.horizontal li {
display: table-cell;
width:20%; // because you have 5 anchors.
}
You can do this by simply adding something like this:
li{width:400px;}
If you have or will have more li's on the page, you can either give them a class, give the ul a class and use:
ul.header > li{width:400px;}
or just use:
.horizontal > ul > li{width:400px;}
Not sure if you are searching for the following, but u can try the following:
.horizontal ul {
height: 40px;
padding: 0 100px;
font-weight: 500;
line-height: 40px;
text-transform: uppercase;
text-align: justify;
background: #222;
}
.horizontal ul li{
display: inline-block;
}
.navigation ul li a {
text-decoration: none;
color: #fff;
}

CSS being ignored in IE & FFox but not Chrome

i am styling a navigation menu using the following css. I have found that it works perfectly in chrome but absolutely none of the css is beign applied in firefox or IE.
Is there something obvious that i havent done or have done here?
<style type="text/css">
#ddm {
margin: 0;
padding: 0;
}
#ddm li {
float: left;
list-style: none;
font: 8pt Tahoma, Geneva, sans-serif;
}
#ddm li a {
display: block;
padding: 5px 12px;
text-decoration: none;
border-right: 1px solid white;
width: 70px;
white-space: nowrap;
color:Red;
border: none;
}
#ddm li a:hover {
color:White;
background-color:#444444;
}
#ddm li ul {
margin: 0;
padding: 0;
position: absolute;
visibility: hidden;
}
#ddm li ul li {
float: none;
display: inline
}
#ddm li ul li a { width: auto; }
#ddm li ul li a:hover { }
</style>
<ul id="ddm">
<li>
<ul>
<li>test 1</li>
<li>test 2</li>
</ul>
</li>
</ul>
Both of you are correct. There must be something else outside of the markup that is interfering somehow with IE and FF. Sorry for any time wasting.
The last line display: inline is missing a ; at the end which is causing your problem

Resources