CSS selector: element without any its children - html

I have a nested unordered list with one "li" element defined with identifier: [data-main]
1 (must be selected)
1.1
1.2
2
using the following html:
<ul>
<li data-main>1 (must be selected)
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
</li>
<li>2</li>
</ul>
I'm trying to find the right CSS selector for selecting only element 1 without its children: 1.1 and 1.2. Selectors, I tried:
li:not([data-main]) - selects all li except main, but i need something opposite
[data-main]:not(:nth-child(1)) - selects nothing
https://jsfiddle.net/DaViking/dtqhag2t/

What you're not realising is that the [data-main] selector in your JSFiddle demo is selecting only that top-level li element. The problem you're facing here is that this li element contains the other li elements. Those aren't selected by this selector individually, but they are contained within the element which is selected:
If you want to style just the text held within the [data-main] element but not within the ul element contained within it, you'll need to override the [data-main] style declarations:
[data-main] {
color: red;
}
[data-main] ul {
color: initial;
}
<ul>
<li data-main>1 (must be selected)
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
</li>
<li>2</li>
</ul>
If you want to place a border around the "1 (must be selected)" text and nothing else, you can wrap that text in a span element and apply styling to that instead:
[data-main] span {
border: 1px solid red;
}
<ul>
<li data-main>
<span>1 (must be selected)</span>
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
</li>
<li>2</li>
</ul>

Related

Using CSS to avoid tagging element that are inside the element

