Select all siblings if one of them has a specific class [duplicate] - html

This question already has answers here:
Is there a "previous sibling" selector?
(30 answers)
Closed 4 years ago.
RE: above regarding duplicate - my question asks if there was a way to select ALL elements inside of a child where one class exists in pure CSS whereas that linked question is just asking if there is an opposite of the + selector which is not what I was asking/looking for. This should be untagged as duplicate.
Main Question:
I want to select all elements inside a parent if one of the siblings has a specific class. How do I do this? I tried:
https://www.w3schools.com/cssref/sel_gen_sibling.asp
The element1~element2 selector matches occurrences of element2 that
are preceded by element1.
But it's not what I want.
CSS sibling selectors (select all siblings)
My question is not a duplicate as the class is in the first element in above question comes first which makes it work.
See below:
.red ~ .item {
background-color: red;
}
<ul>
<li class="item red">list item</li>
<li class="item">list item</li>
<li class="item">list item</li>
<li class="item">list item</li>
<ul>
<ul>
<li class="item">list item</li>
<li class="item">list item</li>
<li class="item red">list item</li>
<li class="item">list item</li>
<ul>
How do you select all siblings if one of them has a class in pure CSS? Or do I need to use JS?

Use jQuery to get the siblings and then apply the red class to them.
$( "li.item.red" ).siblings().addClass("red");
.red {
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="item red">list item</li>
<li class="item">list item</li>
<li class="item">list item</li>
<li class="item">list item</li>
<ul>
<ul>
<li class="item">list item</li>
<li class="item">list item</li>
<li class="item red">list item</li>
<li class="item">list item</li>
<ul>

For this particular case and if you want to apply background to all your elements, you can consider a pseudo element that will cover all of them as your background layer:
ul {
position: relative;
z-index: 0;
}
.red:before {
content: "";
position: absolute;
z-index: -1;
top: 0;
left: 40px;
right: 0;
bottom: 0;
background-color: red;
}
<ul>
<li class="item red">list item</li>
<li class="item">list item</li>
<li class="item">list item</li>
<li class="item">list item</li>
</ul>
<ul>
<li class="item">list item</li>
<li class="item">list item</li>
<li class="item red">list item</li>
<li class="item">list item</li>
</ul>

Related

Nested list alignment issues

I have a dropdown menu (full-width) on my dropdown, I have a nested list and I want each column to have equal height and width. I tried to add a container inside each column and put display: table-cell; but it seemed my code isn't working at all. can ya'll help me with this?
<div class="column">
<div class="col-container">
<ul id="sub-list">
<li class="sub-list-item">
<a class="sub-list-title">창업 프로세스</a>
</li>
<li class="nested-child">
<ul class="sub-sub-list">
<li class="item"><a class="sub-sub-title">경진대회정보</a></li>
<li class="item"><a class="sub-sub-title">명예의 전당</a></li>
</ul>
</li>
<li class="sub-list-item">
<a class="sub-list-title">행사 네트워크</a>
</li>
</ul>
</div>
</div>
snippet from style.scss
.column{
float: left;
margin: 0;
display: table;
.col-container{
display: table-cell;
background: yellow;
padding: 21px;
}
}
I'll provide a photo of what the output should be like.
This is made using nested UL. how do we achieve this one?
You can change the flex-basis value to adjust the number of columns you want to dislplay, hope you have got the solution looking for.
Refer flexbox:
ul,
li {
margin: 0;
padding: 0;
}
.list-item-main {
display: flex;
flex-wrap: wrap;
}
.list-item-main>.list-item {
list-style: none;
flex-basis: 33%;
box-sizing: border-box;
padding: 15px;
}
.list-item-main>.list-item .nested-ul>li {
list-style: none;
box-sizing: border-box;
padding: 0 15px;
}
.list-item-main .list-item-head {
background: gold;
}
<div class="list-container">
<ul class="list-item-main">
<li class="list-item">
<a>Single List item</a>
</li>
<li class="list-item">
<ul class="nested-ul">
<li class="list-item-head">nested list 1 item</li>
<li class="list-item">nested list 1 item</li>
<li class="list-item">nested list 1 item</li>
<li class="list-item">nested list 1 item</li>
</ul>
</li>
<li class="list-item">
<ul class="nested-ul">
<li class="list-item-head">nested list 2 item</li>
<li class="list-item">nested list 2 item</li>
<li class="list-item">nested list 2 item</li>
<li class="list-item">nested list 2 item</li>
</ul>
</li>
<li class="list-item">
<ul class="nested-ul">
<li class="list-item-head">nested list 3 item</li>
<li class="list-item">nested list 3 item</li>
<li class="list-item">nested list 3 item</li>
<li class="list-item">nested list 3 item</li>
</ul>
</li>
<li class="list-item">
<ul class="nested-ul">
<li class="list-item-head">nested list 4 item</li>
<li class="list-item">nested list 4 item</li>
<li class="list-item">nested list 4 item</li>
<li class="list-item">nested list 4 item</li>
</ul>
</li>
</ul>
</div>

Nested Ordered List Numbering Issue

I'm making a Table of Contents with nested numeric ordered lists. It starts off well, but once I go back a level the numbering gets off. As you can see once it gets to the 4th level not only is the numbering off, but now everything has one extra number, regardless of level.
This is what I end up with:
SECTION 1
1 Item 1
1.1 Sub Item
1.1.1 Level 3 Item
1.1.1.1 Level 4 Item
1.1.1.2 Level 4 Item
1.1.1.3 Level 4 Item
1.1.1.4 Level 4 Item
1.1.1.5 Level 4 Item
1.1.1.6 Level 3 Item
1.1.1.1 Level 4 Item
1.1.1.2 Level 3 Item
1.1.1.3 Level 3 Item
1.1.2 Level 2 Item
This is the code I'm using:
ol.toc {
counter-reset: item;
}
li.toc {
display: block;
}
li.toc:before {
content: counters(item, ".") " ";
counter-increment: item;
}
<!DOCTYPE html>
<html>
<head>
<title>Table of Contents</title>
</head>
<body>
<div>
<h2>SECTION 1</h2>
</div>
<div style="float: clear;">
<ol class="toc">
<li class="toc"> Item 1</li>
<ol class="toc">
<li class="toc"> Sub Item</li>
<ol class="toc">
<li class="toc"> Level 3 Item</li>
<ol class="toc">
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
</ol>
<li class="toc"> Level 3 Item</li>
<ol class="toc">
<li class="toc"> Level 4 Item</li>
</ol>
<li class="toc"> Level 3 Item</li>
<li class="toc"> Level 3 Item</li>
</ol>
<li class="toc"> Level 2</li>
</ol>
</ol>
</div>
</body>
</html>
Not sure what's going on. I haven't found too many examples that fit my needs.
I appreciate any help!
I think the wrong numbering is happening because you should wrap every child <ol> in it's parent <li>. And for better look set list-style-type:none; of the top most parent <ol>.
Check it out:
ol.toc {
counter-reset: item;
}
li.toc {
display: block;
}
li.toc:before {
content: counters(item, ".") " ";
counter-increment: item;
}
.outer li {
list-style-type: none;
}
<!DOCTYPE html>
<html>
<head>
<title>Table of Contents</title>
</head>
<body>
<div>
<h2>SECTION 1</h2>
</div>
<div style="float: clear;">
<ol class="toc outer">
<li class="toc"> Item 1</li>
<ol class="toc">
<li class="toc"> Sub Item</li>
<li>
<ol class="toc">
<li class="toc"> Level 3 Item</li>
<li>
<ol class="toc">
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
<li class="toc"> Level 4 Item</li>
</ol>
</li>
<li class="toc"> Level 3 Item</li>
<li>
<ol class="toc">
<li class="toc"> Level 4 Item</li>
</ol>
</li>
<li class="toc"> Level 3 Item</li>
<li class="toc"> Level 3 Item</li>
</ol>
</li>
<li class="toc"> Level 2</li>
</ol>
</ol>
</div>
</body>
</html>

How to create an html list that give a result with the UL Level numbers as the list value

I am trying to make an HTML list that looks like the following with CSS
1. List item
2. List item
3. List item
3. List item
3. List item
4. List item
4. List item
2. List item
2. List item
1. List item
1. List item
2. List item
1. List item
I had tried the following, I don't need the current list item numberings
ul {
counter-reset: section;
list-style-type: none;
}
li:before {
color: red;
counter-increment: section;
content: counters(section, ".") " ";
}
<ul>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
</ul>
You does not need counter here as you are not increasing or decreasing your counter value on same level...use :before pseudo class for each level instead
Stack Snippet
ul {
list-style-type: none;
font: 13px Verdana;
}
ul li:before {
content: "1. Link ";
color: red;
}
ul li li:before {
content: "2. Link ";
}
ul li li li:before {
content: "3. Link ";
}
<ul>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
</ul>
Updated: If you want a dynamic solution use parents() jQuery to calculate the level of each li
Stack Snippet
$("li").each(function() {
$(this).attr("data-level", $(this).parents("ul").length)
})
body {
margin-top: 40px;
}
ul {
list-style-type: none;
font: 13px Verdana;
margin-top: -15px;
position: relative;
background: #fff;
}
ul li:before {
content: attr(data-level) ". ";
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
<li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
</ul>
</li>
<li>LIST ITEM</li>
</ul>
I almost said "you can't do that with CSS"... but turns out you can.
With horrendously specific child selectors, you can :p
ul {
list-style-type:none;
}
ul li:before {
content:"1. ";
}
ul > ul li:before{
content:"2. ";
}
ul > ul > ul li:before{
content:"3. ";
}
ul > ul > ul > ul li:before{
content:"4. ";
}
/* and so on, and so forth :p */
<ul>
<li>LIST ITEM</li>
<ul>
<li>LIST ITEM</li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
</ul>
<li>LIST ITEM</li>
</ul>
<li>LIST ITEM</li>
<ul>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<li>LIST ITEM</li>
<ul>
<li>LIST ITEM</li>
<ul>
<li>LIST ITEM</li>
</ul>
</ul>
</ul>
<li>LIST ITEM</li>
</ul>
c'mon, don't tell me that's not the most ingenious and ridiculous line of CSS you've seen today :)

CSS HTML - How to add more outer colour on the drop down menus and more than one word per drop down

CODE
div.box {
display: block;
text-align: center;
margin: auto;
z-index: 5
}
#navMenu {
margin: 0;
padding: 0;
}
#navMenu ul {
margin: 0;
padding: 0;
line-height: 30px;
}
#navMenu li {
margin: 0;
padding: 0;
list-style: none;
float: left;
position: relative;
background:#999;
}
#navMenu ul li a {
text-align: center;
text-decoration:none;
height: 30px
width:300px;
display: block;
color: #FFF;
border:1px solid #000;
text-shadow: 1px 1px 1px #000;
}
#navMenu ul ul {
position: absolute;
visibility: hidden;
bottom: 31px
}
#navMenu ul li:hover ul {
visibility: visible;
}
/*****************************************************************************/
#navMenu li:hover {
background:#09F;
}
#navMenu ul li:hover ul li a:hover {
background:#CCC;
color: #000;
}
#navMenu a:hover {
color: #000;
}
.clearFloat {
clear:both;
margin: 0'
padding:0;
}
<!DOCTYPE html>
<html>
<head>
<title>ZEFROLITY</title>
<link rel="stylesheet" href="main.css" media="screen" title="no title" charset="utf-8">
<link href="main.css" rel="stlesheet" type="text/css">
</head>
<body>
<div class="box">
<img src="icon1.png" alt="zefrolity" width="50%" height="50%">
</div>
<div id="wrapper">
<div id="navMenu">
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<br class="clearFloat">
</div> <!--end navMenu -->
</div> <!--end wrapper div -->
</body>
</html>
I want the drop down menus to cover the whole bottom not just some of the left and I want more grey colour next to the letters so more than one word can go on the same line and it looks more clean
Kind Regards
Zefrolity thanks in advance
Firstly you missed a semi-colon after one of your CSS rules (height: 30px), so the rest of the rules for that selector weren't being called.
So solve your problem (as I understand it), you need to give the first level li's a percentage width. There's 8 items so to make the menu cover the whole width, it's 100/8 (12.5%). I've added a direct child selector to add styles specifically for this first level li. E.g. #navMenu > ul > li will pick up only the first level li, not the child one. I made a few other comments through the code as direction.
div.box {
display: block;
text-align: center;
margin: auto;
z-index: 5
}
#navMenu {
margin: 0;
padding: 0;
}
#navMenu ul {
margin: 0;
padding: 0;
line-height: 30px;
}
#navMenu li {
margin: 0;
padding: 0;
list-style: none;
position: relative;
background:#999;
}
/* Direct child selector for first level li only */
#navMenu > ul > li {
float: left;
width: 12.5%; /* Set width here (100 / 8 items) */
}
#navMenu ul li a {
text-align: center;
text-decoration:none;
height: 30px; /* Was missing semi-colon */
/* width:300px; Remove, will take up width of parent */
display: block;
color: #FFF;
border:1px solid #000;
text-shadow: 1px 1px 1px #000;
}
#navMenu ul ul {
position: absolute;
width: 100%; /* Be full width of parent */
visibility: hidden;
bottom: 100%; /* Be 100% from bottom of parent */
}
#navMenu ul li:hover ul {
visibility: visible;
}
/*****************************************************************************/
#navMenu li:hover {
background:#09F;
}
#navMenu ul li:hover ul li a:hover {
background:#CCC;
color: #000;
}
#navMenu a:hover {
color: #000;
}
.clearFloat {
clear:both;
margin: 0'
padding:0;
}
<!DOCTYPE html>
<html>
<head>
<title>ZEFROLITY</title>
<link rel="stylesheet" href="main.css" media="screen" title="no title" charset="utf-8">
<link href="main.css" rel="stlesheet" type="text/css">
</head>
<body>
<div class="box">
<img src="icon1.png" alt="zefrolity" width="50%" height="50%">
</div>
<div id="wrapper">
<div id="navMenu">
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<ul>
<li>Products
<ul>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
<li>Link Item</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI -->
</ul><!-- end main UL -->
<br class="clearFloat">
</div> <!--end navMenu -->
</div> <!--end wrapper div -->
</body>
</html>

