Using CSS selectors to hide everything but first UL and H3 - html

I am using CSS selectors to try to hide everything in my .text-wrap div except for the first UL and the first H3, however the selectors I have below are not working as expected.
CSS:
.text-wrap {
>*:not(h3:first-of-type):not(ul:first-of-type) {
display: none;
}
}
HTML:
<div class="text-wrap>
<h3>My h3</h3>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<p>paragraph</p>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</div>

As stated by the specification, the :not() pseudo class currently only accepts simple selectors:
A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.
This means that :not(h3:first-of-type) currently doesn't work since h3:first-of-type is not a simple selector.
6.6.7. The negation pseudo-class
The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument. It represents an element that is not represented by its argument.
To work around this, you can break up your selector and hide all the elements except the first of each type and then use a separate selector to hide everything except h3/ul elements:
.text-wrap > :not(:first-of-type),
.text-wrap > :not(ul):not(h3) {
display: none;
}
.text-wrap > :not(:first-of-type),
.text-wrap > :not(ul):not(h3) {
display: none;
}
<div class="text-wrap">
<h3>My h3</h3>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<p>paragraph1</p>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<p>paragraph2</p>
</div>
Alternatively, in your case, it seems like the easiest solution is to select all the sibling elements after the first ul element using the general sibling combinator, ~:
.text-wrap > ul:first-of-type ~ * {
display: none;
}
.text-wrap > ul:first-of-type ~ * {
display: none;
}
<div class="text-wrap">
<h3>My h3</h3>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<p>paragraph1</p>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<p>paragraph2</p>
</div>

You can do this with the css adjacent selector:
/* hide the elements */
.text-wrap p,
.text-wrap ul {
display: none;
}
/* show the element after the h3 */
.text-wrap h3 + ul {
display: initial;
}
<div class="text-wrap">
<h3>My h3</h3>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<p>paragraph</p>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</div>

Related

How to set bold only to first level list elements? [duplicate]

This question already has answers here:
css how to only make bold fonts for first <ul> set
(4 answers)
Closed 6 years ago.
How to set bold style with CSS only to the "titles" in this code?
Live example: jsbin.com/xofudovoda/
.container > ol {
font-weight: bold;
}
<div class="container">
<ol type="I">
<li>
Title 1
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
<li>
Title 2
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
</ol>
</div>
You can set bold on first level <ol>, and reset it on the second level <ol>s.
.container ol {
font-weight: bold;
}
.container ol ol {
font-weight: normal;
}
<div class="container">
<ol type="I">
<li>
Title 1
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
<li>
Title 2
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
</ol>
</div>
ol li { font-weight: 700 }
ol li ol li {font-weight: 300 }
You can use the following CSS:
.container ol li {
font-weight: bold;
}
.container ol li ol li{
font-weight: normal;
}
li
{
font-weight:normal;
}
.container > ol>li {
font-weight: bold;
}
Add a rule for li. That forces the child <li> elements to use their own style instead of inheriting it from their parent.
Set all the li to font-weight normal, then only apply the bolding to direct children of the original ol.
li {
font-weight: normal;
}
.container > ol:first-child > li {
font-weight: bold;
}
<div class="container">
<ol type="I">
<li>
Title 1
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
<li>
Title 2
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
</ol>
</div>
Take the Title in span tag and than to that span apply font-weight:bold ol >li > span {font-weight:bold;}
working example : http://jsbin.com/gifaliluve/edit?html,css,output
Here is the code :
<html>
<body>
<head>
<style>
ol >li > span {font-weight:bold;}
</style>
</head>
<div class="container">
<ol type="I">
<li>
<span>Title 1</span>
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
<li>
<span>Title 1</span>
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
</ol>
</div>
</body>
</html>
Nth-child will make bold first sub items of each,
ol> li> ol>li:nth-child(odd){
font-weight:bold;
}
Edit 1:
Sorry I understood wrong, this may help.
ol>li {
font-weight:bold;
}
ol > li > ol > li {
font-weight:normal;
}
Hope helps,
Problem is that you've got ordered lists nested within the li's you're trying to target specifically. Try the following:
.container > ol > li {
font-weight: bold;
}
.container > ol > li ol {
font-weight: normal;
}
<div class="container">
<ol type="I">
<li>
Title 1
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
<li>
Title 2
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
</ol>
</div>
Or, another way:
.container li {
font-weight: bold;
}
.container > ol > li > ol > li {
font-weight: normal;
}
<div class="container">
<ol type="I">
<li>
Title 1
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
<li>
Title 2
<ol>
<li>sub 1</li>
<li>sub 2</li>
</ol>
</li>
</ol>
</div>

