Centering a navigation bar CSS - html

I have a nav bar at the top of each page on my site. Here are the HTML and CSS:
HTML
<ul>
<li><a class="active" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>
CSS
ul {
z-index: 100;
position: fixed;
list-style-type: none;
width: 80em;
height: 3em;
margin-left: auto;
margin-right: auto;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
width: 20em;
margin-right: auto;
margin-left: auto;
text-decoration: none;
overflow: hidden;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
I've also set up a CodePen here: https://codepen.io/njpbray/pen/Rerabv
The bar isn't centered at all and it has an extra hanging portion to the left that doesn't react to hovering.
I'm not sure what's going on here. I thought that making something of width 80em and settings left and right margins to auto would make it rest center. I also thought an 80em width bar with 4 subdivision of 20em would be equally divided, but there's that bit on the left.
I would prefer to keep the bar fixed at the top though if possible. Some pages are long, and I want the user to be able to access the nav bar anywhere on the page.

To centralise the links, remove the float: left from li and set it to display: inline-block. Then simply set text-align: center on the <ul>.
The above is enough to centralise the blocks, but your content is still offset to the right of the page. To correct this, significantly shrink your width values. em is based on the font size, which is a good policy for text, but a poor choice for width. You can use percentages for responsiveness, or alternatively go with a fixed px unit depending on how 'indented' you want the links to be. I've gone with 100px for the links in my example.
And as you're making use of a <ul> element, you'll also want to remove the default padding, by setting padding: 0 on ul. This removes the left 'offset' you're seeing.
Finally, don't forget to set margin: 0 on body to get rid of that 8px of whitespace around the edges.
This can all be seen in the following:
body {
margin: 0;
}
ul {
z-index: 100;
position: fixed;
list-style-type: none;
width: 100%;
height: 3em;
margin-left: auto;
margin-right: auto;
background-color: #333;
text-align: center;
padding: 0;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
width: 100px;
margin-right: auto;
margin-left: auto;
text-decoration: none;
overflow: hidden;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
<ul>
<li><a class="active" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>

You have the position set to fixed in your code. What you want is to set it to center.
ul {
z-index: 100;
position: center;
list-style-type: none;
width: 80em;
height: 3em;
margin-left: auto;
margin-right: auto;
background-color: #333;
}
https://codepen.io/anon/pen/dgGMva
I think you might be interested in going through the CSS tutorials at w3shcools. You will benefit immensely from this:
https://www.w3schools.com/Css/

The better way to handle what you want to achieve is to wrap you top bar in an element which take the entire width 100% of the browser with it position property set to fixed and add the list as a sub element.
And the space at the left of the first link it's due to the fact that by default the body tag have some margin and padding. You must remove it. the ul element have also same space padding and margin
html, body {
margin: 0;
padding: 0;
}
header {
width: 100%;
position: fixed;
background-color: blue;
}
ul {
z-index: 100;
list-style-type: none;
width: 80em;
height: 3em;
margin: 0 auto;
padding: 0;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
width: 20em;
margin-right: auto;
margin-left: auto;
text-decoration: none;
overflow: hidden;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
<header>
<ul>
<li><a class="" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>
</header>

It's better with css3 and display:flex instead of float:left and inline-block
<header>
<ul>
<li><a class="active" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>
</header>
<main>
</main>
body {
margin: 0;
padding: 0;
}
header {
display: flex;
justify-content: center;
}
ul {
background-color: #333;
display: flex;
list-style-image: none;
list-style-type: none;
margin: 0;
padding: 0;
position: fixed;
z-index: 100;
}
li a {
align-items: center;
color: white;
display: flex;
justify-content: center;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
width: 20em;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
main {
height: 1500px;
}
https://codepen.io/anon/pen/qJbZMv

As mentioned before: CSS3 and flex-box is supported by almost all relevant web-browsers. Use that instead. Here is a link for learning.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Related

Why are my flex items wrapping off the screen?

I have a flex nav and it's wrapping to the left and partially off of the screen. Does anyone with more experience know what could be causing this? I can't figure out why it's doing this.
header {
background-color: rgba(255, 165, 0, .8);
border-bottom: 12px solid black;
display: flex;
flex-flow: row wrap;
align-content: center;
}
header h1 {
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0, 0, 0, .3);
border: 8px solid black;
}
header h1,
nav a {
font-weight: 700;
font-family: arial;
}
header nav {
display: none;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li {
text-align: center;
margin: 1px;
border: 1px solid white;
border-radius: 15px;
}
a {
text-decoration: none;
}
a:visited,
a,
h1 {
color: white;
}
#media screen and (min-width: 400px) {
header {
height: 120px;
display: flex;
justify-content: space-between;
}
header h1 {
margin: 0 0 0 8%;
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
}
header nav {
display: block;
align-self: flex-end;
}
nav ul {
display: flex;
justify-content: flex-end;
margin: 0 8% 0 0;
}
nav ul li {
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu {
position: absolute;
display: none;
top: 38px;
white-space: nowrap;
left: 0;
background-color: rgba(255, 165, 0, .8);
border: 1px solid rgba(0, 0, 0, .02);
box-shadow: 5px 5px 5px 2px rgba(0, 0, 0, .3);
}
.characters:hover .drop-menu {
display: block;
}
.drop-menu li {
margin: 0;
border-radius: 0;
}
footer nav {
display: none;
}
}
<header>
<h1>Seraph Chronicles</h1>
<nav>
<ul class="main-nav">
<li class="main-nav-item">Home</li>
<li class="main-nav-item">About</li>
<li class="main-nav-item characters">
Characters
<ul class="drop-menu">
<li>Ethan Clarke</li>
<li>Serena Kiriaga</li>
<li>Marcus Flynn</li>
<li>Emily Ashdown</li>
<li>Director Miles West</li>
</ul>
</li>
<li class="main-nav-item">Author</li>
</ul>
</nav>
</header>
https://jsfiddle.net/ca75sqzc/17/
Short Answer
Don't use percentages on flex item margins. Use another unit, such as px or em.
revised demo
Explanation
When you make the primary container (.header) a flex container, its children become flex items.
These are the two children: h1 and nav (red borders added below)
Each nav item (li) has a horizontal margin (1% on each side).
nav ul li{
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
This causes them to overflow the container.
Then, because the container has justify-content: flex-end, the items are aligned to the container's right edge. This means the overflow occurs on the left side (see image above).
On smaller screens, the nav element wraps to the left edge of the header, and the overflowing items go out of view:
If you switch to justify-content: flex-start, the items overflow on the right side.
But the real problem is actually this:
Why isn't the ul container expanding to accommodate the li children?
The answer appears to be the use of percentages for the horizontal margins.
nav ul {
display: flex;
justify-content: flex-end;
margin: 0 8% 0 0;
}
nav ul li {
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
The container is not recognizing this unit on the margins and, therefore, not expanding.
Note that the flexbox spec recommends against using percentage margins and padding on flex items.
4.2. Flex Item Margins and
Paddings
Authors should avoid using percentages in paddings or margins on flex
items entirely, as they will get different behavior in different browsers.
Once you switch to non-percentage units on your margins, everything seems to work.
Change the following in your CSS:
nav ul{
display: flex;
/* justify-content: flex-end; */
margin: 0 8% 0 0;
}
Just erase justify-content: flex-end; from the rule for nav ul in the media query section:
header {
background-color: rgba(255, 165, 0, .8);
border-bottom: 12px solid black;
display: flex;
flex-flow: row wrap;
align-content: center;
}
header h1 {
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0, 0, 0, .3);
border: 8px solid black;
}
header h1,
nav a {
font-weight: 700;
font-family: arial;
}
header nav {
display: none;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li {
text-align: center;
margin: 1px;
border: 1px solid white;
border-radius: 15px;
}
a {
text-decoration: none;
}
a:visited,
a,
h1 {
color: white;
}
#media screen and (min-width: 400px) {
header {
height: 120px;
display: flex;
justify-content: space-between;
}
header h1 {
margin: 0 0 0 8%;
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
}
header nav {
display: block;
align-self: flex-end;
}
nav ul {
display: flex;
margin: 0 8% 0 0;
}
nav ul li {
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu {
position: absolute;
display: none;
top: 38px;
white-space: nowrap;
left: 0;
background-color: rgba(255, 165, 0, .8);
border: 1px solid rgba(0, 0, 0, .02);
box-shadow: 5px 5px 5px 2px rgba(0, 0, 0, .3);
}
.characters:hover .drop-menu {
display: block;
}
.drop-menu li {
margin: 0;
border-radius: 0;
}
footer nav {
display: none;
}
}
<header>
<h1>Seraph Chronicles</h1>
<nav>
<ul class="main-nav">
<li class="main-nav-item">Home</li>
<li class="main-nav-item">About</li>
<li class="main-nav-item characters">
Characters
<ul class="drop-menu">
<li>Ethan Clarke</li>
<li>Serena Kiriaga</li>
<li>Marcus Flynn</li>
<li>Emily Ashdown</li>
<li>Director Miles West</li>
</ul>
</li>
<li class="main-nav-item">Author</li>
</ul>
</nav>
</header>
https://jsfiddle.net/7dubc44L/

How to wrap parent div around float:left child div?

I'm trying to get the headerLinks div to wrap around both headerLink divs so that I can move the links and edit the margins of the links as a whole. Is this the best way to do it? And if so, how would I fix my code? Thank you in advance.
This is my code:
#header {
background-color: #EDEDED;
position: fixed;
width: 100%;
top: 0px;
box-shadow: rgb(0, 0, 0) 0px 0px 10px;
}
#headerItems {
list-style-type: none;
overflow: hidden;
margin: 10px;
padding: 0px;
}
#headerName {
float: left;
display: inline-block;
color: #3D3D3B;
font-size: 28px;
}
.headerLinks {
display: inline-block;
float: right;
}
.headerLink {
text-align: center;
margin: 5px;
float: right;
}
.headerLink a {
color: black;
padding: 5px;
background-color: #E1E1E1;
text-decoration: none;
font-size: 20px;
}
<div id="header">
<ul id="headerItems">
<li id="headerName">My name</li>
<div id="headerLinks">
<li class="headerLink">Link 1
</li>
<li class="headerLink">Link 2
</li>
</div>
</ul>
</div>
Only the links on the right should be in a ul.
And I'm pretty sure you don't want the links reversing the order,
So you will need to add float:right on the parent (headerLinks), and float left on the lis
* {box-sizing: border-box;}
body {margin: 0;}
#header {
background-color: #EDEDED;
position: fixed;
width: 100%;
top: 0px;
box-shadow: rgb(0, 0, 0) 0px 0px 10px;
padding: 10px;
}
#headerName {
float: left;
display: inline-block;
color: #3D3D3B;
font-size: 28px;
}
#headerLinks {
list-style-type: none;
overflow: hidden;
padding: 0;
margin: 0;
float: right;
}
.headerLink {
text-align: center;
margin: 5px;
float: left;
}
.headerLink a {
color: black;
padding: 5px;
background-color: #E1E1E1;
text-decoration: none;
font-size: 20px;
}
<div id="header">
<span id="headerName">My name</span>
<ul id="headerLinks">
<li class="headerLink">Link 1</li>
<li class="headerLink">Link 2</li>
</ul>
</div>
If you want to make a dropdown menu, you will nest another ul inside the li

