How can I select odd and even elements with CSS? - html

Say I have some divs:
<div class="box-1"></div>
<div class="box-2"></div>
<div class="box-3"></div>
<div class="box-4"></div>
If these boxes need to be alternate colours I need to create some CSS which basically does the following:
.box-(odd-number) {
color:#000;
}
.box-(even-number) {
color:#fff;
}
Obviously I know the above is not the correct syntax. Could some one point me in the right direction?
Thanks

You can use the nth-of-type pseudo-class, combined with the keywords odd and even:
.box:nth-of-type(odd) {
background-color:#000;
}
.box:nth-of-type(even) {
background-color:#fff;
}
.box {
display: inline-block;
width: 100px;
height: 100px;
border: 1px solid #f00;
}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

You can do this using nth-child() with Even and odd rules.
.box:nth-child(odd) {
background: blue;
}
.box:nth-child(even) {
background: green;
}
<div class="box">Lorem ipsum dolor sit amet.</div>
<div class="box">Lorem ipsum dolor sit amet.</div>
<div class="box">Lorem ipsum dolor sit amet.</div>
<div class="box">Lorem ipsum dolor sit amet.</div>
Or you can can do this where :nth-child(2n) represents the even and :nth-child(2n+1) represents the odd
.box:nth-child(2n) {
background: red;
}
.box:nth-child(2n+1) {
background: yellow;
}
<div class="box">Lorem ipsum dolor sit amet.</div>
<div class="box">Lorem ipsum dolor sit amet.</div>
<div class="box">Lorem ipsum dolor sit amet.</div>
<div class="box">Lorem ipsum dolor sit amet.</div>

You're looking for nth-child(odd) and nth-child(even), If you don't want to apply .box classname.
[class^="box-"]:nth-child(odd) {
color:#000;
}
[class^="box-"]:nth-child(even) {
color:#fff;
}
An example: https://jsfiddle.net/8tkcuuwm/

To get this working you need a container of which you can adress the odd and even children like this. You set the class to the container and Format it's children accordingly.
By this you only have to set the class once and can exchange it if needed, without having to modify each child separately:
<style type="text/css">
.container div:nth-child(odd) {
color:#F00;
}
.container div:nth-child(even) {
color:#00F;
}
</style>
<div class="container">
<div class="box-1">Lorem ipsum dolor sit amet.</div>
<div class="box-2">Lorem ipsum dolor sit amet.</div>
<div class="box-3">Lorem ipsum dolor sit amet.</div>
<div class="box-4">Lorem ipsum dolor sit amet.</div>
</div>

