how to center responsive navigation bar - html

i cant get this navigation bar to center. Please help.
Here is my html:
<div class="nav">
<ul id="menu">
<li>Home</li>
<li>Latest News</li>
<li>Results & Events</li>
<li>Fundraising</li>
<li>Gallery</li>
</ul>
</div>
Here is my css:
#nav {
margin:0px auto; }
ul {
display: inline-block;
list-style-type:none;
margin:0 auto;
padding:0;
position: absolute; }
li {
display:inline;
float: left;
margin-right: 1px; }
li a {
display:inline-block;
min-width:140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: Century Gothic, Candara, Tahoma, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none; }
li:hover a {
background: #659d32; }
li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 40px;
line-height: 40px; }
li:hover ul a:hover {
background: #659d32;
color: #fff; }
li ul {
display: none; }
li ul li {
display: block;
float: none; }
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px; }
ul li a:hover + .hidden, .hidden:hover {
display: block; }
.show-menu {
font-family: Century Gothic, Candara, Tahoma, sans-serif;
text-decoration: none;
color: #fff;
background: #659d32;
text-align: center;
padding: 10px 0;
display: none; }
input[type=checkbox]{
display: none; }
input[type=checkbox]:checked ~ #menu{
display: block; }
#media screen and (max-width : 760px){
ul {
position: static;
display: none;
}
li {
margin-bottom: 1px;
}
ul li, li a {
width: 100%;
}
.show-menu {
display:block;
}
}
I tried adding a div around the whole thing and setting margin to auto but that hasnt worked. Im not sure what to do.

Firstly you're targeting #nav while it's .nav according to your html.
Then you need to set a width to your nav parent in order to have a centered position.
Working example
.nav {
margin:0px auto;
width:705px;
}
Edit: Since your list items and links aren't responsive, it doesn't make sense to add a percentage to the parent nav.
You can use something like this to have a responsive centered navigation:
.nav {
margin:0px auto;
width:90%;
}
ul {
list-style-type:none;
padding:0;
}
li {
float: left;
width:18%;
}
li a {
width:100%;
}

I think the reason why your auto margins didn't work is because you need to define a width on your wrapper. Also make sure that you are using the right selector # for id and . for class. Here's how id go about centering the navigation.
jsFiddle
<div class="wrapper">
<div class="nav">
<ul id="menu">
<li>Home</li>
<li>Latest News</li>
<li>Results & Events</li>
<li>Fundraising</li>
<li>Gallery</li>
</ul>
</div>
</div>
add this to your css:
.wrapper {
width:80%;
margin-left:auto;
margin-right:auto;
}

You almost had it, use:
.nav {
display: block;
margin: 0 auto;
width: 100%;
max-width: 960px;
}
You will simply need to adjust your max-width to move the navbar into the center (depending the width of your page container).
See Example

Related

How to add a dropdown link to an existing responsive navigation menu

