I've recently come across a nasty issue in IE8/IE9 (surprisingly, it aligns at it should in IE7). Please check out http://targettedmedia.co.uk/httpdocs
As you can see in Firefox/Chrome or other modern browser, on hover over the top menu, the submenu items should be vertically centered (regardless of one or 2-lined items). Yet, in IE8 and IE9 it just doesn't want to work. I've tried editing the code but to no avail.
Can you please help me? It's just getting too frustrating to handle it. I sincerely thank you in advance!
Here's a sample HTML sub-menu structure:
<ul id="nav" class="dropdown dropdown-horizontal">
<li>Health & Safety Courses
<ul class="sub-menu">
<li>NEBOSH National Diploma</li>
<li>NEBOSH Construction Certificate</li>
<li>NEBOSH Fire Certificate</li>
<li>NEBOSH Distance Learning Couse</li>
<li>IOSH Managing Safely</li>
<li>IOSH Working Safely</li>
<li>Site Managers Safety Training Scheme</li>
<li>Health & Safety Awareness for Employees</li>
<li>Health & Safety Awareness for Managers</li>
</ul>
</li>
</ul>
Here are the specific CSS bits that handle the vertical alignment:
ul.dropdown ul {
width:400px;
padding:20px 0 25px 7px;
background:#6c6c6c;
font-size:14px;
font-weight:normal;
}
ul.dropdown ul li {
font-weight: normal;
height:42px;
line-height:42px;
float:left;
margin:0 0 0 15px;
border-top:1px solid #484848;
}
ul.dropdown ul li a:link {
display:inline-block;
width:165px;
padding:0;
color:#fff;
text-align:left;
line-height:normal;
vertical-align:middle;
}
Avoid depending vertical-align: middle as it's not supported in many browsers. Instead, remove all your padding and margins from ul.dropdown ul li a:link and add padding to the <li> itself. If you add equal padding to the top and bottom (I would also do padding in em, that way the padding ratio looks the same no matter what size your user has their text set to) it will be equal all around.
ul.dropdown ul li {
border-top: 1px solid #484848;
float: left;
font-weight: normal;
height: 2em;
margin: 0 0 0 15px;
padding: 1em 0;
}
ul.dropdown ul li a {
color: #FFFFFF;
padding: 0;
text-align: left;
width: 165px;
}
Related
I am new to this and need a little help.
Just designing a random website for some practice and I can't seem to get my topmenu on my header to move up. I've changed padding's & margins in a few places and can't seem to get it to work. Wonder if I'm missing something or if there is a better way to organize my CSS. Any help would be greatly appreciated. Thanks :)
#main {
margin-left: auto;
margin-right: auto;
width: 800px;
}
body {
margin:0;
}
#topbar {
background-color: black;
height: 80px;
width: 100%;
margin-top: 0;
margin-bottom:0;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
#text {
display:block;
color: White;
float:right;
padding-right: 820px;
padding-top: 10px;
font-size:20px;
}
#topmenu {
display:block;
list-style:none;
padding:0 400px 0px 0px;
float: right;
}
#topmenu li {
display: inline;
}
#topmenu a {
position:relative;
padding:0 4px 0 4px;
font-size: 13px;
text-decoration:none;
}
#topmenu a, #topmenu a:visited, #topmenu a:active {
color:white;
}
I want the menu to move up ever so slightly.
<div id="topbar" >
<div id="text"><strong>My Template Website</strong></div>
<div id="topmenu">
<ul>
<li><a href=""/>Home</li>
<li><a href=""/>About Us</li>
<li><a href=""/>Services</li>
<li><a href=""/>Product</li>
<li><a href=""/>Contact Us</li>
</ul>
I assume you already reduced the margin-bottom of the headline and the margin-top of your topmenu to 0 and now you need a way of bringing the topmenu up a bit more without absolute positioning. What you might not know is, that it is allowed to use a negative margin on the menu to bring it up:
#topmenu {
/* ... your other styles */
margin-top: -15px;
}
Before applying this you shoud check the margin of your ul which probably has a margin by the browsers default stylesheet:
You can check this by yourself when debugging the page in the web console (all popular browsers have one; e.g. in Firefox you have to press F12).
I don't know what the rest of your site looks like and if the surrounding div#topmenu is really required. But if not, you could remove it and give the id to the ul, then remove the margin-top from your new #topmenu. The advantage of this is that you don't have to manage two different elements, which possibly was the reason for your confusion in this example.
<div id="topbar" >
<div id="text"><strong>My Template Website</strong></div> <!-- <h1> instead for semantic reasons? -->
<ul id="topmenu">
<li><a href=""/>Home</li>
<li><a href=""/>About Us</li>
<li><a href=""/>Services</li>
<li><a href=""/>Product</li>
<li><a href=""/>Contact Us</li>
</ul>
</div>
I'm still relatively new to coding, so this is probably an easy fix.
I have set up menu with 5 menu items, and submenu items under two of the primary menu items. If I line up the first submenu with its menu item, the second one is too far right. If I line up the second submenu with its menu item, the first one is too far right. Is there a way to make both submenus line up under their respective menu items?
Here is the HTML:
<ul id="menu">
<li>Home</li>
<li>About Me
<ul>
<li>Fairfax Psychological Associates</li>
<li>Credentials</li>
</ul></li>
<li>Publications
<ul>
<li>The Wisdom of the Five Messengers</li>
<li>Other Publications</li>
</ul></li>
<li>Location</li>
<li>Strategic Interactions</li>
And this is the CSS:
#menu {
width: 950px;
height:35px;
font-size: 20px;
font-family: cambria, Georgia, sans-serif;
font-weight: bold;
text-align: center;
background-color: #FFF;
border-radius: 0px;
margin-top: -175px;
margin-left: 25px;
}
#menu li {
display: inline;
padding: 10px;
}
#menu a {
text-decoration: none;
color: #2B297F;
padding: 10px 10px 10px 10px;
}
#menu a:hover {
color: #2B297F;
background-color: #999;
}
#menu li ul
{font-size:15px;
margin-left:-160px;
margin-top:25px;
position:absolute;
text-align:left;
display:none;}
#menu li:hover ul
{display:inline-block;
}
#menu li li
{list-style:none; display:list-item;}
#menu li li a
{color:#2B297F; text-decoration:none;white-space:nowrap;
}
#menu li li a:hover
{color:#2B297F; background-color: #999 text-decoration:none;}
The site is at http://kerryaltmantest.info if you want to see what I mean about the submenu. Thank you!
There are a few changes in css that need to be made:
#menu li ul {
font-size: 15px;
/* margin-left: -160px; REMOVE */
/* margin-top: 25px; REMOVE */
position: absolute;
text-align: left;
top: 30px; /* add this */
left: 0; /* add this */
padding: 0 /* add this */
display: none;
}
#menu li {
display: inline;
padding: 10px;
position: relative; /* add this */
}
The biggest reason that the ul is not positioning properly is because the li it is contained in did not have a position style set. When this happens, absolutely-positioned elements are positioned according to the first ancestor that has a position type set. Additionally, that was apparently not coming into effect because no positioning rules (top/left/bottom/right) were set in the ul. Adding these two things and resetting the margins/padding fixed the issue (css is directly editable/debuggable in chrome's debugger).
https://developer.mozilla.org/en-US/docs/Web/CSS/position:
Absolute positioning
Elements that are positioned relatively are still considered to be in
the normal flow of elements in the document. In contrast, an element
that is positioned absolutely is taken out of the flow and thus takes
up no space when placing other elements. The absolutely positioned
element is positioned relative to nearest positioned ancestor. If a
positioned ancestor doesn't exist, the initial container is used.
Chrome debugging information: https://developer.chrome.com/devtools/index
I am trying to code a horizontal navigation menu in CSS with custom bullet images.
I can get most things working, but the problem is that the custom bullet image (which is different for each navigation item) is not part of the clickable link.
I know I can move the A tag outside of the LI tag, but that is poor coding practice.
Does anyone know how I can make the custom bullet image be part of the clickable link? My gut instinct was to make sure of display:block, but then it messes up the horizontal layout.
Here is what I've got so far:
CSS:
#navlist {
list-style: none;
margin: 0;
padding: 20px 0 0px 20px;
}
#navlist ul {
margin: 0;
padding: 0;
}
#navlist li {
display:inline;
background-repeat: no-repeat;
background-position: 0 50%;
padding: 3px 0 3px 35px;
margin: 0 20px 0 .4em;
}
#navlist li a {
text-decoration:none;
}
#navlist li.contact {
background-image: url(img/contact.png);
}
#navlist li.about {
background-image: url(img/about.png);
}
HTML:
<ul id="navlist">
<li class="contact">Contact Us</li>
<li class="about">About Us</li>
</ul>
I'm assuming that your custom bullets are your background images applied to #navlist li.contact and #navlist li.about.
My suggestion would be to remove the padding from your li's and add the padding to your a's. This will force the spacing that is used to see the bullet to be within your a tag and "clickable".
To get it working as you describe try to use
display: inline-block;
and change left padding/margin as below:
#navlist li {
display: inline-block;
background-repeat: no-repeat;
background-position: 0 50%;
padding: 3px 0 3px 0;
margin: 0 20px 0 .4em;
}
#navlist li a {
text-decoration: none;
padding-left: 35px;
}
There are different ways to achieve what you want:
1. THE LI WAY (not advised)
<ul id="navlist">
<li class="contact">Contact Us</li>
<li class="about">About Us</li>
</ul>
In that case, li a {float:right; width:<fixedwidthforall>;}. So you'd have to give all anchors a fixed width, float them right, and make sure the background-image on liis visible.
Lots of trouble, and you'll have a harder time changing the bullet image on :hover.
2. THE PREFERRED WAY (advised)
<ul id="navlist">
<li>Contact Us</li>
<li>About Us</li>
</ul>
The most common practice is to put some padding to your anchors and move the background-image to the anchors (not the li's). That means also moving the classes to the anchors.
3. THE OKAY WAY
<div id="navlist">
Contact Us
About Us
</div>
For a small website with absolutely no need for extensible navigation, a div with anchors with the following styling is just fine. No need to complicate things needlessly.
<style>
#navlist a {
display: block;
float: left;}
</style>
4. THE REBEL WAY
If you feel like doing something unconventional, rebellious, go for this kind of markup:
<ul id="navlist">
<li><a href="#" class="contact">Contact Us
<img src=""></img></a></li>
<li><a href="#" class="about">About Us
<img src=""></img></a></li>
</ul>
Why not add an image in the navigation link huh? Or why not the entire page as a display:none and show it on hover. No one does it, but there's always gotta be a pioneer!
BOTTOMNOTICE
"I know I can move the A tag outside of the LI tag, but that is poor coding practice."
There are a lot of different ways to style your navigation links. There are conventions and good coding, but it's not a law. Often times, people who deviate from the standards are the new innovators.
If your navigation contains two links, is it worth the trouble creating a ul, li's and a's while you could do much faster, and with the same quality with a's?
PS: Classes are used for recurring elements, id's for unique ones. Unless you re-use the same styling on other elements, they should be id's... But again, that's a convention. You're free to choose!
I not sure if you try this way move the background-image in <a>
#navlist {
list-style: none;
margin: 0;
padding: 20px 0 0px 20px;
}
#navlist ul {
margin: 0;
padding: 0;
}
#navlist li {
display:inline;
}
#navlist li a {
text-decoration:none;
background-repeat: no-repeat;
background-position: 0 50%;
padding: 3px 0 3px 35px;
margin: 0 20px 0 .4em;
}
#navlist li.contact a{
background-image: url(img/contact.png);
}
#navlist li.about a{
background-image: url(img/about.png);
}
working demo hope this help
Add
#navlist li {
list-style-position: inside;
}
Just style the a instead of the li and apply the background-image to that. This is also way more semantic. The changed code would be:
#navlist li {
display:inline;
}
#navlist li a {
text-decoration:none;
background-repeat: no-repeat;
background-position: 0 50%;
padding: 3px 0 3px 35px;
margin: 0 20px 0 .4em;
display: inline-block; // to make the padding work
}
#navlist li.contact a {
background-image: url(img/contact.png);
}
#navlist li.about a {
background-image: url(img/about.png);
}
Please help,
i want to align the header menu/nav links to vertically align. See:
http://hyindia.com/demo/myoffshore/index.html
See the CODE here:
nav ul { list-style-type:none; padding:0px; margin:0px; float:left; width:100%;}
nav ul li { float:left; width:119px; height:66px;}
nav ul li a {
float:left;
width:119px;
height:66px;
font:bold 15px 'Myriad Pro';
color:#fff;
text-shadow:1px 1px #1f1f1f;
text-align:center;
}
<nav>
<ul>
<li>HOME</li>
<li>HEALTH INSURANCE</li>
<li>LIFE INSURANCE</li>
<li>OVERSEAS MORTGAGES</li>
<li>ESTATE PLANNING</li>
<li>BANKING</li>
<li>WEALTH MANAGEMENT</li>
<li>QROPS</li>
</ul>
Since some of your nav items have text spanning several rows you won't be able to use the classic line-height-trick (which would be to set the line-height equal to the height).
Instead I'd suggest changing your menu styling to use display: table/table-row/table-cell since tables are excellent at vertically aligning things in the middle.
What you need to do is to change your entire nav styling to this:
nav {
display: table;
width: 100%;
}
nav ul {
display: table-row;
list-style: none;
padding: 0;
margin: 0;
}
nav ul li {
display: table-cell;
vertical-align: middle;
}
nav ul li a {
display: block;
padding: 5px 10px;
}
Remove all the floats and widths + heights (using padding on the a instead) etc (what I have above is all you should have).
You'll also need to move the actual background styling from the as to the lis since the as won't be equal in height any more (but the lis will).
Here is five methods very well explained : http://blog.themeforest.net/tutorials/vertical-centering-with-css/
I am pretty new at CSS but have been learning, doing my moms small business website to save her money but I'm having a little CSS trouble with my nav bar.
Basically if you go here: http://area25dallas.com/s and look at the nav bar, I'm having trouble with the il listing to have the images line up vertically (instead of aligning with the top which is what they currently do) with the text, also for some reason the images are going on top of each other instead of sitting next to each other (I don't want them in separate lists like the text links because the margins are too spread out).
I have been playing around with the CSS and also googled the hell out of this but still haven't found a solution. Is there any quick fix to this?
Thanks!
EDIT:
Here is the HTML and CSS blips though if you are using chrome I feel just inspecting the elements are the easiest way to see what's going on
<div id = "header">
<div class = "container">
<ul id = "main-menu">
<li class = "active">home</li>
<li>about</li>
<li>gallery</li>
<li>press</li>
<li>contact</li>
<li><img src="images/twitter_newbird_boxed_ white.png" />
<img src="images/Pinterest_Favicon white.png" /></li>
</ul>
</div>
</div>
and the CSS
#main-menu
{
float: right;
position:relative;
top:122px;
right:150px;
}
#main-menu li
{
float: left;
margin: 30px 12px 15px 12px;
padding:0;
height:23px;
list-style:none;
line-height:20px;
}
#main-menu li:hover, #main-menu li.active { background-position: 0 -23px;}
#main-menu li:hover a, #main-menu li.active a{
background-position: 100% -30px;
}
#main-menu li a
{
display:block;
padding:0px 15px 5px 10px;
font-size:17px;
color:#fff;
text-decoration:none;
}
The images are broken onto multiple lines because they reside inside an <a> tag which has been styled as a block level element. Change the style to something like:
#main-menu {
float: right;
position: relative;
right: 75px; /* Changed */
top: 122px;
}
#main-menu li a {
color: #fff;
display: inline-block; /* Changed */
font-size: 17px;
padding: 0 15px 5px 10px;
text-decoration: none;
}
/* New */
#main-menu li a img {
position: relative;
top: -10px;
}
The new rule at the bottom moves the images up a little bit. You can play around with your css and get the same results in a lot of different ways - I went with a method that didn't involve many changes to the existing work.
Thanks for the tips, guys, this helped me out too with images in my css navigation.
I'd also recommend some added code to alleviate your spacing issue ...
#main-menu li a img {
position:absolute;
background:inherit;
top: 0px;
margin-bottom:auto;
max-height: 33px;
}