Issue to horizontally center a menu

I'm trying to center (horizontally) my horizontal menu to the middle of the page but without success. I've put the menu in a container that has margins left and right set to auto, but that doesn't work. Thanks for your help
http://jsfiddle.net/nB6x4/
/***** MENU *******/
.menu-container {
position: absolute;
top: 20px;
margin-right: auto;
margin-left: auto;
width: 100%;
}
nav {
float: right;
margin: 20px auto;
width: 100%;
}
nav ul {
margin-right: -4px;
margin-left: 5px;
}
nav ul li {
display: inline-block;
margin-right: -4px;
margin-left: 5px;
vertical align: top;
}
nav a {
padding: 7px 10px;
text-decoration: none;
color: rgba(255,255,255,0.9);
background: rgba(0,0,0,0);
border-radius: 5px;
font-weight: 300;
text-transform: uppercase;
letter-spacing: 1.5px;
font-size: 14px;
font-weight: 400;
}
nav a:hover {
background: rgba(0,0,0,0.25)
}
.activeNav {
background: rgba(0,0,0,0.25)
}
nav ul li ul {
position: absolute;
display: block;
margin-top: 5px;
border-radius: 5px;
border-top-left-radius: 0;
background: none;
padding-top: 5px
}
nav ul li ul li {
display: block;
float: none;
margin: 0;
padding: 0
}
nav ul li ul li a {
display: block;
text-align: left;
color: rgba(255,255,255,0.9);
text-shadow: 0 1px rgba(0,0,0,0.33);
padding: 10px
}
nav ul li ul li a:hover {
background: rgba(20,150,220,0.5);
color: white;
text-shadow: 0 1px rgba(0,0,0,0.5)
}
.hover a {
display: block;
}
.hover span {
color: rgba(255,255,255,0.9);
background: rgba(20,150,220,0.5);
border-radius: 5px;
position: absolute;
display: block;
margin: 5px 0 0 -57px;
padding: 10px;
font-size: 13px;
font-weight: 300;
letter-spacing: 1.5px;
text-transform: uppercase;
text-align: center;
cursor: default;
}
/**** END MENU ****/
Try this
Actually you need to make position relative and give it some width initially
.menu-container {
position: relative;
top: 20px;
margin: o auto;
max-width: 800px;
}
Hope this help, else try putting your html
You will need to specify a width in order for margin: 0 auto; to work.
http://jsfiddle.net/nB6x4/3/
Updated CSS:
nav {
margin: 20px auto;
width: 624px;
}
Alternatively, you could use text-align: center; along with display: inline-block; to accomplish this with content with an unknown width:
http://jsfiddle.net/nB6x4/4/
nav {
margin: 20px 0;
text-align: center;
}
nav ul {
padding: 0;
margin-right: -4px;
margin-left: 5px;
display: inline-block;
}
nav
{
float: right;
margin: 20px auto;
width: 100%;
text-align: center;
}
you can simple add text-align center to nav
Here is the Fiddle
it is not responsive, because you did not provide a responsive code
There are a few things you need to re-evaluate.
First of all, an absolutely positioned container can not be centered with margin, as margin: 0 auto; only effects relatively positioned elements.
So the very first thing you need to do is delete the position: absolute; on .menu_container.
.menu-container {
top: 20px;
margin-right: auto;
margin-left: auto;
width: 100%;
}
Then, with the div relatively positioned, you have the property width set to 100%. This makes the div take up the full amount of width available to it from whatever element is it's parent. Adding margin: 0 auto; doesn't do anything and the left and right sides of the element are already touching the sides of the parent. You can't center something that "fits perfectly" so to speak in it's parent.
So there are a few things you can do. You can shrink the size of the container, say to 80%, and then it will start to center the container element, but not necessarily centering your nav links.
The better option in my opinion is to use text-align: center; to center the LI's since they are using display: inline-block. They retain their block characteristics but are also treated as "normal text" so to speak.
Simply changing the selector to what I have below should be all you need...
nav ul {
text-align: center;
margin-right: -4px;
margin-left: 5px;
}
JS Fiddle Demo

