Horizontal menu center elements - html

I have the following files:
html file
<nav>
<ul>
<li><b>Alle Rezepte</b></li>
<li><b>Alle Zutaten</b></li>
</ul>
</nav>
css file
nav {
}
nav ul {
text-align: center;
}
nav ul li {
display: inline;
}
And it looks like this in the browser
But I want the two elements two be center in their own half, like this
|----------li1-----------|---------li2------------|
instead of
|---------------------li1|li2---------------------|
What do I have to add to my code have it the way I want it?

Try this:
http://jsfiddle.net/GCu2D/691/
CSS:
nav ul {
text-align: center;
margin:0;
padding:0;
list-style-type:none;
white-space:nowrap;
}
nav ul li {
float:left;
width:50%;
}

try like this: Updated Demo
nav {
margin:0 auto;
text-align: center;
width:100%;
background-color:#ccc;
border:1px solid #666;
}
nav ul {
margin:0 auto;
}
nav ul li {
display:inline-block;
padding:10px 15px;
list-style-type:none;
background-color:#ccc;
border-right:1px solid #666;
text-align: center;
}
nav ul li a {
font-weight:bold;
display:block;
text-align: center;
}

Use float and text-align:
li {float: left; width: 50%; text-align: center; list-style: none;}
li a {display: block;}
http://jsfiddle.net/5gLkdaw5/

Give the li a width.
see it all here http://codepen.io/mike-grifin/pen/bdVZJP
CSS:
nav ul {
top: 0;
left: 0;
}
nav ul li {
}
.example {
background-color: green;
display: inline-block;
padding: 20px;
margin: 0;
width: 46%;
text-align: center;
}
HTML:
<nav>
<ul>
<li class="example"><b>Alle Rezepte</b></li>
<li class="example"><b>Alle Zutaten</b></li>
</ul>
</nav>

See jsFiddle
You can use display:table;
nav ul {
text-align: center;
display:table;
table-layout:fixed;
width:100%;
}
nav ul li {
display: table-cell;
}

Related

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

how to center responsive navigation bar

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

HTML | Center menubar on screen

need some help. I'm just learning HTML and have been looking at how to do menus today, whilst doing so I have ran into a problem.
I can't seem to figure out how to center the menu on screen.
This is what I have so far;
<div id="navMenu">
<ul>
<li>Home</li>
<li>WWW.
<ul>
<li>Through the years</li>
</ul></li>
<li>Browsers
<ul>
<li>IE</li>
<li>Firefox</li>
<li>Chrome</li>
</ul></li>
<li>CSS
<ul>
<li>CSS 2.1</li>
<li>CSS 3</li>
</ul></li>
<li>Scripting
<ul>
<li>JavaScripts</li>
<li>jQuery</li>
</ul></li>
</ul>
</div>
Current CSS;
#navMenu{
margin:0;
padding:0;
}
#navMenu ul{
margin:0;
padding:0;
line-height:30px;
}
#navMenu li{
margin:0;
padding:0;
list-style:none;
float: left;
position: relative;
background-color: #999;
border-radius: 5px;
}
#navMenu ul li a{
text-align: center;
text-decoration:none;
height:30px;
width:150px;
display:block;
color: #FFF;
border: 1px solid #FFF;
text-shadow: 1px 1px 1px #000;
}
#navMenu ul ul{
position:absolute;
visibility:hidden;
top:32px;
}
#navMenu ul li:hover ul{
visibility:visible;
}
#navMenu li:hover{
background-color: #09F;
}
#navMenu ul li:hover ul li a:hover{
background-color: #CCC;
color: #000;
}
#navMenu a:hover{
color:#000;
}
The above produces this .... http://gyazo.com/55cf62d121ef16eb1248e5246d0accae
Anyway to get the menu bar in the center of the screen.
SIDE NOTE:: Any spoilers tags on this site?
Try using display: inline-block and text-align: center.
Set display: inline-block for the <ul> and text-align: center for the container #navMenu. Set text-align: left for #navMenu li to fix the submenus.
Also
Example
#navMenu{
margin:0;
padding:0;
text-align: center;
}
#navMenu ul{
margin:0;
padding:0;
line-height:30px;
display: inline-block;
}
#navMenu li{
margin:0;
padding:0;
text-align: left;
float: left;
list-style:none;
position: relative;
background-color: #999;
border-radius: 5px;
}
Add a width to your menu wrapper and give the left and right an automatic margin.
You can adjust the width depending on where in the center you want to have your nav positioned.
#navMenu {
width: 960px;
margin:0 auto;
padding:0;
text-align: center;
}
The margin and paddings your have in the ul and li are not needed.
#navMenu ul{
list-style: none;
line-height:30px;
display: inline-block;
text-align: center;
}
#navMenu li{
display: inline;
}
Padding should go here to keep all your links even.
#navMenu li a {
display: block;
padding: 10px;
}

