Given the following snippet, I would expect the div containing "Award 1" to have a red background colour. Can someone explain to me why it does not? The first item should have a border top.
.item-wrap:first-child .item {
border-top: 1px solid black;
}
.item {
width: 100%;
border-bottom: 1px solid black;
}
<div class="row awards">
<div class="col-xs-12">
<h2 class="no-border">Awards</h2>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 1</div>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 2</div>
</div>
</div>
The :first-child CSS pseudo-class represents any element that is the first child element of its parent.
:first-child (https://developer.mozilla.org/en-US/docs/Web/CSS/%3Afirst-child)
In your example you are attempting to select an element with the class .item which resides in a parent with the class .item-wrap which itself is the first child of its parent (in this case .awards). As .item-wrap is not the first child of .awards it does not match.
Given your markup the following rule should fit your needs:
.awards :first-child + .item-wrap .item:first-child {
border-top: 1px solid black;
}
.item {
width: 100%;
border-bottom: 1px solid black;
}
<div class="row awards">
<div class="col-xs-12">
<h2 class="no-border">Awards</h2>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 1</div>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 2</div>
</div>
</div>
This will select an element with the class .item which is the first child of its parent .item-wrap which is immediately preceded by the first child that belongs to the element with the class .awards.
you need to wrap child elements with a parent div like this,
HTML
<div class="row awards">
<div class="col-xs-12">
<h2 class="no-border">Awards</h2>
</div>
<div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 1</div>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 2</div>
</div>
</div>
</div>
CSS
.item-wrap:first-child .item {
background-color:red;
}
http://codepen.io/anon/pen/QjJBQL
.item-wrap:nth-child(2){
background: red;
}
<div class="row awards">
<div class="col-xs-12">
<h2 class="no-border">Awards</h2>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 1</div>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 2</div>
</div>
</div>
.item:first-child{
color: red;
}
<div class="row awards">
<div class="col-xs-12">
<h2 class="no-border">Awards</h2>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 1</div>
<div class="item">Award 1a</div>
</div>
<div class="col-xs-12 item-wrap">
<div class="item">Award 2</div>
<div class="item">Award 2a</div>
</div>
</div>
Place the selector on the child, not the parent (http://codepen.io/anon/pen/vNQamR):
.item:first-child{
color: red;
}
Related
I have this:
<div class="row">
<div>something</div>
<div>something</div>
</div>
<div class="row">
<div>something</div>
<div>something</div>
</div>
I would like that for even class="row" all childs div have a background (blue for example) and for odd class="row" all childs div have a background (red for example)
so the result would be:
<div class="row">
<div style="background:blue">something</div>
<div style="background:blue">something</div>
</div>
<div class="row">
<div style="background:red">something</div>
<div style="background:red">something</div>
</div>
You can do it with CSS
.container > .row:nth-child(even) > div{
background: red;
}
.container > .row:nth-child(odd) > div{
background: blue;
}
<div class="container">
<div class="row">
<div>something</div>
<div>something</div>
</div>
<div class="row">
<div>something</div>
<div>something</div>
</div>
<div class="row">
<div>something</div>
<div>something</div>
</div>
<div class="row">
<div>something</div>
<div>something</div>
</div>
</div>
Put them inside of a container like so:
<div class="container">
<div class="row">
<div>something</div>
<div>something</div>
</div>
<div class="row">
<div>something</div>
<div>something</div>
</div>
</div>
.container row:nth-child(even) div
{
background: blue
}
.container row:nth-child(odd) div
{
background: red
}
Hi guys I'm using bootstrap 3 and trying to crate this effect here :
However i cant seem to create this using bootstrap at all Mine keep lying under each other, Any help on recreating this format would be great
HTML:
<div id="night">
<div class="container">
<div class="row">
<div class="col-xs-3 col-md-6 first">
<p>a</p>
</div>
</div>
<div class="row">
<div class="col-xs-3 col-md-3 first">
<p>a</p>
</div>
<div class="col-xs-3 col-md-3 first">
<p>a</p>
</div>
</div>
<div class="row">
<div class="col-xs-3 col-md-3 first">
<p>a</p>
</div>
<div class="col-xs-3 col-md-3 first">
<p>a</p>
</div>
</div>
</div>
</div>
css:
.first {
border-style: solid;
border-color: red;
}
Thanks
Try this please
<div id="night">
<div class="container">
<div class="row">
<div class="col-md-4 first">
<div class="second">a</div>
</div>
<div class="col-md-4 first">
<div class="third">a</div>
<div class="third">a</div>
</div>
<div class="col-md-4 first">
<div class="third">a</div>
<div class="third">a</div>
</div>
</div>
</div>
</div>
CSS:
.first {
border-style: solid;
border-color: red;
}
.second {
border-style: solid;
border-color: blue;
display: block;
height: 400px;
}
.third {
border-style: solid;
border-color: blue;
display: block;
height: 200px;
}
This works in full page, maybe you can work with this aproximation:
<div class="container">
<div class="row">
<div class="col-sm-4" style="background-color:yellow;">
1
</div>
<div class="col-sm-8" style="background-color:pink;">
<div class="row">
<div class="col-sm-6" style="background-color:blue;">
2
</div>
<div class="col-sm-6" style="background-color:red;">
3
</div>
<div class="col-sm-6" style="background-color:green;">
4
</div>
<div class="col-sm-6" style="background-color:black;">
5
</div>
</div>
</div>
</div>
</div>
As you can see, there are .row elements that are parent to .cell elements.
I have a selected element inside a .row element, I want to target:
An element that is a child of the parent element that follows the parent containing .selected
Is this possible in CSS only?
Assume I want to select the second .cell of the parent next to the parent containing .selected
How do I turn the background color of the div containing the number 13 green?
.row .cell.selected {
background-color: red
}
.row .cell.selected+.cell+.cell {
background-color: red;
}
.row .cell.selected+.cell+.cell+.cell {
background-color: red;
}
.row .cell.selected+.cell+.cell+.cell+.cell+.cell {
background-color: red;
}
#month-view .row .cell.selected+.cell {
background-color: yellow;
}
.row {
padding: 50px;
}
<div id="month-view">
<div class="row">
<div class="cell">
<div class="day"> <span>5</span></div>
</div>
<div class="cell selected">
<div class="day"><span>6</span></div>
</div>
<div class="cell">
<div class="day"><span>7</span></div>
</div>
<div class="cell">
<div class="day"><span>8</span></div>
</div>
<div class="cell">
<div class="day"><span>9</span></div>
</div>
<div class="cell">
<div class="day"><span>10</span></div>
</div>
<div class="cell">
<div class="day"><span>11</span></div>
</div>
</div>
<div class="row">
<div class="cell ">
<div class="day"><span>12</span></div>
</div>
<div class="cell">
<div class="day"><span>13</span></div>
</div>
<div class="cell">
<div class="day"><span>14</span></div>
</div>
<div class="cell">
<div class="day"><span>15</span></div>
</div>
<div class="cell">
<div class="day"><span>16</span></div>
</div>
<div class="cell">
<div class="day"><span>17</span></div>
</div>
<div class="cell">
<div class="day"><span>18</span></div>
</div>
</div>
/div>
If I understood your question you want that the cell that gets a ".selected" class in the first row gets a styling in a cell in the same position in the second row.
That is not possible with CSS only, just using JS. CSS can't give you the index position of your ".selected" cell.
If you want a solution that is pure HTML and CSS I recommend you to add a second class like ".selected-column" to the next rows and style after this.
Not possible in CSS as you can't go backwards/up the DOM in CSS. But in case you can use JS or jQuery, here's a way. It's pretty easy and intuitive with jQuery using $.parent(), $.next(), and :nth-child.
$('.selected').parent('.row').next('.row').find('.cell:nth-child(2)').addClass('green');
.row .cell.selected {
background-color: red
}
.row {
padding: 50px;
}
.green {
background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="month-view">
<div class="row">
<div class="cell">
<div class="day"> <span>5</span></div>
</div>
<div class="cell selected">
<div class="day"><span>6</span></div>
</div>
<div class="cell">
<div class="day"><span>7</span></div>
</div>
<div class="cell">
<div class="day"><span>8</span></div>
</div>
<div class="cell">
<div class="day"><span>9</span></div>
</div>
<div class="cell">
<div class="day"><span>10</span></div>
</div>
<div class="cell">
<div class="day"><span>11</span></div>
</div>
</div>
<div class="row">
<div class="cell ">
<div class="day"><span>12</span></div>
</div>
<div class="cell">
<div class="day"><span>13</span></div>
</div>
<div class="cell">
<div class="day"><span>14</span></div>
</div>
<div class="cell">
<div class="day"><span>15</span></div>
</div>
<div class="cell">
<div class="day"><span>16</span></div>
</div>
<div class="cell">
<div class="day"><span>17</span></div>
</div>
<div class="cell">
<div class="day"><span>18</span></div>
</div>
</div>
</div>
I am obviously missing something really fundamental but I have the following html
<div class="test-cont">
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">TEST2</div>
</div>
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">HELLO WORLD</div>
</div>
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">TEST2</div>
</div>
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">HELLO WORLD</div>
</div>
</div>
and am using the following selectors
.test-cont .row:nth-of-type(odd) {
background: purple;
}
.test-cont .row:nth-of-type(even) {
background: red;
}
Which I believe expresses 'select the nth child items of .text-cont where class contains .row and is either odd or even'.
Yet the <div class="md-margin"></div> break these selectors.
So I end up with
Instead of the alternating pattern I expect. The problem is resolved when I remove the md-margin divs. What am I missing?
it is because you have those md-margin divs in between, making them the even ones.
(althought :nth-of-type will show the result you want when using classes - depending on the markup -, but it refers to the element type, that's why div with md-margin are "counting" here.
snippet with md-margin
.test-cont .row:nth-of-type(4n+2) {
background: purple;
}
.test-cont .row:nth-of-type(4n+4) {
background: red;
}
<div class="test-cont">
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">TEST2</div>
</div>
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">HELLO WORLD</div>
</div>
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">TEST2</div>
</div>
<div class="md-margin"></div>
<div class="row">
<div class="col-md-12">HELLO WORLD</div>
</div>
</div>
Snippet without md-margin
.test-cont .row:nth-of-type(odd) {
background: purple;
}
.test-cont .row:nth-of-type(even) {
background: red;
}
<div class="test-cont">
<div class="row">
<div class="col-md-12">TEST2</div>
</div>
<div class="row">
<div class="col-md-12">HELLO WORLD</div>
</div>
<div class="row">
<div class="col-md-12">TEST2</div>
</div>
<div class="row">
<div class="col-md-12">HELLO WORLD</div>
</div>
</div>
My CSS isn't picking up my different columns. They are stacked on top of each other. I want each of them each to span 4 columns of the same row.
<div class="container">
<div class="row">
<div class="col-md-4" id="directionsPanel1">
<h3 class="directions-discription" id="directions-info1"></h3>
</div>
<div class="col-md-4" id="directionsPanel2">
<h3 class="directions-discription" id="directions-info2"></h3>
</div>
<div class="col-md-4" id="directionsPanel3">
<h3 class="directions-discription" id="directions-info3"></h3>
</div>
How about this using floats. You can apply the float which is the pull-left helper class to the div.col-md-4 and then they won't stack upon each other.
Updated JS fiddle but you shouldn't need to float left in the CSS, couldn't get JS fiddle to pull bootstrap stuff. http://jsfiddle.net/4xEPr/7/
<div class="container">
<div class="row">
<div class="col-md-4 pull-left" id="directionsPanel1">
<h3 class="directions-discription" id="directions-info1"></h3>
</div>
<div class="col-md-4 pull-left" id="directionsPanel2">
<h3 class="directions-discription" id="directions-info2"></h3>
</div>
<div class="col-md-4 pull-left" id="directionsPanel3">
<h3 class="directions-discription" id="directions-info3"></h3>
</div>
AND CSS
div.col-md-4 {
border: 1px solid #333;
width: 200px;
background-color: red;;
}