Z-Index not working on top-navigation menu and sub menu

I have a z-index issue on top-navigation with a menu and its sub-menu, i want menu to overlap sub-menu, i have set z-index of menu higher than sub-menu, but it is not working sub-menu is overlapping menu as default.
Please see and suggest any possible way to do it.
jsFiddle
HTML
<div id="login">
<ul>
<li id="overlap">Log In | Join
<ul class="tsm">
<li>Log In</li>
<li>Join</li>
</ul>
</li>
</ul>
</div>
CSS
#login {
margin: 0px auto;
width: 1000px;
height: 38px;
background: #343438;
}
#login ul {
float: right;
margin: 0px;
padding: 0px;
width: auto;
height: 38px;
}
#login ul li {
float: left;
width: auto;
height: 34px;
display: inline;
list-style-type: none;
}
#login ul a {
color: #FFF;
display: block;
width: auto;
height: 34px;
font: bold 16px/34px "Arial Narrow", Arial, sans-serif;
text-decoration: none;
text-align: center;
padding: 0px 15px;
text-shadow: 0px -1px #000;
}
#login .tsm {
padding: 20px;
background-color: #F2F2F4;
width: 230px;
height: auto;
text-align: left;
border: 4px solid #777;
position: absolute;
visibility: hidden;
top: 32px;
right: 0px;
z-index: 100;
}
#login .tsm li a {
width: 230px;
height: 30px;
margin-bottom: 1px;
text-align: left;
padding: 0px;
text-decoration: none;
color: #000;
text-shadow: 0px 1px #fff;
font: 15px/30px Arial, sans-serif;
border-bottom: 1px solid #DDDDDF;
}
#login ul #overlap {
position: relative;
border: 2px solid #900;
z-index: 1000;
}
#login ul li:hover .tsm {
visibility: visible;
}
#login ul ul li {
border: none;
}
The problem stems from the fact that you're trying to position the parent above its child, which causes problems - if the parent moves up a level, so do its children. Instead of z-indexing the parent list, you need to z-index the login link (a tag):
http://jsfiddle.net/SaNJA/
The code is rough, but it should get you started.
Make sure you add a background colour to the link, otherwise you'll still be able to see through it.

