Reputation: 13
New to HTML/CSS and I'm trying to align the h1 with the navigation bar but it's not working. I want the header and the two navigation links to be on the same line.
.navigation {
width: 100%;
height: 80px;
background: #2eb82e;
display: inline-block;
}
.main-nav {
display: inline-block;
vertical-align: top;
height: 0px;
line-height: 0px;
margin-left: 1000px;
}
h1 {
font-size: 30px;
color: whitesmoke;
display: inline-block;
vertical-align: top;
width: 400px;
height: 50px;
margin-left: 20px;
margin-top: 20px;
}
<section class="navigation">
<nav>
<header>
<h1>Heading</h1>
</header>
<ul>
<li class="main-nav"><a class="link" href="">Nav 1</a></li>
<li class="main-nav"><a class="link" href="">Nav 2</a></li>
</ul>
</nav>
</section>
Would appreciate any help. Thanks!
Upvotes: 1
Views: 288
Reputation: 7066
Here is you can do to make it simple:
- Since the
nav
are outside ofheader
, you can makingnav
asposition:relative;
so that thenav
elements will be relative toheader
.- Then you can make
position:absolute;
tonav
and made ittop:25px;
since you have yourheader
as 50px(half to make it center).Margin-left
is not needed I have removed that as well.
fiddle to play-around.
nav {
position: relative;
}
.links {
position: absolute;
top: 25px;
/* 25px because your header height is 50px, i made it center align */
right: 10%;
}
.navigation {
width: 100%;
height: 80px;
background: #2eb82e;
display: inline-block;
}
.main-nav {
display: inline-block;
vertical-align: top;
height: 0px;
line-height: 0px;
}
h1 {
font-size: 30px;
color: whitesmoke;
display: inline-block;
vertical-align: top;
width: 400px;
height: 50px;
margin-left: 20px;
margin-top: 20px;
}
<section class="navigation">
<nav>
<header>
<h1>Heading</h1>
</header>
<ul class="links">
<li class="main-nav"><a class="link" href="">Nav 1</a></li>
<li class="main-nav"><a class="link" href="">Nav 2</a></li>
</ul>
</nav>
</section>
Upvotes: 0
Reputation: 18973
You need remove margin-left: 1000px;
in .main-nav
and set padding-left
for ul to 0
.navigation {
width: 100%;
height: 80px;
background: #2eb82e;
display: inline-block;
}
.main-nav {
display: inline-block;
vertical-align: top;
height: 0px;
line-height: 0px;
}
h1 {
font-size: 30px;
color: whitesmoke;
display: inline-block;
vertical-align: top;
width: 400px;
height: 50px;
margin-left: 20px;
margin-top: 20px;
}
ul{
padding-left:0;
}
<body>
<section class="navigation">
<nav>
<header><h1>Heading</h1></header>
<ul>
<li class="main-nav"><a class="link" href="">Nav 1</a></li>
<li class="main-nav"><a class="link" href="">Nav 2</a></li>
</ul>
</nav>
</section>
Upvotes: 1