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);
}
Related
I am trying to follow some online HTML CSS course and tried to code an website like this website
https://www.w3schools.com/w3css/tryw3css_templates_band.htm#
When I tried to see the hided button Merchandise, Extras, Media 's information, I saw the Elements and I saw that the padding is 8px 16px, as you can see in this picture
But when I coded like them, used their information, the block became small. Like this picture
Here is some of my code
<div id="header">
<ul id="nav">
<li>HOME</li>
<li>BAND</li>
<li>TOUR</li>
<li>CONTACT</li>
<li>
<a href="">MORE
<ul class="subnav">
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</a></li>
</ul>
</div>
My styles.css file is here
#nav li {
position: relative;
}
#nav > li {
display: inline-block;
}
#nav li a {
text-decoration: none;
line-height: 46px;
padding: 0 24px;
display: block;
}
#nav li:hover .subnav {
display: block;
}
#nav .subnav {
/*display: none;*/
position: absolute;
background-color: #fff;
top :100%;
left :0;
}
#nav .subnav a {
color: #000;
padding: 0 16px;
}
My question is, why I set padding exactly they display but it became fell in ?
Thank you very much for your time.
If I understand correctly, your question is why the widths of your <li> elements are shorter than the example.
In short, add min-width: 160px; under your #nav .subnav a CSS selector.
#nav .subnav a {
min-width: 160px;
color: #000;
padding: 0 16px;
}
Explanation:
As in your screenshot, the original example has CSS "width: 100%" which I believe you've tried but didn't work as expected. In fact, that 100% means it will follow the father <div> width (shown in following screenshot), which is set to "min-width: 160px;". Instead of following the exact structure, you may want to keep it simple, so just add "min-width" to <a>!
CSS of example page
Ok this is simple thing. I firstly created a usual "Home" Button linking to the Home Page of the website, but the word "Home" looked too obvious. Hence I tried to insert an icon in place of that word, but its not fitting properly. I have tried some things in my css but its messing up the whole (used to create the navigation menu). The screenshot is attached. Please if someone can see whats wrong.
CSS:-
ul#menu
{
padding: 0px;
position: relative;
margin: 0;
}
ul#menu li
{
display: inline;
text-decoration:solid;
}
ul#menu li a
{
color: black;
background-color: #f5b45a;
padding: 10px 20px;
text-decoration: none;
line-height: 2.8em;
/*CSS3 properties*/
border-radius: 4px 4px 0 0;
}
HTML:-
<ul id="menu">
<li id="Home_Link"><img src="../../Image_Data/Home_Icon.ico" id="Home_Icon"/></li>
<li>MEN</li>
<li>WOMEN</li>
<li>KIDS</li>
<li>DESIGN!!</li>
With your current styles you will need to play around with the vertical-alignment and margins for the image, something like:
ul#menu li#Home_Link a img {
vertical-align: text-bottom;
margin-bottom: -5px;
}
As a side note, your use of ID's for elements is not recommended - use classes if needed. And reduce the specificity of your style declarations, e.g. .home-link img
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;
}
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;
}
I have a sort of menu like this one, but how you can see the code is not so "well".
I'd like that margin between word and border is always 5px for example, for every word.
I know I should use List for this kind of stuff, but I don't know how to apply css style with cross-browser compatibility.
Can you give to me an example of that menu with List?
This is how I'd do it:
See: http://jsfiddle.net/thirtydot/554BT/3/
<ul class="menu">
<li>Home</li>
<li>Incredible</li>
<li>One</li>
</ul>
.menu {
width:545px;
float:left;
margin: 0;
padding: 0;
list-style: none
}
.menu li {
float: left;
text-align: center;
padding: 0 15px;
border-left: 2px solid red
}
.menu li:first-child {
border: 0
}
This is the way I would do it, keeping it as easy (simple) as possible. It probably doesn't get any less complex than this:
HTML
<ul id="menu">
<li>Home</li>
<li>Incredible</li>
<li>One</li>
</ul>
CSS
#menu {
list-style-type: none;
}
#menu li {
display: inline-block;
padding: 0 10px;
border-left: 2px solid red;
}
#menu li:first-child {
border-left: none;
}
DEMO: jsfiddle
Check out Listmatic for examples of all the basic list layouts.
Looks like you want something like this one.
Try this...
fiddle:http://jsfiddle.net/anish/Laqqn/
<style type="text/css">
.menu
{
width:500px;
}
.menu li
{
width:100px;
text-align:center;
float:left;
border-right:1px solid red;
}
</style>
<ul class="menu">
<li>Home</li>
<li>Incredible</li>
<li>One</li>
</ul>
A CSS3 example, not really cross browser as it uses CSS3 pseudo-selectors
CSS3 List menu
This other example uses a pipe character to separate the links and is cross browser safe:
CSS2 List menu
Space between the borders do this =
Put a border on the right side of the li and the second button put a border on the left side of the li.
Now add margin-left (or margin-right) and see it expand.
This worked in my case.
Good luck.