Why can't I hide my 'ul' with CSS like this? - html

I've used a ul in a paragraph tag and used CSS to hide it, but it won't work. How can I fix it?
Here's my HTML content:
<p id="transport">
<img class="draco" src="../image/draco.png"/>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li></li>
</ul>
</p>
And here is my CSS content:
p#transport ul {
background-color: rgba(0,0,0,1.0);
position: absolute;
float: right;
display: none;
}
p#transport:hover ul {
display: block;
}

That is because you cannot put a ul inside a p. The browser interprets it as if you have forgotten to close the p tag and closes it itself. That is the reason the CSS rules do not apply.
Change the p tag to a div and it'll work just fine.

This is because you can't have an ul inside a p tag. Most browsers will change the HTML for you: place the ul outside the p tag. This is what it well may end up looking like:
<p id="transport">
<img class="draco" src="../image/draco.png">
</p>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li></li>
</ul>
<p></p>
Check http://jsfiddle.net/6a0awqgv/embedded/result/. Compare the source code with the element inspector in your console.
So to overcome it you could do:
p#transport + ul {
background-color: rgba(0,0,0,1.0);
position: absolute;
float: right;
display: none;
}
p#transport:hover + ul{
display: block;
}
The + is the adjacent selector

Related

Make ul list to look better no matter of li's heights

Here is a simple html + css code:
https://jsfiddle.net/bxn72cyk/
The problem here is ul list doesn't look nice if one element has a different height than others.
How to fix this list no matter of each height with a css?
ul {
list-style: none;
}
li {
border: 1px solid red;
float: left;
padding: 10px;
line-height: 20px;
width: 30%;
}
.different {
height: 19px;
background-color: green;
}
<ul>
<li>a</li>
<li>d</li>
<li class="different">smaller height</li>
<li>set it from the left</li>
<li>d</li>
<li>d</li>
<li>d</li>
<li>d</li>
<li>d</li>
</ul>
P.S. Setting li {min-height: 20px} is one of the possible solutions, but it's not applicable. Any other options? It's not applicable as the actual size of each button could be even smaller than 20px under some circumstances.

How to turn CSS off with links?

How to turn off CSS link in HTML?
I have this in my styles.css page
li {
display: inline;
padding: 80px;
}
This makes:
<li>Home</li>
Appear horizontal.
However when I want to use an ordered list in HTML, the numbers will not appear.
I think because the CSS applies to all <li> tags.
You could try ul li to only inline items in an unordered list.
ul li {
display: inline;
padding: 80px;
}
Or better, use a class to designate lists that should display inline:
.inline-list li {
display: inline;
padding: 80px;
}
<ul class="inline-list">
<li>Home</li>
...
Ordered list has the ol tag, the ul tag is an unordered list
In your css, change the ul to ol and you will be able to css an ordered list
If you want the number to appear then you have to add ol tag.
<ol>
<li>Home</li>
<li>About</li>
</ol>
Then, if you want them to be inline, you can do
li {
padding: 80px;
}
ol {
display: flex;
}
Example:
https://jsfiddle.net/sngzv67o/
You can do it by:
a:link {
text-decoration: none;
}
w3school
At least thre exists 2 possibilities;
the first (but i guess no the one you seeked) is to specifiy the path for style, if you can something like will satisfy your need
ul > li {
display: inline;
padding: 80px;
}
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
<ol>
<li>1</li>
<li>3</li>
<li>4</li>
</ol>
this way, the inlining will only apply to unordered list items.
Another solution is to use vanilla js to explictly remove the style
ul > li {
display: inline;
padding: 80px;
}
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
<ol>
<li>1</li>
<li>3</li>
<li>4</li>
</ol>
<input type='button' onclick='document.getElementsByTagName("style")[0].remove()' value='Remove style tag'>

CSS - floating elements lose alignment

I hope someone can explain this to me and offer a solution..
I created a fiddle to demonstrate the issue and to explain the question
http://jsfiddle.net/s6ow1oq3/
Here is the question..
When elements such as <li> or <div> are floated left and they are set to a particular width but they don't/can't have a fixed height,,, how can I insure that element that breaks into new row gets aligned to the beginning of the next row?
In the fiddle example the second example demonstrates the issue while the first one is desired.
Here is the fiddle code too:
CSS:
li {
list-style: none;
}
#one li, #two li {
width: 22%;
float: left;
margin: 7px;
padding: 7px;
border:1px solid #DDD;
}
.clearfix {
clear: both;
float: none;
}
HTML:
<!-- Unordered List Examples -->
<div id="one">
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
<div class="clearfix">
</div>
<hr/>
<div id="two">
<ul>
<li>a</li>
<li>b</li>
<li>c1<br/>c2<br/>c3<br/>c4</li>
<li>d</li>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
<div class="clearfix">
</div>
Assuming it's always 4 columns, you could use li:nth-child(4n + 1) to clear the first element on every row. For example:
li:nth-child(4n + 1) {
clear: both;
}
jsFiddle: http://jsfiddle.net/s6ow1oq3/2/
Use display: inline-block; - the most under-appreciated display type IMO.
The main thing you can't do is have 100% width coverage, but since your example doesn't require the items to butt up against one another, it's a perfect use case for inline-block
The trick is to make sure that all of your width's and margins add up to roughly 99% instead of 100%.
http://jsfiddle.net/ryanwheale/s6ow1oq3/3/
#one li, #two li {
display: inline-block;
vertical-align: top;
width: 22%;
margin: 1.2%;
padding: 7px;
border:1px solid #DDD;
}

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 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