How to make a CSS horizontal navigation menu? - html

I need to make a css navigation according to the following style:
DESIRED LOOK
Below are the designs that I have done:-
1)Exhibit A - made using sprites
Note: Ignore the arrangement of the menu items
Pros: works well and has the desired look
Cons: if there is a need to add another menu item, another image must be manually made for that particular menu item. ie. not extensible
2)Exhibit B
Pros: very extensible. If another menu item must be added, new extra images need not be made. Only the menu name need to be typed in the html code.
Cons: the hover effect is not the same as the desired look.
My Requirement
Is to use Exhibit B, along with the hover effect from Exhibit A, but without having to add extra images when a menu item is created(this is what happens in Exhibit A, although it has the desired hover effect).
My approach:
Start working with Exhibit B
For the hover effect in the case of a single menu item use 3 images
a)left most edge
b)repeating slice of the middle area
c)right most edge
Is this correct ?
Is this possible ?
Is there a better way? A link to a tute would be fine.
Thanks
1] css code for Exhibit A
#charset "UTF-8";
* {
margin:0;
padding:0;
list-style: none;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
img {
border: none;
}
.clear {
clear:both;
}
.nav-container {
width: 960px;
}
#navMenu{
display: inline;
margin: 0;
padding: 0px;
position: relative;
z-index: 5;
}
#navMenu li{
float: left;
display: inline;
}
#navMenu li.navRepeat{
float: left;
display: inline;
background-image:url("../images/navigation_repeat.gif");
width:425px;
height:40px;
}
#navMenu li.navRepeatEnd{
float: right;
display: inline;
background-image:url("../images/navigation_repeat_end.gif");
width:1px;
height:40px;
}
a.navReservations{
display:block;
float:left;
width:89px;
height:40px;
background-repeat:no-repeat;
background: url("../images/reservations.gif")
}
a.navReservations:hover{
background: url("../images/reservations.gif") 0 40px;
}
a.navRentals{
display:block;
float:left;
width:62px;
height:40px;
background-repeat:no-repeat;
background: url("../images/rentals.gif")
}
a .navReservations {
float: left;
display: inline;
height: 100px;
width: 400px;
}
a.navRentals:hover{
background: url("../images/rentals.gif") 0 40px;
}
a.navTariffs{
display:block;
float:left;
width:59px;
height:40px;
background-repeat:no-repeat;
background: url("../images/tariffs.gif")
}
a.navTariffs:hover{
background: url("../images/tariffs.gif") 0 40px;
}
a.navFleet{
display:block;
float:left;
width:64px;
height:40px;
background-repeat:no-repeat;
background: url("../images/fleet.gif")
}
a.navFleet:hover{
background: url("../images/fleet.gif") 0 40px;
}
a.navTools{
display:block;
float:left;
width:56px;
height:40px;
background-repeat:no-repeat;
background: url("../images/tools.gif")
}
a.navTools:hover{
background: url("../images/tools.gif") 0 40px;
}
a.navReports{
display:block;
float:left;
width:71px;
height:40px;
background-repeat:no-repeat;
background: url("../images/reports.gif")
}
a.navReports:hover{
background: url("../images/reports.gif") 0 40px;
}
a.navSystem-Management{
display:block;
float:left;
width:133px;
height:40px;
background-repeat:no-repeat;
background: url("../images/system_management.gif")
}
a.navSystem-Management:hover{
background: url("../images/system_management.gif") 0 40px;
}
2] css code for Exhibit B
#navigation {
width: 959px;
height: 36px;
margin: 0;
padding: 0;
background-image: url(images/navigation-bg.gif);
background-repeat: no-repeat;
background-position: left top;
border: 1px solid #CCC;
}
#navigation ul {
list-style: none;
margin: 0;
padding: 0;
}
#navigation ul li {
display: inline;
margin: 0px;
}
#navigation ul li a {
height:27px;
display: block;
float: left;
color: #000;
text-decoration: none;
font-family: Arial;
font-size: 12px;
font-weight: bold;
background-image: url(images/navigation-separator.gif);
background-repeat: no-repeat;
background-position: right center;
padding-top: 6px;
padding-right: 15px;
padding-left: 15px;
vertical-align: 10%;
padding-bottom: 4px;
}
#navigation ul li a:hover {
color:#FFF;
background-image: url(images/navigation-hover.gif);
background-repeat: repeat-x;
background-position: left top;
}
#navigation ul li#active a {
color:#363636;
background: url(images/navigation-hover.png) repeat-x left top;
}

