margin-left: auto seems not working
1- Header with an image and a navbar
2- Two unorder list in a navbar
2- Try to get one of the list on the left side and the second in the right side
If I use margin-left: 800px, it's working however, it's not with auto.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
header{
display: flex;
justify-content: flex-start;
align-items: center;
padding: 10px 10px;
}
nav{
font-family: "Montserrat", sans-serif;
font-weight: 500;
font-size: 16px;
margin-left: 15px;
}
.nav__links_R, .nav__links_L, a {
text-decoration: none;
list-style: none;
float: left;
color:rgba(0, 0, 0, .50);
}
.nav__links_R {
margin-left: auto;
}
.nav__links_L li {
display: inline-block;
padding: 0px 20px;
}
.nav__links_R li {
display: inline-block;
padding: 0px 20px;
}
<header>
<img src="../photo/logo_dark.png" alt="logo">
<nav>
<ul class=nav__links_L>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<ul class=nav__links_R>
<li>Register</li>
<li>Login</li>
</ul>
</nav>
</header>
Everything is displaying well as expecting. Only ".nav__links_R" not going to the right side with "auto"
Add the following two style rules to nav:
width: 100%;
display: flex;
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
header{
display: flex;
justify-content: flex-start;
align-items: center;
padding: 10px 10px;
}
nav{
font-family: "Montserrat", sans-serif;
font-weight: 500;
font-size: 16px;
margin-left: 15px;
display: flex;
width: 100%;
}
.nav__links_R, .nav__links_L, a {
text-decoration: none;
list-style: none;
float: left;
color:rgba(0, 0, 0, .50);
}
.nav__links_R {
margin-left: auto;
}
.nav__links_L li {
display: inline-block;
padding: 0px 20px;
}
.nav__links_R li {
display: inline-block;
padding: 0px 20px;
}
<header>
<img src="http://placekitten.com/200/40" alt="logo">
<nav>
<ul class=nav__links_L>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<ul class=nav__links_R>
<li>Register</li>
<li>login</li>
</ul>
</nav>
</header>
Related
I am trying to set the list to float to the right inside a div but it's not working.
Following is the html code,
<div class="topdiv">
<header>MakeItWork</header>
<ul class="navigation">
<li>Home</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</div>
And this is the css I am trying to make it work
.topdiv{
width: 100%;
overflow: hidden;
background-color: black;
color: white;
display: flex;
}
.topdiv header{
margin-top: auto;
margin-bottom: auto;
font-size: xxx-large;
margin-left: 5%;
}
.navigation{
text-decoration: none;
margin: none;
padding: none;
float: right;
}
.navigation li{
padding: 10px;
}
edit: My intention is to keep the header text (MakeItWork) to the left of the page and the list(containing home, content and shop as list items) to the right of the page.Something as shown in the provided picture
Add float:right to .navigation li and add justify-content:space-between to .topdiv to align title and list and different ends.
Note: I hope that you are making a navigation menu. Therefore further I added list-style: none rule to .navigation. Please consider that it is an extra rule that I've added.
.topdiv{
width: 100%;
overflow: hidden;
background-color: black;
color: white;
display: flex;
justify-content: space-between;
}
.topdiv header{
margin-top: auto;
margin-bottom: auto;
font-size: xxx-large;
margin-left: 5%;
}
.navigation{
text-decoration: none;
margin: none;
padding: none;
float: right;
list-style: none;
}
.navigation li{
padding: 10px;
float: right;
}
<div class="topdiv">
<header>MakeItWork</header>
<ul class="navigation">
<li>Home</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</div>
You can use justify-content:space-between, and float doesn't work with flex
.topdiv{
width: 100%;
overflow: hidden;
background-color: black;
color: white;
display: flex;
justify-content:space-between
}
.topdiv header{
margin-top: auto;
margin-bottom: auto;
font-size: xxx-large;
margin-left: 5%;
}
.navigation{
text-decoration: none;
margin: none;
padding: none;
}
.navigation li{
padding: 10px;
}
<div class="topdiv">
<header>MakeItWork</header>
<ul class="navigation">
<li>Home</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</div>
.topdiv {
width: 100%;
background-color: black;
color: white;
display: flex;
justify-content: space-between;
}
.topdiv header {
font-size: xxx-large;
}
.navigation {
float: right;
list-style: none;
}
.navigation li {
padding: 10px;
float: right;
}
<div class="topdiv">
<header>MakeItWork</header>
<ul class="navigation">
<li>Home</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</div>
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;
}
This is what I am trying to recreate. A basic nav.
As you can see here, there is an image next to the "ABOUT US" text, that is what I am having trouble with. How exactly can I make the img appear before the "ABOUT US" text? It always appears above it. Thanks in advance!
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
img {
max-width: 100%;
height: auto;
}
body {
font-family: "Open Sans",sans-serif;
}
.container {
max-width: 900px;
margin: 0 auto;
}
header {
padding: 2em 4em;
background-color: #121b21;
}
.nav-area {
list-style: none;
text-align: center;
}
.nav-area li {
display: inline-block;
padding: 0 1.5em;
color: #c4cbcf;
font-weight: 700;
font-size: 0.9em;
background-color: transparent;
}
<header>
<div class="container">
<nav>
<img src="header_logo.png" alt="Logo header">
<ul class="nav-area">
<li>ABOUT US</li>
<li>CONSULTING</li>
<li>SKYLIGHT</li>
<li>CONTACT</li>
</ul>
</nav>
</div>
</header>
Give a display:flex; property to nav and it will work :) Then you can adjust other CSS accordingly.
You may also set display:flex; on .nav-area, then you don't need inline-block on .nav-area li's.
.nav-area {
list-style: none;
text-align: center;
display:flex;
align-items:center;
}
CODEPEN WORKING DEMO: https://codepen.io/emmeiWhite/pen/WNGYJjq
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
img {
width:25px;
height: auto;
margin-left:1.5em;
}
body {
font-family: "Open Sans",sans-serif;
}
.container {
max-width: 900px;
margin: 0 auto;
}
header {
padding: 2em 4em;
background-color: #121b21;
}
nav{
display:flex;
align-items:center;
}
.nav-area {
list-style: none;
text-align: center;
}
.nav-area li {
display: inline-block;
padding: 0 1.5em;
color: #c4cbcf;
font-weight: 700;
font-size: 0.9em;
background-color: transparent;
}
<header>
<div class="container">
<nav>
<img src="https://picsum.photos/200" alt="Logo header">
<ul class="nav-area">
<li>ABOUT US</li>
<li>CONSULTING</li>
<li>SKYLIGHT</li>
<li>CONTACT</li>
</ul>
</nav>
</div>
</header>
I tried like this.
nav {
display: flex;
align-items: center;
justify-content: center;
}
If you want logo goes to left and nav-menu goes to right, try like this.
nav {
display: flex;
align-items: center;
justify-content: space-between;
}
Add the float attribute to the img CSS declaration to obtain the desired lay-out.
img {
max-width: 100%;
height: auto;
float: left;
}
So I've just learnt flexbox, however even after stating justify-content: flex-start; the list is not aligned to the left side of the column like so:
Image of the problem
Essentially I would like to align the list to the left side of the #navigation container so that there is not space present like in the image above. Any ideas what to change?
#navigation {
min-height: 50px;
width: 100%;
background-color: #2F4E6F;
}
.navflex {
display: flex;
flex-direction: row;
margin: 0;
list-style-type: none;
justify-content: flex-start;
}
.navflex a {
text-decoration: none;
text-decoration: none;
font-family: 'Open Sans';
font-size: 25px;
color: #FFFFFF;
display: block;
padding: 5px 25px;
text-align: center;
}
#home_button {
width: 38px;
height: 38px;
}
.tilde {
color: #FFFFFF;
font-size: 22px;
font-weight: bold;
font-family: 'Open Sans';
padding: 7px 0px;
}
#media all and (max-width: 800px) {
.navflex {
flex-direction: column;
flex-wrap: wrap;
}
.tilde {
display: none;
}
}
<div id="navigation">
<ul class="navflex">
<li>
<img src="images/home_button.png" alt="An icon representing a house." id="home_button">
</li>
<li>Phantom of the Opera</li>
<li class="tilde">~</li>
<li>The Lion King</li>
<li class="tilde">~</li>
<li>Wicked</li>
<li class="tilde">~</li>
<li>Bookings</li>
<li class="tilde">~</li>
<li><a href='#'>Location</a></li>
</ul>
</div>
Add padding: 0; to your ul (= .navflex) to avoid the default list padding. And if that isn't close enough to the left, also add padding-left: 0 to the li elements:
#navigation {
min-height: 50px;
width: 100%;
background-color: #2F4E6F;
}
.navflex {
display: flex;
flex-direction: row;
margin: 0;
list-style-type: none;
justify-content: flex-start;
padding: 0;
}
.navflex li {
padding-left: 0;
}
.navflex a {
text-decoration: none;
text-decoration: none;
font-family: 'Open Sans';
font-size: 25px;
color: #FFFFFF;
display: block;
padding: 5px 25px;
text-align: center;
}
#home_button {
width: 38px;
height: 38px;
}
.tilde {
color: #FFFFFF;
font-size: 22px;
font-weight: bold;
font-family: 'Open Sans';
padding: 7px 0px;
}
#media all and (max-width: 800px) {
.navflex {
flex-direction: column;
flex-wrap: wrap;
}
.tilde {
display: none;
}
}
<div id="navigation">
<ul class="navflex">
<li><img src="images/home_button.png" alt="An icon representing a house." id="home_button"></li>
<li>Phantom of the Opera</li>
<li class="tilde">~</li>
<li>The Lion King</li>
<li class="tilde">~</li>
<li>Wicked</li>
<li class="tilde">~</li>
<li>Bookings</li>
<li class="tilde">~</li>
<li><a href='#'>Location</a></li>
</ul>
</div>
So I've got my navbar set up pretty close to the way I'd like it to look, but for some reason it won't go to the center of my page. I've tried putting text-align: center; on most of the different elements that make up my nav bar, but it won't go no matter what I do. What am I doing wrong?
.navbar {
margin: 0;
padding: 0;
text-align: center;
border-bottom: solid #000;
border-width: 1.5px 0;
list-style: none;
height: 25px;
width: 1000px;
}
.navbar a {
text-decoration: none;
padding: 0;
margin: 10px;
border-bottom: 10px;
color: #000;
text-align: center;
}
li {
display: inline-block;
font-family: 'Muli', sans-serif;
font-size: 19px;
margin: 0;
padding: 0;
text-align: center;
}
<div class="banner">
<h1>Brian Funderburke Photography & Design</h1>
<div class="nav">
<ul class="navbar">
<li>Home
</li>
<li>Photography
</li>
<li>Design
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
</div>
Set the .banner width to 100% and then set .nav to center:
.banner{width: 100%;}
.nav{text-align: center;}
See fiddle: https://jsfiddle.net/c51kr3jo/
How I fix this problem usually is by following 3 steps:
Adding a full-width or a big width to the navbar container (.nav div in your case)
Adding a width to the navbar (usually try to find a width that will fit the most elements)
Adding margin: 0 auto to the navbar (this will center align the .navbar div)
Here is a jsfiddle I've created. Hope it helps.
I added
.nav {
display: flex;
justify-content: center;
}
Which I got from this AMAZING article.
http://jsfiddle.net/abalter/44w7b73f/
navbar center
.navbar {
margin: 0;
padding: 0;
text-align: center;
border-bottom: solid #000;
border-width: 1.5px 0;
list-style: none;
height: 25px;
width: 1000px;
}
.navbar a {
text-decoration: none;
padding: 0;
margin: 10px;
border-bottom: 10px;
color: #000;
text-align: center;
}
li {
display: inline-block;
font-family: 'Muli', sans-serif;
font-size: 19px;
margin: 0;
padding: 0;
text-align: center;
}
/* center */
.nav1 {
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-items: center;
align-items: center;
width: 100%;
height: auto;
text-align: center;
}
.navbar1 {
-webkit-align-self: center;
align-self: center;
}
li a:hover {
color: red;
cursor: pointer;
}
/* || center */
<div class="nav1">
<h1>Brian Funderburke Photography & Design</h1>
</div>
<div class="nav nav1">
<ul class="navbar navbar1">
<li>Home
</li>
<li>Photography
</li>
<li>Design
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
</div>
Your code appears to already be working from what I can tell (though you were missing a closing </div> in your example). Make sure your CSS is being properly imported and applied.
Here is a working example to prove it is looking okay: http://jsfiddle.net/611mbvtu/