Select adjacent elements which are nested - html

My syntax looks like this:
<div class="parent">
<div class="child">content</div>
</div>
<div class="parent">
<div class="child">content</div>
</div>
<div class="parent">
<div class="child">content</div>
</div>
I want to style child elements which are siblings, like this:
.parent .child + .parent .child {
/* Some pretty nice styles */
}
Unfortunately this code not work. Does it possible with pure CSS3 or will be available only in future version 4?

Your question is unclear. Add more details. Check These Selectors
Child combinator
Adjacent sibling combinator
General sibling combinator
What your css means,
.parent .child + .parent .child {
/* Some pretty nice styles */
}
Parent with a child followed by another parent with a child. Style will apply to the 2nd parents child.
Ex:
.parent .child + .parent .child {
color: red;
}
<div class="parent">
a
<div class="child">
b
</div>
<div class="parent">
a
<div class="child">
b
</div>
</div>
</div>
<div class="parent">
a
<div class="child">
b
</div>
<div class="parent">
a
<div class="child">
b
</div>
</div>
</div>

Since parenthesis don't exist as such in css, it reads,
.parent .child + .parent .child
as "a child inside a parent immediately following a child inside a parent". So basically you're selecting :
<div class = "parent">
<div class = "child"></div>
<div class = "parent">
<div class = "child"></div>
</div>
</div>
As a previous commenter pointed out, a solution would be to alternate between parent1 and parent2 and select the divs with .parent1 + .parent2.

.parent + .parent .child{
background-color: red;
height: 100px;
width: 100px;
}
<div class="parent">
<div class="child">content</div>
</div>
<div class="parent">
<div class="child">content</div>
</div>
<div class="parent">
<div class="child">content</div>
</div>
This may work. Is that what you want? But it styles childs considering if the parents are following each other. So it doesn't really care about children.
If you really want to select only based on children you may consider a jquery solution (you didn't tag as jquery so...)

Related

How can i add CSS on that DIV

I have a below div structure and I want to add css on first .column element, not its sibling
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
I want to add CSS only first .column that comes just after #team div. So how can I select a class for that .column not for the inner .column?
You would use the direct descendant / child combinator ">" which in effect says - target the .column class that DIRECTLY descends from the #team parent div.
In the following - I am placing a border around the targetted .column div and not around the nested children .column divs.
and if there are other divs that are siblings of that particvular div - then you could use the :first-child pseudo selector as well..
#team > .column:first-child {...}
which says - target the .column div that is a direct descendant AND the first child of the #team div.
#team > .column {
border: solid 1px red;
}
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
The most specific selector in this case is #team>.column, with > between parent and child to make sure the nested divs which also have the .column class are not affected.
#team .column would not work in this case, since it also selects the .column divs which are nested in lower instances.
BTW: You mention "siblings", which is a bit confusing, since there are not any siblings to that element...
#team>.column {
background: yellow;
}
<div class="row" id="team">
<div class="column">
<div class="row">
<div class="column">
A
</div>
<div class="column">
B
</div>
<div class="column">
C
</div>
</div>
</div>
</div>
Ok, so I think you may have confused your HTML 'parent/child' structure.
You could use
#team > .column:first-child {
}
However, I don't know if you are aware that you can add any number of classes to HTML elements. You could have many classes to easily distinguish between your components and to be able to grab hold of them with CSS or JS.
For the sake of ease, you could just add another class to the element you want to add another separate class style, as I have below.
Then you could just add CSS styling for that class.
<div class="row" id="team"> //this is parent
<div class="column main"> // a child that I've added the
// class of .main to
<div class="row"> // a grandchild
<div class="column"> // then great grandchildren
A //these are siblings
</div>
<div class="column"> //these are siblings
B
</div>
<div class="column"> //these are siblings
C
</div>
</div>
</div>
</div>
/*Then you would just add stylings for*/
.main {
}

How to select the first child from all similar class in css [duplicate]

This question already has answers here:
CSS selector for first element with class
(23 answers)
Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?
(8 answers)
Closed 3 years ago.
How to select the first child from all similar class in css
HTML
<div class="parent">
<div class="no-a-child"><span>1</span></div>
<div class="child"><span>2</span></div>
<div class="child"><span>3</span></div>
<div class="child"><span>4</span></div>
</div>
CSS
.parent .child:nth-child(1) span {
color:red;
}
.parent .child:nth-child(1) span {
color: red;
}
<div class="parent">
<div class="no-a-child"><span>1</span></div>
<div class="child"><span>2</span></div>
<div class="child"><span>3</span></div>
<div class="child"><span>4</span></div>
</div>
Here I want a red color:
<div class="child"><span>2</span></div>
One approach to achieve this effect requires two stages:
Declare a style for a given class
Undo that style for all elements of that class which follow the first element of that class
Working Example:
/* Declare a style for a given class */
.child {
color: rgb(255, 0, 0);
}
/* Undo that style for all elements of that class which follow the first */
.child ~ .child {
color: inherit;
}
<div class="parent">
<div class="no-a-child"><span>1</span></div>
<div class="child"><span>2</span></div>
<div class="child"><span>3</span></div>
<div class="child"><span>4</span></div>
</div>
I'm not sure if I understood well, but i guess you want just the first class child to be in red, and not the class no-a-child...if so, just use this
.parent :nth-of-type(2) span{
color: red;
}
<div class="parent">
<div class="no-a-child"><span>1</span></div>
<div class="child"><span>2</span></div>
<div class="child"><span>3</span></div>
<div class="child"><span>4</span></div>
</div>
If you want the first div that has class child you could try something like:
div.child:nth-of-type(1).
Use a adjacent sibling combinator https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator to target the next element after the no-a-child class element as that seems to be what you really desire.
I added another example (cyan color) that illustrates that option
.parent > .no-a-child+.child>span {
color: #FF0000;
}
.parent-second> div:first-of-type + div>span {
color: #00FFFF;
}
<div class="parent">
<div class="no-a-child"><span>1</span></div>
<div class="child"><span>2</span></div>
<div class="child"><span>3</span></div>
<div class="child"><span>4</span></div>
</div>
<div class="parent-second">
<div class="no-a-child"><span>1</span></div>
<div class="child"><span>2</span></div>
<div class="child"><span>3</span></div>
<div class="child"><span>4</span></div>
</div>

