CSS targetting last child container of class - html

Not sure this is possible with CSS only. Any help is appreciated.
My HTML looks like this:
<div class="row invoice with-tax">
<div class="mobile-body-header mobile"><strong>Items</strong></div>
<div class="invoice-item">
<div class="item-filler"></div>
<div class="quantity">2</div>
<div class="amount-mobile total">$13,336.60</div>
<div class="item-number">ABC123</div>
<div class="title"><span class="quantity-mobile">2 x </span>Dance classes and tutoring for upcoming Michael Jacksons</div>
<div class="amount-item-ex">$153.00</div>
<div class="amount-subtotal">$306.00</div>
<div class="amount-tax">$30.60</div>
<div class="amount-total total">$13,336.60</div>
<div class="item-filler"></div>
</div>
<div class="invoice-item">
<div class="item-filler"></div>
<div class="quantity">2</div>
<div class="amount-mobile total">$13,336.60</div>
<div class="item-number">ABC123</div>
<div class="title"><span class="quantity-mobile">2 x </span>Dance classes and tutoring for upcoming Michael Jacksons</div>
<div class="amount-item-ex">$153.00</div>
<div class="amount-subtotal">$306.00</div>
<div class="amount-tax">$30.60</div>
<div class="amount-total total">$13,336.60</div>
<div class="item-filler"></div>
</div>
....
</div>
I am trying to remove the border-bottom of the final div with class title in the last div with class invoice-item. These are essentially rows of div elements that can vary in numbers (as it is and invoice I am working on).
I have tried unsuccessfully the examples below. Although I can see it working in the Fiddle from #Steve below it is not removing the last border when placed inside the rest of the CSS. And I can confirm the border is set just above per the examples below.
div.invoice-item > div.title {
border-bottom: 2px dotted red;
}
div.invoice-item:last-child > div.title {
border: none !important;
}
also
div.invoice-item > div.title {
border-bottom: 2px dotted red;
}
div.invoice > div.invoice-item:last-child > div.title {
border: none !important;
}
also
div {
border-bottom: 1px solid red;
}
div.invoice-item:last-of-type > div.title {
border: none;
}

Since it's an e-mail template, there are some clients that doesn't support :last-child. Instead of using :last-child, try to add a new Class to the div that you want to select.

Try div.invoice-item:last-of-type > div.title { border: none; }
UPDATE
Since there's an unknown CSS evil, here's a trick that might work:
div.invoice-item:last-of-type > div.title.title { border: none !important; }
div {
border-bottom: 1px solid red;
}
/* div.invoice-item:last-of-type > div.title {
border: none;
} */
div.invoice-item:last-of-type > div.title.title {
border: none !important;
}
<div class="row invoice with-tax">
<div class="mobile-body-header mobile"><strong>Items</strong>
</div>
<div class="invoice-item">
<div class="item-filler"></div>
<div class="quantity">2</div>
<div class="amount-mobile total">$13,336.60</div>
<div class="item-number">ABC123</div>
<div class="title"><span class="quantity-mobile">2 x </span>Dance classes and tutoring for upcoming Michael Jacksons</div>
<div class="amount-item-ex">$153.00</div>
<div class="amount-subtotal">$306.00</div>
<div class="amount-tax">$30.60</div>
<div class="amount-total total">$13,336.60</div>
<div class="item-filler"></div>
</div>
<div class="invoice-item">
<div class="item-filler"></div>
<div class="quantity">2</div>
<div class="amount-mobile total">$13,336.60</div>
<div class="item-number">ABC123</div>
<div class="title"><span class="quantity-mobile">2 x </span>Dance classes and tutoring for upcoming Michael Jacksons</div>
<div class="amount-item-ex">$153.00</div>
<div class="amount-subtotal">$306.00</div>
<div class="amount-tax">$30.60</div>
<div class="amount-total total">$13,336.60</div>
<div class="item-filler"></div>
</div>
....
</div>

Related

Not adding a css property to a last child inside a nested div- CSS

