Links within and outside List elements not working - html

I'm trying to create a unordered list, with each li element having it's own background image (no text, just image), but I'm having trouble (in Firefox at least, it works in Safari) getting the link to work. In Firefox, the image changes on hover, but doesn't let you click. How do I get it to work in Firefox? I've tried the A tag within and outside the li tag.
Here's the CSS...
#menu {
width:107px;
height:200px;
}
#menu-1, #menu-1-active, #menu-2, #menu-2-active, #menu-3, #menu-3-active, #menu-4, #menu-4-active, #menu-5, #menu-5-active, #menu-6, #menu-6-active {
width:107px;
height:29px;
padding-bottom:5px;
background-repeat: no-repeat;
display:block;
text-indent: -999px;
}
#menu-1 {
background-image: url(menu1.png);
}
#menu-1:hover {
background-image: url(menu1on.png);
}
#menu-1-active {
background-image: url(menu1on.png);
}
#menu-2 {
background-image: url(menu2.png);
}
#menu-2:hover {
background-image: url(menu2on.png);
}
#menu-2-active {
background-image: url(menu2on.png);
}
etc
And here's the HTML...
<div id="menu">
<ul>
<a href="1"><li id="menu-1-active">
One
</li></a>
<a href="2"><li id="menu-2">
Two
</li></a>
<a href="3"><li id="menu-3">
Three
</li></a>
<a href="4"><li id="menu-4">
Four
</li></a>
<a href="5"><li id="menu-5">
Five
</li></a>
<a href="6"><li id="menu-6">
Six
</li></a>
</ul>
</div>

The link needs to be inside the <li>, for a start, as a <li> is a block-level element whereas an <a> is inline.
Also, setting :hover on elements other than <a> - while supported in the likes of FF, etc - is in my experience a bit spotty at working right and doesn't work at all in older IEs.
Personally, if it were me writing the HTML, it would look something like this:
<ul id="menu">
<li id="menu-1-active">One</li>
<li id="menu-2">Two</li>
<li id="menu-3">Three</li>
<li id="menu-4">Four</li>
<li id="menu-5">Five</li>
<li id="menu-6">Six</li>
</ul>
And the CSS would be something like the following:
#menu{
width:107px;
height:200px;
}
#menu li{
padding: 0, 0, 5px;
}
#menu li a{
display: block;
text-indent: -999px;
height: 29px;
background: transparent, none, center, center, no-repeat;
}
#menu-1 a:link, #menu-1 a:visited { background-image: url(menu1.png); }
#menu-1 a:hover, #menu-1 a:active, #menu-1-active { background-image: url(menu1on.png); }
/** Continue on with your other links here... **/

You need to put the a tag inside the li tag. And then set the a tag to display: block; This will cause the a tag to fill up all the space inside the li tag and make the whole area clickable.
For example:
<style type="text/css" media="screen">
a {
display: block;
}
</style>
<ul>
<li id="menu-1-active">One</li>
<li id="menu-2-active">One</li>
</ul>

Related

CSS drop down menu sub-items overlapping

