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.
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 {
}
I need to select specific DIV's without selecting children/nested DIV's within them, using only CSS.
There are two DIV's I want to select individually. These are the DIV's containing the text:
image-content (1) and
image-content (2)
.home_image_widget-2 > div:first-child div:first-child {
background-color: gold;
}
<div class="home_image_widget home_image_widget-2">PARENT DIV (wrapper)
<div class="image-content">image-content (1)
<div class="imageWidget_img"> imageWidget_img (1)</div>
<div class="home_image_widget_caption"> home_image_widget_caption (1)</div>
</div>
<div class="image-content">image-content (2)
<div class="imageWidget_img"> imageWidget_img (2)</div>
<div class="home_image_widget_caption"> home_image_widget_caption (2)</div>
</div>
</div>
With CSS, when I try .home_image_widget-2 > div:first-child div:first-child {}, it only selects the DIV for imageWidget_img (1), but I need its parent instead.
I have spent a few hours on this with no success, so for all you CSS Ninja's, slice and dice me up a solution. Thanks!
not prefect.
.home_image_widget-2 > div {
background-color: gold;
}
.home_image_widget-2 > div div{
background-color: white;
}
<div class="home_image_widget home_image_widget-2">PARENT DIV (wrapper)
<div class="image-content">image-content (1)
<div class="imageWidget_img"> imageWidget_img (1)</div>
<div class="home_image_widget_caption"> home_image_widget_caption (1)</div>
</div>
<div class="image-content">image-content (2)
<div class="imageWidget_img"> imageWidget_img (2)</div>
<div class="home_image_widget_caption"> home_image_widget_caption (2)</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;}
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;
}
I'm trying to apply some CSS to the last .topic-wrapper div (which is inside the entry-content div):
<div class="entry-content">
<div class="bbp-pagination">
<h2 class="dark-title">Top Topics</h2>
<div class="topic-wrapper">
<h4></h4>
<div class="topic-wrapper">
<h4></h4>
<div class="topic-wrapper"> <!-- I'm trying to get this one -->
<h4></h4>
<div class="mainbar">
<div class="bbp-pagination">
with this code: .entry-content div:last-child
But it seems like the CSS is being applied to some divs inside .topic-wrapper.
Like:
<div class="topic-wrapper">
<div class="topic-left">
<h2>
<span>
<span class="bbp-topic-started-in">
<div class="bbp-topic-tags"> <!-- this one -->
<p>
Any suggestions to solve this?
</div>
Unless I'm misreading your nesting you can't. :last-child finds elements that are the last child of their parents, but the last child of entry-content is bbp-pagination.
Additionally .entry-content div:last-child finds ALL divs that are descendants of .entry-content - NOT just the direct child.
You probably want .entry-content>div:last-child which looks only at immediate descendants. (i.e. children, but not grandchildren)
This should do it, using ">" gets you just the direct descendents:
.entry-content > div:last-child
Here is an example:
<div class="container">
<div class="outer">
<div class="inner"></div>
</div>
<div class="outer">
</div>
<div class="outer">
</div>
<div class="outer">
</div>
</div>
.container
{
width:300px;
background-color:yellow;
}
.outer
{
width:100px;
height:100px;
background-color:red;
margin:20px;
}
.inner
{
width:50px;
height:50px;
background-color:pink;
margin:5px;
}
.container > div:last-child
{
background-color:blue;
}
/* uncomment this statement to see the alternative broken version*/
/*
.container div:last-child
{
background-color:green;
}
*/
You can view the example on jsfiddle