I have an existing responsive nav menu that I want to add a sub-menu to (actually it is just one link under one of the top menu items). Sounds like it should be very EASY, but I cannot figure it out. As soon as I add the link, it either ends up just below the top item (making the whole nav grow down with it) or displaying "none" makes it disappear and not come back on hover. Is there a simple way to do this with CSS only? I hope my question is clear enough. I will include my necessary code. If you give me code, please tell me where to put it. I am a Newbie. Thanks so much for any help.
HTML:
<nav><a href="index.html">
<div id="logo"><img src="images/logo-text.png" alt="CBS Stuctures, Inc."></div>
</a>
<label for="drop" class="toggle">MENU</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>HOME</li>
<li>COMPLETED PROJECTS</li>
<li>STRUCTURES
<ul>
<li>Video Presentation</li>
</ul>
</li>
<li>NEW PRODUCTS</li>
<li>CONTACT</li>
</ul>
</nav>
CSS:
nav {
height: auto;
margin: 0;
padding: 0;
background-color: #000;
}
#logo {
display: block;
float: left;
padding: 0;
}
nav:after {
content: "";
display: table;
clear: both;
}
nav ul {
display: inline-block;
font-size: 1.5em;
list-style: none;
float: right;
}
nav ul li {
display: inline-block;
float: left;
}
nav a {
display: block;
padding: 10px 20px;
color: #fff;
text-decoration: none;
}
.toggle, [id=drop] {
display: none;
}
nav a:hover {
color: #70E4FC;
}
nav ul li ul{
display: none;
}
nav ul li ul:hover{
display: block;
}
#media (max-width: 1024px) {
#logo {
display: block;
width: 100%;
text-align: center;
float: none;
padding: 0;
}
nav ul{
width: 100%;
padding:0;
margin:0;
float: none;
background-color: rgba(16,70,56,1.00);
}
nav ul li {
width: 100%;
box-sizing: border-box;
padding: 10px;
background-color:rgba(11,51,41,1.00);
}
nav ul li:hover{
background-color:#0F4739;
}
.toggle + a, .menu{
display: none;
}
.toggle{
display:block;
background-color: #333333;
padding: 14px;
font-size: 1.5em;
cursor: pointer;
}
.toggle:hover {
background-color:#515151;
}
[id^=drop]:checked + ul{
display: block;
}
}
You can simply add a hover selector on child ul inside li of a parent ul
ul li:hover ul {display: block;}
To apply it to specific element, you must add a class to your child ul:
ul li:hover ul.childul {display: block;}
check working example here

css-menu not working properly

I am working on a new version of a website. Right now I am almost satisfied with the results, except one problem. The website consists of a header (with logo and menu) and a content-part. I want it to work on both larg/small-size screens.
On a small screen I encounter one problem. When I expand the menu it is on top of the content-part, but the content-part is still clickable, so the menu-options are not clickable and the menu is not working right.
What I want is as follows (for small screen, i.e. Smartphone):
- When I click 'Show Menu' all menu-options will show up (Menu1, Menu2, Menu3, Menu4) and are clickable. Underlying text (div 'page') not visible (dissapears under the menu-options).
- When I click one of the menu-options, all options in submenu will show up and are clickable. Underlying text (div 'page') not visible.
I already searched and tried many options, but not with the expected result.
Thanks in advance for any hints :)
My html is as follows:
<body>
<div id="wrapper">
<div id="logo">
<img src="#" style="width:180px;height:50px;">
</div>
<div id="menu">
<label for="show-menu" class="show-menu">Show Menu</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu">
<li>Home</li>
<li>Menu1 ↓
<ul class="hidden">
<li>Submenu11</li>
<li>Submenu12</li>
</ul>
</li>
<li>Menu2 ↓
<ul class="hidden">
<li>Menu21</li>
<li>Menu22</li>
<li>Menu23</li>
</ul>
</li>
<li>Menu3 ↓
<ul class="hidden">
<li>Menu31</li>
<li>Menu32</li>
<li>Menu33</li>
</ul>
</li>
<li>Menu4 ↓
<ul class="hidden">
<li>Menu41</li>
<li>Menu42</li>
<li>Menu43</li>
<li>Menu44</li>
<li>Menu45</li>
<li>Menu46</li>
</ul>
</li>
</ul>
</div>
<div id="header">
<h3>Header</h3><hr>
</div>
<div id="pageliquid">
<div id="page">Page-text<br />
</div>
</div>
</div>
</body>
My CSS is as follows:
body {
margin: 20px;
font-family: Tahoma, Arial;
font-size: 12pt;
color: #001245;
}
#wrapper {
width: 100%;
min-width: 1000px;
max-width: 2000px;
margin: 0 auto;
}
#logo {
height: 50px;
width: 100%;
position: static;
}
#menu {
height: 50px;
width: 100%;
position: static;
}
#header {
height: 75px;
width: 100%;
position: static;
}
#pageliquid {
width: 100%;
position: static;
}
#page {
top: 200px;
max-width: 100%;
position: absolute;
overflow: scroll;
bottom: 1px;
left: 20px;
right: 0px;
}
ul, img {
list-style-type:none;
margin:0;
padding:0;
position: absolute;
}
li {
display:inline-block;
float: left;
margin-right: 1px;
}
li a {
display:block;
min-width:200px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: small;
color: #fff;
background: #0b0b3b;
text-decoration: none;
}
li:hover a {
background: #0b0b3b;
}
li:hover ul a {
background: #08088a;
color: #d8d8d8;
height: 40px;
line-height: 40px;
}
li:hover ul a:hover {
background: #08088a;
color: #ffffff;
}
li ul {
display: none;
}
li ul li {
display: block;
float: none;
}
li ul li a {
width: 160px;
min-width: 100px;
padding: 0 20px;
}
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
.show-menu {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #fff;
background: #19c589;
text-align: center;
padding: 10px 0;
display: none;
}
input[type=checkbox] {
display: none;
-webkit-appearance: none;
}
input[type=checkbox]:checked ~ #menu{
display: block;
}
#media screen and (max-width : 760px) {
body {
margin: 0px;
}
#wrapper {
min-width: 0px;
margin: 0;
}
#header {
position: absolute;
}
#page {
left: 0px;
}
ul {
position: absolute;
width: 100%;
display: none;
}
li {
margin-bottom: 1px;
margin-right: 0px;
}
ul li, li a {
width: 100%;
}
li ul li a {
width: 100%;
padding: 0 0px;
}
.show-menu {
display:block;
}
}
The problem is with the z-index of the menu since it is underneath later elements on the page due to the fixed height and natural height of #menu and absolute positioning of that menu. You have duplicate ID's here, but forcing a higher z-index on #menu puts the navigation above other elements on the page. That also requires changing position: static; to position: relative; on #menu as well. Here's a demo - http://codepen.io/anon/pen/MJwoOL (PS - there were some overlapping issues with your nested li's and ul's that I tweaked a little in your media query, too, but are unrelated to your question here)