Well you technically only need two sprites, a wide left + body of the tab and a right side. By wide, I mean, 400px or some arbitrarily wide size that you don't anticipate hitting. You're trading a kb for easy of use. You can accomplish this by having markup like:
<ul class="list">
<li>Text</li>
</ul>
with css like:
ul.list
{
list-style-type: none;
margin: 0;
}
ul.list li
{
float: left;
background: url(leftpluswide.png) top left no-repeat;
}
ul.list li a
{
background: url(right.png) top right no-repeat;
}
The only caveat is that since the right.png will be overlapping the background on the li, you'll need to make sure that it doesn't have any transparency.
Also for completeness sake, you might need to apply a height to the li and the a (which will potentially require a display:inline-block or a line-height to take it) to make everything line up well.

Related

<li> with 2 elements not align properly why?

Hi I am struggling to align my numbers under my images they don't want to be aligned. this is a list and in the each line there an image and a number.
.category_icon { height:100px; width:300px; position:absolute; left:350px; top:150px;
ul { list-style:none; height:70px; width:220px;
li { display: inline; margin-right: 4px; width:65px; height:90px;
img { width:65px; height:64px;}
span { font-family:Tahoma; font-weight:bold; font-size:12px; position:absolute; bottom:7px; width:64px; }
}
}
}
<div class="category_icon">
<ul>
<li><img src="<%=atm.buttonPath%>/family_category_btn.png" alt="" /><span>346</span></li>
<li><img src="<%=atm.buttonPath%>/camping_category_btn.png" alt="" /><span>12</span></li>
<li><img src="<%=atm.buttonPath%>/cooking_category_btn.png" alt="" /><span>546</span></li>
</ul>
Thanks you if someone can help me thanks.
solution with text below
solution with centralised text below
for images, you always have to change their diplay properties before you start playing around with them....add the same here and li to display: inline-block; that would help
To do :
li {
display: inline-block; /*changed */
margin-right: 4px;
width:65px;
height:90px;
text-align:center;
}
img {
display:block; /*added */
width:65px;
height:64px;
}
PS : I am not aware of LESS...so gave you a generalized solution
http://jsfiddle.net/Qm6Bm/
.category_icon {
height: 100px;
width: 300px;
position: absolute;
left: 350px;
top: 150px;
ul {
list-style: none;
height: 70px;
width: 220px;
li {
display: inline-block; /* CHANGED */
margin-right: 4px;
width: 65px;
height: 90px;
text-align: center; /* ADDED */
img {
display: block; /* ADDED */
width: 65px;
height: 64px;
margin-bottom: 8px; /* ADDED */
}
span {
font-family: Tahoma;
font-weight: bold;
font-size: 12px;
/* Removed positioning and width */
}
}
}
}
I made the li an inline-block and the img a block. This means the image will push the text (from the span) under it. Then, by putting text-align: center in the li, that text (from the span) is centred nicely.
I put margin-bottom: 8px on your img just like how you had the span's bottom: 8px :P

CSS - want to remove a blank space but have no idea how to get rid of it?

OK so here is my code, I don't understand why the first blank space is appearing to the left hand side of the page.
CSS
.header-left ol{
width: auto;
float: left;
display: block;
background-color: #6899D3;
}
.header-left li{
list-style:none;
float:left;
}
.header-left a {
color:#000;
display:block;
height: 50px;
text-align:center;
vertical-align:middle;
text-decoration:none;
min-width: 100px;
overflow: auto;
border-radius: 20px;
}
.header-left a:hover {
color:#000000;
text-decoration:none;
}
#nav1 a:hover { background-color: #FF5B0D; }
#nav2 a:hover { background-color:#00FF40;}
#nav3 a:hover { background-color:#FF0080;}
#nav4 a:hover { background-color:#00CCFF;}
#nav5 a:hover { background-color: #FFFF00; }
HTML
<div class="header-left">
<ol>
<li id="nav1">a</li>
<li id="nav2">b</li>
<li id="nav3">c</li>
<li id="nav4">d</li>
<li id="nav5">e</li>
</ol>
</div>
You need to reset the natural padding and margin of the body and ol. Set:
body{
margin: 0;
padding: 0;
}
.header-left ol{
width: auto;
float: left;
display: block;
background-color: #6899D3;
margin: 0; //RESET DEFAULT
padding: 0; // RESET DEFAULT
}
Add padding-left:0; to your .header-left ol rules.
.header-left ol {
width: auto;
float: left;
display: block;
background-color: #6899D3;
padding-left:0;
}
jsFiddle example
The reason to that is because u are using <ol><li></li></ol> which by default has a left padding. Even if you set list-type:none, you are not getting rid of the left padding. Instead, in the ol code block set padding:0; or padding-left:0 like this:
.header-left ol{
width: auto;
float: left;
display: block;
background-color: #6899D3;
padding-left:0;
}
This should solve your problem.
In your margin try putting in something like this as this will drag it up and pull the bottom up
This is to be put in your styles CSS sheet
margin: -30px 0px -30px 0px;

