I'm trying to make a navigation bar with a center-aligned menu, but the logo is stopping the menu from being in the center of the page and instead pushing the menu to the right. So the menu is offset from being horizontally centered by the width of the logo. I want it so that my menu is in the center of the page rather than pushed 150px to the right by the logo.
How can I make it so that my logo doesn't shift my menu to the right, stopping it from being center-aligned in the body?
body {
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 16px;
margin: 0;
}
h1 {
font-size: 6rem;
font-weight: 400;
}
h2 {
font-size: 4.5rem;
font-weight: 400;
}
header {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
margin: 20px 5% 0;
}
header img {
width: 150px;
}
nav {
width: 100%;
overflow: hidden;
text-align: center;
vertical-align: middle;
}
nav ul {
list-style: none;
}
nav ul li {
display: inline-block;
padding-right: 1rem;
text-transform: uppercase;
}
nav ul li a {
text-decoration: none;
color: black;
}
<header>
<img src="../Images/Logo/Black-Logo.png" alt="Logo" />
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
You could erase the flex settings from the container, apply absolute position to the logo (and relative to the header), add text-align: center; to the header to center the nav and erase/reset most of the paddings and margins except those shown below (exact settings see snippet below).
body {
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 16px;
margin: 0;
}
header {
width: 90%;
margin: 20px 5% 0;
text-align: center;
position: relative;
}
header img {
width: 50px;
position: absolute;
left: 0;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
display: inline-block;
padding-right: 1rem;
text-transform: uppercase;
}
nav ul li:last-child {
padding-right: 0;
}
nav ul li a {
text-decoration: none;
color: black;
}
img {
}
<header>
<img src="../Images/Logo/Black-Logo.png" alt="Logo" />
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
Use position: absolute for the logo and it will make it "not take up space" in the navbar:
#logo {
position: absolute;
top: 0 left: 0;
}
body {
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 16px;
margin: 0;
}
h1 {
font-size: 6rem;
font-weight: 400;
}
h2 {
font-size: 4.5rem;
font-weight: 400;
}
header {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
margin: 20px 5% 0;
}
header img {
width: 150px;
}
nav {
width: 100%;
overflow: hidden;
text-align: center;
vertical-align: middle;
}
nav ul {
list-style: none;
}
nav ul li {
display: inline-block;
padding-right: 1rem;
text-transform: uppercase;
}
nav ul li a {
text-decoration: none;
color: black;
}
<header>
<img id="logo" src="../Images/Logo/Black-Logo.png" alt="Logo" />
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
I'd use a container (see div with class wrapper below) and make the logo position: absolute. This simplifies your CSS.
.logo {
position: absolute;
}
.wrapper {
margin: 0 auto;
}
<div class="logo"><img src="../Images/Logo/Black-Logo.png" alt="Logo" /></div>
<div class="wrapper">
<nav>
<ul id="MenuItems" class="NavMenu">
<li>Home</li>
<li>About Us</li>
<li>Hours</li>
<li>Get in Touch</li>
</ul>
</nav>
</div>
You can get rid of this CSS:
nav {
width: 100%;
overflow: hidden;
text-align: center;
vertical-align: middle;
}
Related
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 3 years ago.
I am building the top navigation of a page. To the left, I have a logo and directly beside it I have my primary navigation links. I have two buttons that I want to position all the way to the right of the page, but I can't figure out how to get them over there. When I am able to get the buttons to move at all, it's only about halfway through the page...which isn't correct.
The buttons should be all the way to the right, like on https://webflow.com/
Notes: I used flex center to vertically center my navigation items.
body {
font-family: 'Poppins', sans-serif;
}
.wrap {
margin: 0 auto;
width: 90%;
}
nav {
display: flex;
align-items: center;
height: 80px;
width: 1000px;
}
nav ul li {
display: inline;
list-style: none;
padding: 0 24px 0 24px;
}
nav a {
color: #353D49;
font-size: 14px;
font-weight: 500;
text-decoration: none;
}
nav a:hover {
color: #247BFA;
}
<div class="wrap">
<nav>
<img src="img/logo.svg" />
<ul class="navItems">
<li>Overview</li>
<li>Features</li>
<li>Blog</li>
<li>Learn</li>
</ul>
<div class="actionButtons">
<button class="primary">Request a demo</button>
</div><!--actionButtons-->
</nav>
</div><!--wrap-->
.actionButtons {margin-left: auto}
or
nav {justify-content: space-between}
cheers :)
You can position the element to the right of the flex parent using margin-left: auto. You can do the same for vertical positioning with margin-top: auto.
body {
font-family: 'Poppins', sans-serif;
}
.wrap {
margin: 0 auto;
width: 90%;
}
nav {
display: flex;
align-items: center;
height: 80px;
width: 1000px;
}
nav ul li {
display: inline;
list-style: none;
padding: 0 24px 0 24px;
}
nav a {
color: #353D49;
font-size: 14px;
font-weight: 500;
text-decoration: none;
}
nav a:hover {
color: #247BFA;
}
nav .actionButtons {
margin-left: auto;
}
<div class="wrap">
<nav>
<img src="img/logo.svg" />
<ul class="navItems">
<li>Overview</li>
<li>Features</li>
<li>Blog</li>
<li>Learn</li>
</ul>
<div class="actionButtons">
<button class="primary">Request a demo</button>
</div><!--actionButtons-->
</nav>
</div><!--wrap-->
Great case for margin-left: auto; though I would create a separate class. The goal is to make code reusable, so nesting it in your nav tag will make it unwieldy if you continue with this method throughout your project.
I created a separate class for .align-right and you can reuse this in other areas of your project , rather than being limited to .nav or applying it to all of your .actionButtons
body {
font-family: 'Poppins', sans-serif;
}
.wrap {
margin: 0 auto;
width: 90%;
}
nav {
display: flex;
align-items: center;
height: 80px;
width: 1000px;
}
nav ul li {
display: inline;
list-style: none;
padding: 0 24px 0 24px;
}
nav a {
color: #353D49;
font-size: 14px;
font-weight: 500;
text-decoration: none;
}
nav a:hover {
color: #247BFA;
}
.align-right {
margin-left: auto;
}
<div class="wrap">
<nav>
<img src="img/logo.svg" />
<ul class="navItems">
<li>Overview</li>
<li>Features</li>
<li>Blog</li>
<li>Learn</li>
</ul>
<div class="actionButtons align-right">
<button class="primary">Request a demo</button>
</div><!--actionButtons-->
</nav>
</div><!--wrap-->
What I know so far is that a parent with nothing but floated children has no height.
In the header for example, I have the left logo floated and the text has an absolute position. The only thing keeping the header from collapsing is the right logo.
header {
position: relative;
font-family: "Felix Titling Regular", Times, serif;
border-bottom: 2px double white;
font-size: 0;
}
header a {
display: inline-block;
}
header>a:first-child {
position: relative;
padding-left: 3%;
}
header>a:last-child {
float: right;
padding-right: 3%;
}
#center-wrapper {
position: absolute;
left: 50%;
top: 25%;
font-size: 16px;
}
header h1 {
font-size: 300%;
display: inline-block;
position: relative;
text-transform: capitalize;
right: 50%;
}
<header>
<img src="media/logo-small.png" alt="Godfather Logo" title="Godfather Small Logo" />
<div id="center-wrapper">
<h1> loyal capos to the don </h1>
</div>
<img src="media/logo-small.png" alt="Godfather Logo" title="Godfather Small Logo" />
</header>
However, I don't understand why my first parent 'ul' in the 'nav' still has height even though all the li elements are floated.
nav {
position: relative;
border-top: 2px double #660000;
text-align: center;
font-size: 0;
background-color: #660000;
}
nav>ul {
display: inline-block;
font-size: 16px;
width: 100%;
}
ul {
list-style-type: none;
}
nav>ul>li {
min-width: 20%;
float: left;
}
nav ul ul,
nav ul ul ul {
position: absolute;
display: none;
width: 100%;
}
nav>ul>li:hover>ul {
display: block;
}
nav>ul>li>ul>li:hover>ul {
left: 100%;
top: 0%;
display: block;
}
ul>li {
position: relative;
padding: .4em 0;
background-color: white;
}
li>a {
color: #660000;
text-decoration: none;
text-transform: capitalize;
font-size: 150%;
font-family: "Felix Titling Regular", Times, serif;
background-color: white;
}
<nav>
<ul>
<li>wiki</li>
<li>media
<ul>
<li>images</li>
<li>videos
<ul>
<li>best scenes</li>
<li>bloopers</li>
</ul>
</li>
</ul>
</li>
<li>home</li>
<li>facts</li>
<li>about
<ul>
<li>feedback</li>
<li>contact us</li>
</ul>
</li>
</ul>
</nav>
Here's my code on fiddle (https://jsfiddle.net/vwb6g740/1/)
This is true for block elements and inline elements, but not for inline-block elements.
What I know so far is that a parent with nothing but floated children has no height.
You have display:inline-block; set on that parent element.
I can't explain why, but I can reproduce that behavior.
p {
display: inline-block;
background: blue;
}
div {
background: red;
}
span {
background: green;
}
i {
float: left;
}
<div><i>test</i></div>
<span><i>test</i></span>
<p><i>test</i></p>
I am designing a hotel website with a fixed navbar. I am having trouble getting the items to be inline with an image item. It's getting quite frustrating.
Here's my HTML
<nav id="navigation">
<ul>
<li class="left">Rooms</li>
<li class="left">Dining</li>
<li class="home"><img src="assets/navbarimg.png" height="64.5px" weight="250px"></img></li>
<li class="right">Activities</li>
<li class="right">Book a Stay</li>
</ul>
</nav>
and here's my CSS
#navigation ul {
text-align: center;
position: fixed;
width: 100%;
max-width: 100%;
font-size: 2vh;
font-family: 'Raleway', sans-serif;
z-index: 100;
background-color: rgba(255,255,255,.55);
display: inline-block;
}
#navigation li {
display: inline-block;
list-style-type: none;
}
#navigation a {
text-decoration: none;
margin: 0px 75px 0 75px;
color: black;
font-weight: 600;
text-transform: uppercase;
display: inline-block;
}
.left {
display: inline;
}
.home {
display: inline;
}
.right {
display: inline;
}
My code is super basic, so work with me here.
#navigation ul {
text-align: center;
position: fixed;
width: 100%;
max-width: 100%;
font-size: 2vh;
font-family: 'Raleway', sans-serif;
z-index: 100;
background-color: rgba(255, 255, 255, .55);
display: inline-block;
}
#navigation li {
display: inline-block;
list-style-type: none;
}
#navigation a {
text-decoration: none;
margin: 0px 75px 0 75px;
color: black;
font-weight: 600;
text-transform: uppercase;
display: inline-block;
}
.left {
display: inline;
}
.home {
display: inline;
}
.right {
display: inline;
}
<nav id="navigation">
<ul>
<li class="left">Rooms</li>
<li class="left">Dining</li>
<li class="home">
<a href="index.html"><img src="https://placehold.it/250x65" height="64.5px" weight="250px"></img>
</a>
</li>
<li class="right">Activities</li>
<li class="right">Book a Stay</li>
</ul>
</nav>
Live Example I made: https://akainth015.github.io/Inked-Out
Often, you can align things with vertical-align(CSS). However, it is a little counter-intuitive. When you use the vertical-align style, the element it is applied to becomes the standard for the rest of the elements. So, if you have this structure:
li {
display: inline-block;
}
ul {
list-style-type: none;
}
li img {
vertical-align: middle;
}
<ul>
<li>Home</li>
<li><img src="https://placehold.it/64x64"></li>
<li>About</li>
</ul>
Notice how it is applied to the image, not the the text you want in the center. Good luck!
So I'm working on a navigation bar for my website and I'm trying to space everything centred evenly in the bar, but for some reason, I can't get things to work out evenly. Either margins don't work, or they do but not all the way, and it's getting quite frustrating. Here's my code:
body {
margin: 0 px;
font - family: Helvetica;
}
.navbar {
background - color: grey;
width: 100 % ;
height: 70 px;
text - align: center;
}
.items li {
display: inline - block;
padding - left: 50 px;
}
.items a {
text - decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
padding-top: -25px;
}
<body>
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
</body>
To center vertically links with the logo, use vertical-align:middle;.
Also, If you set margin or padding don't set it to <a> because it's an item with inline properties but set it on li items which are inline-block.
In addition to that, it's strange to write li items without ul container.
body {
margin: 0px;
font-family: Helvetica;
}
.navbar {
background-color: grey;
width: 100% ;
height: 70px;
text-align: center;
}
.items img
{
vertical-align:middle;
}
.items li {
display: inline-block;
vertical-align:middle;
padding-left: 50px;
}
.items a {
text-decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
}
<body>
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
</body>
Note:See it in full page!
body {
margin: 0px;
font-family: Helvetica;
}
.navbar {
background-color: grey;
width: 100%;
height: 70px;
text-align: center;
position: relative;
}
.items {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.items li {
display: inline-block;
padding-left: 50px;
}
.items a {
text-decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
display: block;
}
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
I can't figure out how to put them on the same line.
http://codepen.io/anon/pen/dovZdQ
<body>
<div class="navigation-bar">
<div id="navigation-container">
<img src="logo.png">
<ul>
<li>Home</li>
<li>Projects</li>
<li>About</li>
<li>Services</li>
<li>Get in Touch</li>
</ul>
</div>
</div>
</body>
The <ul> is by default a block element, make it inline-block instead:
.navigation-bar ul {
padding: 0px;
margin: 0px;
text-align: center;
display:inline-block;
vertical-align:top;
}
CodePen Demo
Firstly, let's use some semantic HTML.
<nav class="navigation-bar">
<img class="logo" src="logo.png">
<ul>
<li>Home</li>
<li>Projects</li>
<li>About</li>
<li>Services</li>
<li>Get in Touch</li>
</ul>
</nav>
In fact, you can even get away with the more minimalist:
<nav class="navigation-bar">
<img class="logo" src="logo.png">
Home
Projects
About
Services
Get in Touch
</nav>
Then add some CSS:
.navigation-bar {
width: 100%; /* i'm assuming full width */
height: 80px; /* change it to desired width */
background-color: red; /* change to desired color */
}
.logo {
display: inline-block;
vertical-align: top;
width: 50px;
height: 50px;
margin-right: 20px;
margin-top: 15px; /* if you want it vertically middle of the navbar. */
}
.navigation-bar > a {
display: inline-block;
vertical-align: top;
margin-right: 20px;
height: 80px; /* if you want it to take the full height of the bar */
line-height: 80px; /* if you want it vertically middle of the navbar */
}
Obviously, the actual margins, heights and line-heights etc. depend on your design.
Other options are to use tables or floats for layout, but these are generally frowned upon.
Last but not least, I hope you get cured of div-itis.
You need to apply the logo class to the image...then float the ul
Codepen Demo
HTML
<img class="logo" src="http://i.imgur.com/hCrQkJi.png">
CSS
.navigation-bar ul {
padding: 0px;
margin: 0px;
text-align: center;
float: left;
background: white;
}
I'll advise you use CSS Flex.
body {
margin: 0;
padding: 0;
}
#navigation-container {
position: relative;
background-color: #352d2f;
display: flex;
flex-direction: row;
justify-content: space-between;
}
ul {
display: flex;
flex-direction: row;
list-style-type: none;
}
a {
text-decoration: none;
color: black;
font-size: 16px;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
padding: 5px 15px;
opacity: 0.7;
}
li {
display: flex;
flex-direction: row;
align-items: center;
}
<head>
<link href="./answer.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="navigation-bar">
<div id="navigation-container">
<img src="https://i.imgur.com/hCrQkJi.png">
<ul>
<li>Home</li>
<li>Projects</li>
<li>About</li>
<li>Services</li>
<li>Get in Touch</li>
</ul>
</div>
</body>
Try this CSS:
body {
margin: 0;
padding: 0;
}
.logo {
float: left;
}
/* ~~ Top Navigation Bar ~~ */
#navigation-container {
width: 1200px;
margin: 0 auto;
height: 70px;
}
.navigation-bar {
background-color: #352d2f;
height: 70px;
width: 100%;
}
#navigation-container img {
float: left;
}
#navigation-container ul {
padding: 0px;
margin: 0px;
text-align: center;
display:inline-block;
}
#navigation-container li {
list-style-type: none;
padding: 0px;
height: 24px;
margin-top: 4px;
margin-bottom: 4px;
display: inline;
}
#navigation-container li a {
color: white;
font-size: 16px;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
text-decoration: none;
line-height: 70px;
padding: 5px 15px;
opacity: 0.7;
}
#menu {
float: right;
}
1) you can float the image to the left:
<img style="float:left" src="http://i.imgur.com/hCrQkJi.png">
2)You can use an HTML table to place elements on one line.
Code below
<div class="navigation-bar">
<div id="navigation-container">
<table>
<tr>
<td><img src="http://i.imgur.com/hCrQkJi.png"></td>
<td><ul>
<li>Home</li>
<li>Projects</li>
<li>About</li>
<li>Services</li>
<li>Get in Touch</li>
</ul>
</td></tr></table>
</div>