See this jsfiddle:
HTML
<div class="box box-1">Hello World</div>
<div class="box box-2">Hello World</div>
<div class="box box-3">Hello World</div>
<div class="box box-4">Hello World</div>
CSS
.box:nth-child(odd) {
background-color: #336699;
}
.box:nth-child(even) {
background-color: #222;
}
Short explaination:
We added another class to the boxes, called box. This is, so we can refer to every element of this type. (My hint: use ID's for the box-1, box-2 stuff, since they appear to be unique).
Using the pseudo-class nth-child in combination with odd or even, will affect every (as you may assume) odd- or even element.

if colours should alternate depending only on the order of the div elements, (no matter the class name) then you could use div:nth-child(2n) and div:nth-child(2n + 1)
On the contrary if it depends only on the last digit of your class name (no matter if your divs are in the right order) then you could write
[class^="box"][class$="2"],
[class^="box"][class$="4"],
[class^="box"][class$="6"],
[class^="box"][class$="8"],
[class^="box"][class$="0"] { ... }
[class^="box"][class$="1"],
[class^="box"][class$="3"],
[class^="box"][class$="5"],
[class^="box"][class$="7"],
[class^="box"][class$="9"] { ... }

Use nth-child in order to achieve this.
HTML
<div class="box"></div>
<div class="box"><div>
<div class="box"></div>
<div class="box"></div>
CSS
.box:nth-child(odd) {
background-color: #000;
}
.box:nth-child(even) {
background-color: #FFF;
}

Related

Is there a way to nest CSS selectors? [duplicate]

This question already has answers here:
Nesting CSS classes
(8 answers)
Closed 8 months ago.
imagine a html code which I cannot change. something like:
<div class="a">
<div class="aa">
<div class="aa1">
</div>
<div class="aa1">
</div>
<div class="aa1">
</div>
<div class="aa1">
</div>
</div>
<div class="b">
<div class="bb">
...
</div>
</div>...
</div>
so what I want to change is in 'aa1' s and each one should have different styling.
so in css I could write
.a .aa .aa1:first-child{
styling1:;
}
.a .aa .aa1:nth-child(2){
styling2:;
}
.a .aa .aa1:nth-child(3){
styling3:;
}
.a .aa .aa1:nth-child(4){
styling4:;
}
.a .aa .aa1:nth-child(5){
styling5:;
}
but there is lot of those 'aa1' parts. Is there a way to simplify this where I only have to specify nth-child(x) for each element?
something like:
.a .aa .aa1
{
:first-child{
styling1:;
}
:nth-child(2){
styling2:;
}
:nth-child(3){
styling3:;
}
:nth-child(4){
styling4:;
}
:nth-child(5){
styling5:;
}
}
With the current CSS (2.1) version, nesting selectors isn't possible.
You'll have to use something called a CSS preprocessor which does several things, including allowing nested selectors.
I'd recommend SASS (SCSS), and another out there is LESS. You'll need some extra things setup on your PC/Mac for automatically converting preprocessor files to regular CSS files - plenty of other questions and research points on that out there.
If you use SCSS, you can check out what its like on SassMeister so you can see what the nested selectors will look like compiled.
one possible solution would be to use the adjacent sibling selector (+) for each "aa1" class and + one for each new style
for example:
.a .aa .aa1 p {
color: black;
}
.a .aa .aa1 +.aa1 p {
color: red;
}
.a .aa .aa1 + .aa1 + .aa1 p {
color: blue;
}
.a .aa .aa1 + .aa1 + .aa1 +.aa1 p {
color: green;
}
<div class="a">
<div class="aa">
<div class="aa1">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</p>
</div>
<div class="aa1">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</p>
</div>
<div class="aa1">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</p>
</div>
<div class="aa1">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</p>
</div>
</div>
</div>

Display text inline from a separate container div

I'm trying make "the end" in the following code to appear inline with the lorem ipsum, and can't figure out how. Is it possible? I can't change the HTML structure at all. (nor can I add js, etc)
#parent {
width: 300px;
border: 1px solid red;
}
#block2 a {
color: #00f;
}
<div id="parent">
<div id="block1">
<a> Lorem ipsum dolor sit amet, consectetur elit. dolor nulla. Duis lob.</a>
</div>
<div id="block2">
<a>The end</a>
</div>
</div>
I want it to look like this:
If you are able to make changes to the CSS, then this is an easy solution. Just use display: inline, which will make the element only take as much space as necessary (acting like a <span> element).
However, if by chance, you are unable to, then there is no way I can think of for you to achieve this given your situation.
#parent {
width: 300px;
border: 1px solid red;
}
#block1, #block2 {
display: inline;
}
#block2 a {
color: #00f;
}
<div id="parent">
<div id="block1">
<a> Lorem ipsum dolor sit amet, consectetur elit. dolor nulla. Duis lob.</a>
</div>
<div id="block2">
<a>The end</a>
</div>
</div>
You need to set the two block containers to display: inline:
#parent {
width: 300px;
border: 1px solid red;
}
#block2 a {
color: #00f;
}
#block1, #block2 {
display: inline;
}
<div id="parent">
<div id="block1">
<a> Lorem ipsum dolor sit amet, consectetur elit. dolor nulla. Duis lob.</a>
</div>
<div id="block2">
<a>The end</a>
</div>
</div>

CSS First-child in a Div element strange behavior

So here is my Code :
.tagBoxContent p:first-child {
color: blue;
}
.tagBoxContent p:last-child {
color: red;
}
<div class="tagBoxContent">
<a class="tagHeadline">with <span>'chat'</span> tagged articles</a>
<p>Lorem ipsum dolor sit amet,</p>
<p>Integer ma</p>
</div>
Why first-child dont trigger to the p tag but the last child element do? and how can i call the first p tag in this without first-child or with working first-child? I researched a lot but do not find any duplicate with the same problem.
You're using :first-child and it's not the first child. Instead, use :first-of-type.
.tagBoxContent p:first-of-type {
color: blue;
}
.tagBoxContent p:last-child {
color: red;
}
<div class="tagBoxContent">
<a class="tagHeadline">with <span>'chat'</span> tagged articles</a>
<p>Lorem ipsum dolor sit amet,</p>
<p>Integer ma</p>
</div>
Use nth-child() to select a child not the first or last child
.tagBoxContent p:nth-child(2) {
color: blue;
}
.tagBoxContent p:last-child {
color: red;
}
<div class="tagBoxContent">
<a class="tagHeadline">with <span>'chat'</span> tagged articles</a>
<p>Lorem ipsum dolor sit amet,</p>
<p>Integer ma</p>
</div>

width:auto not working right for absolutely positioned div when scroll bar is present