How do I avoid tagging css to the below code. I've tried a few things e.g. tried first:child but that didnt seem to work. I would just like the outer lis to be red not the second lis within the parent li
li {
color: red;
}
<ul>
<li>Tag this as red
<ul>
<li>
Dont' tag this as color red
</li>
</ul>
</li>
<li>Tag this as red
<ul>
<li>
Dont' tag this as color red
</li>
</ul>
</li>
</ul>
```
Use direct child combinator > along with initial:
ul>li {
color: red;
}
ul>li>ul>li {
color: initial;
}
<ul>
<li>Tag this as red
<ul>
<li>
Dont' tag this as color red
</li>
</ul>
</li>
<li>Tag this as red
<ul>
<li>
Dont' tag this as color red
</li>
</ul>
</li>
</ul>
The reason you need to reset it on the inner lis is a) color is one of the inherited properties (any element will have the text color defined closest in its ancestor tree, unless it explicitly has a color set), and b) that ul>li will also match the inner lis.
you can use just classes to separate each one of them
.tagged-red {
color: red;
}
.untagged-red {
color: black;
}
<ul>
<li class="tagged-red">Tag this as red
<ul>
<li class="untagged-red">
Dont' tag this as color red
</li>
</ul>
</li>
<li class="tagged-red">Tag this as red
<ul>
<li class="untagged-red">
Dont' tag this as color red
</li>
</ul>
</li>
</ul>
The child combinator > is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. (for more information about child combinator you can read this.
But in your case, This doesn't work, because inner <li> inherits color from outer <li>, and both turn red.
.outer{
color:red;
}
.inner{
color:initial;
}
<ul>
<li class="outer"> This shoud be red
<ul>
<li class="inner">
This shoudn't be red
</li>
</ul>
</li>
<li class="outer"> This shoud be red
<ul>
<li class="inner">
This shoudn't be red
</li>
</ul>
</li>
</ul>
For more read about inheritance in css, you can read this article

Horizontal list inline styling works but css referencing does not

Trying to make a horizontal list via the use of a CSS document. When I use inline sttyling the list displays as wanted but when I use CSS selectors, it doesn't. My CSS isn't being overwritten as I've also tested in JSBin.
#footer-list li {
display: inline;
}
<ul class="footer-list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<ul>
<li style="display:inline;">1</li>
<li style="display:inline;">2</li>
<li style="display:inline;">3</li>
<li style="display:inline;">4</li>
</ul>
You use wrong selector, try "dot" instead of "hash"
.footer-list li {
display: inline;
}
As Dariusz Majchrzak stated, you are incorrectly using a '#' rather than a '.'
This is an issue because in CSS element ID's are marked with a # whereas classes are marked with a . and here you are referencing a class "footer-list"
Had this been
id="footer-list"
than your original code of #footer-list would have worked as expected.

How to apply last-child only for first nested li?

I have the following ul list:
<ul class="list">
<li>
<ul>
<li></li>
</ul>
</li>
<li></li>
<ul>
How can I apply CSS style to last li of parent class="list", not for nested ul inside ul
You need last-of-type and >
.list > li:last-of-type {
color: red;
}
<ul class="list">
<li>a
<ul>
<li>a</li>
</ul>
</li>
<li>c</li>
</ul>
As you mention that you want last element of li's parent you use last-of-type selector, which matches every element that is the last child of a particular type, of its parent.
Second, to only match the outer most li and not nested one's, you use the child selecor > which in this case says: match the last of type which is an immediate child of an element having a class named .list
You also want to have a look at this.
try this
demo
css
ul.main > li > ul> li:first-child > a {
background:green;
}
DIRECT CHILD SELECTOR (CSS3):
ul.list >li:last-of-type{
color:red;
}
<ul class="list">
<li>Parent First Child
<ul>
<li>Child</li>
</ul>
</li>
<li>Parent Another Child</li>
<li>Parent Last Child</li>
<ul>
Note : > is used for selecting direct child of ul.list
Here is the details about CSS Pseudo-classes

Joomla menu styling first child

i have a menu:
<li id="current" class="selected parent item556">
Parent
<ul>
<li class="item557">
Child
</li>
<li class="item558">
Child
</li>
</ul>
</li>
And i want to style only the Parent "a" element, but it styles all the Child elements too, since their are under the parent "li".
I tried something like:
li#current a:first-child{
color: #F2F2F2;
}
Didn't work, how do i style only the Parent "a"?
You can use the immediate child operator > to target the link (i.e. only links directly in the li element):
li#current > a
Try: li#current a:first-of-type to get the first a
Or you can try: li#current:nth-child(1) to get the first child

a:hover does not change my anchor's list properties

Consider my html as follows:
<ul id="menu">
<li class="highlighted" id="first_item">Home</li>
<li class="non_selected_tabs">Join</li>
<li class="non_selected_tabs">Fixtures</li>
<li class="non_selected_tabs">Our Club</li>
<li class="non_selected_tabs">History</li>
<li id="hover" class="non_selected_tabs">Club Gear</li>
</li>
</ul>
My lists are styled as tabs, and I have my anchors as their parents so that when a user hovers over a tab it becomes selectable
My issue is that I was hoping to use a:hover, or the other anchor properties to change the background colour of my list item...is this possible using CSS?
I can't get it to work so I'm thinking I may have to use some JavsScript?
Wrapping the <li>'s in <a> is improper HTML and may not render properly in all browsers. A better solution would be to set the display property of the anchor to display:inline-block. Then you will be able to set the width and height of the anchor to the width and height of the li's. This way you can also use the hover property of the anchors.
<ul id="menu">
<li class="highlighted" id="first_item">Home</li>
<li class="non_selected_tabs">Join</li>
<li class="non_selected_tabs">Fixtures</li>
<li class="non_selected_tabs">Our Club</li>
<li class="non_selected_tabs">History</li>
<li id="hover" class="non_selected_tabs">Club Gear</li>
</ul>
#menu li a
{
display: inline-block;
width: 100%;
height: 100%;
}
#menu li a:hover
{
background-color:red;
}
The direct children of a ul element should only ever be list items elements, not an a.
You could either use :hover on the li element it's self, this works in browsers that aren't IE, maybe even IE8 and up..
Or you could style the a to take up the entire space area of the li, and style the li to be inline, so not to display as a typical list.
You can use :hover on other elements, not just anchors.