how do I remove this black space/box at the end of my menu

I'm making a simple ebook site so me and my friends can access my ebooks and all that jazz. My menu has this black box or space on the end and I'm not sure why. I tried resizing the overall menu and I don't know any way I can make the box act like the home button. It's perfect, there's no space near it. Just at the end. html and css for reference.
<body>
<img src="logo2.png" class="logo" />
<div class="br" />
<ul class="menu">
<li class="list">Home</li>
<li>Ebooks
<ul class="dropdown1">
<li>Case studies, theses, academia, educational</li>
</ul>
</li>
<li>Extras
<ul class="dropdown2">
<li>test</li>
</ul>
</li>
<li>News</li>
<li>Site map</li>
</ul>
<div class="content">
<br>
<p>test</p>
<br>
</div>
</body>
</html>
And the css:
body{
background-color:#0e0d0d;
}
#font-face {
font-family: Lato-Light;
src: url('Lato-Light.ttf');
}
#font-face {
font-family: Lato-Light-Italic;
src: url('Lato-LightItalic.ttf');
}
img.logo {
width: 500px;
height: auto;
display: block;
margin-left: auto;
margin-top:60px;
margin-right: auto
}
div.br {
margin-top:60px;
}
ul{
padding:0px;
font-family:Lato-Light;
background: #000000;
color:#f9a724;
width:535px;
margin-left:auto;
margin-right:auto;
}
ul li{
padding:0px;
margin:0px;
display: inline-block;
position: relative;
line-height: 21px;
text-align: center;
}
ul li a{
display: block;
padding: 8px 25px;
color: #f9a724;
text-decoration: none;
}
ul li a:hover{
color: #000000;
background: #f9a724;
}
ul li ul.dropdown1 {
min-width: 150px; /* Set width of the dropdown */
max-width:350px;
background: #000000;
display: none;
position: absolute;
z-index: 999;
}
ul li ul.dropdown2 {
min-width: 150px; /* Set width of the dropdown */
max-width:200px;
background: #000000;
display: none;
position: absolute;
z-index: 999;
}
ul li:hover ul.dropdown1 {
display: block; /* Display the dropdown */
}
ul li ul.dropdown1 li {
display: block;
}
ul li:hover ul.dropdown2 {
display: block; /* Display the dropdown */
}
ul li ul.dropdown2 li {
display: block;
}
div.content {
width:535px;
background: #000000;
margin-left:auto;
margin-right:auto;
}
p {
color: #f9a724;
text-align:center;
word-wrap: break-word;
margin-right:50px;
margin-left:50px;
font-family:Lato-Light;
}
https://jsfiddle.net/mncvhoz9/
If you don't want to have to worry about getting down to exact pixel numbers, and/or making each item the same width, you should be able to add the following declarations, though I'm not sure how well it's supported by all the browser versions (it's been a while since I've looked into it):
ul {
display: table;
}
li {
display: table-cell;
}
Example with those added to your code: https://jsfiddle.net/nfqs0j0u/
Just add the following CSS in your file
ul li a{
display: block;
padding: 9px 29.5px;
color: #f9a724;
text-decoration: none;
}
it's not a black box it is the background of your ul element you can simply change the width of your ul to remove this black area:
ul {
width: 488px;
}
ul have margin top and bottom, because of that it has black space.So give like this,it will work.
div.br ul.menu{
margin-bottom:0px
}
Working Demo
If you don't need to reduce the width of ul, then you should set the width of li.
ul li{ width: 103px; }
Please try this one:
Css code like this:
ul{
padding:0px;
font-family:Lato-Light;
background: #000000;
color:#f9a724;
width:488px;
margin-left:auto;
margin-right:auto;
}
Demo