Good day, this is my first ever question on Stack Overflow, so I hope I get it as right as possible.
I have done extensive research on my problem, mostly reading all the questions I could find on Stack Overflow and some other sites, but I could not find one answer that worked.
Some background: I am trying to write a website for recruiting for my work and it's the first ever website I have ever written. I am using a wamp server to run the site on localhost for testing. My issue is described as best as I could in the title. Find below my html code:
<html>
<head>
<title> BCB Call Plus SRL Home </title>
<link rel="stylesheet" href="Main Style.css">
</head>
<body>
<div id = "main_content">
<ul id = "nav_container">
<li> <img id = "logo" src= Logo.png style ="width:150px;height:75px"> </li>
<li> Home </li>
<li> Menu 1 </li>
<li> Menu 2 </li>
<li> Menu 3 </li>
<li id ="angajari"> <a class="dropdown_toggle" data-toggle="dropdown" href= "Page4.html"> Angajari </a>
<ul class="sub_menu">
<li>Ce Vrem</li>
<li>Aplica</li>
</ul>
</li>
</ul>
</div>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
And below my CSS code:
body {
text-align:center;
}
a {
font-size: 150%;
text-decoration: none;
color:#000000;
font-weight: bold;
vertical-align:middle;
}
a:hover{
background-color:#338533;
}
ul {
padding:0;
margin:0;
}
ul#nav_container{
background-color:#F2FFF2;
list-style-type:none;
text-align:center;
}
ul#nav_container li{
display:inline-block;
padding-left:5px;
padding-right:5px;
vertical-align:middle;
position:relative;
}
.sub_menu li a{
display:none;
}
#angajari ul.sub_menu li {
float:left;
}
#angajari ul.sub_menu li a {
position:absolute;
top:0;
white-space: nowrap;
height:auto;
}
#angajari:hover ul.sub_menu li a {
display:block;
}
Here's a picture of what happens when I hover over the problematic menu item:
Display Issue
Final notes: I am running this only under Chrome for now. I have noticed that it doesn't read my css right in IE 8 (yes, I use IE 8, because one of my bosses wants us to.) Cross-platform compatibility fixes are welcome, but not in the scope of my current question. My WAMPSERVER has apache 2.4.9 and PHP 5.5.12.
I even tried my code on some online web code testing site whose name I forgot and got the same results. If you find that my code actually displays properly, then it may be an issue with my configuration.
Here is a jsfiddle.
You need your .sub_menu to be absolute, not your li as. That's it!
.sub_menu {
position:absolute;
}
Working demo here: http://jsfiddle.net/pxzhqqnb/1/
And I'd make the .sub_menu hidden instead of its children. Personal preference, but I think it makes more sence.
Why does it happen?
Consider this simple example: (think of .relative as position: relative and .absolute as position: absolute)
<div class="relative">
Text
<div class="absolute">Link 1</div>
<div class="absolute">Link 2</div>
</div>
Link 1 is absolute. It searches for the closest relative element. That's .relative. Now Link 1 gets right under the relative div.
Link 2 follows the same rules, thus both links overlap.
Now let's change the code a little:
<div class="relative">
Text
<div class="absolute-wrapper">
<div>Link 1</div><!-- these are now static by default -->
<div>Link 2</div>
</div>
</div>
absolute-wrapper is absolute, so it searches for the closest .relative element and gets right under it. Now both links are normal elements wrapped in a div, so they render as expected.
Demo of both examples here: http://jsfiddle.net/w0h7cdhe/2/
I've done a few tweaks to your css code:
body {
text-align: center;
}
a {
font-size: 150%;
text-decoration: none;
color: #000000;
font-weight: bold;
vertical-align: middle;
padding: 0px 10px; /* this is just for the hover effect to lose the spaces in the html */
}
a:hover {
background-color: #338533;
}
ul {
padding: 0;
margin: 0;
}
ul#nav_container {
background-color: #F2FFF2;
list-style-type: none;
text-align: center;
}
ul#nav_container li {
display: inline-block;
padding-left: 5px;
padding-right: 5px;
position: relative;
}
#angajari ul.sub_menu { /* do this with the menu, not just the link */
display: none;
position: absolute; /* set correct position */
}
#angajari ul.sub_menu li {
display: inline-block;
}
#angajari ul.sub_menu li a { /* we don't want top: 0 because it should not overlap */
white-space: nowrap;
}
#angajari:hover ul.sub_menu { /* see above -> menu not link */
display: block;
}
<div id="main_content">
<ul id="nav_container">
<li>
<img id="logo" src="http://lorempixel.com/150/75" style="width:150px;height:75px">
</li>
<li> Home <!-- I've removed the spaced and added the gap in css -->
</li>
<li> Menu 1
</li>
<li> Menu 2
</li>
<li> Menu 3
</li>
<li id="angajari"> <a class="dropdown_toggle" data-toggle="dropdown" href="Page4.html">Angajari</a>
<ul class="sub_menu">
<li>Ce Vrem
</li>
<li>Aplica
</li>
</ul>
</li>
</ul>
</div>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
So i tried to fix your Problem i end up with this result
I've adjusted the margin of the logo as shown below:
<li> <img id = "logo" src= Logo.png style ="width:150px;height:75px;margin-left: -50px;"> </li>
because I adjust the width of the text container and replace the last 4 lines in your CSS CODE as shown below:
body {
text-align:center;
}
a {
font-size: 150%;
text-decoration: none;
color:#000000;
font-weight: bold;
vertical-align:middle;
}
a:hover{
background-color:#338533;
}
ul {
padding:0;
margin:0;
}
ul#nav_container{
background-color:#F2FFF2;
list-style-type:none;
text-align:center;
}
ul#nav_container li{
display:inline-block;
padding-left:5px;
padding-right:5px;
vertical-align:middle;
position:relative;
width: 95px;
}
#main_content ul ul {
position: absolute;
visibility: hidden;
}
#main_content ul li:hover ul {
visibility: visible;
}
so i made minor changes but i dont know if that's what you want to happenenter code here

