I have the following markup:
<div class="class-XXX">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
For simplicity, lets assume that class-XXX can only have the values class-1, class-2, class-3 and class-4.
I want to apply the rule color: #fff; to every child of class-5 that is not a child of class-1. Here that part of my stylesheet:
.class-2 .class-5,
.class-3 .class-5,
.class-4 .class-5 {
color: #fff;
}
This is not working and I'm not really sure why. I don't believe that the rule is being overridden either.
UPDATE
As AndrewBone pointed out, the rule appears to work in a minimal example. I now understand what is wrong, but I don't know how to fix it:
There is a rule being applied to h1 in another CSS file (can't be removed) and that rule is being given higher priority than the rule I was writing. How can I fix this?
Here is an example JSFiddle.
SOLUTION
Vucko pointed out that the h1 type selector has higher priority and so the rule will not be applied. So, in order to avoid listing all possible combinations one should use the * selector!
End result:
.class-2 .class-5 *,
.class-3 .class-5 *,
.class-4 .class-5 *{
color: #fff;
}
My thanks to Paulie_D and David Wilkinson for teaching me about the :not pseudo-selector.
This would do it..
[class^="class-"]:not(.class-1) .class-5 {
*/ your styles here */
}
...but this only works for a specific methodolody in classnames as above.
[class^="class-"]:not(.class-1) .class-5 {
color: red;
}
<div class="class-1">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
<div class="class-2">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
<div class="class-3">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
<div class="class-4">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
<div class="class-5">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
If you have some container for those divs, you can then use the :not selector (as Harry mentioned in the comment):
.main :not(.class-1) .class-5 {
color: red;
}
<div class="main">
<div class="class-1">
<div class="class-5">
<!-- could be any text element -->
<h1>1</h1>
</div>
</div>
<div class="class-2">
<div class="class-5">
<!-- could be any text element -->
<h1>2</h1>
</div>
</div>
<div class="class-3">
<div class="class-5">
<!-- could be any text element -->
<h1>3</h1>
</div>
</div>
<div class="class-4">
<div class="class-5">
<!-- could be any text element -->
<h1>4</h1>
</div>
</div>
<div class="class-5">
<div class="class-5">
<!-- could be any text element -->
<h1>5</h1>
</div>
</div>
</div>
.main :not(.class-1) .class-5 {
color: red;
}
JSFiddle
This does the trick: https://jsfiddle.net/023rox1k/
CSS:
.wrapper :not(.class-1) .class-5 {
color: blue;
}
HTML:
<div class="wrapper">
<div class="class-1">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
<div class="class-2">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
<div class="class-3">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
<div class="class-4">
<div class="class-5">
<!-- could be any text element -->
<h1>Content</h1>
</div>
</div>
</div>
The :not selector is quite powerful and obviously targets elements not of a certain class in this case.
Related
I want to set every div with the class="wp-block-column" to 'display:none;' --> but not the first one.
I am having trouble :not selecting the first div with the class="wp-block-column"
BOTTOM LINE: I want to select all divs with the class="wp-block-column" throughout the page regardless of where they land or who their parent is --> but not the first instance. I want to select instances from the body, not a parent div.
<div class="menu-column"> only appears inside <div class="seventy-thirty-block"> but there could be 1 or 5 of these blocks, they are generated from a loop.
There could also be any number of blocks that are not <div class="seventy-thirty-block">before.
The css on this page works because :not(:nth-child(2) gets the second child of <div class="singleBlogContent">
But if I uncomment
<!--
<div class="callout-full-block">
</div>
-->
then it no longer works because now it's :not(:nth-child(3)
Here is the JS Bin:
https://jsbin.com/xowefaj/edit?html,css,output
The output that I want in the JS Bin is:
content
menu
content
content
This is my html:
<div class="singleBlogContent">
<div class="callout-full-block">
</div>
<!-- <div class="callout-full-block">
</div> -->
<div class="seventy-thirty-block">
<div class="container">
<div class="seventy-thirty-columns">
<div>
<div>
content
</div>
</div>
<div class="wp-block-column">
<div class="menu-column">
menu
</div>
</div>
</div>
</div>
</div>
<div class="seventy-thirty-block">
<div class="container">
<div class="seventy-thirty-columns">
<div>
<div>
content
</div>
</div>
<div class="wp-block-column">
<div class="menu-column">
menu
</div>
</div>
</div>
</div>
</div>
<div class="seventy-thirty-block">
<div class="container">
<div class="seventy-thirty-columns">
<div>
<div>
content
</div>
</div>
<div class="wp-block-column">
<div class="menu-column">
menu
</div>
</div>
</div>
</div>
</div>
</div>
This is my css:
div.seventy-thirty-block:not(:nth-child(2)) .wp-block-column .menu-column {
display:none;
}
Assign the first one a unique class then assign it display: block; after the initial ruleset for display: none. If you want a dynamic solution, you should use JavaScript. See Figure I
Figure I
document.querySelectorAll('.wp')[0].classList.add('.first');
.wp {
display: none;
}
.first {
display: block
}
<main>
<div class='wp first'>First</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
<div class='wp'>WP</div>
</main>
I just wanna apply my custom style into the elements which are inside a specific div. I mean there are many elements with the same class all over the page and I want only apply these custom CSS to elements are inside a div with the data attr like div data-some-feature=... and not apply to the rest
<div class="row" data-language>
<section class="posts by-tags show-grid" data-language>
<div class='right a'>a</div>
<div class="row">';
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 m-right">
<div class="a">
<h6>apple</h6>
<p>some text...</p>
</div>
</div>
</div>
</section>
</div>
<div class='row'>
<section .....>
......
</section>
</div>
<style>
div[data-dictionary-language].posts {
//some style
}
</style>
I want to use only div[data-dictionary-language] once and then apply my custom CSS like below:
<style>
div[data-dictionary-language]{
.tags{
//some style only apply to all elements are inside the specific div
}
.posts{
//some style only apply to all elements are inside the specific div
}
}
</style>
You were almost there, try like so:
/* ___________ select the div with data-language attribute
| ______ note the space here
| | ________ select child of div[data-language] with class posts
______|________ | __|__ */
div[data-language] .posts {
background-color: orange;
}
<div class="row" data-language>
<section class="posts by-tags show-grid" data-language>
<div class='right a'>a</div>
<div class="row">';
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 m-right">
<div class="a">
<h6>apple</h6>
<p>some text...</p>
</div>
</div>
</div>
</section>
</div>
<div class='row'>
<section .....>
......
</section>
</div>
I have a sass block that i have tried several different ways:
I've tried this:
.progress-body {
display: none;
&:first-of-type {
display: block;
}
}
and this:
.progress-body {
&:not(:first-of-type) {
display: none;
}
}
and this:
.progress-body:not(:first-of-type) {
display: none;
}
when applied to HTML that looks like this:
<div class="panel">
<div class="panel-heading">
</div>
<div class="panel-body progress-body">
<div class="row">
<div class="col-md-12">
<p class="lead">Step 1: Choose your template...</p>
</div>
</div>
</div>
<div class="panel-body progress-body">
<div class="row">
<div class="col-md-12">
<p class="lead">Step 2: Compose your email...</p>
</div>
</div>
</div>
</div>
the result is that it hides all the elements with the progress-body class. This is normally pretty straight forward CSS so no idea what is wrong here...
In this case progress-body is not the first-of-type, this would technically be .panel-heading since the first-of-type refers to the type element selector (div) and not the class.
The :first-of-type CSS pseudo-class represents the first element of
its type among a group of sibling elements.
Ref: :first-of-type - CSS | MDN
Consider wrapping your .progress-body elements in a containing element, you will achieve the expected behaviour, since .progress-body would be the first of its type with the class name .progress-body.
Code Snippet Demonstration:
.progress-body:not(:first-of-type) {
display: none;
}
<div class="panel">
<div class="panel-heading">
</div>
<div class="panel-outer-body">
<div class="panel-body progress-body">
<div class="row">
<div class="col-md-12">
<p class="lead">Step 1: Choose your template...</p>
</div>
</div>
</div>
<div class="panel-body progress-body">
<div class="row">
<div class="col-md-12">
<p class="lead">Step 2: Compose your email...</p>
</div>
</div>
</div>
</div>
</div>
If you can't wrap as UncaughtTypeError wrote in his answer, youcan use (general) sibling selectors.
.progress-body + .progress-body {display: none;}
or
.progress-body ~ .progress-body {display: none;}
I expect the first is block by default, if you didn't change it elsewhere.
I want to grab all the class name ('my-class') and change it's color to red EXCEPT for the last one
Apparently I've been googling and there's no such thing as :last-of-class or whatever. I'm having trouble trying to find a work around without using JS.
div1 and div2 are both dynamic! If div2 doesn't exist, then div1 should have the first p element red and the second not.
Please note I left a 'p' tag at the top because I don't want that being part of my selector. I just need the 'my-class' specifically.
or is there a selector I can write to grab all "p"s inside of my-container which include nested P's
<p>Some text</p>
<div class="my-container">
<div class="div1">
<p class="my-class"></p>
<p class="my-class"></p>
</div>
<div class="div2">
<p class="my-class"></p>
<p class="my-class"></p>
<p class="my-class"></p>
<p class="my-class"></p> <!-- This tag should not be red-->
</div>
</div>
I can also use sass so feel free to include that in if need be.
I don't know of any SINGLE rule that would do this, but a simple workaround would be to use 2 separate rules in conjunction:
.my-class {
color: red;
}
.div-2 .my-class:last-child {
color: // whatever you want the default to be
}
note that the order is important, setting the last child's color should be done after setting everything first
You can use the workaround below.
use div:last-child . that will select the last div in the container and if there is only one, it will select it and so...the last p from the last div will be of other color ( in this example )
.my-container div p.my-class {
color:red;
}
.my-container div:last-child p.my-class:last-child {
color:blue;
}
<p>Some text</p>
<div class="my-container">
<div class="div1">
<p class="my-class">a</p>
<p class="my-class">a</p>
</div>
<div class="div2">
<p class="my-class">a</p>
<p class="my-class">a</p>
<p class="my-class">a</p>
<p class="my-class">b</p> <!-- This tag should not be red-->
</div>
</div>
This will get the behavior you're looking for without any forced reflow:
.my-class:not(:last-child) {
color: red;
}
<p>Some text</p>
<div class="my-container">
<div class="div1">
<p class="my-class">a</p>
<p class="my-class">b</p>
</div>
<div class="div2">
<p class="my-class">c</p>
<p class="my-class">d</p>
<p class="my-class">e</p>
<p class="my-class">f</p> <!-- This tag should not be red-->
</div>
</div>
<div class='container'>
<div class='item'>
<div class='date'>
<a></a>
</div>
<div class='work'>
<a></a>
</div>
<div class='info'>
<a>$row[info]</a>
</div>
</div>
<div id="footer">
</div>
</div>
I have border-bottom for class info:
.info {padding-bottom: 10px; border-bottom: 1px dashed #C8C8C8;}
But I dont want border for last child but it is not working. All borders disappears.
.info:last-child {border-bottom: none;}
Why is that? And what is the best solution?
I am not sure what you're trying to achieve? Your trying to apply :last-child when you've only got one child to target, therefore it will not appear.
It would work if there were more than one .info
See here: http://jsfiddle.net/ELk4E/ - In the fiddle I've replicated div.info three times, and the 3rd and last has the border-bottom removed via the :last-child
However, if you have div.info on your page several times but within different parents, this will not work. Such as:
<div class='container'> <!-- parent 1 -->
<div class='item'>
<div class='date'>
<a></a>
</div>
<div class='work'>
<a></a>
</div>
<div class='info'>
<a>Last Child</a>
</div>
</div>
<div id="footer">
</div>
</div>
<div class='container'><!-- parent 2 -->
<div class='item'>
<div class='date'>
<a></a>
</div>
<div class='work'>
<a></a>
</div>
<div class='info'>
<a>Last Child</a>
</div>
</div>
<div id="footer">
</div>
</div>
In this example you could use the following CSS to remove the border-bottom of the last container's div.info:
.info {padding-bottom: 10px; border-bottom: 1px dashed #C8C8C8;}
.container:last-of-type .info {border-bottom: none;}
Basically the :last-child selector matches every element that is the last child of its parent. See here: http://www.w3schools.com/cssref/sel_last-child.asp
Try this one
.info:last-child {border-bottom: none; !important}