I am currently learning how to create a web template without using tables, using HTML and CSS. I have some questions regarding the positioning of elements in my template.
Here is an image of how my final template should look:
Next, I need to position the horizontal main menu (the green one inside the header).
The tutorial suggests using absolute positioning to move the nav div containing the navigation menu to the right.
To achieve this, the tutorial recommends the following steps:
- Set the position of the parent div as relative and specify the exact height of this parent div
- Use absolute positioning for the nav div
Therefore, for my template, the HTML code should look something like this:
<div id="header"> <!-- HEADER -->
<div id="logo"> <!-- My Logo -->
<h1><a href="#">My website</a></h1>
<p id="slogan">
Welcome to my website
</p>
</div>
<div id="nav">
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Portfolio</a>
</li>
<li>
<a href="#">Blog</a>
</li>
<li>
<a href="#">Contact</a>
</li>
<li>
<a href="#">About</a>
</li>
</ul>
</div>
And here is how the CSS code would look:
#header {
background: #092F85;
position: relative;
height: 193px;
}
#nav{
background: #93D459;
position: absolute;
top: 166px;
right: 0;
}
I believe that understanding the positioning of the #nav div is straightforward (please correct me if I'm mistaken). It seems to be positioned absolutely on the right side and pushed down by 166px.
What confuses me is why the position of the parent div (#header) needs to be set as relative?
In general, when using position: absolute, the parent element is often specified as position: relative. But what does this mean exactly?
Thanks,
Andrea