CSS attribute :hover not working - hover

I am making a nav bar and using the :hover attribute.
My question is why does the opacity work when I hover over it but not the border bottom? I have tried it in Chrome, Firefox and Edge, and wrote a jsfiddle for you to look at.
Thanks
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
li:hover {
border-bottom:1px soild red;
opacity:.4;
}
https://jsfiddle.net/pb4759jh68/yq3bwhjv/

Simply a typo in "solid":
li:hover {
border-bottom:1px solid red; //soild changed to `solid`
opacity:.4;
}

Related

Combine two borders become one and set each function to the hover li

I have two questions here:
First: I have four <li> with its display is inline block and floating to left. I want EACH <li> has the style of the red colored border bottom on mouse hover.
Second: I want to make the border bottom of the text is overlapped with the border bottom of its wrapper (in this case is the <ul> of the <li>.)
<ul id="menu">
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>
And
#menu {
list-style:none;
}
#menu li {
display:inline-block;
}
#menu:hover li {
display:inline-block;
border-bottom:solid 5px red;
}
Here is my Fiddle
In the fiddle, the border colored blue is seperated with the border colored red. I want them to become one border.
Simply change your CSS to:
#menu li:hover {
display:inline-block;
border-bottom:solid 5px red;
margin-bottom:-5px;
}
Firstly you want to apply the :hover to the individual li being hovered, and not all li when the ul is hovered, then you want to offset its bottom (achieved with a negativemargin-bottom`) by the width of the border applied.
Demo Fiddle
Try this one with smooth transition
Demo
#menu li:hover {
border-bottom:solid 5px red;
margin-bottom:-5px;
}

Fill whole line of LI using CSS

I have these nested ul and li . When i fill background color, nested li leaves indented portion white. I have a number of li like this that gets filled from database so i cannot give margin left to individual text in li . How can i do this so that background fills whole line along with the indentation?
Right now it looks like this
I want it like this
Any suggestions how can do this? Thanks in advance. I cannot change the html markup as i'll have to change a lot of code. Is there a way to do this using this markup. these li are coming from db query so i dont have exact number of li in this case.
Demo http://jsbin.com/uReBEVe/1/
By default, <ul> has padding-left to accomodate the bullet point.
If you add this to your CSS:
ul {padding-left:0}
ul>li {padding-left:40px}
You should get the effect you want.
EDIT: Also you need to correct your HTML :p <ul> can ONLY have <li> as children.
Best thing to do is to use a structure which makes it easy for database management , html and styling(CSS) .
HTML:
<body>
<ul class="main">
<li>1.</li>
<li><ul>2</ul></li>
<li><ul><li><ul>3.</ul></li></ul></li>
</ul>
</body>
CSS:
.main{
position:relative;
right:40px;
}
li{
list-style:none;
background:red;
margin-top:1px;
}
Fiddle 1.
I dont know if ul not containing li is valid or invalid.If its invalid then you can use:
<body>
<ul class="main">
<li>1.</li>
<li><ul><li>2</li></ul></li>
<li><ul><li><ul><li>3.</li></ul></li></ul></li>
</ul>
</body>
Fiddle 2
Flexible, Multi-Level Nesting Solution
This is very similar to another question I answered here, and I've composed a similar solution for you below. You will want valid html by having all nested li elements inside their own ul (as others have noted here), and it would be best to control all this by some class on the outermost ul (though that is not required, but makes targeting this list a whole lot easier).
The key here is supplying the background through the :before pseudo-element, which is made to span the whole width of the outermost ul.
Here is my demo jsbin.
HTML
<ul class="full-width-list">
<li>A</li>
<li>
<ul>
<li>B</li>
<li>
<ul>
<li>B</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
.full-width-list {
position: relative;
padding: 0 0 0 4px;
}
.full-width-list ul {
margin: 0;
padding: 0
}
.full-width-list li {
list-style:none;
padding: 0;
margin: 0;
height: 1.2em;
line-height: 1.2em;
}
.full-width-list ul > li {
margin-top: 4px;
padding: 0 0 0 36px;
}
.full-width-list li:first-child:before {
content: '';
height: 1.2em;
position: absolute;
left: 0;
right: 0;
z-index: -1;
background:red;
}
.full-width-list li:first-child:hover:before {
background:green
}
Limitations
This solution has two main limitations:
None of the ul or li elements can have a position other than the default static set on them, as the :before pseudo-element of the li elements needs to have its only positioned parent be the .full-width-list element.
There has to be a set height on the li items. In my example I use 1.2em, but whatever height you set, it means that the li elements cannot go to two or more lines of text (so this solution only works with a single line of text).
You can do this with :before hack as you have no access to the code
Working jsBin Demo
CSS
li{list-style:none;background:red;margin-top:4px; }
li:hover{background:green}
li:hover:before {background:green}
li:before {background:red; width:100%; content:'.'; position:absolute; left:0; z-index: -1;}
This works at arbitrary depths without hacks or nonsense.
The people saying "can't" and "impossible" in this thread really need to learn what those words mean with respect to CSS (generally, "haven't figured out how yet.") :)
The idea is simple: set a :before selector which fits the left and right edges by absolute positioning and paints a background color. You need to set a z-index: to put it behind its content, a content: '\0020' to force it to paint (that's a non-breaking space,) and you're good.
You can bound this by setting it inside a position: relative container.
<!doctype html>
<html>
<head>
<style>
li {
list-style-type : none;
margin-bottom : 0.25em;
}
li:before {
position : absolute;
left : 0;
right : 0;
background-color : #eef;
content : "\00a0";
z-index : -1;
}
</style>
</head>
<body>
<ul>
<li>Test</li>
<li><ul>
<li>Test</li>
<li><ul>
<li>Test</li>
</ul></li>
</ul></li>
</ul>
</body>
</html>
Your markup is broken, you should nest li in a single ul like this:
<ul>
<li>Text</li>
<li>Text 1</li>
</ul>
This was your markup
<ul>
<li>A</li>
<ul>
<li>B</li>
<ul>
<li>B</li>
</ul>
</ul>
<ul>
I assume you see why this is wrong.
I've fixed the JSBin for you and it has the correct effect.
EDIT: You could of course add the padding-left by looping over all lis using javascript.
You could not be sure enough about browser consistency until markup cleanup and consistency, sad but true. All the suggestions from above looks good, there is bit of alternative from my practical view.
The markup:
<ul>
<li>A</li>
<li><p>B</li>
<li><p><p>B</li>
<li><p><p><p>B</li>
....
</ul>
And CSS:
li p {
padding-left: 1em;
display: inline;
}
JSbin
p tag is optional to close in HTML subset, and generally should works in every browser anyway no matter of doctype. In case you are on xHTML and worry about validation an option could be using closing tags like:
<ul>
<li>A</li>
<li><p></p>B</li>
<li><p></p><p></p>B</li>
....
</ul>
Try this:
<ul class="lvl1">
<li>A</li>
<ul class="lvl2"><li>B</li>
<ul class="lvl3"><li>B</li></ul>
</ul>
</ul>
li {
background: none repeat scroll 0 0 #FF0000;
list-style: none outside none;
margin-top: 4px;
}
ul { padding:0px;}
ul.lvl1>li {padding-left:30px;}
ul.lvl2>li {padding-left:60px;}
ul.lvl3>li {padding-left:90px;}
See here: http://jsfiddle.net/x5K4a/
1) Your HTML is invalid (missing <li> around <ul>)
2) The only way to make indentation work as you expected is a CSS rule for each level.
ul ul li.line { padding-left: 20px !important }
ul ul ul li.line { padding-left: 40px !important; }
...
http://jsbin.com/uReBEVe/12/edit
if it is just a matter of background-color, you can use a shadow of same color.
http://codepen.io/gc-nomade/pen/fxBAl (html structure fixed)
<ul class="ulparent">
<li>
<p>A</p>
<ul>
<li>
<p>B</p>
<ul>
<li>
<p>B</p></li>
</ul>
</li>
</ul>
</li>
</ul>
.ulparent {overflow:hidden;}
li p {background:green;box-shadow:-200px 0 0 green;/* -200px for instance or whatever suits your need */margin:4px 0;}
li p:hover {background:red;box-shadow:-200px 0 0 red;}
Else, if it is a background-image, i would use pseudo-element and background-attachment:fixed;(demo included in codepen , using a linear-gradient as image )
I am going to give you the proper idea how to apply css rules over the HTML contents.Below the css rules I have created just copy it and see the answer.It is the child combinator which I used!I inspect whole the answers provided by the different users which is not followed the css rules at all. Just let me know! Hope the answer!
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style type="text/css">
li{
list-style:none;
background:red;
margin-top:4px;
}
body>ul>ul>li{
margin: 4px 0 0 -40px;
}
body>ul>ul>ul>li{
margin: 4px 0 0 -80px;
}
body>ul>ul>li {
padding:0px 0px 0px 40px;
}
body>ul>ul>ul>li{
padding:0px 0px 0px 80px;
}
li:hover{
background:green;
}
</style>
</head>
<body>
<ul>
<li>A</li>
<li>
<ul>
<li>B</li>
<li>
<ul>
<li>C</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
Save the image at first to your local drive or drag and drop this image into the new tab browser to see more visible.
Here is the proper HTML structure that you should follow, with each UL element having two LI elements. One for the value of each line and one as the parent for the next indented value.
<ul>
<li>A</li>
<li>
<ul>
<li>B</li>
<li>
<ul>
<li>C</li>
</ul>
</li>
</ul>
<li>
</ul>
For the CSS, this solution requires you to have a max number of 'levels' in your list hierarchy (see code comment)
li {
list-style:none;
padding-left:0px;
box-sizing:border-box;
}
ul {
padding-left:0
}
ul > li:nth-of-type(1):hover {
background:green
}
ul li:nth-of-type(1) {
padding-left:50px;
background:red;
margin-top:4px
}
ul li li:nth-of-type(1) {
padding-left:100px;
}
ul li li li:nth-of-type(1) {
padding-left:150px;
}
/*
Continue incrementing the padding for the li
children for however many levels you want
*/
Make note, the nth-of-type selector is supported by all browsers EXCEPT for IE8 and below.
See JSBin for working example: http://jsbin.com/uReBEVe/51
Good luck!
Both UL and OL inherit margins. Your fix would be to zero out the margin:
ul, ol
{
margin:0;
}
You can add this CSS in your code to get your desired results:
li {
list-style: none;
background: red;
margin-top: 4px;
}
ul {
padding: initial !important;
}
ul ul li {
padding-left: 40px;
}
ul ul ul li {
padding-left: 80px;
}
li:hover {
background: green;
}
Result on jsbin is here: http://jsbin.com/uReBEVe/33/edit
#AsrafulHaque has the correct idea about using padding to extend the background width without changing nesting indents.
However, because you don't know how many < li> there will be, you can't expect this to be a pure CSS solution.
You're attempting to do a pretty awkward thing but it would be possible to loop over them and inject dynamic padding using javascript/jquery or something:
i = 40;
$('img.yourImageClass').each(function() {
$(this).css('padding-left', i+'px');
i = i + 40;
});
You could also do this type of injection with pre-processing on the server side I am sure, but definitely not with CSS alone. You need a dynamic solution (i.e. the ability to use variables) to support your dynamic output.
A very very fiddly jsfiddle but it works with a little nudge in the right direction from jQuery. Not a great resolve but a resolve none the less.
HTML
<ul>
<li>A</li>
<ul>
<li>B</li>
<ul>
<li>B</li>
</ul>
</ul>
</ul>
CSS
ul {
list-style-type:none;
margin-top:5px;
padding-left:40px;
float:left;
width:400px;
overflow:hidden;
background:#ff0000;
}
li {
padding-top:5px;
}
ul div {
position:absolute;
left:0;
width:100%;
border-top:3px solid #fff;
}
jQuery
$(document).ready(function(){
$('ul').prepend('<div></div>');
});
jsFiddle here. Hopefully this works for you!
You can do like this
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<ul class="mainUL">
<li>A</li>
<ul><li>B</li>
<ul><li>C</li></ul>
</ul>
</ul>
</body>
</html>
CSS Code
li{list-style:none;background:red;margin-top:4px; }
li:hover{background:green}
li:hover:before {background:green}
li:before {background:red; width:100%; content:'.'; position:absolute;left:0; z-index: -1;color:red;}
.mainUL {padding-left: 0px;}
You can see the working demo : http://jsbin.com/uReBEVe/71/edit
from your demo:
if you apply
ul{
padding:0;
margin:0;
}
everything sits flush to the wall like you want.
if you want text indents
ul ul li{
text-ident:20px;
}
which is nesting. will only targets li's that are in ul's that are nested in ul's. then what you want works and you don't need to change your code
you can also keep nesting that code
add more ul's and li's depending on the depth of your structure, but this should give you a very good base

CSS dynamic horizontal navigation menu to fill up specific width (table behavior)

I need to create a horizontal navigation menu with a changing number of items (this is important - I can't hard-code widths into the CSS and I don't want to calculate them with JS) that fill up to a certain width, let's say 800px.
With tables,
<table width="800" cellspacing="0" cellpadding="0">
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
<td>Four</td>
<td>Five Seven</td>
</tr>
</table>
<style>
table td {
padding: 5px 0;
margin: 0;
background: #fdd;
border: 1px solid #f00;
text-align: center;
}
</style>
Note that longer items take up more space and I can add items into the HTML without changing anything in CSS and the menu items shrink to accommodate additional items - the whole menu never being shorter or longer than 800px.
As a menu isn't a semantically correct use of a table, can this be done with say a list and pure CSS?
In browsers that support the table display CSS rules, yes:
<style>
nav {display:table; width:800px; background:yellow}
ul {display:table-row; }
li {display:table-cell; border:1px solid red}
</style>
<nav>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five Seven</li>
</ul>
</nav>
Basically, you'd have to build a token table. In action: http://jsbin.com/urisa4
Otherwise: no, not if you can't compromise your requirements.
You could do something like this:
<style type="text/css">
#container { width: 800px ; border: 1px dashed #333; }
#container div { width: inherit; display: table-cell; background: #ccc}
</style>
<div id="container">
<div>Something</div>
<div>Something</div>
<div>Something</div>
</div>
That way it fills up what you want, but can grow to a lot of items.
Hope this helps.
The only way to do this with CSS is to hard-code the width of the li elements (assuming you'd be using the following structure):
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five Seven</li>
</ul>
ul {
width: 80%; /* or whatever */
}
ul li {
width: 16%;
padding: 1%;
}
JS Fiddle demo.
This leaves, potentially, an 'unused' 10% (to be used for margin or additional padding).
At some point in the future css calculations might be able to perform this more fluidly.
This works, but i don´t think you want some think like this :)
<div class="master">
<ul>
<li>test1</li>
<li>test2</li>
<li>aölsdkfaösdlfk</li>
<li>yeah baby</li>
<li>hi</li>
</ul>
</div>
.master {
width:800px;
background-color:black;
height:100px;
color:white;
display:table;
}
table {
width:100%;
}
ul {
display:table-row;
width:100%;
}
li {
display:table-cell;
width:auto;
margin:1px;
border:1px solid white;
background-color:red;
text-align:center;
vertical-align:middle;
}
http://jsfiddle.net/UnNyS/

HTML UL CSS Border style

I have a little question concerning html-lists and CSS. I want to have a list (whit sublist) that looks like this (view the result by coping the code into http://htmledit.squarefree.com/):
<style type="text/css">
ul
{
border: 0px solid #90bade;
}
li
{
list-style-type:none;
border: 1px solid black;
}
.lv1
{
margin:0px 0px 0px 10px;
}
</style>
<ul>
<li>One</li>
<li class ="lv1">Sub</li>
<li>One</li>
</ul>
But I would prefer to use html-code like this for the list:
<ul>
<li>One
<ul>
<li>Sub</li>
</ul>
</li>
<li>One</li>
</ul>
Unfortunately I cannot figure out, how to style the second list whit CSS in order to make it look like the first one. Does anyone know if it’s possible by only using CSS or do I have to make one big list and use classes (like in the first list) to get the desired layout?
Thanks in advance
You can wrap the text with a div and give the div the desired border. Add the style for the div to your CSS and remove the border from your li. That should do the trick. I don't see a possible sollution without editing the HTML, though.
.border {
border: 1px solid black;
}
<ul>
<li>
<div class="border">One</div>
<ul>
<li><div class="border">Sub</div></li>
</ul>
</li>
<li><div class="border">One</div></li>
</ul>
As Elwhis said, what you can do is add the <div> in <li> but you can avoid "class" like this:
<style type="text/css">
ul
{
border: 0px solid #90bade;
}
li
{
list-style-type:none;
border: 0px solid black;
}
ul li div
{
border:1px solid black
}
</style>
<ul>
<li><div>One</div>
<ul>
<li><div>Sub</div></li>
</ul>
</li>
<li><div>One</div></li>
</ul>
Replace list-style-type:none;
with list-style-position:inside;
li
{
list-style-position:inside;
border: 1px solid black;
}

css hover over li, blur all others

<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
Can anyone tell me if it's possible to use css to blur the ul elements containing a,d,e when the user moves their mouse over the element containing c, without using JavaScript?
Do you mean something like this:
http://jsfiddle.net/S4TMS/
HTML
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
CSS
ul li {
background-color: red;
margin: 2px;
}
ul:hover li {
opacity: .5;
}
ul li:hover {
opacity: 1;
}
As opposed to using multiple selectors, you can combine them into one using the :not selector.
ul:hover li:not(:hover) {
opacity: .5;
}
Which will set opacity:.5 on all the li, exept the one being hovered over.
jsFiddle here
Here is a great example of what your after. That code sample demonstrates how to blur everything but the hover element.
You could try something like this, which is not supported in all browsers due to the text-shadow attribute:
ul:hover li {
text-shadow: 0px 0px 3px black;
color: transparent;
}
ul:hover li:hover {
text-shadow: none;
color: black;
}
EDIT: Added a link to a jsfiddle above, since that's apparently the cool thing that gets you votes. :P