CSS Rule for the 8th and on element in a div - css

Is it possible to create a CSS rule that applies to the every element except for the first 8 elements? Ie, the 8th plus elements should have a margin top of 65px.
My below less code applies margins to every odd and even button within a menu. Now I want to add a specific margin to the 8th plus buttons. And then ideally apply a specific margin to the 16th plus buttons and so on.
.foo-menu {
.foo-menu-btn {
float: left;
margin: 1px;
}
// Apply specific margin to every second(even) button
.foo-menu-btn:nth-child(even) {
margin-left: -23px;
margin-top: 46px;
}
// Apply specific margin to every odd button
.foo-menu-btn:nth-child(odd) {
margin-left: -23px;
}
// For every button after the 8th one; apply a specific margin
.foo-menu-btn:nth-child( ??? ) {
margin-top: 65px;
}
}
<div class="foo-menu">
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<!-- Now every foo-menu-btn should have a top margin of 65px -->
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
<div class="foo-menu-btn"></div>
</div>

Try below code, i think help full to you.
hr {
display: block; float: left;
width: 50px; height: 50px;
border: solid 2px #aaa; margin: 10px;
}
hr:nth-child(n+9):not(:nth-last-child(-n)) {
background-color: #ddd;
}
<div id=t>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
<hr>
</div>

You can use the native CSS :nth-child pseudo-class to specify a range. According to the case you specified it might look like this:
div.foo-menu div.foo-menu-btn:nth-child(n+8):nth-child(-n+15) {
margin-left: 50px
}
The downside is that you still have to manually define each range.

To select everything other than the first 8 divs you can use .foo-menu-btn:nth-child(n+9). See it applied to your HTML below (I took out the negative margins so that the divs would be visible for this example):
.foo-menu-btn {
float: left;
margin: 1px;
background-color: #ccc;
height: 1rem;
}
.foo-menu-btn:nth-child(even) {
margin-top: 46px;
}
.foo-menu-btn:nth-child(n+9) {
margin-top: 65px;
}
<div class="foo-menu">
<div class="foo-menu-btn">1</div>
<div class="foo-menu-btn">2</div>
<div class="foo-menu-btn">3</div>
<div class="foo-menu-btn">4</div>
<div class="foo-menu-btn">5</div>
<div class="foo-menu-btn">6</div>
<div class="foo-menu-btn">7</div>
<div class="foo-menu-btn">8</div>
<!-- Now every foo-menu-btn should have a top margin of 65px -->
<div class="foo-menu-btn">9</div>
<div class="foo-menu-btn">10</div>
<div class="foo-menu-btn">11</div>
<div class="foo-menu-btn">12</div>
<div class="foo-menu-btn">13</div>
<div class="foo-menu-btn">14</div>
<div class="foo-menu-btn">15</div>
<div class="foo-menu-btn">16</div>
</div>

Use :
.foo-menu .foo-menu-btn:nth-child(n+9){
color: blue;
}
.foo-menu .foo-menu-btn:nth-child(odd){
color: red;
}
.foo-menu .foo-menu-btn:nth-child(even){
color: green;
}
.foo-menu .foo-menu-btn:nth-child(n+9){
color: blue;
}
<div class="foo-menu">
<div class="foo-menu-btn">1</div>
<div class="foo-menu-btn">2</div>
<div class="foo-menu-btn">3</div>
<div class="foo-menu-btn">4</div>
<div class="foo-menu-btn">5</div>
<div class="foo-menu-btn">6</div>
<div class="foo-menu-btn">7</div>
<div class="foo-menu-btn">8</div>
<!-- Now every foo-menu-btn should have a top margin of 65px -->
<div class="foo-menu-btn">9</div>
<div class="foo-menu-btn">10</div>
<div class="foo-menu-btn">11</div>
<div class="foo-menu-btn">12</div>
<div class="foo-menu-btn">13</div>
<div class="foo-menu-btn">14</div>
<div class="foo-menu-btn">15</div>
<div class="foo-menu-btn">16</div>
</div>

Related

Show/hide sibling div based on content