I want to add certain property to a specific <div>, here's my structure:
<div class="container">
<div class="parent">
<div class="child1">
<div class="child2">
</div>
<div class="parent">
<div class="child1">
<div class="child2">
</div>
<div class="parent">
<div class="child1">
</div>
</div>
Now I'm trying to apply css border property on class="child1" except for the last div that has only class="child1". I tried:
.container {
& > div.child1:not(:last-child) {
border-right: none;
}
}
But this doesn't work. Any ideas around?
.container .parent:not(:last-child) .child1{
border-right: none;
}
You should select the last parent and in your case you're using ">" which selects a direct child of the container
Your selector is wrong. You do not need the first > as the child divs are not DIRECT children of the container
.container {
width: 80%;
}
.container .child1:not(:last-child) {
border-right: 3px solid red;
}
.parent {
margin-bottom: 1em;
<div class="container">
<div class="parent">
<div class="child1">Child 1</div>
<div class="child2">Child 2</div>
</div>
<div class="parent">
<div class="child1">Child 1</div>
<div class="child2">Child 2</div>
</div>
<div class="parent">
<div class="child1">Child 1</div>
</div>
</div>
Answer:
You're trying to tell CSS to:
find parents which have multiple children
then find specific child in each of these parent(s)
As of 2020, this is not supported by pure CSS, and the answer is ironically from 2009. Read this, this, and this.
Other Workarounds:
A) By jQuery (or similar solution by JS)
// Find all parents
$('.parent').each(function() {
// Find all children of this parent
var $children = $(this).find('div[class^="child"]');
if($children.length > 1) {// if has 2 or more children
$children.css('borderRight', 'none');
// -- or --
$children.addClass('my_child_no_border_class');
} else {// Has 1 or 0 children
$children.css('borderRight', '1px solid red');
// -- or --
$children.addClass('my_child_border_class');
}
});
If you prefer adding classes, make sure to create CSS classes .my_child_border_class and .my_child_no_border_class
B) By HTML & CSS
Add special classes for children with border and no border:
<!-- HTML -->
<div class="container">
<div class="parent">
<div class="child1 noborder">
<div class="child2">
</div>
<div class="parent">
<div class="child1 noborder">
<div class="child2">
</div>
<div class="parent">
<div class="child1 withborder">
</div>
</div>
<!-- CSS -->
.noborder {
border-right: none;
}
.withborder {
border-right: 1px solid red;
}
C) By CSS
/* All child1 get this css */
.parent > child1 {
border-right: none;
}
/* then we override last parent's child1 with different css */
.parent:last-child > child1 {
border-right: 1px solid red;
}
Conclusion:
There might be other workarounds, but not pure CSS solution.
You can target the last parent and child1 like this.
.parent {
padding: 1rem;
background-color: deepskyblue;
}
.child1 {
height: 3rem;
background-color: aqua;
}
.parent:last-child > .child1 {
background-color: lime;
}
<div class="container">
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
<div class="parent">
<div class="child1"></div>
</div>
</div>
you can add a nother class to the last div or an id so you can target it as follow:
<div class="container">
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
<div class="parent">
<div class="child1 last-child" id="child"></div>
</div>
</div>
and css property as follow:
.last-child {
border-right: none;
}

Target only the first direct child not working despite :nth-child(1)

So basically I have this markup, which is a very simplified output of a component I'm working on.
On the CSS, I just want to style (apply gray color) the first direct child, but it's also applying the style to the indirect nested children despite adding nth-child(1). Why is this? And how can I fix this?
.row {
margin: 5px 0;
}
.row.row-selected{ border-left: 1px solid red; }
.row.row-selected .row-data:nth-child(1) {
background-color: #d5d5d5;
}
<div class="row row-selected">
<div class="row-data">Foo1</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo1Child1</div>
</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo1Child2</div>
</div>
</div>
<div class="row">
<div class="row-data">Foo2</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo2Child1</div>
</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo2Child2</div>
</div>
</div>
I think you are looking for the parent/child selector?
.row.row-selected > .row-data {
background-color: #d5d5d5;
}
This makes sure that only direct child of .row.row-selected with a class of .row-data is selected.
It is applying to all nested elements because you are using a descendant combinator (a space character) between selectors:
.row.row-selected .row-data:nth-child(1) { }
This combinator targets all specified descendants (not just children) of the first selector.
Plus, your targeted element (.row-data) is, in fact, the first child (:nth-child(1)) of a parent element in all cases.
Instead, use the child combinator (>), which targets only the children of an element.
.row.row-selected > div:nth-child(1) {
background-color: #d5d5d5;
}
.row {
margin: 5px 0;
}
.row.row-selected {
border-left: 1px solid red;
}
<div class="row row-selected">
<div class="row-data">Foo1</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo1Child1</div>
</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo1Child2</div>
</div>
</div>
<div class="row">
<div class="row-data">Foo2</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo2Child1</div>
</div>
<div class="row" style="margin-left: 20px">
<div class="row-data">Foo2Child2</div>
</div>
</div>

CSS target specific class

I am attempting to target a specific class which is the first class name inside a div. The attempt I have made looks like the following:
.container-fluid:first-child div.row {
padding-top: 200px;
}
The HTML for the CSS.
<div class="container-fluid">
<div class="row justify-content-md-center"> <<TRYING TO TARGET
<div class="col-3 text-center">
<div class="row">
<img src="/assets/images/fssLogo.png"/>
</div>
</div>
</div>
</div>
As you can see i want to target only the first row inside the container-fluid tag but the padding is also getting applied to the row inside the col-3 class.
Could somone point me in the right direction.
It should be
.container-fluid >.row:first-child {
padding-top: 200px;
}
.container-fluid > .row:first-child {
background-color: red;
padding-top: 200px;
}
.innerRow {
background-color: pink;
}
<div class="container-fluid">
<div class="row justify-content-md-center">
<div class="col-3 text-center">
<div class="row innerRow">
<img src="http://via.placeholder.com/350x150" />
</div>
</div>
</div>
</div>
Or
.container-fluid >.row {
padding-top: 200px;
}
.container-fluid > .row {
background-color: red;
padding-top: 200px;
}
.innerRow {
background-color: pink;
}
<div class="container-fluid">
<div class="row justify-content-md-center">
<div class="col-3 text-center">
<div class="row innerRow">
<img src="http://via.placeholder.com/350x150" />
</div>
</div>
</div>
</div>
But the 2nd snippet is more preferable, it is because you are targeting .row that is the direct child of container-fluid, unless you have another row that is also a direct child of container-fluid, you can use the 1st snippet to only target the first row child.
> is used to target the direct child of a parent, regardless if there is a class that has the same name lower in the hierarchy
.parent > .someClass {
color: blue;
}
<div class="parent">
<p class="someClass">TARGETED</p>
<div>
<p class="someClass">NOT TARGETED</p>
</div>
</div>
Remove > and both text will be blue

