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
Related
I'm kind of new to HTML and CSS. I have a nav bar that I want (the logo and links) aligned to being above the sidebar on my page. It's in different positions on different screens (and when browser is resized) and I don't know how to fix it. I made a fiddle thing and I'll link to the site so that you will know what I mean.
This is the fiddle: The Fiddle
This is the page: The page
#header {
width: 100%;
height: 91px;
margin: 0;
top: 0;
}
#menu {
float: right;
width: 100%;
height: 54px;
margin-top: 0;
background: #ffffff url(http://i1378.photobucket.com/albums/ah105/WinPhanNick/menu_bar_zps6212d723.jpg) repeat-x left top;
position: fixed;
}
#menu ul {
margin: 0;
padding: 0px 0 0;
list-style: none;
line-height: normal;
margin-left: 28%;
}
#menu li {
display: inline;
text-align: center;
}
#menu a {
display: block;
float: left;
height: 36px;
padding: 18px 20px 0px 20px;
text-decoration: none;
text-align: center;
text-transform: uppercase;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
color: #ffffff;
}
li#logo {
height: 52px;
width: 52px;
background: url(http://i1378.photobucket.com/albums/ah105/WinPhanNick/cb_logo_zpsd1b14443.png) no-repeat;
margin-top: 1px;
margin-right: 1px;
margin-left: 1px;
}
#menu a:hover,
#menu .active a {
background: #1687ef;
color: #FFFFFF;
}
#logo {
float: left;
width: 270px;
height: 76px;
margin: 0px;
padding: 15px 0px 0px;
margin-left: 27%;
}
Make your link wrapper (which is the ul) the same width as your page container (which is 892px) and set it to margin: auto to center it. You can remove the margin-left: 28% as well. That's not needed. You also have a float issue so your ul is collapsed. You need to clear your floated elements. You can add overflow:hidden.
#menu ul{
overflow: hidden; //clear float issue
width: 892px; // same width as #page
margin: 0 auto; //will center
padding: 0px 30px; //add to keep the same padding as #page-bgtop
}
FIDDLE
I am trying to move an LI Nav bar to the top right of a page, however when I position: absolute; and top: 0; on my <li> I get my list which is also display: inline; all stacked on top of each other. They go to the part of the screen I want because I'm also float: right; but they won't stay in a line.
Any ideas?
HTML(html and doctype and link tags left out, they're there):
<div id="search">Google Search</div>
<div id="lucky">I'm feeling lucky</div>
<form>
<input id="search_box" type="text" name="">
</form>
<ul>
<li class='nav'>+You</li>
<li class='nav'>Gmail</li>
<li class='nav'>Images</li>
</ul>
CSS:
#search {
font-family: Arial;
font-size: 10;
font-weight: bold;
border: 1px solid black;
width: 7.2em;
white-space: nowrap;
padding-left: 4px;
padding-top: 3px;
padding-right: 4px;
padding-bottom: 4px;
margin: 240px 14px 100px 225px;
display: inline-block; }
#lucky {
font-family: Arial;
font-size: 10;
font-weight: bold;
padding-left: 4px;
padding-top: 3px;
padding-right: 4px;
padding-bottom: 4px;
border: 1px solid black;
display: inline-block; }
#search_box {
width: 600px;
position: absolute;
left: 65px;
top: 212px; }
ul {
list-style-type: none;
margin: 0;
padding: 0; }
.nav {
display: inline-block;
float: left;
padding: 20px;
position: absolute; }
The li's are stacked on top of each other because you are absolute positioning them to the same place. You want to make the ul absolute positioned instead. Include the code below in your css and for good measure you should also tell include something that it is positioned relative to.
ul {
list-style-type: none;
margin: 0;
padding: 0;
position: absolute;
top: 0;
right: 0;
}
.nav {
display: inline-block;
/* float: left;*/
padding: 20px;
/*position: absolute;*/ }
body{position: relative;}
Try this http://jsfiddle.net/csdtesting/oe55maf0/2/
Added #header :
#header{
float:right;
}
and removed position:absolute; from .nav class .Also added #content :
#content{
position:absolute;
}
to contain the form .
Now the structure is clear.
Hope this helps!
How do I make the hover background-color fill the entire height of the navigation bar plus the padding of the text instead of only the text.
I want to make it look like this.
Fiddle
What am I missing? Thank you in advance!
Here you go. I've created a jsfiddle for you. Its working good:
http://jsfiddle.net/cRjF4/3/
Updated CSS:
<style>
.fa {
font-size: 15px;
margin-top: -10px;
margin-bottom: -10px;
}
.navbar-height {
height: 65px;
}
.clearboth {
clear: both;
}
/* workbar styles */
.fixed-top {
position: fixed;
top: 0;
right: 0;
left: 0;
background: #FFF;
text-align: center;
height: 67px;
margin: 0 auto;
width: 100%;
}
.workbar {
text-align: center;
margin: 0 auto;
background-color: #ffe6e6;
}
.worknav {
list-style: none;
padding-left: 0px;
margin: 0 auto;
text-align: center;
}
.worknav> li {
text-align: center;
display: inline-block;
padding-left: 20px;
padding-right: 20px;
padding-top: 11px;
padding-bottom: 11px;
}
.worknav> li:hover a {
color: yellow;
}
.worknav>li:hover {
background-color: black;
}
.worknav > li > a {
text-decoration: none;
color: #666;
}
.centered {
float: none;
margin-left: auto;
margin-right: auto;
}
.worktop {
margin-top: 70px;
}
</style>
If you wish to colour both the padding and the text try something like:
a:hover{
background-color:yellow;
color:red;
}
Try to do Anchor link css as
.worknav > li > a {
text-decoration: none;
display:inline-block;
color: #666;
line-height:65px;
}
Check on http://jsfiddle.net/kka284556/cRjF4/9/
You can do it by applying padding on anchor tag. Remove height on ul and li tag and instead of height just apply padding on anchor. And also apply background-color on anchor.
It will work i think. If I am right then let me know i'll share some code if you want.
So I have a horizontal navigation bar styled in CSS. My issue is that there seems to be some bottom padding on the bar, but I do not know where it is coming from.
Additionally, when the nav bar becomes really narrow (resizing the window, mobile, etc), it overflows out of the encircling div. How can I prevent this?
Here is the JSFiddle:
http://jsfiddle.net/m8pWa/
CSS:
html, body {
margin: 0;
padding: 0;
background-color: #E0F2F7;
}
.header {
margin: 0 auto;
text-align: center;
background-color: #81BEF7;
padding: 1px;
color: #FAFAFA;
}
ul {
margin: 0;
padding: 0;
}
.nav {
margin: 0 auto;
padding: 0;
width: 75%;
height: 2em;
}
.nav ul li {
list-style-type: none;
display: inline;
overflow: hidden;
}
.nav li a {
display: block;
float: left;
padding: 5px 10px;
color: #fff;
text-decoration: none;
border-right: 1px solid #fff;
}
.nav li a:hover {
background: white;
color: #A4A4A4;
}
.nav-wrapper {
background-color: #58ACFA;
padding: 1px;
}
.wrapper {
margin-right: auto;
margin-left: auto;
width: 75%;
}
/* this centers the internal elements */
.centered {
text-align: center;
}
/* gives a main content window to the left that is 70% of the main */
.content {
width: 70%;
float:left;
}
/* makes a sidebar to the right that is 30% of the main and floating right */
.sidebar {
width: 30%;
float:right;
}
.sidebar ul {
padding: 10px;
}
Your elements should fill the space naturally, and .nav should not depend on an explicit height. This will allow for things like font-size to change, etc., and prevent the children from overflowing the parent.
Here's the solution: http://jsfiddle.net/m8pWa/3/
Remove the height on .nav.
On .nav li a, change display: block; to display: inline-block;, and remove float: left;.
.nav {
margin: 0 auto;
padding: 0;
width: 75%;
}
.nav li a {
display: inline-block;
padding: 5px 10px;
color: #fff;
text-decoration: none;
border-right: 1px solid #fff;
}
Nothing to do with padding this time. You have your height set at a fix height I changed it to 1.74ems and it was spot on.
.nav {
margin: 0 auto;
padding: 0;
width: 75%;
height: 1.74em;
}
For the nav collapsing when the screen size is a set width,
You do not need to worry unless the screen size is 379px in width and to be honest, no screen is that small unless it is a mobile device.
In that case if you will need to support mobile devices and should look into media queries.
JSFIDDLE
i guess the problem came from the height of 1px you added in thenav
margin: 0 auto;
padding: 0;
width: 75%;
height: 2em;
}
i made changed it to 1.8em and it solved it
margin: 0 auto;
padding: 0;
width: 75%;
height: 1.8em;
}
also it depends also on the monitors, they differ in size therefor their display
Here you can see the code: http://jsfiddle.net/LQSK3/1/
I can't get display: inline; working there for every li element.
Also got the problem width the line.png image, as it's height is the same as the font height, I want it to has 35px height with margin left and right set to 5px.
Where is the problem?
You need to update your style sheet. Please add this new style:
#menu {
position: relative;
clear: both;
top: -3px;
background-color: coral;
border: 1px solid black;
width: 800px;
height: 35px;
float:left;
}
li { display: inline;float:left; }
#menu ul {
position: absolute;
font-family: Century Gothic, sans-serif;
font-size: 14px;
list-style-type: none;
padding: 0;
margin: 9px 0 0 123px;
width: 649px;
height: 39px;
color: #FFF;
float:left;
}
a { font-weight: bold; color: red; text-decoration: none; }
a:hover { text-decoration: underline; }
#menu a {
background: url('http://i.imgur.com/rzNj0.png') top right no-repeat;
width: 65px;
height: 18px;
float: left;
margin: 0px 5px;
}
You need to add float: left; to menu div,ul,li and a . Also should set width and height and margin of the a tag.
Here is a working sample : http://jsfiddle.net/YjeBs/
Hope this helps :)
EDIT:
If you want your line to extend from top to bottom of the menu div you can change your styles to:
#menu {
position: relative;
clear: both;
top: -3px;
background-color: coral;
border: 1px solid black;
width: 800px;
height: 35px;
float:left;
}
li {
float: left;
height: 35px;
display:inline;
}
#menu ul {
color: #FFFFFF;
float: left;
font-family: Century Gothic,sans-serif;
font-size: 14px;
height: 35px;
list-style-type: none;
margin: 0 0 0 123px;
padding: 0;
position: absolute;
width: 649px;
}
a { font-weight: bold; color: red; text-decoration: none; }
a:hover { text-decoration: underline; }
#menu a {
background: url("http://i.imgur.com/rzNj0.png") no-repeat scroll right top transparent;
float: left;
height: 29px;
margin: 0 5px;
padding-top: 8px;
width: 65px;
}
Hope this is you want :)
just change li { diplay: inline; } with li { display: inline; } it works.