Reputation: 4650
I'm really new to CSS, so I decided to try to recreate this page on my own.
But I came across the following problem:
HTML
<!-- Header -->
<header>
<h1>w3schools.com</h1>
<img src="Images/w3schoolslogo.gif">
<form method="post" action="w3_schools.html">
<input type="text" name="search" placeholder="Search w3schools.com"/>
<input type="submit" value="Search">
</form>
</header>
<!---->
<!-- Top navigation -->
<nav class="top_navigation">
<ul>
<li><a href="">Home</a></li>
<li><a href="">HTML</a></li>
<li><a href="">CSS</a></li>
<li><a href="">Javascript</a></li>
<li><a href="">JQuery</a></li>
<li><a href="">XML</a></li>
<li><a href="">ASP.NET</a></li>
<li><a href="">PHP</a></li>
<li><a href="">SQL</a></li>
<li><a href="">More...</a></li>
</ul>
<aside>
<ul>
<li><a href="">References</a></li>
<li><a href="">Examples</a></li>
<li><a href="">Forum</a></li>
<li><a href="">About</a></li>
</ul>
</aside>
</nav>
<!---->
CSS
ul {
list-style-type: none;
margin: 0px;
padding: 0px;
}
/* Header */
header {
position: relative;
}
header h1 {
display: none;
}
header form {
position: absolute;
bottom: 0px;
right: 0px;
}
header form input[type="text"] {
border: 1px solid #D3D3D3;
}
/* Top navigation */
.top_navigation {
clear: both;
text-transform: uppercase;
margin-top: 10px;
font-size: 80%;
background-color: green;
}
.top_navigation a:link, .top_navigation a:visited{
color: #555555;
text-decoration: none;
}
.top_navigation a:hover {
color: red;
}
.top_navigation li {
float: left;
margin-left: 10px;
}
.top_navigation aside {
float:right;
}
The problem is that the background of the nav element is not green. I suppose that it has something with the float, but I have no idea how to fix. I've tried putting clear:both
almost everywhere, but nothing green was there. :(
Can you please help me? Thanks in advance!
Upvotes: 1
Views: 1245
Reputation: 27321
Your LI elements are floated so the height of the UL bg is 0. So you have a couple options
A CSS 'Clear Fix' is one way to solve this:
CSS:
.cf:before, .cf:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.cf:after {
clear: both;
}
.cf {
*zoom: 1;
}
HTML:
<ul class="cf">
<li><a href="">References</a></li>
<li><a href="">Examples</a></li>
<li><a href="">Forum</a></li>
<li><a href="">About</a></li
</ul>
Example: http://jsfiddle.net/8UDVw/1/
Or you can manually set the height of the UL if you know the height of the LI's are not going to change:
.top_navigation ul{height:30px;}
Upvotes: 1
Reputation: 12190
You don't need additional DIV for this. Add overflow: hidden;
to .top_navigation
.top_navigation {
text-transform: uppercase;
margin-top: 10px;
font-size: 70%;
background-color: green; overflow: hidden;
}
Upvotes: 2
Reputation: 138027
You need a clear:both
element at the end of the <nav>
, to make it resize to the floated elements:
<nav class="top_navigation">
<ul>
...
</ul>
<aside>
...
</aside>
<div style="clear:both;"></div>
</nav>
(this is just an example, of course, don't use inline styles for that)
Example: http://jsfiddle.net/7gWEu/
Upvotes: 1