Change div layout with CSS? (Alternating divs) - html

I am writing a plugin for WordPress. The layout is as following, displayed in a horizontal row:
<div class="row">
<div class="thumbnail">
<div class="information">
</div>
The PHP-function alternates the divs so every other row is the opposite order, like so:
<div class="row">
<div class="information">
<div class="thumbnail">
</div>
This is all working out in a fullscreen page, but on smaller screen I would like to get the "row" displayed vertically non-alternating.
So I'd like to get the thumbnail displayed first, then the information for every row.
When adding the #media statements for the div to pop down, I get the wrong order due to the alternating divs like so:
1st thumbnail
1st information
2nd information
2nd thumbnail
So I wonder if it's even possible to change through CSS? Been trying a lot of different floats and n-th childs but I end up with the same results.
Here is a jsbin to show the layout I am using:
JSBIN

You can use order with a mediaquery or with a min-width to set a break point .
.row {
margin: 0;
padding: 0;
height: 100%;
display: flex;
flex-wrap:wrap;
}
.information {
flex: 1;
padding: 5%;
min-width:250px;/* set a breakpoint at 500px where thumb is set at 50% */
}
.thumbnail {
flex: 1;
min-width: 50%;/* min-width will allow to spray on the whole line when alone */
background: teal;
}
#media screen and (max-width: 640px) {
.row:nth-child(odd) .thumbnail {
order:1;
}
}
<div class="row">
<div class="thumbnail">thumb</div>
<div class="information">information goes here</div>
</div>
<div class="row">
<div class="information">information goes here</div>
<div class="thumbnail">thumb</div>
</div>
<div class="row">
<div class="thumbnail">thumb</div>
<div class="information">information goes here</div>
</div>
min-width + order http://jsbin.com/xupurikuxu/1/edit?html,css,output
mediaquerie http://jsbin.com/senobugubi/1/edit?html,css,output

Related

How to get a layout of Divs that looks like this? Better way than trying to use Ionic Grid?

I'm trying to get a css layout like the one below. It would consist of 6 divs total... 3 on top and 3 on the bottom. I'm wanting them to be static sizes so they don't change height/width. I'm using angular/ionic and tried to use an ionic grid but couldn't get the spacing to look correct using a grid system.
I'm filling each box with a directive/component but can handle the css around that part.
Easy with flexbox
.d-flex {
display: flex ;
height: 200px;
&.short {
height: 40px;
}
div {
flex: 1 1 0 ;
border: solid 1px black ;
&.small {
flex-grow: 0;
min-width: 50px;
}
}
}
<div class="d-flex">
<div class=""></div>
<div class="small"></div>
<div class=""></div>
</div>
<div class="d-flex short">
<div class=""></div>
<div class="small"></div>
<div class=""></div>
</div>

How to divide divs into rows equally

Apologies if this has been asked before but I couldn't find exactly the same question.
I have the following:
.main{
display: inline-flex;
flex-direction: row;
}
<div class = "main">
<div class="sub-div">1 </div>
<div class="sub-div">2 </div>
<div class="sub-div">3 </div>
<div class="sub-div">4 </div>
<div class="sub-div">5 </div>
<div class="sub-div">6 </div>
</div>
What this does is it displays all the divs in same line. But how can I divide the divs such that there will be 3 divs on top row and 3 divs on bottom row?
Additionally, if the screen size gets smaller, how can I divide the divs such that there will be 2 divs on top row, 2 on middle row, and 2 on last row?
Can I do it without changing HTML structure or using Javascript?
Use flex-wrap: wrap on .main to allow its children to wrap onto multiple lines. Then, you could explicitly set the width equal to calc(100% / n) where n is the number of children you want per row.
To change the layout from 2 divs per row to 3 divs per row, use a media query, as shown in the code snippet below.
.main {
display: inline-flex;
flex-wrap: wrap;
}
.sub-div {
width: calc(100% / 2);
}
#media screen and (min-width: 600px) {
.sub-div {
width: calc(100% / 3);
}
}
<div class="main">
<div class="sub-div">1 </div>
<div class="sub-div">2 </div>
<div class="sub-div">3 </div>
<div class="sub-div">4 </div>
<div class="sub-div">5 </div>
<div class="sub-div">6 </div>
</div>
You may achieve it with Grid and media query.
.main {
display: grid;
/* grid-gap: 50px 100px; */
grid-template-columns: auto auto auto;
}
#media only screen and (max-width: 768px) {
.main {
grid-template-columns: auto auto;
}
}
<div class="main">
<div class="sub-div">1 </div>
<div class="sub-div">2 </div>
<div class="sub-div">3 </div>
<div class="sub-div">4 </div>
<div class="sub-div">5 </div>
<div class="sub-div">6 </div>
</div>

How to make a Bootstrap row background color fill the rest of the page?

When we make a webpage with Bootstrap, we use rows that we put in a container, so if do this with 2 rows, how to make the 2nde one to fill the entire page ?
I.e if I have those 2 rows:
here
How do I make the 2nde one (the blue one) to fill the rest of the page ?
Thank you
The HTML code is:
<body>
<div class="container-fluid">
<div class="row row1">
<br>
</div>
<div class="row row2">
<br>
</div>
</div>
</body>
And the css is:
.row1{
background-color: orange;
}
.row2{
background-color: blue;
}
p.s: I just want to transform the rows properties, not use one row and color the rest of the page with the backgroud color of the body.
You can use flex layout to implement what you want. Here is an example:
* {
margin: 0;
}
html, body {
height: 100%;
}
.row1{
background-color: orange;
}
.row2{
background-color: blue;
}
.container-fluid {
display: flex;
flex-direction: column;
height: 100%;
}
.row:last-child {
flex: 1;
}
<div class="container-fluid">
<div class="row row1">
<br>
</div>
<div class="row row2">
<br>
</div>
</div>
A jsfiddle is also made for reference.
Some explanation for the key code:
display: flex;: use flex layout for items inside of display: flex.
flex-direction: column;: arrange .rows vertically.
flex: 1;: make the last child of .container-fluid (.row2 in your case) grow/shrink automatically.

