I am currently studying Code Academy – Front End Developer course and next to it, I am trying to create my website.. I am really trying as this is my first time doing such thing. And I am already feeling lost and stupid.
I got stuck on my header already!
I have my header where I want to place my logo (on the left side) and next to it, I want my nav bar to appear (not right next to it, I want the nav bar to be more less in the centre of the page)
here is how it looks like now:
How can I do that? Also, I would love if you could explain why it should be done the way it should be, so I don’t just copy paste the code but actually learn something. Thank you millions for your help!
<!--THIS IS MY HEADER-->
<div>
<header class="header">
<img class="logo" src="./Pictures/Screenshot 2023-12-22 at 19.40.06.png" alt="My Image">
<nav class="navbar">
<ul>
<li>ABOUT ME</li>
<li>MY WORK</li>
<li>CONTACT</li>
</ul>
</nav>
</header>
</div>
CSS
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
.header {
background-color: white;
border-bottom: solid;
}
.logo {
width: 188px;
height: 90px;
}
.navbar{
list-style-type: none;
display: flex;
width: 100%;
justify-content: space-evenly;
}
3
Answers
To create a header with a logo on the left and a centered navigation bar, you can utilize CSS Flexbox for layout control. In the header’s CSS, set display: flex to establish a flex container. Use justify-content: space-between to push the logo to the left and the navigation bar to the right, creating space between them. Adding align-items: center vertically centers the items within the header. For the navigation bar, apply display: flex for its layout, justify-content: space-evenly for even spacing, and adjust margin on its list items for desired spacing. This combination of flex properties provides a clean and responsive layout for your header, ensuring the logo is on the left, and the navigation bar is centered on the page. Adjust values as needed for your specific design preferences.
HTML5 brought clearer semantics which basically just means that there are now lots of elements to use that match their meaning. Having a
<header>
for your heading top-bar and a<nav>
for your main navigation are good examples of ones you’ve already adopted.My suggestion would be to use
<a>
tags within that<nav>
because semantically that is what you are trying to do, link the user to another page. In the snippet below that is what I have done. These work seamlessly within a<nav>
which means also you don’t have to use a<ul>
. You still can of course, and nothing wrong with it, but unless you have a good reason to use one then you can just omit it.By styling the
<header>
asdisplay:flex
you can have the<img>
and<nav>
sit horizontally beside each other with the defaultflex-direction: row;
style. The<img>
will take up it’s space with the height/width styles and then with the<nav>
style offlex: 1;
you are telling the browser that the<nav>
should take up the rest of the available space of that<header>
. This means you can then center the links inside the<nav>
.With images it’s good set the height or width and then set the other option to auto so that the image stays in proportion to it’s ratio, otherwise your image can look stretched by hard-coding both values.
I have added some comments to the styles as you seem keen to actually learn.