Resizing window makes tabs go to next line

Here is my css and html
#header {
position: absolute;
left: 0;
right: 0;
top: 0;
background-image: url('/public/images/header.png');
background-repeat: repeat-x;
}
#nav {
background: transparent;
height: 2.5em;
left: -25px;
list-style: none;
margin: 1em 0 1em;
min-width: 100%;
min-height: 100%;
overflow: hidden;
padding: 60px 0 30px 0;
}
#nav a {
color: white;
display: block;
float: left;
height: 2.5em;
padding-left: 30px;
text-decoration: none;
padding-right: 30px;
text-shadow: 0.1em 0.1em #333;
}
#nav a:hover {
text-shadow: 0.1em 0.1em white;
background-color: white;
color: darkred;
padding-bottom: 5px;
text-shadow: 0.1em 0.1em lightgray;
}
#nav a:hover.active, #nav a.active {
background-color: white;
background-position: 0 -60px;
color: darkred;
text-shadow: 0.1em 0.1em lightgray;
padding-bottom: 5px;
}
#nav li {
float: left;
margin: 0 8px 0 0;
/* Spacing between tabs */;
}
#nav span {
cursor: pointer;
display: block;
float: left;
line-height: 1.5em;
padding: .5em 5px 0 0;
}
<div id="header">
<div id="navigation">
<ol id="nav">
<li><a id="overview" href="/overview"><span>Overview</span></a></li>
<li><a id="analysis" href="/overview" class="active"><span>Analysis</span></a></li>
<li><a id="dashboard" href="/dashboard"><span>My Dashboard</span></a></li>
<li><a id="preferences" href="/overview"><span>Preferences</span></a></li>
<li><a id="contact" href="/overview"><span>Contact</span></a></li>
<li><a id="logout" href="/overview"><span>Sign In</span></a></li>
</ol>
</div>
</div>
This looks fine on my screen but if I start resizing the window it makes the tabs jump around and go to the next line. I've found that on other sites with tabs this is not the case. What am I doing wrong here? Thanks.
These "other sites" probably have their <div id="header"> contained in another element, such as <div id="container">, with an explicit width set with CSS, such as width: 960px:
See: http://jsfiddle.net/mBJQN/
<div id="container">
YOUR HTML HERE
</div>
#container {
width: 960px;
position: relative
}
I added position: relative so that the position: absolute on #header will be relative to #container (see here).
There is another option here, but it's probably not the one you're after.
You can add white-space: nowrap to #header, and change from floats to display: inline-block. If this is the option you want, let me know, because it needs a little more work.
http://jsfiddle.net/mBJQN/1/