Reputation: 3
I'm just new to front end coding, I am facing some problems about the margin
here.
I have a #header
div as a base, and other div
inside it.
body {
margin: 0;
padding: 0;
}
#header {
max-width: 100%;
height: 135px;
background-color: #ebebeb;
}
#headWrapper {
max-width: 1200px;
height: 85px;
margin: auto;
float: left;
}
.logo {
background-image: url("img/logo.png");
float: left;
width: 350px;
height: 78px;
margin-top: 20px;
}
#naviWrapper {
float: left;
max-width: 530px;
height: 40px;
margin-left: 275px;
font-family: 'Open Sans', sans-serif;
margin-top: 50px;
}
.homeBTN,
.aboutBTN,
.productBTN,
.solutionBTN,
.contactBTN {
float: left;
margin: 5 20;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="header">
<div id="headWrapper">
<div class="logo"></div>
<div id="naviWrapper">
<div class="homeBTN">Home</div>
<div class="aboutBTN">About us</div>
<div class="productBTN">Products</div>
<div class="solutionBTN">Solutions</div>
<div class="contactBTN">Contact us</div>
</div>
</div>
</div>
</body>
</html>
My problem is the inside div
if I add margin-top
on it, the whole div
will goes down together, how can I just move the div
inside the #header
? Is there anything wrong with my code?
Upvotes: 0
Views: 934
Reputation: 3386
There are many problems with your code.
Summary of the changes:
<a href=""></a>
instead of divs
and consider having ul li a
structure. Then to position the navigation vertically in the middle:
#header
container a display: table;
#headWrapper
a display: table-cell; vertical-align: middle;
To align the navigation's text in the middle use text-align: center;
Code:
body {
margin: 0;
padding: 0;
}
#header {
display: table;
max-width: 100%;
height: 135px;
background-color: #ebebeb;
width: 100%;
}
#headWrapper {
display: table-cell;
vertical-align: middle;
width: 100%;
height: 40px;
font-family: 'Open Sans', sans-serif;
text-align: center;
}
#naviWrapper a {
text-decoration: none;
padding: 10px;
}
#naviWrapper ul li {
list-style: none;
display: inline-block;
}
.logo {
background-image: url("img/logo.png");
float: left;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="header">
<div id="headWrapper">
<div class="logo"></div>
<nav id="naviWrapper">
<ul>
<li><a href="#">Home</a>
</li>
<li><a href="#">About us</a>
</li>
<li><a href="#">Products</a>
</li>
<li><a href="#">Solutions</a>
</li>
<li><a href="#">Contact</a>
</li>
</ul>
</nav>
</div>
</div>
</body>
</html>
Upvotes: 1