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.)
Related
I previously had the anchor element inside of the list item and could target it using "nth-child(4)" but as soon as I placed the anchor element outside of the list item to make the clickable area bigger it became untargetable. Any suggestion on how to target that particular element?
section.social-section ul a li {
width: 22%;
display: inline-block;
float: left;
border: 1px solid #e2e0e0;
margin-right: 30px;
padding: 10px;
}
section.social-section ul.group a:nth-child(4) {
margin-right: 0;
}
<!-- Social section -->
<section class="social-section">
<ul class="group">
<a href="#">
<li>facebook</li>
</a>
<a href="#">
<li>twitter</li>
</a>
<a href="#">
<li>pinterest</li>
</a>
<a href="#">
<li>google+</li>
</a>
</ul>
</section>
Two problems here:
You can't swap the positions of your a and li elements like that. The only children a ul can contain are li elements. The only valid structure for such a list is ul > li > a.
Validity aside, the reason your rules no longer match is because your first rule is still targeting li elements, but your second rule targets a elements. They're targeting different elements, so no overriding is taking place.
You can make the clickable area bigger without altering your HTML by moving some of the CSS properties such as padding to the a elements, as well as making them blocks:
section.social-section ul li {
width: 22%;
display: block;
float: left;
border: 1px solid #e2e0e0;
margin-right: 30px;
}
section.social-section ul.group li:nth-child(4) {
margin-right: 0;
}
section.social-section ul li a {
display: block;
padding: 10px;
}
<!-- Social section -->
<section class="social-section">
<ul class="group">
<li>facebook
<li>twitter
<li>pinterest
<li>google+
</ul>
</section>
(Note that moving the padding declaration causes the width of your li elements to be reduced, but for the purposes of this answer I'm ignoring this side-effect.)
As the comments rightly point out, the immediate children of ul tag must always be zero or more li tags.
However, you say that you are doing this to increase the clickable area. You can do that by putting the a tags back inside the li tags, and then make the a tags display : inline-block. You could then apply width : 100% and height : 100% on a to increase the clickable area (which would then be equal to the entire area of the li). In this manner, you can use nth-child() to target the 4th child.
Your code becomes :
HTML
<!-- Social section -->
<section class="social-section">
<ul class="group">
<li>facebook</li>
<li>twitter</li>
<li>pinterest</li>
<li>google+</li>
</ul>
</section>
CSS
section.social-section ul li {
display: inline-block;
position: relative;
}
section.social-section ul.group li:nth-child(4) a {
display: inline-block;
height : 100%; //not absolutely required
height : 100%; //not absolutely required
}
You can add CSS properties to position the list as you require.
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
I'm creating a website using my own design, and I had a complication recently. It's about the way the menu style is positioned. I find this problem hard to explain, so firstly, I'll show the images.
It should look like:
http://i.imgur.com/XINIlXC.jpg
It looks like:
http://i.imgur.com/RKrFZVz.jpg
As you can see, It's a simple horizontal menu with seperators between menu items, but the tricky part is the corner. In the image where everything works perfectly, I use a messy code, that's not compatable with lists, which I will need for drop-down menus later.
So, this is the messy code:
<div class="menu" id="menu-item">
<img src="images/top_menu_edge.png" style="margin-left:-23px;"/> <span style="margin-right:10px"> 1aaaa <span class="ms"></span> 2bbbb <span class="ms"></span> 3 </span>
</div>
The span.ms is just the menu seperator. As for all the other parts, I'm gonna show a css code.
#menu-item
{
position: absolute; /* The absolute position shouldn't be a problem, because everything is contained in div#header, and it's position is relative. */
bottom: 12px;
right: 0;
height: 34px;
background-position: bottom right;
z-index: 5;
color: #192c51;
}
.menu
{
position: absolute;
bottom: 0;
right: 0;
height: 34px;
background-image: url('images/top_menu_bg.png');
background-position: bottom right;
z-index: 5;
color: #192c51;
}
And this is the code that's fixed up, but for some reason, the image is not working:
<div class="menu" id="menu-item">
<div class="menu-item">
<img src="images/top_menu_edge.png" style="margin-left:-23px;"/>
<ul id="nav">
<li> Pagrindinis </li>
<li> Apie mus </li>
<li> Paslaugos </li>
<li> Kontaktai </li>
</ul>
</div>
</div>
And the extra css lines for ul and li:
#nav
{
list-style:none;
float:left;
}
#nav > li {
float: left;
}
#nav li+li
{
background:url('images/top_menu_sep.png') no-repeat top left;
padding-left:10px;
}
That's pretty much it, and I don't really get it why the tag ruins it all, yet I need it to do it. Does anyone have any subjections, how to fix it, or could show me the way? I'd be really thankful.
Cheers :)
If you give elements float, they will not have width if you place a non-float element nexto it. Remove all the floats and switch to display: inline-block or give everything float
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.
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>