set style only for elements are inside a specific div - css

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>

Related

How do I select every instance of a class on a page, but not the first instance - regardless of parent?

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>

How to click a div with certain text in a nested div using cypress?

Below is my HTML code :
<div class="main">
<div class="box">
<div class="cell">checkbox</div>
<div class="cell"></div>
<div class="cell">
<a>link1</a>
</div>
<div class="cell">type</div>
<div class="cell">description</div>
</div>
<div class="box"> //i want to click this element
<div class="cell">checkbox</div>
<div class="cell"></div>
<div class="cell">
<a>link2</a>
</div>
<div class="cell">type1</div>
<div class="cell">description1</div>
</div>
<div class="box">
<div class="cell">checkbox</div>
<div class="cell"></div>
<div class="cell">
<a>link3</a>
</div>
<div class="cell">type2</div>
<div class="cell">description2</div>
</div>
</div>
I'd like to click on the div with the class box containing the <a>link2</a> tag.
I have tried this :
cy.get(`.main>div`).contains('link2').click();
This clicks on the link element itself, but I'd like it to click the div element that contains it (the <a>link2</a> tag) instead.
Is there any way to do this?
I'm looking forward to any tips and solutions you might provide.
Thanks.
You could try getting the link2's parent element which is the div you want to click, in the following way:
cy.get(`.main>div`).contains('link2').parent().click();
I believe you can use a selector in 'contains' itself like below
cy.contains('.main>div', 'link2').click();

Apply CSS rule to multiple elements within a sub-class

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.

How to target the first occurrence of an element in the parent container by using css?

I am trying to target the first occurrence of .discount class but some how its not working. I am trying .discount:first-child but its targeting all the .discount class inside the .container class.
Any one has idea how can I target only the first .discount class element inside the .container element?
My HTML code looks like this
<div class="container">
<div class="wrapper">
<div class="some-class">
Product
</div>
<div class="some-class">
Discount
</div>
</div>
<div class="wrapper">
<div class="product">
Product 1
</div>
<div class="discount">
$1
</div>
</div>
<div class="wrapper">
<div class="product">
Product 2
</div>
<div class="discount">
$5
</div>
</div>
<div class="wrapper">
<div class="product">
Product 4
</div>
<div class="discount">
$10
</div>
</div>
</div>
why you dont put another class inside the div you want. like this http://jsfiddle.net/8L0un657/
<div class="wrapper">
<div class="product">
Product 1
</div>
<div class="discount selected">
$1
</div>
</div>
Try this
.wrapper:nth-child(2) > .discount:nth-child(2)
This selects the wrapper class which is the 2nd child of its parent (i.e. container)and then selects the discount class which is the second child of its parent i.e. wrapper div.
JsFiddle
But you can also use 'id' to reference any particular element.

how to put content in webkit column horizontally

how to put content in column horizontally like below,
(column1) (column2) (column3)
1.asd 2.asd 3.asd
4.asd 5.asd
this flow is required in the website
For columns in css3, you have to have a div structure for that...
Html:-
<div class="table">
<div class="column">
<div class="row">1</div>
<div class="row">4</div>
<div class="row">7</div>
</div>
<div class="column">
<div class="row">2</div>
<div class="row">5</div>
<div class="row">8</div>
</div>
<div class="column">
<div class="row">3</div>
<div class="row">6</div>
<div class="row">9</div>
</div>
</div>
Css:-
.table { columns:3; -webkit-columns:3; -moz-columns:3; text-align:center }
.column { }
.row { background-color:red; }
Demo:- http://jsfiddle.net/5P9c4/1/

Resources