Why is nav not aligning in the center of the header? - html

Please have a look at this fiddle
HTML:
<nav id="main_nav">
<ul>
<li>Blog</li>
</ul>
</nav>
CSS:
#main_nav {
text-align:center;
width:50%;
padding-top: 35px;
}
Why is the nav not aligning in the center of the header

Don't give unwanted margin of padding to adjust your html
Here is a working Demo
removed padding-top margin etc and added:
header{
display: flex;
align-items: center;
overflow: scroll;
justify-content: center;
}
header h1{
min-width: 250px;
margin: 0;
}
#search_and_avatar{
flex: 1;
display: flex;
justify-content: flex-end;
}
body {
margin:0;
font-family: 'Source Sans Pro', sans-serif;
}
header {
background-color:#151515;
overflow:hidden;
height: 100px;
}
h1 {
color:#FFF;
font-family: "Arial Rounded MT", "Helvetica Rounded", Arial, sans-serif;
font-size: 40px;
font-weight: normal;
line-height:25px;
padding: 0px 20px;
}
header h1, img, #search_form {
float:left;
}
#main_nav a {
color:#FFF;
text-decoration:none;
}
#main_nav {
display: table;
text-align:center;
width:50%;
}
#main_nav ul {
display: table-row;
}
#main_nav ul li {
padding: 15px;
display: table-cell;
vertical-align: middle;
top: 50%;
display:inline;
}
header img {
width:35px;
}
header{
display: flex;
align-items: center;
overflow: scroll;
justify-content: center;
}
header h1{
flex: 1;
min-width: 250px;
margin: 0;
}
header form{
text-align: right;
}
#search_and_avatar{
flex: 1;
display: flex;
justify-content: flex-end;
}
<div id="wrapper">
<header>
<h1>CSS-Tricks</h1>
<nav id="main_nav">
<ul>
<li>Blog</li>
<li>Videos</li>
<li>Almanac</li>
<li>Snippets</li>
<li>Forums</li>
<li>Shop</li>
<li>Lodge</li>
<li>Jobs</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
<!--
<ul id="account_nav">
<li>Log In</li>
<li>Sign Up</li>
</ul>
-->
</header>
</div>

#main_nav {
text-align:center;
width:50%;
padding-top: 35px;
margin:0px auto;
}

Remove display: table; and add margin: 0 auto; to your nav
#main_nav {
text-align: center;
width: 50%;
padding-top: 35px;
margin: 0 auto;
}
Check this Example fiddle

Related

Image in navigation

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;
}

Text displays under image instead of inline