How to hide element if it contain specific div with a class [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 3 years ago.
I have this html structure:
<div>
<div class="parent">
<div>
<div class="label">
<div class="xyz"></div>
</div>
</div>
</div>
<div class="parent">
<div>
<div class="label">
</div>
</div>
</div>
</div>
I want to hide the parent if it is contains div with class="xyz"
I tried that:
.parent div.xyz {
display: none;
}
but seems like my selector does not work.
You can Hide the parent element with the following jquery
jQuery('.parent').each(function(){
if (jQuery(this).find('div.xyz').length != 0) {
jQuery(this).closest('.parent').hide();
}
});
Hope it will helpful.
In css, there's no parent element selector. In your case, you can use jquery to achieve the solution.
$('.parent:has(.xyz)').addClass('hide');
.hide {
visibility: hidden;
}
div.xyz .parent {
display: none;
}
<div>
<div class="xyz">
<div class="parent">
<div>
<div class="label">
<div class="xyz"></div>
</div>
</div>
</div>
</div>
<div class="parent">
<div>
<div class="label">
</div>
</div>
</div>
</div>
You can not select an ancestor, just descendants, an example of how you could do is like this:
<div class="xyz">
<div class="parent">
<div class="label">
<div class="xyz"></div>
</div>
</div>
<div class="parent">
<div class="label">
</div>
</div>
</div>
div.xyz .parent {
display: none;
}
in this case you will hide both .parent class
See more about descending selector
to select a parent or element inside another wapper you will need to use javascript

How to hide all elements except a grandchild with a specific class with CSS? [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 5 years ago.
I'm not sure if this can be done entirely with CSS (imperative), but it's halfway working at the moment. I have this current HTML setup:
<div class="content">
<div>
<div class="image"></div>
</div>
<div class="text"></div>
<div class="text"></div>
<div>
<div class="button"></div>
</div>
<div>
<div class="image"></div>
</div>
</div>
My current CSS hides all of the child elements of ".content" that don't have a class.
.content > *:not([class]):first-child {
display:block;
}
Of the remaining 3 visible class child elements of ".content", I need to hide them all except the first child element that has the grandchild element with the ".image" class. This is the CSS I have, but it's not working:
.content > *:not([class]):not(.image):first-child {
display:block;
}
It's imposible on CSS. You tryed not show parent element by attribute of child. CSS so does not work. But you can small js for this:
document.querySelector(".image").parentNode.style.display = "block";
.content>div {
display: none;
}
<div class="content">
<div>
<div class="image">1</div>
</div>
<div class="text">2</div>
<div class="text">3</div>
<div>
<div class="button">4</div>
</div>
<div>
<div class="image">5</div>
</div>
</div>
Andrey’s answer is good, however if you don’t want to use JS I think you will need to have a class on the intermediary children as well since the entire tree to the element you want must be visible. That is, if any parent of the element you want to show is hidden then the children will be too. Something like this might do:
<div class="content">
<div>
<div class="image"></div>
</div>
<div class="text"></div>
<div class="text"></div>
<div>
<div class="button"></div>
</div>
<div class="visible">
<div class="image"></div>
</div>
</div>
.content > * {
display: none;
}
.content > .visible {
display: block;
}

Selecting the last general sibling

I have a bunch of divs with class B following a div with class A:
<div class="B"></div>
<div class="A"></div>
<div class="A"></div>
<div class="A"></div>
<div class="B"></div>
<div class="A"></div>
I'm trying to apply a CSS rule to the last A after each B. I tried
.B ~ .A:last-of-type
but that didn't work. Is this possible?
You can't currently do this with css. The :last-of-type pseudo class works with element types such as span div etc - not with classes
Being that:
1) There is no currently no previous sibling selector
2) You can't change the structure of your markup and
3) You don't know in advance how many .A divs you have after each .B div ...
... you're out luck, CSS currently can't do this.
You can use :last-of-type selector in this case.
.A:last-of-type {
/*apply styling */
}
In your case I recommend you to group them in divs. http://jsfiddle.net/GtLWJ/
This works.
<div class="sec1">
<div class="head">Set</div>
<div class="sub">Item 1</div>
<div class="sub">Item 2</div>
<div class="sub">Item 3</div>
</div>
<div class="sec1">
<div class="head">Set</div>
<div class="sub">Item 1</div>
</div>
CSS:
.head {
font-weight: 800;
}
.sec1 .sub:last-of-type {
border-bottom: 1px solid #000;
}