Image hover not working..on pure css

<div id="headermenu">
<ul >
<li id="menu1"><img src="images/menu1.png"/></li>
<li id="menu2"><img src="images/menu2.png"/>
<ul class="submenu2">
<li>submenu2</li>
<li>submenu2</li>
<li>submenu2</li>
<li>submenu2</li>
<li>submenu2</li>
<li>submenu2</li>
<li>submenu2</li>
<li>submenu2</li>
</ul>
</li>
<li id="menu3"><img src="images/menu3.png" /></li>
<li id="menu4"><img src="images/menu4.png"/></li>
<li id="menu5"><img src="images/menu5.png"/></li>
</ul>
</div>
css
#headermenu ul ul {
display: none;
}
#headermenu ul{
padding:0;
margin:0;
white-space:nowrap;
}
#headermenu ul li{
width: 20%;
list-style-type:none;
display:inline-block;
margin-bottom:15px;
float:left;
left:0;
}
#menu1:hover{
background: url('images/menu1hover.png');
}
Hover is not working, I wonder how to make a list with image hover, I also want to know how to make a sub list when a li is hover. And if there is another list on sub list how to make it.. on pure css..
As you haven't given us all the information we need to solve this I will take a guess. As I said in the comments. It could be caused by the <img> sitting on the background, so when you hover you cant see the background at all.
HTML:
<div id="headermenu">
<ul>
<li id="menu1"><img src="http://img.gawkerassets.com/img/19euo1gaaiau9jpg/original.jpg"/>
</li>
</ul>
</div>
CSS:
#headermenu ul ul {
display: none;
}
#headermenu ul {
padding:0;
margin:0;
white-space:nowrap;
}
#headermenu ul li {
width: 20%;
height: 50px;
list-style-type:none;
display:inline-block;
margin-bottom:15px;
float:left;
overflow: hidden;
border: 1px solid;
}
#menu1:hover {
background: url('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
}
Here is your code as is, the background image is changing but cannot be seen because of the <img> in front.
DEMO HERE
Now here is the same code but the <img> being removed.
<div id="headermenu">
<ul>
<li id="menu1">
</li>
</ul>
</div>
DEMO HERE
We can see that the hover does work but the <img> was coving it up.
Solutions:
Just simply set a background on each li and then a background when on hover.
CSS:
#menu1 {
background: url('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
}
#menu1:hover {
background: url('http://img.gawkerassets.com/img/19euo1gaaiau9jpg/original.jpg');
}
DEMO HERE
You could also set the display to none for the img when on hover.
CSS:
#menu1 {
background: url('http://icdn4.digitaltrends.com/image/microsoft_xp_bliss_desktop_image-650x0.jpg');
}
#menu1:hover img {
display: none;
}
DEMO HERE
There are many more ways but these are two that will work.
You need to use either a background image, or an actual img for both before and after states. In your example, when the mouse hovers over #menu1 the background behind the menu1.png image is changed but menu1.png is obscuring it from view.
try this code
DEMO
<body> </body>
.urlImg { width: 140px; height:140px; display:block; background-image: url("http://imgsrc.ru/images/reco/140/windkitten_38083968.jpg"); } .urlImg:hover { background-image: url('http://placehold.it/140x140'); }

Display Image in Horizontal UL LI with Sprite in CSS