Fixed position navigation bar width and overlap issue

Hello fellow community members,
I am currently having a problem where I have set my top navigation bar to be fixed so that it follows users as they scroll down my web page but I am currently faced with two issues.
The content of my site is overlapping my navigation bar and goes ontop of it making it look messy and not function as designed. (The background is not transparent but rather a solid image)
The website is fluid and I have been fiddling with this for hours now trying to get it to work but as I set a min-width and max-width it doesn't seem to obey the min-width and width as a percentage (100% of the container). Sorry if I am a little unclear in explaining this but I have attached some code in jsfiddle for people to see.
If you would like to see where I am stuck at right now you can do so at subnovaled.com (the blue bar along the top) also the jsfiddle link is as follows:
#navMainWrapper {
height: 32px;
font-size: 12px;
position: fixed;
min-width: 1000px;
max-width: 1280px;
width: 100%;
background: cyan;
}
http://jsfiddle.net/jXJMx/23/
Note: the above code was done in a rush, sorry
Thank you to anyone who is able to help me out!!
check this fiddle:
http://jsfiddle.net/jXJMx/26/
CSS:
#navMainWrapper {
height: 32px;
font-size: 12px;
position: fixed;
min-width: 750px;
max-width: 1280px;
width: 100%;
background: cyan;
}
#navMainSearch {
float:left;
width:120px;
}
#navMain {
float:left;
width:750px;
line-height:normal;
height:32px;
}
#navMain ul {
float:right;
list-style:none;
}
#navMain li {
display:inline;
}
#navMain a {
float:left;
text-decoration:none;
}
#navMain a span {
float:left;
display:block;
padding: 7px 15px 0 15px;
text-align:center;
width:90px;
cursor:pointer;
height:25px;
}
/* Commented Backslash Hack hides rule from IE5-Mac \*/
#navMain a span {
float:none;
}
/* End IE5-Mac hack */
#navMain a:hover {
background-position:0% -32px;
}
#navMain a:hover span {
background-position:100% -32px;
}
#content {
background:yellow;
min-width:750px;
width:100%;
height:768px;
position:relative;
margin-top:32px;
}
Update CSS as show
#navMainWrapper {
height: 32px;
font-size: 12px;
position: fixed;
min-width: 1000px;
max-width: 1280px;
width: 100%;
background: cyan;
}
#navMainSearch {
float:left;
width:120px;
}
#navMain {
float:left;
line-height:normal;
height:32px;
}
#navMain ul {
list-style:none;
}
#navMain li {
display:inline;
}
#navMain a {
float:left;
text-decoration:none;
}
#navMain a span {
float:left;
display:block;
padding: 7px 15px 0 15px;
text-align:center;
/*width:90px;*/
cursor:pointer;
height:25px;
}
/* Commented Backslash Hack hides rule from IE5-Mac \*/
#navMain a span {
float:none;
}
/* End IE5-Mac hack */
#navMain a:hover {
/* background-position:0% -32px;*/
}
#navMain a:hover span {
/* background-position:100% -32px;*/
}
#content {
background:yellow;
width:100%;
float:left;
height:1500px;
}

How to make nav bar stay in the middle

