I want to color the 1st, 6th, 7th, 12th, 13th, and 18th boxes. Is there a quick method to do it in CSS
.a {
height: 50px;
width: 50px;
background-color: teal;
display: inline-block;
margin: 5px;
}
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
The nos. are of the form 6n and 6n+1.
So, add a selector .a:nth-of-type(6n), .a:nth-of-type(6n + 1).
Add background-color: red; to them.
More about :nth-of-type() from W3C:
The :nth-of-type() pseudo-class represents an element that has an+b siblings with the same expanded element name before it in the document tree, for any zero or positive integer value of n, and has a parent element.
Snippet :
.a{
height:50px;
width:50px;
background-color:teal;
display:inline-block;
margin:5px;
}
.a:nth-of-type(6n), .a:nth-of-type(6n + 1){
background-color: red;
}
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div><br />
<div class="a"></div>
<div class="a"></div>
<div class="a"></div>
Related
I'd like to find a way to select all the makes of cars except those that are inside a div with the class discontinued or scrapped. Here's my markup:
div:not(.discontinued):not(.scrapped) > .make {
color: green;
}
<div class="car">
<div class="make">NISSAN</div>
<div class="model">MICRA</div>
</div>
<div class="discontinued">
<div class="car">
<div class="make">FORD</div>
<div class="model">MONDEO</div>
</div>
</div>
<div class="scrapped">
<div class="car">
<div class="make">SEAT</div>
<div class="model">IBIZA</div>
</div>
</div>
<div class="scrapped">
<div class="preowned">
<div class="car">
<div class="make">BMW</div>
<div class="model">100</div>
</div>
</div>
</div>
<div class="car">
<div class="make">HONDA</div>
<div class="model">INTEGRA</div>
</div>
</div>
<div class="car">
<div class="make">PEUGEOT</div>
<div class="model">206</div>
</div>
<div class="car">
<div class="make">TOYOTA</div>
<div class="model">COROLLA</div>
</div>
As you can see above, I tried the following:
div:not(.discontinued):not(.scrapped) > .make
...but this still included FORD, SEAT, and BMW.
Unfortunately CSS selectors cannot traverse up parent elements, so if you are just trying to style them differently you may want to reverse your thought process and select ones that are .discontinued or .scrapped and apply overriding styles:
.model {
padding-left: 10px;
}
.make {
color: green;
}
.scrapped .make,
.discontinued .make {
color: red;
}
<div class="car">
<div class="make">NISSAN</div>
<div class="model">MICRA</div>
</div>
<div class="discontinued">
<div class="car">
<div class="make">FORD</div>
<div class="model">MONDEO</div>
</div>
</div>
<div class="scrapped">
<div class="car">
<div class="make">SEAT</div>
<div class="model">IBIZA</div>
</div>
</div>
<div class="scrapped">
<div class="preowned">
<div class="car">
<div class="make">SEAT</div>
<div class="model">IBIZA</div>
</div>
</div>
</div>
<div class="car">
<div class="make">HONDA</div>
<div class="model">INTEGRA</div>
</div>
<div class="car">
<div class="make">PEUGEOT</div>
<div class="model">206</div>
</div>
<div class="car">
<div class="make">TOYOTA</div>
<div class="model">COROLLA</div>
</div>
Why don't you try this style code,
div.make:not(.discontinued .make):not(.scrapped .make) {
color: green;
}
layout
How can i use the grid in bootstrap 4 to do the layout in the image? Can u make rows inside of a main row? I cant seem to do it.
You can use nested grid system as in the example
.b{
border: 1px black solid;
height: 50px;
margin: 5px;
}
.a{
border: 1px black solid;
height: 170px;
padding:5px;
margin:5px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<div class="row">
<div class="col-md-4 col-sm-4">
<div class="a"></div>
</div>
<div class="col-md-8 col-sm-8">
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="b"></div>
</div>
<div class="col-md-4 col-sm-4">
<div class="b"></div>
</div>
<div class="col-md-4 col-sm-4">
<div class="b"></div>
</div>
<div class="col-md-4 col-sm-4">
<div class="b"></div>
</div>
<div class="col-md-4 col-sm-4">
<div class="b"></div>
</div>
<div class="col-md-4 col-sm-4">
<div class="b"></div>
</div>
<div class="col-md-4 col-sm-4">
<div class="b"></div>
</div>
</div>
</div>
</div>
I got give a jsfiddle that asked the following:
Four Column Layout: Using a Bootstrap class, edit these divs from stacked to a 4 column layout
HTML:
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
<div class="c"></div>
<div class="a"></div>
<div class="d"></div>
<div class="b"></div>
CSS:
div{
height: 50px;
min-width: 50px;
}
div.a{
background: #00FF00;
}
div.b{
background: #1234FF;
}
div.c{
background: #FbbF00;
}
div.d{
background: #FF3344;
}
For my answer I did the following:
HTML:
<div class="row-fluid">
<div class="span 3">
<div class="a"></div>
<div class="b"></div>
</div>
<div class="span 3">
<div class="c"></div>
<div class="d"></div>
</div>
<div class="span 3">
<div class="c"></div>
<div class="a"></div>
</div>
<div class="span 3">
<div class="d"></div>
<div class="b"></div>
</div>
CSS:
div{
height: 50px;
min-width: 50px;
display: inline-block;
}
div.a{
background: #00FF00;
}
div.b{
background: #1234FF;
}
div.c{
background: #FbbF00;
}
div.d{
background: #FF3344;
}
But I was told I was wrong with no explanation. Does anyone know how to achieve four stacked columns using bootstrap for the above code?
Here you go with the solution https://jsfiddle.net/yczxm9px/1/
.div{
height: 50px;
}
div.a{
background: #00FF00;
}
div.b{
background: #1234FF;
}
div.c{
background: #FbbF00;
}
div.d{
background: #FF3344;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 div a">
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 div b">
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 div c">
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 div d">
</div>
</div>
</div>
All the class are bootstrap class.
xs for Extra Small Screen
sm for Small Screen
md for Medium Screen
lg for Large Screen.
If I have something like this
<div class="A">
<div class="B"></div>
<div class="C"></div>
</div>
css
.B, .C {
display:inline-block;
}
For both B and C, the width wraps it's content and doesn't fill width of A fully. But if I set its width to 100%, then each width is equal to the width of A, which then extends A to twice it's original size in width.
How can I change it so that, the width of B + the width of C = the original width of A.
Basically increase the width of B and C such that it fills the width of A, but keep them inline.
Does anyone know?
Thanks
The best use-case for this would be using Flexbox as you are saying they are dynamic:
.A {border: 1px solid #99f;}
.A div {border: 1px solid #999; height: 10px;}
/* Flex Box */
.A {display: flex;}
.A div {flex: auto;}
Two:
<div class="A">
<div class="B"></div>
<div class="C"></div>
</div>
Three:
<div class="A">
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
</div>
Random:
<div class="A">
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
</div>
Please note that this is not as elegant as Praveen Kumar's answer, but I thought I'd post a table-based answer:
.A {
display: table;
height: 50px;
width: 100%;
border: 2px solid #000;
}
.A > div {
display: table-cell;
border: 1px solid #fff;
background-color: red;
}
Example 1:
<div class="A">
<div class="B"></div>
<div class="C"></div>
</div>
Example 2:
<div class="A">
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
</div>
Example 3:
<div class="A">
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
</div>
Example 4:
<div class="A">
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
</div>
Example 5:
<div class="A">
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
</div>
Again, the amount of children is irrelevant. You can have as many as you require.
Here is my code
http://jsfiddle.net/B8JYN/1/
<style>
html, body {
height: 100%;
padding: 0px;
margin: 0px;
}
#outerdiv {
height: 50%;
width: 99%;
left:1%;
background-color: #000000;
position:absolute;
top:25%;
}
#fretboard {
position:absolute;
width: 100%;
top:5%;
height: 90%;
background-color: red;
}
.stringTitle {
height: 50%;
background-color: #000000;
position:absolute;
top:25%;
width:1%;
background-color: #fff;
}
.string{
height:16.666%
}
.fret{
width:8%;
float:left;
height:100%;
}
.fretEnd{
width:.3%;
background-color:#e7ebea;
float:left;
height:100%;
}
.E1{
background-color:green;
height: 3%;
margin-top: 11%;
}
.B{
background-color:green;
height: 3%;
margin-top: 23.5%;
}
.G{
background-color:green;
height: 3%;
margin-top: 23.5%;
}
.D{
background-color:green;
height: 3%;
margin-top: 23.5%;
}
.A{
background-color:green;
height: 3%;
margin-top: 23.5%;
}
.E2{
background-color:green;
height: 3%;
margin-top: 23.5%;
}
</style>
<div class="stringTitle">
<div class="string">E</div>
<div class="string">B</div>
<div class="string">G</div>
<div class="string">D</div>
<div class="string">A</div>
<div class="string">E</div>
</div>
<div id="outerdiv">
<div id="fretboard">
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fret">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
<div class="fretEnd">
<div class="E1"></div>
<div class="B"></div>
<div class="G"></div>
<div class="D"></div>
<div class="A"></div>
<div class="E2"></div>
</div>
</div>
</div>
I'm trying to get the green lines to be spaced equally in the vertical direction and continuously across from left to right. Hope that makes sense? I'm trying to make this using only percentages rather than specifying px
The percentage based margin-top value is relative to the width, therefore the positioning won't be consistent if the width of the browser is changed.
8 Box model - 8.3 Margin properties
The percentage is calculated with respect to the width of the generated box's containing block. Note that this is true for 'margin-top' and 'margin-bottom' as well. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.
One solution would be to absolutely position the elements and use a percentage based top value. It's important to relatively position the parent element in order for them to be positioned absolutely relative to the parent, in this case, .fret.
UPDATED EXAMPLE HERE
.E1, .B, .G, .D, .A, .E2 {
position:absolute;
width:100%;
background-color:green;
height: 3%;
}
.E1 {
top:9.6%;
}
.B {
top:26.2%;
}
.G {
top:42.9%;
}
.D {
top:59.6%;
}
.A {
top:76.4%;
}
.E2 {
top:91.2%;
}
Those are just rough estimates. You can of course change the values depending on your needs.