Reputation: 751
I am working on a navigation bar for my website. The navigation bar contains a menu icon, a header 'Reviews' and a search icon. The icons need to have a distance of 3% from the border. The problem is that I can't figure it out how I center the text. I think the problem is with the floating elements and I don't know how I center a floating element between to other elements(in this case two icons). I hope someone could provide me with an answer :)
This is the result:
* {
padding: 0px;
margin: 0px;
}
#nav {
width: 94%;
background-color: blue;
margin: 3%;
}
.menuButton {
float: left;
}
#nav h1 {
float: left;
}
.searchButton {
float: right;
}
<!DOCTYPE html>
<html>
<head>
<title> Reviews </title>
<link rel="stylesheet" type="text/css" href="main.css">
<meta charset="utf-8">
</head>
<div id="dropMenu"></div>
<body>
<div id="nav">
<img src="menu.jpg" class="menuButton">
<h1> REVIEWS. </h1>
<img src="search.jpg" class="searchButton">
</div>
<div id="content"></div>
<div id="footer"></div>
</body>
</html>
Upvotes: 2
Views: 70
Reputation: 106008
You may use the flex boxmodel and margins :
* {
padding: 0px;
margin: 0px;
}
#nav {
width: 94%;
background-color: blue;
/* see center */
background-image:linear-gradient(to right, transparent 50%, rgba(0,0,0,0.5) 50%);
margin: 3%;
display: flex;
justify-content: center;
}
.menuButton {
float: left;
margin: auto auto auto 0;
}
#nav h1 {
float: left;
}
.searchButton {
margin: auto 0 auto auto;
}
<!DOCTYPE html>
<html>
<head>
<title> Reviews </title>
<link rel="stylesheet" type="text/css" href="main.css">
<meta charset="utf-8">
</head>
<div id="dropMenu"></div>
<body>
<div id="nav">
<img src="menu.jpg" class="menuButton">
<h1> REVIEWS. </h1>
<img src="search.jpg" class="searchButton">
</div>
<div id="content"></div>
<div id="footer"></div>
</body>
</html>
edit.
Considering only float, then float elements should be ahead in the flux to let other non-floatting elements comes in between, so h1
can be standing in between and get text-align
or be centered via display
and margin
.
float won't allow vertical centering of elements where flex does .
* {
padding: 0px;
margin: 0px;
}
#nav {
width: 94%;
background-color: blue;
margin: 3%;
}
.menuButton {
float: left;
}
#nav h1 {
display:table;/* shrinks on itself */
margin:auto;
}
.searchButton {
float: right;
}
<div id="dropMenu"></div>
<div id="nav">
<img src="menu.jpg" class="menuButton">
<img src="search.jpg" class="searchButton">
<h1> REVIEWS. </h1>
</div>
<div id="content"></div>
<div id="footer"></div>
* {
padding: 0px;
margin: 0px;
}
#nav {
width: 94%;
background-color: blue;
margin: 3%;
}
.menuButton {
float: left;
}
#nav h1 {
text-align:center
}
.searchButton {
float: right;
}
<div id="dropMenu"></div>
<div id="nav">
<img src="menu.jpg" class="menuButton">
<img src="search.jpg" class="searchButton">
<h1> REVIEWS. </h1>
</div>
<div id="content"></div>
<div id="footer"></div>
Upvotes: 2
Reputation: 455
Is this output are you expecting
* {
padding: 0;
margin: 0;
}
#nav {
width: 100%;
background-color: blue;
margin: 3%;
}
.menuButton {
float: left;
}
.searchButton {
float: right;
}
.a {
text-align: center;
}
<head>
<title> Reviews </title>
<link rel="stylesheet" type="text/css" href="main.css">
<meta charset="utf-8">
</head>
<div id="dropMenu">
</div>
<body>
<div id="nav">
<img src="menu.jpg" class="menuButton">
<h1 class="a"> REVIEWS. </h1>
<img src="search.jpg" class="searchButton">
</div>
<div id="content">
</div>
<div id="footer">
</div>
</body>
Upvotes: 1