How can I make my horizontal navigation bar a drop down menu?

I've tried making horizontal drop down navigation bars following tutorials, however they are never centered and I can't figure out how to center them. I tried going in the opposite direction and centering my navigation bar first, and then attempting to make it a drop down menu, though this seems to throw everything off. This is the code I have.
EDIT: The problem I am having is that the submenu is displayed when the page is loaded, along with a bullet point, which I'm sure can be fixed by setting the list-style to none, however I'm not sure where in the CSS this should be.
I'm trying to create a menu similar to THIS. I understand this uses joomla and I am not.
#header {
height: 100px;
margin-left: auto;
margin-right: auto;
text-align: center;
}
#content {
max-width: 700px;
margin-left: auto;
margin-right: auto;
padding: 20px;
}
#footer {
height: 85px;
padding-top: 40px;
margin-left: auto;
margin-right: auto;
text-align: center;
}
#menu {
margin: 0 auto;
display: inline-block;
list-style: none;
padding: 0;
border-top: 1 solid #ccc;
border-left: 1 solid #ccc;
border-bottom: 1 solid #ccc;
}
#menu li {
float: left;
}
#menu li a {
display: block;
padding: 10px 10px;
text-decoration: none;
font-weight: bold;
}
#menu li a:hover {
color: #c00;
}
<ul id="menu">
<li>Home
</li>
<li>Kandi
<ul>
<li>Claim Kandi
</li>
</li>
<li>Events
</li>
<li>Artists
</li>
<li>Community
</li>
<li>Merchandise
</li>
</ul>
Add this CSS:
#menu, #menu ul {
margin:0 auto;
padding:0;
}
#menu li {
float: left;
position: relative;
list-style: none;
}
#menu > li:hover > ul {
display: block;
}
#menu > li > ul {
display: none;
position: absolute;
}
#menu li a {
white-space: nowrap;
}
http://jsfiddle.net/tcKvH/1/
use this css
#menu{
position:absolute;
top:150px;
left:8%;
padding:0;
margin:0;
}
#menu ul{
padding:0;
margin:0;
line-height:30px;
}
#menu li{
position:relative;
float:left;
list-style:none;
background:rgba(0,0,0,1);
border-radius:5px;
}
#menu ul ul{
position:absolute;
visibility:hidden;
padding:0;
margin:0;
top:30px;
}
#menu ul li a{
text-align:center;
font:"Arial Black", Arial;
font-size:24px;
color:rgba(255,255,255,9);
width:150px;
height:30px;
display:block;
text-decoration:none;
}
#menu ul li:hover{
background-color:rgba(128,128,128,1);
text-decoration:none;
}
#menu ul li:hover ul{
visibility:visible;
z-index:1;
}
#menu ul li:hover ul li a{
background:rgba(0,0,0,9);
z-index:1;
border-bottom:1px solid rgba(160,160,164,1);
opacity:0.9;
text-decoration:none;
border-radius:5px;
}
#menu ul li ul li:hover{
background:rgba(128,128,128,1);
opacity:0.8;
text-decoration:underline;
}
with this html code
<div id="menu">
<ul>
<li>Home</li></ul>
<ul>
<li>Video <!--This is in main menu-->
<ul>
<li>Technology</li> <!--This is in drop downmenu-->
<li>Tutorial</li> <!--This is in drop downmenu-->
</ul>
</li>
</ul>