How do I properly use display:block on this menu?

Code:
<div class="menu">
<ul class="top_thing">
<li class="one_one">Services</li>
<ul class="the_one">
<li class="second">Language 1</li>
<li class="second">Language 2</li>
<li class="second">Language 3</li>
<li class="second">Language 4</li>
<li class="second">Language 5</li>
</ul>
<li class="one_one">About Us</li>
<li class="one_one">Contact</li>
</ul>
</div>
For CSS: I am using display:none on .the_one. Now once the services tab is hovered I want to then display .the_one. How can I do this? I tried doing:
li.the_one:hover {
display:block;
}
But that doesn't work either.
Your HTML is not valid. <ul> must have only <li> as children (and that includes other <ul>. Once you fix that, add these rules:
.the_one {
display: none;
}
.one_one:hover .the_one {
display: block;
}
http://jsfiddle.net/xvEvj/
You need to target the the inner list in your css to make it appear.
Change your css as below:
li.the_one:hover ul.the_one
{
display:block
}
the "li.the_one:hover" states that this will occur when the li with the class "the_one" is hovered over, then it applies a display:block to the ul with the class "the_one" inside the list item with the class "the_one".
Hope this helps.
Cheers.
Your HTML is not valid.
<div class="menu">
<ul class="top_thing">
<li class="one_one">Services
<ul class="the_one">
<li class="second">Language 1</li>
<li class="second">Language 2</li>
<li class="second">Language 3</li>
<li class="second">Language 4</li>
<li class="second">Language 5</li>
</ul>
</li>
<li class="one_one">About Us</li>
<li class="one_one">Contact</li>
</ul>
The second unordered list should rest within a list item of the first unordered list.