I'm trying to centralize my nav bar, I've tried text align center, and margin auto, but it stays fixed to the left. I also tried to add a width, but still it stays fixed. Thanks in advance for your help. Please check out the JSFIDDLE. The HTML is as follows:
<section class="contain">
<div id="section-nav" class="section-nav">
<div class="top">
<ul>
<li class="logo">Magna Contra</li>
<li class="active">Festival: Paris</li>
<li>Festival: Paris</li>
<li>Festival: Paris</li>
<li>Festival: Paris</li>
</ul>
</div>
</section>
And the CSS:
ul
{
list-style-type:none;
margin:0;
padding:0;
}
li
{
display:inline;
padding:15px;
text-align: center;
margin: auto 0;
}
li a{
text-decoration: none;
color:#bbbbbb;
font-family: "proxima-nova",sans-serif;
text-transform: uppercase;
text-align: center;
font-size: 0.78em;
letter-spacing: .2em}
li a:hover{
color:white;
}
.logo a{
color:#bbb;
font-size: 0.78em;
text-decoration: none;
text-transform: uppercase;
}
.logo a:hover{
color:white;
}
.active a{
color:white;
}
.container {
display: table;
width:980px;
height: 100%;
}
.contain{
display: table;
width: 100%;
text-align: center;
margin: 0 auto;
}
.block {
display: table-row;
height: 1px;
}
.navigation {
display: inline-block;
padding: 10px;
width:100%;
margin: auto;
height: 150px;
}
.top {
background-color: #444;
width:100%;
display: inline-block;
padding: 10px;
text-align: left;
}
.navigation:nth-child(odd) {
background: #222;
}
.push {
height: auto;
}
.container {
margin: 0 auto;
text-align: center;
}
.block:nth-child(odd) {
background: #fff;
}
.search {
border:0px;
background-color:transparent;
color:white;
display: inline-block;
height:28px;
}
.section-nav a{-webkit-transition:400ms;-moz-transition:400ms;-o-transition:400ms;transition:400ms;color:#bbb;font-weight:700;outline:0;text-decoration:none}
.section-nav a.active,.section-nav a:hover{color:#fff;text-decoration:none}
The easiest option is to add text-align: center to the ul:
ul
{
list-style-type:none;
margin:0;
padding:0;
text-align: center;
}
I would also set the lis to display: inline-block to give you more control over their styling.
Your div "section-nav" is not closed. I would fix this first.
You have also applied text-align:left to your .top div, which is the main container for the navigation buttons.
In the given fiddle you have
.top {
text-align: left;
}
change it to
.top {
text-align: center
}
fidd ->http://jsfiddle.net/ztyUF/2/ Is this what u ve asked?
I used this when I had the same problem and have pretty much duplicated it for multiple sites. He explains it much better than I can.
http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support

How to horizontally center an unordered list of unknown width?

It is common to have a set of links in a footer represented in a list, such as:
<div id="footer">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
I want everything inside div#footer to be centered horizontally. If it was a paragraph, you would just easily say: p { text-align: center; }. Or if I knew the width of the <ul> I could just say #footer ul { width: 400px; margin: 0 auto; }.
But how do you center the unordered list items without setting a fixed width on the <ul>?
EDIT: clarification - the list items should be next to each other, not below.
The solution, if your list items can be display: inline is quite easy:
#footer { text-align: center; }
#footer ul { list-style: none; }
#footer ul li { display: inline; }
However, many times you must use display:block on your <li>s. The following CSS will work, in this case:
#footer { width: 100%; overflow: hidden; }
#footer ul { list-style: none; position: relative; float: left; display: block; left: 50%; }
#footer ul li { position: relative; float: left; display: block; right: 50%; }
Use the below css to solve your issue
#footer{ text-align:center; height:58px;}
#footer ul { font-size:11px;}
#footer ul li {display:inline-block;}
Note: Don't use float:left in li. it will make your li to align left.
One more solution:
#footer { display:table; margin:0 auto; }
#footer li { display:table-cell; padding: 0px 10px; }
Then ul doesn't jump to the next line in case of zooming text.
It depends on if you mean the list items are below the previous or to the right of the previous, ie:
Home
About
Contact
or
Home | About | Contact
The first one you can do simply with:
#wrapper { width:600px; background: yellow; margin: 0 auto; }
#footer ul { text-align: center; list-style-type: none; }
The second could be done like this:
#wrapper { width:600px; background: yellow; margin: 0 auto; }
#footer ul { text-align: center; list-style-type: none; }
#footer li { display: inline; }
#footer a { padding: 2px 12px; background: orange; text-decoration: none; }
#footer a:hover { background: green; color: yellow; }
Try wrapping the list in a div and give that div the inline property instead of your list.
The answer of philfreo is great, it works perfectly (cross-browser, with IE 7+). Just add my exp for the anchor tag inside li.
#footer ul li { display: inline; }
#footer ul li a { padding: 2px 4px; } /* no display: block here */
#footer ul li { position: relative; float: left; display: block; right: 50%; }
#footer ul li a {display: block; left: 0; }