Auto adjustable space between divs for 100% height

I've prepared a mobile site, which has the right aspect ratio of images, but on some phones with bigger screens it's to short and after the footer I get white gap. I would prefer to add some DIVs between contents which they would auto stretch when needed.
I'm thinking of something like this:
<div id="wrapper">
<div id="header"></div>
<div id="content1"></div>
<div class="empty_gap"></div>
<div id="accordion"></div>
<div class="empty_gap"></div>
<div id="footer"></div>
</div>
The .empty_gap DIVs would get evenly height so the page would fit the screen height. How to get this done? Or do you have any other solution for such a situation when the page is to short to fill the entire mobile screen?
EDIT: I have to add one thing which I didn't think before it will cause a problem. None of these solutions are working for me, because I use jquery accordion, and when I execute $("#wrapper").outerHeight(); I get much bigger value than the screen size even when I have all panels collapsed. I guess that this is also the reason for flexible boxes that those solutions don't work too.
EDIT2: The JS solution is working now, I just needed to subtract the panel's content from the wrapper.
If you're OK with a JavaScript solution, try this:
Usually your divs should be wrapped with a div container. If it's not the case, then go ahead and add one like this:
<div id="wrapper">
<div id="header"></div>
<div id="content1"></div>
<div class="empty_gap"></div>
<div id="content2"></div>
<div class="empty_gap"></div>
<div id="footer"></div>
</div>
Then in JavaScript, calculate the height of this container div and subtract it from the window's height. That would be the total of all your gaps, so divided by the number of your gaps (2 in your example) to get the height of each gap. Something like this:
var totalGap = $(window).height() - $("#wrapper").outerHeight();
var gap = totalGap / 2; //2 is the number of your gaps
$(".empty_gap").css("height", gap);
Place this code in event for when the page finished loading. However, if your app supports rotation, then you'll have to place this code in the $(window).resize() event.
I suggest to use flexbox layout, for the equal space above and below #content2 you will just need to set #content2 { margin: auto 0; } to make it to work.
jsFiddle
html, body {
height: 100%;
}
body {
display: flex;
flex-direction: column;
margin: 0;
}
#content2 {
margin: auto 0;
}
<div id="header">header</div>
<div id="content1">content1</div>
<div id="content2">content2</div>
<div id="footer">footer</div>
If you do not need to support old browsers, it can be a job for flexible boxes.
Have a look here for browsers support (94.22% support when used jointly the with -webkit prefixed version).
You just have to set your container element to display: flex; with flex-direction: column; then add flex-grow: 1 (or anything > 0) to the .empty_gap elements.
#container {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
}
#header, #footer {
height: 25px;
background-color: red;
}
#content1, #content2 {
height: 50px;
background-color: blue;
}
.empty_gap {
background-color: yellow;
flex-grow: 1;
}
<div id="container">
<div id="header"></div>
<div id="content1"></div>
<div class="empty_gap"></div>
<div id="content2"></div>
<div class="empty_gap"></div>
<div id="footer"></div>
</div>

css and floating divs so they dont break the grid

Ive been struggling finding the right solution to this.
say you have 6 divs all different height varying content etc.
in responsive design at a 1080p screen all 6 divs fit on one row... but when the page gets small enough it changes to 3 on each row.
problem is say the second div, is the tallest out of the 1,2,3 divs then 4 5 and 6 divs get shifted out of place
Meaning the 4th div doesnt end up in line with the first div (column wise), how would you make this happen? to ensure the content ends up in the right place no matter what.
<style>
#maincontent { width: 100%; }
.content{ width: 33%; float:left;}
</style>
<div id="maincontent">
<div class="content">a</div>
<div class="content">I have a lot more content so i'm taller</div>
<div class="content">a</div>
<div class="content">I try to float left but can't get inline with the first child</div>
<div class="content">a</div>
<div class="content">a</div>
</div>
You can use the clear property:
This property indicates which sides of an element's box(es) may not be
adjacent to an earlier floating box.
Values other than 'none' potentially introduce clearance. [...] It
is used to push the element vertically past the float.
.content:nth-child(3n + 1) {
clear: left;
}
.content {
width: 33.3%;
float: left;
}
.content:nth-child(3n + 1) {
clear: left;
}
<div id="maincontent">
<div class="content">a</div>
<div class="content">I have a lot more content so i'm taller</div>
<div class="content">a</div>
<div class="content">I try to float left but can't get inline with the first child</div>
<div class="content">a</div>
<div class="content">a</div>
</div>
Alternatively, there is the amazing flexbox:
#maincontent {
display: flex; /* Magic begins */
flex-wrap: wrap; /* Multiline */
}
.content { width: 33.3%; }
#maincontent {
display: flex; /* Magic begins */
flex-wrap: wrap; /* Multiline */
}
.content {
width: 33.3%;
}
<div id="maincontent">
<div class="content">a</div>
<div class="content">I have a lot more content so i'm taller</div>
<div class="content">a</div>
<div class="content">I try to float left but can't get inline with the first child</div>
<div class="content">a</div>
<div class="content">a</div>
</div>