Jump to content

About styling a list horizontally to create a navigation menu


ke2211975

Recommended Posts

Below is the way showed on W3 tutorial, I curious what the difference to put CSS property: float in "li a" style but "li"? Why?

<!DOCTYPE html>
<html>
<head>
<title></title>
<style>

ul{
list-style-type: none;
padding: 0px;
margin: 0px;
overflow: hidden;
background-color: #333333
}

li{
float: left;
}

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

li a:hover{
background-color: #111111;
}

</style>
</head>

<body>
<h1>nvigation menu</h1>
<p>Use CCS to style the list horizonally, to create navigation menu</p>

<ul>
<li><a href="deafalt.asp"  target="_self" >Home</a></li>
<li><a href="deafalt.asp"  target="_self">News</a></li>
<li><a href="deafalt.asp"  target="_self" >Contact</a></li>
<li><a href="deafalt.asp"  target="_self" >About</a></li>
</ul>

</body>
</html>

 

Link to comment
Share on other sites

<li> elements are block level tags, which means they usually take up the full page width, forcing the next li element to go on the next line.

By floating them, we can take them out of normal page flow, and put them next to each other.

 

Putting the float in li means that we float the li. This is the reason why its horizontal.

Styling li a is actually styling the links and not the container. The container (li) normally goes vertically

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...