I'm trying to say "my website bby" right next to the image logo and for some reason it displays under the logo. Help me please. Here is what it is right now, check the image
Here is my html:
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background:#6BD326;
}
#logo {
float:left;
display:inline-block;
}
.branding {
float: left;
}
nav li {
float:left;
padding: 0px 20px;
list-style-type: none;
}
nav {
float:right;
}
#logo {
margin-top:5px;
width:40%;
}
<header>
<div class="branding">
<div id="logo">
<img src="https://picsum.photos/200/300" style="width:100px;" />
</div>
<span><h1><u>My</u> Website bby</h1></span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</header>
To have a better understanding of what I'm talking about, check the picture above. I just started out coding, really need to understand what's wrong.
You can also use a combination of display: inline-block and vertical-align: middle.
For example:
body {
font-size: 20px;
font-family: Optima, sans-serif;
line-height: 1.5;
padding: 0;
margin: 0;
}
.container {
width: 80%;
overflow: hidden;
}
header {
font-family: Monaco, sans-serif;
color: white;
padding-top: 100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background: #6BD326;
}
#logo {
float: left;
display: inline-block;
vertical-align: top;
}
.i-b {
display: inline-block;
vertical-align: top;
width: calc(100% - 40%);
}
.branding {
float: left;
}
nav li {
float: left;
padding: 0px 20px;
list-style-type: none;
}
nav {
float: right;
}
#logo {
margin-top: 5px;
width: 40%;
}
<div class="branding">
<div id="logo">
<img src="https://i.picsum.photos/id/237/536/354.jpg?hmac=i0yVXW1ORpyCZpQ-CknuyV-jbtU7_x9EBQVhvT5aRr0" style="width:100px;" />
</div>
<span class="i-b"><h1><u>My</u> Website bby</h1></span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</div>
Jsfiddle code
check this out i removed all the floats and worked with flex
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
margin: 0;
padding: 10px;
background:#6BD326;
display: flex;
justify-content:space-between;
flex-wrap: wrap;
}
#logo {
margin-top: 5px;
display: flex;
align-items: center;
}
#logo span{
margin-left: 10px;
}
nav ul {
display: flex;
flex-wrap: wrap;
}
nav li {
padding: 0px 20px;
list-style-type: none;
}
<header>
<div class="branding">
<div id="logo">
<img src="https://cdn.mos.cms.futurecdn.net/BVb3Wzn9orDR8mwVnhrSyd-1200-80.jpg" style="width:100px;" />
<span><u>My</u> Website bby</span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</div>
</header>
Here is your code which is work
body like that :
<body>
<div class="branding">
<div id="logo">
<img src="./img/logo.png" style="width:100px;" />
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
<span><h1 style="padding: 30px; margin-left:130px;"><u>My</u> Website bby</h1></span>
</div>
</div>
</body>
your css should be like that :
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.body img{
display: inline;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background:#6BD326;
}
#logo {
float:left;
display:inline-block;
}
nav li {
float:left;
padding: 0px 20px;
list-style-type: none;
}
your mistake was here
.branding {
float: left;
}
inside the <div class="branding"> you can add another div with a style:
display: flex;
justify-content: space-between
Please run your code through the W3C Validator.
You will see that you have an error in the HTML:
Error: Element h1 not allowed as child of element span in this context. (Suppressing further errors from this subtree.)
You can find out more about the distinction between block and inline elements at Can <span> tags have any type of tags inside them? which in turn references the formal spec at https://www.w3.org/TR/html401/struct/global.html#h-7.5.3
For your immediate problem you could make the span a div (which can have h1 elements as children) and make both it and the image inline-blocks.
try with flex and delete float:left
.branding {
display:flex;
align-items:center;
}

why is my nav not centred like everything else

just trying to make everything centred and clean. Everything seems to be, apart from my nav bar at the top which seems to stick to the right ?
I can't figure out why?
Thank you
html
..............
<div id="nav">
<ul>
<li>Home</li>
<li>Music</li>
<li>About</li>
<li>Musician</li>
<li>Instagram</li>
<li>Twitter</li>
<li>Contact</li>
</ul>
</div>
<!-- BELOW THE NAV -->
<div id="landing">
<br><br><br> <p>more content</p>
</div>
............
CSS
.............
body { font-family: 'Source Code Pro', monospace; color: white;
background-color: black;
margin: 0 auto;
max-width: 100%;
text-align: center;
}
div#container {
margin: 0 auto;
}
div#nav {
position: fixed;
vertical-align: top;
display: inline-block;
}
li {
display: inline;
}
div#landing {
background-image: url('rose1.png');
background-size:cover;
background-position:center;
background-repeat:no-repeat;
height: 100%;
margin: 0 auto;
}
not sure why the nav is floating to the right ?
Try this:
div#nav {
position: fixed;
vertical-align: top;
display: block;
margin: 0 auto;
top: 0;
left: 0;
right: 0;
}
I just update your code with few CSS changes. I hope it'll help you out. Thanks
Remove display: inline-block; and add width: 100%; in div#nav.
div#nav {
position: fixed;
vertical-align: top;
width: 100%;
}
Also remove default padding-left from ul.
ul {
padding-left: 0;
}
body {
font-family: 'Source Code Pro', monospace; color: white;
background-color: black;
margin: 0 auto;
max-width: 100%;
text-align: center;
}
div#container {
margin: 0 auto;
}
div#nav {
position: fixed;
vertical-align: top;
width: 100%;
}
ul {
padding-left: 0;
}
li {
display: inline;
}
div#landing {
background-image: url('rose1.png');
background-size:cover;
background-position:center;
background-repeat:no-repeat;
height: 100%;
margin: 0 auto;
}
<div id="nav">
<ul>
<li>Home</li>
<li>Music</li>
<li>About</li>
<li>Musician</li>
<li>Instagram</li>
<li>Twitter</li>
<li>Contact</li>
</ul>
</div>
<!-- BELOW THE NAV -->
<div id="landing">
<br><br><br> <p>more content</p>
</div>
From the div#nav CSS block just remove the position property:
div#nav {
vertical-align: top;
display: inline-block;
}