css positioning drop down under parent

I have a dropdown list item in my navbar and can't get the dropdown section to align underneath the parent link. I am trying to use just css and know I've done it before, it's just stumping me at the moment. None of the other examples I've come across use the same menu format so it's been troubling trying to force fit pieces of code. Please help me with this easy solution
HTML
<div id="navbar">
<li>Home</li><!--
--><li>Link2</li><!--
--><li>Link3</li><!--
--><li><a href="#">Link4
<ul>
<li>SubLink1</li><br />
<li>SubLink2</li><br />
<li>SubLink3</li><br />
<li>SubLink4</li>
</ul>
</a></li><!--
--><li>Link5</li>
</div>
CSS
#navbar {
width:75%;
margin:0px auto;
text-align:right;
position:relative;
top:218px;
}
#navbar li {
list-style:none;
display:inline;
position:relative;
}
#navbar a {
background-color:#862D59;
font-size:18px;
width:60px;
margin:0px;
padding:10px 15px;
color:#FFF;
text-decoration:none;
text-align:center;
}
#navbar a:hover {
background-color:#602040;
border-bottom:solid 4px #969;
}
#navbar li ul {
display:none;
}
#navbar li:hover ul {
position:absolute;
display:block;
}
Working Example
https://jsfiddle.net/o6Ldutp5/
Firstly, you should use a reset css of some kind to remove the default margin / padding attached to ul & li.
Then validate your HTML, it contained a number of errors such as missing the opening ul etc.
Then it's just a matter of using position:absolute and appropriate values.
top:100% will place the menu directly below the li parent (with position:relative) regardless of the height of the li.
left:0 will align the left edge of the submenu to the left side of the parent li.
#navbar {
margin: 0px auto;
text-align: right;
}
ul,
li {
margin: 0;
padding: 0;
}
#navbar li {
list-style: none;
display: inline-block;
position: relative;
}
#navbar a {
background-color: #862D59;
font-size: 18px;
width: 60px;
margin: 0px;
padding: 10px 15px;
color: #FFF;
text-decoration: none;
text-align: center;
display: block;
}
#navbar a:hover {
background-color: #602040;
border-bottom: solid 4px #969;
}
#navbar li ul {
display: none;
position: absolute;
top: 100%;
left: 0;
}
#navbar li:hover ul {
display: block;
}
<div id="navbar">
<ul>
<li>Home
</li>
<li>Link2
</li>
<li>Link3
</li>
<li>Link4
<ul>
<li>SubLink1
</li>
<li>SubLink2
</li>
<li>SubLink3
</li>
<li>SubLink4
</li>
</ul>
</li>
<li>Link5
</li>
</ul>
</div>
I've written my own minimal CSS without the styling, try replacing your whole CSS with this -
I've also edited your HTML by removing the comments and <br /> tags
div#navbar li {
display: inline-block;
}
div#navbar li ul {
width: 200px;
position: absolute;
display: none;
top: 10px;
}
div#navbar li ul li {
display: block;
width: 150px;
}
div#navbar li:hover ul {
display: block;
}
ul,ol,li {
margin-left: 0;
padding-left: 0;
}
Here is the fiddle