Hello – I would like to show content in a div based on the content in another div. For example, if sibling1 is empty, I would like to hide sibling2 (in parent1 below). If sibling1 has content, I would like to show sibling2 (parent2 below). I'd prefer to be able to do this with CSS, is this possible? If not, I can work with a simple javascript suggestion as well.
<!---hide sibling2--->
<div class="parent1">
<div class="sibling1"></div>
<div class="sibling2">hide</div>
</div>
<!---show sibling2--->
<div class="parent2">
<div class="sibling1">has content</div>
<div class="sibling2">show</div>
</div>
.parent {
height: 100px;
border: 1px solid;
}
.sibling1 { background: green; }
.sibling2 { background: red; }
.sibling1:empty + .sibling2 { display: none; }
<!---hide sibling2--->
<div class="parent">
<div class="sibling1"></div>
<div class="sibling2">hide</div>
</div>
<!---show sibling2--->
<div class="parent">
<div class="sibling1">has content</div>
<div class="sibling2">show</div>
</div>

css to mimic a simple bar chart

I am trying to create this with css and having a hard time getting it to align correctly. I am just trying to fill a box that starts at middle point of a div and have its height equal to the percentage. If it gets over a certain percentage I want it to be inside the filled div. Guessing this is not that hard but I can't seem to figure it out. Mainly how to get the fill box to start half way down and either fill up or down depending on positive or negative.
I would appreciate any help.
jsbin link
http://jsbin.com/lexifetuja/edit?html,css,js,output
.col-sm-3 {
border: 1px solid #000;
height:200px;
}
.fill-box {
background-color: #213F5E;
width:100%;
position: absolute;
top: 50%
}
.fill-box1 {
height: 6%;
}
.fill-box2 {
height: 41%;
}
<div class="container">
<div class="row">
<div class="col-sm-3">
<span class="percentage text-left">12%</span>
<div class="fill-box fill-box1"></div>
</div>
<div class="col-sm-3">
<span class="percentage text-left">-82%</span>
<div class="fill-box fill-box2"></div>
</div>
</div>
</div>
You could add a class for negative percentage fill-box elements and set the top position to 100px (so that the top of the element starts at the half way point of the container) and then for the positive percentage fill-box just set the top position to negative the remaining percentage:
.col-sm-3 {
border: 1px solid #000;
height:200px;
}
.fill-box {
background-color:#213F5E;
width:100%;
position:absolute;
top:50%;
left:0;
}
.fill-box.negative {
top:100px;
}
.fill-box1 {
height:6%;
transform:translateY(-94%);
}
.fill-box2 {
height:41%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-sm-3">
<span class="percentage text-left">12%</span>
<div class="fill-box fill-box1"></div>
</div>
<div class="col-sm-3">
<span class="percentage text-left">-82%</span>
<div class="fill-box fill-box2 negative"></div>
</div>
</div>
</div>

Two color section full width with "restrcited" container width

The idea is that certaint section of the page have two color by column:
[ Column left color red ] [ Column right color green ]
So basically the html right now looks like this.
<section id="foo">
<div class="wrapper-content">
<div class="col-sm-5 bg-red">Some content</div>
<div class="col-sm-7 bg-green">Some Other Content</div>
</div>
</section>
That gives me how I need it to look, full width 2 columns with different colours, the problem is the container.
First image is what I need
Second image is what I'm getting
Following code is not working at all....
<section id="foo">
<div class="wrapper-content">
<div class="col-sm-5 bg-red same-height"></div>
<div class="col-sm-7 bg-green same-height"></div>
<div class="container absolute">
<div class="col-sm-5">Some content</div>
<div class="col-sm-7">Some Other Content</div>
</div>
</div>
Colour on the left hast to go all the way left, the colour on the right all the way right, bu the content has to stay center and between the two columns.
Any ideas?
You have to add a container class to your content section too.
Or put your content section into the header wrapper div.
Example: If your header div is wrapped with a class <div class="container">...</div>, also wrap the content div with this class too.
Sample 1: if you want different sections for both header and content
<div class="container">
<div>Header</div>
</div>
<div class="container">
<div>Your content section</div>
</div>
Sample 2: if you dont need header and content sections to be separated
<div class="container">
<div>Header</div>
<div>Your content section</div>
</div>
This may work. You have to put divs of the same background color behind your foreground divs, absolutely positioned.
The container doesn't restrict the size at small widths, so you may have to expand the snippet to full page to see the results (the black borders are there to show the boundaries of your content areas)
.header {
background-color: beige;
margin-bottom: 20px;
}
.main-content {
position: relative;
}
.left-bg {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 50%;
background-color: #dddddd;
}
.right-bg {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 50%;
background-color: #aaaaaa;
}
.left-fg {
background-color: #dddddd;
border: 1px solid black;
padding: 30px 0;
}
.right-fg {
background-color: #aaaaaa;
border: 1px solid black;
padding: 30px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="header">Header</div>
<div class="main-content">
<div class="left-bg"></div>
<div class="right-bg"></div>
<div class="container">
<div class="row">
<div class="left-fg col-xs-5">Hello</div>
<div class="right-fg col-xs-7">Goodbye</div>
</div>
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Bootstrap code design modfication

Im using Bootstrap in my application. I am trying to make it so my whole left side is #fff, while rest is #f9f9f9.
This is what I have so far.
HTML:
<div class="container">
<section class="content">
<div class="col-xs-8">
// Left side
</div>
<div class="col-xs-4">
// Right side side-bar
</div>
</section>
</div>
CSS:
. container {
width: 1200px !important;
}
PS: I don't want/need responsive design.
Image below is a example for what I want to achieve.
Here is a link that hast the same design.
Please try the following code:
HTML
<div class="container">
<section class="content">
<div class="col-xs-8 left-side">
// Left side
</div>
<div class="col-xs-4 side-bar">
// Right side side-bar
</div>
</section>
</div>
CSS
.container{
margin: 0;
padding: 0;
width: 100%
}
.content{
height: 100%;
width: 100%;
}
.left-side{
background-color: #fff;
min-height: 100%;
}
.side-bar{
background-color: #f9f9f9;
min-height: 100%;
}
.no-margin{
margin: 0;
padding: 0
}
This would be a way to achieve what you want to do.
.leftside {
background: #fff;
}
.rightside {
background: #f9f9f9;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-xs-8 leftside">
// Left side
</div>
<div class="col-xs-4 rightside">
// Right side side-bar
</div>
</div>
</div>

Child element shifting parent div

I've searched quite a bit looking for an explanation as to why this behavior is occurring.
Essentially I've setup 2 columns, each with a nav bar and content area.
CSS
#mainContainer {
background-color: lightblue;
width: 100%;
height: 300px;
}
#leftContainer, #rightContainer {
border: 1px solid black;
display: inline-block;
background-color: red;
width: 40%;
height: 100%;
}
#leftBar, #rightBar {
background-color: purple;
height: 10%;
}
#leftMain, #rightMain {
background-color: grey;
height: 90%;
}
HTML
<div id="mainContainer">
<div id="leftContainer">
<div id="leftBar"></div>
<div id="leftMain"></div>
</div>
<div id="rightContainer">
<div id="rightBar"></div>
<div id="rightMain"></div>
</div>
</div>
Whenever I add an element to the nav bar in only one column it shifts the entire column down.
http://jsfiddle.net/qn6rs0q2/3/
<div id="mainContainer">
<div id="leftContainer">
<div id="leftBar">
<button>Test</button>
</div>
<div id="leftMain"></div>
</div>
<div id="rightContainer">
<div id="rightBar"></div>
<div id="rightMain"></div>
</div>
</div>
But if I add another element to the other column they line up again.
http://jsfiddle.net/qn6rs0q2/5/
<div id="mainContainer">
<div id="leftContainer">
<div id="leftBar">
<button>Test</button>
</div>
<div id="leftMain"></div>
</div>
<div id="rightContainer">
<div id="rightBar">
<button>Test 2</button>
</div>
<div id="rightMain"></div>
</div>
</div>
To clarify, I'm not looking for a solution to fix this behavior. Rather I'm hoping someone can explain the underlying reason behind why it's behaving as it is. Thanks in advance.
It happens because the default vertical alignment of inline elements is the baseline. If you set the vertical alignment to top (or middle) for both sides, they line up as you want:
#leftContainer, #rightContainer {
border: 1px solid black;
display: inline-block;
background-color: red;
width: 40%;
height: 100%;
vertical-align:top;
}
jsFiddle example

Resources