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;
}
Related
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 {
}
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>
I'm newer to CSS, and after searching google I'm still having a hard time getting the :last-child selector to work.
HTML:
<div class="A">
<div class="B">
<div class="C"></div>
</div>
<div class="B">
<div class="C"></div>
</div>
<div class="B">
<div class="C"></div>
</div>
</div>
CSS:
.A > .B > .C {
margin-bottom: 8px;
}
.A > .B > .C:last-child {
margin-bottom: 0px;
}
My goal is I want the first two .C divs to have a bottom margin, but the last .C div should not have a margin. However, :last-class isn't being registered - all 3 .C divs have a bottom-margin of 8px.
I've also tried variations of :last-child on different classes, and the child selector vs no child selector
I'm not sure what I'm doing wrong / what I need to do to set the last .C class margin-bottom to 0px.
Edit: I'm actually able to get the above working in codepen.io... below is my actual code, which I can't get to work correctly.
<div class="A" ng-repeat="component in components">
<div title="{{component.name}}" ng-show="rightStatusBarFlags[component.id] === true" class="B" ng-class="(component.notifyFlag === true)? 'right-status-alert' : ''" ng-click="toggleComponentVisibility(component)">
<span class="C">
<img class="icon-component_{{component.id}}" ng-show="!component.notifyFlag"></img>
</span>
</div>
</div>
This will repeat for the number of components, a max of 4 components.
Edit 2:
This was solved by trying different things that people below suggested. I changed the span to a div, then realized that angular repeats the Class A div. Below is my final html and css:
HTML:
<div class="A">
<div class="B" ng-repeat="component in components">
<div title="{{component.name}}" ng-show="rightStatusBarFlags[component.id] === true" class="C" ng-class="(component.notifyFlag === true)? 'right-status-alert' : ''" ng-click="toggleComponentVisibility(component)">
<div class="btn btn-status-sections">
<img class="icon-component_{{component.id}}" ng-show="!component.notifyFlag"></img>
</div>
</div>
</div>
</div>
CSS:
.A .B .C {
margin-bottom: 8px;
}
.A .B:last-child .C {
margin-bottom: 0px;
}
Your C divs are all just single children of the B divs. So every single one will have a margin of 0.
You want to add a margin to the C that's inside the last B. So
.A > .B:last-child > .C {
margin-bottom: 0px;
}
Alternatively, you can rearrange your html and keep the css as you defined it as follows:
<div class="A">
<div class="B">
<div class="C"></div>
<div class="C"></div>
<div class="C"></div>
</div>
</div>
I see you want to apply css on you div having class "C" under "A". for this case you don't need to bother Class "B".
For example if you have parent class. you are able to access nth depth child. like I tried.
<div class="A">
<div class="B">
<div class="C">2</div>
</div>
<div class="B">
<div class="C">2</div>
</div>
<div class="B">
<div class="C">3</div>
</div>
Here is the CSS
.A > div > div {
background-color:yellow;
}
.A > div:last-child > div:last-child {
background-color:red;
}
https://jsfiddle.net/kyaLh19n/
if you have look at this image what I did. I did apply CSS on all childs inside class A and then override the last one.
Hope I am able to help you .
With your code you can use .A > .B:last-child > .C and set margin-bottom:0.
See the below Snippet:
.A {
border:1px solid red;
}
.B > .C {
margin-bottom: 8px;
background-color:lightpink;
color:white;
}
.A > .B:last-child > .C {
margin-bottom: 0px;
}
<div class="A">
<div class="B">
<div class="C">1</div>
</div>
<div class="B">
<div class="C">2</div>
</div>
<div class="B">
<div class="C">3</div>
</div>
</div>
Thank you everyone!
I needed to do a combination of what people suggested.
First was changing the span to a div.
Second was adding a wrapper div above the angular repeated div, which became Class A.
Third was using .A .B:last-child .C in my css.
I also learned that posting the actual code instead of a basic example helps more.
I have a nested HTML element structure where I need to apply css styles for some condition.
Apply styles to class starts with "a-" in the .a-comp element
Ignore styles to .a-col class and its child elements which have "a-*" class
The code below works for the above scenario. But it doesn't work for other child elements which have "a-*" class.
How can I achieve that?
.a-comp :not(.a-col) [class^="a-"],
.a-comp :not(.a-col) [class*="a-"] {
color: red;
}
<div class="a-comp">
<div class="a-one">
<div class="a-col">
<div class="a-text">
Text1
</div>
</div>
</div>
<div class="a-row">
<div class="a-text">
Text 2
</div>
</div>
</div>
View on JSFiddle
Your selector matches .a-* elements that have an .a-comp ancestor, with some intermediate element that is not .a-col. However, both of your examples match that selector.
The first one has .a-one as a descendant of .a-comp and ancestor of a-text. The second one has .a-row as a descendant of .a-comp and ancestor of a-text.
One solution might be to set the appropriate children of .a-col elements not to be red.
.a-comp [class^="a-"] {
color: red;
}
.a-comp .a-col [class^="a-"] {
color: black;
}
<div class="a-comp">
<div class="a-row">
<div class="a-two">
<div class="a-text">
In a ROW
</div>
</div>
</div>
<div class="a-one">
<div class="a-col">
<div class="a-text">
In a COL
</div>
</div>
</div>
<div class="a-row">
<div class="a-text">
In a ROW
</div>
</div>
<div class="a-one">
<div class="a-something">
<div class="a-text">
In something else
</div>
</div>
</div>
</div>
I want to style(say change width or color) the div inside first child, using the classes .child and .grandchild, which are also inside other child divs, but keeping the other child div intact. Here i only able to select the first child only.
DIV.parent> DIV:first-child.grandchild {background:red;width:70px;}
.grandchild{font-size:10px;width:50px;height:50px;background:yellow;}
.child{padding:5px;}
<div class='parent'>
<div class='child'>
<div class='grandchild'>
FIRST
</div>
</div>
<div class='child'>
<div class='grandchild'>
SECOND
</div>
</div>
<div class='child'>
<div class='grandchild'>
THIRD
</div>
</div>
<div class='child'>
<div class='grandchild'>
FOURTH
</div>
</div>
</div>
you mean something like this ? jsfiddle
your question it's a bit unclear
code
.child:first-child .grandchild {border:solid 10px #000;}
.child:first-child > .grandchild{
width:200px;
background:#f22;
}
You can try with nth-child(n) selector. for example:
DIV.parent> DIV:nth-child(1) {border:solid 10px #000;}
DIV.parent> DIV:nth-child(2) {border:solid 10px #000;}