Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "overflow" do for this <ul> tag?

Tags:

html

css

I was creating this header menu using <ul> and I wanted to stretch it to all top of the page. I first used width:100% but it didn't work.Then I saw a similar example somewhere , it had used overflow:hidden so when I used that it streched the list to the end.I wanted to know what does overflow do in here and why width:100% can not do the similar action?

ul {
	list-style-type: none;
	padding: 0;
	margin: 0;
	overflow: hidden;
	background-color: black;
}
li {
	
	background-color: black;
	color: red;
	
	float: left;
}
li a{
	display: inline-block;
	color: white;
	text-decoration:none;
	padding: 20px;
	text-align: center;
	
}
li a:hover{
	background-color:red;
}
<!DOCTYPE html>
<html lang="fa">
	<head>
		<link rel="stylesheet" type="text/css" href="menu.css"> 
	</head>
     <body>
	<ul>
		 <li><a target="_blank" href="http://www.google.com">Google</a></li>
		<li><a target="_blank" href="http://www.yahoo.com">Yahoo!</a></li>
		 <li><a target="_blank" href="http://www.Amazon.com">Amazon</a></li>
		 
		 </ul>
	
		
	</body>
</html>
	
like image 720
Reza Avatar asked Sep 07 '16 13:09

Reza


2 Answers

using overflow makes element stretch to match its children when they have set float property - it's basically one of ways to implement clearfix

like image 139
pwolaq Avatar answered Oct 21 '22 18:10

pwolaq


Float property in li affect the parent ul width 100%. so It not worked. Try this below code.

ul {
	list-style-type: none;
	padding: 0;
	background-color: black;
    width:100%;
    text-align:center;
}
li {
	background-color: black;
	color: red;
	width:33.3333%;
	float: left;
    
}
li a{
	display: inline-block;
	color: white;
	text-decoration:none;
	padding: 20px;
}
li:hover{
	background-color:red;
}
<!DOCTYPE html>
<html lang="fa">
	<head>
		<link rel="stylesheet" type="text/css" href="menu.css"> 
	</head>
     <body>
	<ul>
		 <li><a target="_blank" href="http://www.google.com">Google</a></li>
		<li><a target="_blank" href="http://www.yahoo.com">Yahoo!</a></li>
		 <li><a target="_blank" href="http://www.Amazon.com">Amazon</a></li>
		 
		 </ul>
	
	</body>
</html>
like image 24
Dhaarani Avatar answered Oct 21 '22 20:10

Dhaarani