I have a div with some content that's absolutely positioned and has an explicit height. When the content goes outside the height, a scroll bar appears, but it doesn't respect my width:auto - the scroll bars cover up the content.
Example:
<style>
#main {
height: 100px;
border: 1px solid red;
overflow-y: auto;
position: absolute;
}
</style>
<div id='main'>
<div>test</div>
<div>test</div>
<div>testiiiiiiiing</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
</div>
What's going on here? Is this browser bug? How can I make it correctly respect my automatic width?
You can use overflow-y: scroll;
<style>
#main {
height: 100px;
border: 1px solid red;
overflow-y: auto;
position: absolute;
padding-right: 10px;
}
#a {}
</style>
CodePen
Two possiblties occur to me although it's unlikely you will find many words being 100px wide.
First, just add some padding-right to make space for the scrollbar.
.main {
height: 100px;
border: 1px solid red;
overflow-y: auto;
position: absolute;
padding-right: 25px;
}
<div class="main">
<div class="a">Lorem ipsum.</div>
<div class="a">Lorem ipsum dolor sit.</div>
<div class="a">Lorem ipsum.</div>
<div class="a">Loremipsumdolorsitametconsecteturametconsectetur.</div>
<div class="a">Lorem</div>
<div class="a">Lorem ipsum.</div>
<div class="a">Lorem ipsum dolor sit.</div>
<div class="a">Lorem ipsum.</div>
<div class="a">Lorem ipsum dolor sit amet, consectetur.</div>
<div class="a">Lorem</div>
</div>
Secondly, force all words to break if they reach that far edge using word-wrap: break-word;
.main {
height: 100px;
border: 1px solid red;
overflow-y: auto;
position: absolute;
padding-right: 25px;
}
.main.breaking {
padding-right: none;
word-wrap: break-word;
}
<div class="main breaking">
<div class="a">Lorem ipsum.</div>
<div class="a">Lorem ipsum dolor sit.</div>
<div class="a">Lorem ipsum.</div>
<div class="a">Loremipsumdolorsitametconsecteturametconsectet</div>
<div class="a">Lorem</div>
<div class="a">Lorem ipsum.</div>
<div class="a">Lorem ipsum dolor sit.</div>
<div class="a">Lorem ipsum.</div>
<div class="a">Lorem ipsum dolor sit amet, consectetur.</div>
<div class="a">Lorem</div>
</div>

vertical align bottom for a specific child

I have a parent element that has 2 children. I want to move 1st child to top, and second one to bottom. The parent element has a scroll and its children size isn't fixed. Children size expands dynamically depending on theirs content
So If theirs size are smaller than parent's one they would look like in the left picture, else they should expand parents div like in the right picture. Usually to move element to edges I'd use absolute position like this:
.parent {
position: relative;
}
.top-child {
position: absolute;
top: 0;
}
.bottom-child {
position: absolute;
bottom: 0;
}
But this case brakes the flow. Parent width and height wouldn't adjust depending by children size. Another solution is to use vertical-align
.parent {
display: table-cell;
vertical-align: bottom;
}
But in this scenario all children would move to bottom.
Here's jsfiddle. Green background is parent. topdiv and bottomdiv and childrens.
How should I CSS divs to attach children to edges without breaking the flow?
you can achieve this use display:table and table-row:
#scroller {
height:300px; /* this height is the min height before you want to scroll */
overflow:auto;
}
.table {
min-height:100%;
width:100%;
display:table;
}
.row {
display:table-row;
}
.row:first-child {
height:100%; /* this is needed to "push" the second row to the bottom (it will actually be 100% minus the bottom row height */
background:blue;
}
.row:last-child {
background:green;
}
<div id="scroller">
<div class="table">
<div class="row">expands to fill space</div>
<div class="row">stays at bottom</div>
</div>
</div>
Example fiddle
Fiddle with content and scrolling
Update
Applying my styles to your fiddle
Something like this?
.main {
width: 300px;
height: 300px;
overflow-y: auto;
border: 1px solid #999;
margin-bottom: 30px;
}
.parent {
min-height: 100%;
display: flex;
flex-direction: column;
}
.child-a {
flex: 1;
background: #ccc;
}
.child-b {
background: #ddd;
}
<div class="main">
<div class="parent">
<div class="child-a">
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
</div>
<div class="child-b">
<p>Amet ipsum dolor</p>
</div>
</div>
</div>
<div class="main">
<div class="parent">
<div class="child-a">
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
<p>Lorem ipsum dolor</p>
</div>
<div class="child-b">
<p>Amet ipsum dolor</p>
</div>
</div>
</div>