How to place div in second row directly under div in first row with same column size but blocked by the height of another column in first row

Sorry if the title is confusing. I have included a screenshot of my issue to make it more clear.
Essentially, I want the green-bordered div to be directly underneath the red-bordered div without the blue-bordered div interfering.
Here is my HTML code (I am using Bootstrap v3.3.7)
<div class="container appParent">
<div class="row">
<div class="col-md-6" id="song-view">
<app-selected-song-view></app-selected-song-view>
</div>
<div class="col-md-6" id="songs-added">
<app-songs-added></app-songs-added>
</div>
</div>
<div class="row">
<div class="col-md-6" id="playlist-form">
<app-playlist-form></app-playlist-form>
</div>
</div>
</div>
My CSS is only putting the borders around the div. Everything else is bootstrap.
#song-view{
border: 2px solid red;
}
#songs-added{
border: 2px solid blue;
}
#playlist-form{
border: 2px solid green;
}
In this case, use pull-right, and keep all of the col- in a single row.
<div class="container appParent">
<div class="row">
<div class="col-md-6" id="song-view">
<app-selected-song-view></app-selected-song-view>
</div>
<div class="col-md-6 pull-right col-xs-12" id="songs-added">
<app-songs-added></app-songs-added>
</div>
<div class="col-md-6" id="playlist-form">
<app-playlist-form></app-playlist-form>
</div>
</div>
</div>
http://www.codeply.com/go/189ofQG5Sj
You can re-arrange your HTML.
#song-view {
border: 2px solid red;
height: 100px;
margin-bottom: 30px;
}
#songs-added {
border: 2px solid blue;
height: 150px;
}
#playlist-form {
border: 2px solid green;
height: 100px;
margin-bottom: 30px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container appParent">
<div class="row">
<div class="col-md-6">
<div id="song-view">
<app-selected-song-view></app-selected-song-view>
</div>
<div id="playlist-form">
<app-playlist-form></app-playlist-form>
</div>
</div>
<div class="col-md-6">
<div id="songs-added">
<app-songs-added></app-songs-added>
</div>
</div>
</div>
</div>
You can simple use min-heights and surround the containers and rows with a div for extra styling. You can check this example I quickly made: JsFiddle
<div class="song-list">
<div class="row">
<div class="container">
<div class="col-md-6" id="song-view">
Song View
</div>
<div class="col-md-6" id="songs-added">
Songs Added
</div>
</div>
</div>
</div>
<div class="playlist">
<div class="row">
<div class="container">
<div class="col-md-6" id="playlist-form">
Playlist Form
</div>
</div>
</div>
</div>
CSS:
.song-list #song-view {
border:1px solid blue;
min-height:300px;
}
.song-list #songs-added {
border:1px solid red;
min-height:500px;
}
.playlist #playlist-form {
border:1px solid green;
}

apply different style to odd divs

I'm working with owl-caroussel plugin, and I need to apply a border-right style to odd divs, when I apply this selector it just adds the border to all divs of class "info", it doesn't matter if it's even or odd:
section#presentation section#last div.info:nth-of-type(odd){
border-right: 1px solid #ede1d9;
}
I must be doing something wrong, this is the code in the razor view:
<section id="last">
<div class="row">
<div class="col-lg-9 col-md-8 col-sm-12 col-xs-12 newswidth">
<div id="news-wrap">
<div id="owl-example" class="owl-carousel owl-theme">
<!--item-->
#{ var counter = 0;}
#foreach (var itemBlogList in Model.BlogList)
{
if (counter == 4)
{
break;
}
<div class="info">
#itemBlogList.Title
<div class="credentials">
<div class="author">PETER SCHULTZ</div>
<div class="date">#itemBlogList.PublishDate</div>
</div>
<div class="cut">
#Html.Raw(#itemBlogList.Text)
</div>
</div>
counter++;
}
</div>
</div>
</div>
</div>
</section>
How can I add that border to only odd divs of class "info"?
Take a look at this: https://www.w3.org/Style/Examples/007/evenodd.en.html
.test:nth-child(odd) {
background: #ddd;
}
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
<div class="test">test</div>
Try to use this selector instead:
section#presentation section#last .owl-carousel > div:nth-child(odd){
border-right: 1px solid #ede1d9;
}
Yours will count all DIVs, (nth-of-type, i.e. DIV), not just the ones with class .info
To get the odd ones use this pseudo class
.info:nth-child(2n-1) {
your styles
}