Floated div not aligning in the middle vertically?

I floated the search_and_avatar div right, and used display and vertical-align properties to align it in middle of parent container. But it sites on the bottom. Why is that?
Here is the complete code in fiddle
HTML:
<header>
<h1>CSS-Tricks</h1>
<nav id="main_nav">
<ul>
<li>Blog</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
</header>
CSS:
header {
background-color:#151515;
overflow:hidden;
height: 100px;
}
header h1, img, #search_form {
float:left;
}
#main_nav a {
color:#FFF;
text-decoration:none;
}
header img {
width:35px;
display: table-cell;
vertical-align: middle;
}
#search_and_avatar {
display: flex;
justify-content: flex-end;
flex: 1;
display: table;
display: table-row;
float:right;
}
header form {
text-align:right;
display: table-cell;
vertical-align: middle;
}
Added flex property into header
body {
margin:0;
font-family: 'Source Sans Pro', sans-serif;
}
ul {
padding:0;
}
header {
background-color:#151515;
overflow:hidden;
height: 100px;
display:flex;
flex:1 1 0;
align-items: center;
justify-content: center;
}
h1 {
color:#FFF;
font-family: "Arial Rounded MT", "Helvetica Rounded", Arial, sans-serif;
font-size: 40px;
font-weight: normal;
line-height:25px;
padding: 10px 20px 0px;
flex: 1;
}
#main_nav a {
color:#FFF;
text-decoration:none;
}
#main_nav {
display: table;
text-align:center;
margin:0px auto;
}
#main_nav ul {
display: table-row;
}
#main_nav ul li {
padding: 15px;
display: table-cell;
vertical-align: middle;
top: 50%;
display:inline;
}
header img {
width:35px;
display: inline-block;
vertical-align: middle;
}
#search_and_avatar {
/*display: flex;
justify-content: flex-end;
flex: 1;
display: table;
display: table-row;
float:right;*/
}
header form {
text-align:right;
display:inline-block;
vertical-align: middle;
}
<div id="wrapper">
<header>
<h1>CSS-Tricks</h1>
<nav id="main_nav">
<ul>
<li>Blog</li>
<li>Videos</li>
<li>Almanac</li>
<li>Snippets</li>
<li>Forums</li>
<li>Shop</li>
<li>Lodge</li>
<li>Jobs</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
<!--
<ul id="account_nav">
<li>Log In</li>
<li>Sign Up</li>
</ul>
-->
</header>
</div>
Here is fiddle Demo
the normal DOM rendering is that it adds element to next line. This can be easily fixed by wrapping both search_and_avatar and nav_main inside new main_nav_div Div.
Remove the padding from main_nav and have that in main_nav_id
Here is the complete code in Fiddle https://jsfiddle.net/sd2eqkuc/2/
<nav id="main_nav">
<ul>
<li>Blog</li>
<li>Videos</li>
<li>Almanac</li>
<li>Snippets</li>
<li>Forums</li>
<li>Shop</li>
<li>Lodge</li>
<li>Jobs</li>
</ul>
</nav>
<div id="search_and_avatar">
<form action="index.html" method="post" id="search_form">
<input type="text" id="search" name="search">
</form>
<img src="images/avatar.png">
</div>
#main_nav {
display: table;
text-align:center;
margin:0px auto;
float:left;
}
#main_nav_div
{
padding-top: 35px;
}

CSS / HTML Navigation and Logo on same line

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>