Image Sprites, by themselves, are not evil; however, trying to get these to work in an unordered list is difficult. Going a step further and trying to get this unordered list to display horizontally (working example >> HERE <<) is certainly an enigma.
Here is my jsFiddle Project: jsFiddle: hNJmD
It seems like every time I get something working, I make the smallest edit to customize it for my app and it breaks.
Does it matter what order items in a CSS file are declared?
Does a <ul> tag have to be defined before a <li> tag?
Does height: need to be specified before width:? (I generally try to list things alphabetically so I don't accidentally have duplicates)
Here is the image I am using as my Sprite:
My Goal:
Center the menu horizontally the page
Have the full image display (all 50 px)
Disable the a.hover effect for the active item (Link 1)
The CSS is:
#nav {
text-shadow: 4px 4px 8px #696969;
white-space:nowrap;
vertical-align: middle;
}
#nav ul {
list-style-type:none; /* removes the bullet from the list */
}
#nav li {
display: inline-block;
text-align: center;
height: 50px;
width: 192px;
}
#nav a {
background: url('http://i.imgur.com/Sp7jc.gif') 0 -100px no-repeat;
display: block;
color:Blue;
}
#nav a:hover {
background-position: 0 -50px;
color:Red;
}
#nav .active, a:hover {
background-position: 0 0;
color:Black;
}
#nav span {
top: 15px;
padding-left: 5px;
}
​
The HTML used in the jsFiddle is repeated here as well:
<body>
<div>
<ul id="nav">
<li>
<a class="active" href="#">
<span>Link 1</span>
</a>
</li>
<li>
<a href="#">
<span>Link 2</span>
</a>
</li>
<li>
<a href="#">
<span>Link 3</span>
</a>
</li>
</ul>
</div>
<br/>
<br/>
<br/>
<br/>
<div style="text-align:center;">
<p>REFERENCE: Sprite (Width: 192, Height: 150):</p>
<img src="http://i.imgur.com/Sp7jc.gif">
</div>
</body>​
Can somebody show me how to get this crazy thing to work?
If you want it to look like this: jsFiddle example, then this is what I did:
Combined your #nav and #nav ul rules since they refer to the same thing.
To that rule I added margin:0 auto; and width:600px; which centers the items
Then on #nav a I added display: block; and height:50px; which allows the links to take up the proper amount of height.
Finally I added a rule, #nav .active:hover that only affects the active element so it doesn't appear to change.
And to answer one of your questions about whether the order of CSS rules matters, the answer is yes and no. With respect to width and height in a rule, the order is irrelevant, but the order the rules appear in can matter, as does the specificity of the rule.

Using an image for a list item that is not a link

I have the following code:
<footer id="footer">
<ul id="labels">
<li id="label1">About</li>
<li id="label2">blah</li>
<li id="label3">blah</li>
<li id="label4">blah</li>
<li id="label5">blah</li>
</ul>
</footer>
I know how to specify an image for li items 2-5 but not sure how to for label1?
I have the following CSS but it only works when label1 has the <a href=""> tag. Is there any way to do it the way I am trying to and if not how should I approach it?
#label1 a {
height:9px;
width:43px;
background: url(/Content/images/footer/about.png) no-repeat 0 0;
padding:0;
}
Sorry I'm very new to CSS/HTML
#label1 { /* I've edited this rule */
height: 9px;
width: 43px;
background: url(/Content/images/footer/about.png) no-repeat 0 0;
padding: 0;
}
remove the a bit. It makes you select the <a> element within #label1. But because you want to apply these rules to #label1, you only have to remove the a selector.
You can use CSS's list-style-image for each of the labels
li#label1{ list-style-image:url('img1.jpg'); }
li#label2{ list-style-image:url('img2.jpg'); }
li#label3{ list-style-image:url('img3.jpg'); }
li#label4{ list-style-image:url('img4.jpg'); }
li#label5{ list-style-image:url('img5.jpg'); }
li#label6{ list-style-image:url('img6.jpg'); }

Image inside list item inside link not clickable in IE

The HTML code:
<ul>
<a href='index.php'>
<li>
<img src='images/icons/home.png' alt='' />
<p>Home</p>
</li>
</a>
</ul>
The CSS:
ul {
height:100%;
position: absolute;
right: 0;
text-align: left;
}
ul li {
height: 100%;
width:90px;
float: left;
}
ul li p {
margin-top: 4px;
width: 100%;
}
ul a li {
display:block;
}
ul li img {
margin-top: 8px;
width: 43px;
height: 43px;
}
(I've left all the properties here except the font stuff)
The problem:
In Internet Explorer only: the whole link (which is a square block with text and an image inside) functions normally as a link except for the part where the image is. In that part when clicked on the link does not work. It does, however, strangely, still, show the link in the status bar when you hover over any part, including the image.
You should provide a larger sample of your HTML, but I can already see that it's invalid:
<a href='index.php'>
<li>
..
</li>
</a>
You either have an a element as a direct child of a ul element which is invalid.
Or, you don't have a containing ul element, which is also invalid.
It's only valid to use an li element inside a ul or ol element (and a couple of other less common scenarios).
Valid HTML would look like this (assuming HTML5!):
<ul>
<li>
<a href="#">
<img src='images/icons/home.png' alt='' />
<p>Home</p>
</a>
</li>
</ul>
Once you use valid HTML, it should work in IE.
(But, you didn't specify what version of IE, so I'm just guessing that it will.)