Problems with display nav properly

I'm having trouble with adding space to the hovered "home" right/left.
Adding proper spacing so after the hovered section of "home" appears that about and the other pages would follow.
CSS:
nav {
width:460px;
height:50px;
background-color:#0066ff;
float: left;
margin: 15px 0 0 324px;
position: fixed;
}
nav ul {
margin: 0;
padding: 0;
position: fixed;
width:493px;
border: 1px solid green;
}
nav li {
float: left;
text-align: left;
margin:0;
padding: 0 0 0 24px;
display: block;
width: 51px;
height: 50px;
}
nav li:first-child {
float: left;
text-align: left;
margin:0;
padding: 0 15px 0 0;
display: block;
height: 50px;
}
nav a:first-child {
margin: 0;
padding: 0;
height: 50px;
min-width:51px;
display:block;
position: fixed;
line-height:50px;
float: left;
text-align: center;
}
nav a {
margin: 0;
padding: 0;
height: 50px;
min-width:51px;
display:block;
position: fixed;
line-height:50px;
float: left;
text-align: center;
}
nav ul li a:link, nav ul li a:visited {
text-decoration: none;
color:#fff;
display:block;
}
nav ul li a:hover, nav ul li a:active {
background: #929292;
text-decoration: none;
display:block;
}
This problem has been giving me headaches for hours.
Link Update
The blue space beside about can't happen.
Nick, your issue is in the li:first-child selector. Specifically the padding attribute, where it clears the padding, where you're missing the spacing.
Many of your :first-child selectors are redundant, and don't need to be re-specified.
Mixing position:fixed with float:left is generally not a good idea as your CSS will be fighting layout structure.
You only need a position:fixed for the main container, the rest the nav's children will be relative to that.
There's a lot of unnecessary padding and such, you should use your browser's DOM inspector to play with the layout.
Check this JSFiddle that's cleaned it up.
A lot of the time, a small <div> is placed to the left of the "home" link to push it over like so:
#fillerdiv{
width:20px;
background-color:#0066ff;
}
then you could place it like so:
<nav>
<ul>
<div id="fillerdiv"></div>
<li> Home</li>
<li>About</li>
<li>Work</li>
<li>Services</li>
<li>Clients</li>
<li>Contact</li>
</ul>
</nav>
That produces this:
Or you could give the "home" button a specific class and add extra padding for it alone.
#home{
padding-left:20px;
}
And the HTML:
<nav>
<ul>
<li id="home"> Home</li>
<li>About</li>
<li>Work</li>
<li>Services</li>
<li>Clients</li>
<li>Contact</li>
</ul>
</nav>
I played around your code a bit and tried to simplify it. I hope you don't mind.
JSFiddle
/* styles.css */
nav {
float: left;
background: #0066ff;
border: 1px solid green;
}
nav ul {
margin: 0;
padding: 0;
}
nav li {
float: left;
display: block;
}
nav a {
margin: 0;
padding: 0;
padding:20px;
color:#fff;
text-align: center;
}
nav ul li a:link, nav ul li a:visited {
text-decoration: none;
display:block;
}
nav ul li a:hover, nav ul li a:active {
background: #929292;
text-decoration: none;
display:block;
}