moving a check box in html list and moving it

Really simple guys: making a collapsible list in html and css and trying to move the checkbox that controls the drop down to the FRONT of the text, as well as get rid of the dot to list the items. Right now the checkbox is placed at the end of the text. I tried to simply switch the order of the html but that screws up the drop down action.
Here is the fiddle: https://jsfiddle.net/gyetxsLu/
HTML:
<div class="CHECKBOXMENU">
<ul class="collapsibleList">
<li>
<label for="mylist-node1">Click to open list 1</label>
<input type="checkbox" id="mylist-node1" />
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
<li>
<label for="mylist-node2">Click to open list 2 with subfolders</label>
<input type="checkbox" id="mylist-node2" />
<ul>
<li>
<label for="mylist-node3">Click to expand</label>
<input type="checkbox" id="mylist-node3" />
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
</ul>
</div>
CSS:
.collapsibleList li > input + * {
display: none;
}
.collapsibleList li > input:checked + * {
display: block;
}
.collapsibleList label {
cursor: pointer;
}
No need to restructure the HTML. float: left the required checkboxes and remove the bullets using list-style-type: none
ul.collapsibleList,
ul.collapsibleList ul {
list-style-type: none;
}
#mylist-node1,
#mylist-node2,
#mylist-node3 {
float: left;
margin-right: 5px;
}
JSfiddle

Can't change a div style with hover on another div

Code seems to be correct, but simply doesn't work.
html of it
<div class="logo5">
<a class="underlogolink" id="expandlogonav" href="whatever.com" >
<div class="underlogotext alcenter">{{settings.underlogotext}}</div>
</a>
</div>
<div class="logo6" id="underlogonav">
<ul>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
<li>Test 4</li>
<li>Test 5</li>
</ul>
</div>
and the css
#underlogonav {
height:0px;
overflow-y:hidden;
-webkit-transition:0.5s;
-moz-transition:0.5s;
-ms-transition:0.5s;
-o-transition:0.5s;
transition:0.5s;
}
#expandlogonav:hover ~ #underlogonav {
height:auto;
}
Could you please point me to where i went wrong.
#underlogonav is not a sibling of #expandlogonav.
You need to have a selector that actually matches the element.
You could hover over the div containing #expandlogonav instead.
NB: If you fix that, the transition still won't work because you can't transition to auto heights.

How can I style all <li> except for the fifth and sixth using nth-child()

I have a simple list:
<ul>
<li>Test</li>
<li>Test1</li>
<li>Test2</li>
<li>Test3</li>
<li>Test4</li>
<li>Test5</li>
<li>Test6</li>
<li>Test7</li>
</ul>
I'd like to give all <li> a color of red except for the 5 + 6
http://jsfiddle.net/7yDGg/1/
Can this be done using only one selector?
Use css selector :not(target) to explicitly select which child is going to be excluded. replace the target with another selector.
We can combine selector :not() and :nth-child() to exclude specific elements.
For example in this case, we want to exclude the 5th and 6th element, then use this: :not(:nth-child(5)) and :not(:nth-child(6)).
ul li:not(:nth-child(5)):not(:nth-child(6)) {
color: red;
}
<ul>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
<li>Test 4</li>
<li>Test 5</li>
<li>Test 6</li>
<li>Test 7</li>
</ul>
The easiest way is this:
ul li {
color: red;
}
ul li:nth-child(5), ul li:nth-child(6) {
color: black;
}
fiddle updated.

Center element without knowing its width

I have a menu element like:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3
<ul>
<li>SubItem 1</li>
<li>SubItem 2</li>
<li>SubItem 3</li>
</ul>
</li>
<li>Item 4</li>
</ul>
The element is positioned absolutely. How can I center it without knowing its width (number of parent elements might change).
Regards,
Dave
I think what you're after is possible if you have a parent element to the ul:
<div class="example">
<ul>
<!-- lots of li's -->
</ul>
</div>
Then use the old school text-align trick that was used to center layouts:
.example {
text-align: center;
}
.example ul {
text-align: left;
display: inline-block;
}
See: http://jsfiddle.net/chippper/WK5Z4/