I am trying to create a header bar with logo on the right, horizontal navigation on the left as usual. I need navigation items to be centered to the logo vertically.
I have tried several methods. I would appreciate if someone could show me the way around and explain why it worked, and what I was probably missing.
body {
max-width: 995px;
margin: 0 auto;
padding: 20px 0;
box-sizing: border-box;
font-family: 'Arial';
}
a {
text-decoration: none;
color: #222;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.logo img {
max-width: 235px;
height: auto;
float: left;
}
.menu li {
display: inline-flex;
padding-left: 30px;
}
.menu {
float: right;
}
.nav {
padding-bottom: 25px;
}
.group:after {
content: "";
display: table;
clear: both;
}
<div class="nav group">
<div class="logo"><img src="img/ahlogo.png" alt="Abdulla Hussain's Logo"></div>
<div class="menu">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Thoughts</li>
<li>Contact</li>
<li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
</ul>
</div>
</div>
Thank you very much!
I suppose you want to align the navigation to the bottom end of the logo? If yes, you can use display:flex with the following settings on the parent element of both:
.nav.group {
display: flex;
justify-content: space-between;
align-items: flex-end;
}
body {
max-width: 995px;
margin: 0 auto;
padding: 20px 0;
box-sizing: border-box;
font-family: 'Arial';
}
.nav.group {
display: flex;
justify-content: space-between;
align-items: flex-end;
}
a {
text-decoration: none;
color: #222;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.logo img {
max-width: 235px;
height: auto;
float: left;
}
.menu li {
display: inline-flex;
padding-left: 30px;
}
.menu {
float: right;
}
.nav {
padding-bottom: 25px;
}
.group:after {
content: "";
display: table;
clear: both;
}
<div class="nav group">
<div class="logo"><img src="http://placehold.it/120x120/0af" alt="Abdulla Hussain's Logo"></div>
<div class="menu">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Thoughts</li>
<li>Contact</li>
<li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
</ul>
</div>
</div>
Alternatives: If you want to center-align them vertically, change align-items: to center; (or to top for top alignment)
body {
max-width: 995px;
margin: 0 auto;
padding: 20px 0;
box-sizing: border-box;
font-family: 'Arial';
}
.nav.group {
display: flex;
justify-content: space-between;
align-items: center;
}
a {
text-decoration: none;
color: #222;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.logo img {
max-width: 235px;
height: auto;
float: left;
}
.menu li {
display: inline-flex;
padding-left: 30px;
}
.menu {
float: right;
}
.nav {
padding-bottom: 25px;
}
.group:after {
content: "";
display: table;
clear: both;
}
<div class="nav group">
<div class="logo"><img src="http://placehold.it/120x120/0af" alt="Abdulla Hussain's Logo"></div>
<div class="menu">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Thoughts</li>
<li>Contact</li>
<li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
</ul>
</div>
</div>
This uses display:flex to set items in a line.
If you need to use browsers that do not support display:flex (I think IE<9) and you want a different way, let me know.
Open sans included because I like how it looks.
Orange used to demonstrate the middle-ness of the elements.
This will work if you know the height of the logo. If it is unknown, or perhaps changes height (a lot of places have navbars that shrink down a bit once the page scrolls) you may need a different solution.
#import url('https://fonts.googleapis.com/css?family=Open+Sans:400,700');
.nav {
display: flex;
background-color: orange;
margin: 0;
padding: 0;
height: 50px;
font-family: "Open Sans";
font-weight: bold;
justify-content: space-between;
}
.menu ul {
list-style: none;
margin: 0;
padding: 0;
}
.menu ul li {
display: inline-block;
padding: 5px 10px;
box-sizing: border-box;
line-height: 40px;
font-size: 20px;
}
.menu ul li:first-child {
margin-left: 10px;
}
.menu ul li a {
text-decoration: none;
}
<div class="nav group">
<div class="logo"><img src="//placehold.it/200x50" alt="Abdulla Hussain's Logo"></div>
<div class="menu">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Thoughts</li>
<li>Contact</li>
<li><i class="fa fa-facebook social" aria-hidden="true"></i></li>
</ul>
</div>
</div>
Related
I have this HTML code:
<body>
<header id="masthead">
<div id="container">
<!-- logo -->
<img src="logo.png" alt="" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>
And this CSS code:
.container {
width: 80%;
margin: 0 auto;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
header::after {
content : '';
display: table;
clear: both;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 2px;
position: relative;
padding-right: 0.1rem;
}
nav a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
However I want to make my nav bar to the left from the logo, but not down below. How can I do it using the given initial code that i hav pointed ? As you can see, align: right and align: left has been used, but had not helped me
Like on photo (Used arrows to point it )
Create two columns. In one column, place your logo, and in the second column, place your navigation bar.
<div class="row">
<div class="column" style="background-color:#aaa; width:20%;">
<!--pLACE Logo here--->
</div>
<div class="column" style="background-color:#bbb; width:80%">
<!--Place Navbar code here-->
</div>
</div>
Remember Adjust your css accordingly
Give your div with id container a display property of flex, direction property of row and then align or justify items as per your liking
#container{
display:flex;
flex-direction:row;
justify-content:space-between;
}
Also in your HTML code you've given tags ids but you're using class selectors in your CSS
Some resources that'll help you:
A Complete Guide to Flexbox
Basic Concepts of Flexbox
Flexbox Cheatsheet
You will have to change your CSS as shown below:
/*add this line to adjust paddings on the columns and remove default margin padding for all the html elements*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/*change class to # it's ID and not class*/
#container {
width: 80%;
margin: 0 auto;
}
/*recommended to add width in percentage in css and remove fix width from <img width='200px' /> tag*/
.logo {
float: left;
width:20%;
padding: 10px 0;
}
/*add width 80% for <nav> tag*/
nav {
float: right;
width: 80%;
margin-top: 10%;
}
nav li {
display: inline-block;
/* margin-left: 70px; */ /*remove*/
/* padding-top: 2px; */ /*remove*/
position: relative;
/* padding-right: 0.1rem; */ /*remove*/
padding: 0 5px; /*instead add this for space between li content*/
}
I would recommend you to use CSS FLEXBOX.
I used flexbox to do this. your id="container" was not the same as the CSS so I changed it to class="container"
I added some simple 1px borders just to illustrate what is where on the page.
This is likely not a finished solution and only a starting point.
.container {
width: 90%;
margin: 0 auto;
display: flex;
flex-direction: row;
flex: space-between font-size: 16px;
justify-content: center;
align-items: center;
}
.logo {
padding: 10px 0;
height: 3em;
border: 1px solid lime;
}
header::after {
content: '';
display: table;
clear: both;
}
nav {
border: 1px solid cyan;
justify-content: center;
align-items: center;
}
nav ul li::before {
content: "\200B";
}
nav ul {
display: flex;
flex-direction: row;
border: 1px solid blue;
list-style-type: none;
justify-content: center;
list-style-position: inside;
margin-left: 0;
padding-left: 0;
}
nav ul li {
padding-top: 0.2em;
padding-right: 0.1rem;
border: 1px solid pink;
margin-left: 0;
padding-left: 0;
}
nav li a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 0.875em;
margin-left: 1em;
margin-right: 1em;
}
<header id="masthead">
<div class="container">
<img src="logo.png" alt="logo png" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>
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>
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'm currently working on building my own website but ran into some trouble when it came to centering my actual ul within my navigation bar. The nav bar is already centered and fine but i am trying to center the actual ul and still keep it left aligned. I am using Brackets text editor. Any Help ?
Thanks in advance and heres my CSS code:
body {
background-color: #333333;
margin: 100 0;
}
.mainimg {
border-style: solid;
width: 80%;
margin: 10 auto;
}
.mainimg img {}
.nav-bar {
text-align: center;
width: 85%;
margin: 0 auto;
}
.nav-bar ul {
width: 100%;
list-style-type: none;
margin: 0 auto;
padding: 0 auto;
overflow: hidden;
background-color: #333333;
}
.nav-bar li {
float: left;
width: 10%;
}
.nav-bar li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}
.nav-bar li a:hover {
background-color: #111111;
}
<html>
<head>
<title>Satori</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="imageslider">
<img src="images/SATORI%20ARROW%20LEFT.png" width=100px alt="left">
<img class="mainimg" src="images/satori-for-web-background.png" alt="oauihgoiw">
<img src="images/SATORI%20ARROW%20RIGHT.png" width=100px alt="right">
</div>
<div class="nav-bar">
<ul>
<li>Station</li>
<li>Search</li>
<li>Shop</li>
<li>Films</li>
<li>Art</li>
<li>Podcast</li>
<li>Blog</li>
<li>Games</li>
<li>Music</li>
</ul>
</div>
</body>
</html>
Update below css part
.nav-bar li {
display:inline-flex; /* OR display:inline-block */
/* float:left; */
/* width: 10%; */
}
body {
background-color: #333333;
margin: 100 0;
}
.mainimg {
border-style: solid;
width: 80%;
margin: 10 auto;
}
.mainimg img {}
.nav-bar {
text-align: center;
width: 85%;
margin: 0 auto;
}
.nav-bar ul {
width: 100%;
list-style-type: none;
margin: 0 auto;
padding: 0 auto;
overflow: hidden;
background-color: #333333;
}
.nav-bar li {
display:inline-flex; /* OR display:inline-block */
/* float:left; */
/* width: 10%; */
}
.nav-bar li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}
.nav-bar li a:hover {
background-color: #111111;
}
<div class="imageslider">
<img src="images/SATORI%20ARROW%20LEFT.png" width=100px alt="left">
<img class="mainimg" src="images/satori-for-web-background.png" alt="oauihgoiw">
<img src="images/SATORI%20ARROW%20RIGHT.png" width=100px alt="right">
</div>
<div class="nav-bar">
<ul>
<li>Station</li>
<li>Search</li>
<li>Shop</li>
<li>Films</li>
<li>Art</li>
<li>Podcast</li>
<li>Blog</li>
<li>Games</li>
<li>Music</li>
</ul>
</div>
How to center ul inside of div.
A common advice are
left:50%
some variations of display:inline
but it does not work. How to fix this?
body {
margin: 0;
background: #f4f5f5;
}
.menu {
text-align: center;
margin: 0 auto;
width: 1023px;
background: #f4f5f5;
}
.picture {
margin: 0 auto;
width: 1023px;
height: 255px;
background: green;
}
ul {
list-style: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
<div class="content">
<div class="menu">
<ul>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
</ul>
</div>
<div class="picture">
</div>
</div>
set text-align:center and to ul and inline-block to li(removing float:left)
Tweaked your code, by removing duplicated code.
body {
margin: 0;
background: #f4f5f5;
}
.content {
margin: 0 auto;
width: 1023px;
}
.picture {
height: 255px;
background: green;
}
ul {
list-style: none;
background: #f4f5f5;
padding: 0;
margin: 0;
text-align: center;
}
li {
display: inline-block
}
li a {
display: block;
padding: 14px 16px;
text-decoration: none;
}
<div class="content">
<ul>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
<li><a>Test</a>
</li>
</ul>
<div class="picture">
</div>
</div>
This should solve the problem
body{
margin:0;
background:#f4f5f5;
}
.menu {
text-align: center;
margin: 0 auto;
width: 1023px;
background: #f4f5f5;
}
.picture {
margin:0 auto;
width: 1023px;
height: 255px;
background: green;
}
ul {
list-style: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
display: inline-block;
}
li{
float:left;
}
li a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
<div class="content">
<div class="menu">
<ul>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
<li><a>Test</a></li>
</ul>
</div>
<div class="picture">
</div>
</div>
Use a CSS3 Layout mode called Flexbox.
.menu {
display: flex; // <-- necessary to activate flexbox
justify-content: center; // <-- centers horizontally
// align-items: center; // <-- would center vertically
margin: 0 auto;
width: 1023px;
background: #f4f5f5;
}
learn more:
http://flexboxin5.com/
http://flexboxfroggy.com/