Responsive Design, how to change order from div containers? - css

I am just starting with responsive webdesign. I have a 2 column layout (sidebar and content).
<div class="main-container">
<div class="main wrapper clearfix">
<div class="con1">
<header>
<h1>container 1 h1</h1>
<p>text1</p>
</header>
<section>
<h2>overview section h2</h2>
<p> test</p>
</section>
</div>
<div class="con2">
<header>
<h1>container 2 article header h1</h1>
<p></p>
</header>
<section>
<h2>article section h2</h2>
<p>text</p>
</section>
</div>
<aside>
<h3>aside</h3>
<p>text</p>
</aside>
</div> <!-- #main -->
</div> <!-- #main-container -->
The content got 2 div container. On a small screen I want
Div1
Div2
On a large screen I want them swapped,
Div2
Div1
In my CSS I now got the following Media Query:
#media only screen and (min-width: 768px) {
.main div.con1 {
float: right;
width: 57%;
}
.main div.con2 {
float: right;
width: 57%;
}
Is it possible to swap the order around and if so, how can I do it? Anyone got maybe a link to a good tutorial?
Thanks a lot in advance !

The method used in the StackOverflow question "CSS positioning div above another div when not in that order in the HTML" seems to be your best bet.

with js:
//on load
responsive_change_box_order();
//on resize
window.addEventListener('resize', responsive_change_box_order );
function responsive_change_box_order() {
if (window.matchMedia("(max-width: 1118px)").matches) {
jQuery("#block-menu-block-1").remove().insertBefore(jQuery("#content"));
}
else{
jQuery("#block-menu-block-1").remove().prependTo(jQuery(".region-sidebar-second .region-inner"));
}
}

This snippet uses flexbox and related properties to meet your end requirement.Also kindly note that you use use appropriate vendor prefixes for flexbox when you implement or use something like autoprefixer or prefixfree.
.main{
display:flex;
flex-direction:column
}
.con1{
order:2;
background-color: green;
}
.con2{
order:1;
background-color: red;
}
/*For Large screen only*/
#media(min-width:1200px){
.con1{
order:1;
}
.con2{
order:2;
}
}
<div class="main-container">
<div class="main wrapper clearfix">
<div class="con1">
<header>
<h1>container 1 h1</h1>
<p>text1</p>
</header>
<section>
<h2>overview section h2</h2>
<p> test</p>
</section>
</div>
<div class="con2">
<header>
<h1>container 2 article header h1</h1>
<p></p>
</header>
<section>
<h2>article section h2</h2>
<p>text</p>
</section>
</div>
<aside>
<h3>aside</h3>
<p>text</p>
</aside>
</div>
<!-- #main -->
</div>
<!-- #main-container -->

Related

Difficulties in stacking elements with screen size changes

I am having difficulties with stacking my divs alongside one another when the screen size is smaller, then stack on top of one another once I hit phone size.
I am using Foldy Grids by Paravel, On full desktop size, there are 3 divs that are side by side, but I want this to shrink to 2 side by side and then on phone, just 1 on top of one another. However I feel as though I am applying the media queries to the incorrect <div>'s. Any guidance would be much appreciated. Within the CSS the reason I put 33% in is due to the divs being divided into 3 on full desktop mode.
CSS:
#media screen and (max-width: 900px) {
.dashboardIconsMod {
width: 50%;
}
}
#media screen and (max-width: 600px) {
.dashboardIconsMod {
width: 100%;
}
}
.dashboardIconsMod {
color: pink;
border-style: solid;
border-color: red;
}
HTML:
<section id="content">
<div class="container">
<section id="grid" class="clearfix">
<div class="cf show-grid">
<div class="row">
<div class="grid-2 dashboardIconsMod">
<!-- 1st Div -->
<p> 1st Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 2nd Div -->
<p> 2nd Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 3rd Div -->
<p> 3rd Grid </p>
</div>
</div>
<div class="row">
<div class="grid-2 dashboardIconsMod">
<!-- 4th Div -->
<p> 4th Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 5th Div -->
<p> 5th Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 6th Div -->
<p> 6th Grid </p>
</div>
</div>
</div>
</section>
</div>
</section>
I am not sure about Foldy Grids by Paravel as never worked with it and don't have much time to read documentation, but you can fix it by giving different size on media query, just make sure your media query need to be below to your desktop code.
For now, 3 items on the desktop, on smaller screens like iPad there are 2 items and on the smaller screen below 600px, there is a single item.
.dashboardIconsMod {
color: pink;
border-style: solid;
border-color: red;
width:33.33%;
}
* {box-sizing: border-box;}
.show-grid .row {display:flex; flex-wrap: wrap;}
#media screen and (max-width: 900px) {
.dashboardIconsMod {
width: 50%;
}
}
#media screen and (max-width: 600px) {
.dashboardIconsMod {
width: 100%;
}
}
<section id="content">
<div class="container">
<section id="grid" class="clearfix">
<div class="cf show-grid">
<div class="row">
<div class="grid-2 dashboardIconsMod">
<!-- 1st Div -->
<p> 1st Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 2nd Div -->
<p> 2nd Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 3rd Div -->
<p> 3rd Grid </p>
</div>
</div>
<div class="row">
<div class="grid-2 dashboardIconsMod">
<!-- 4th Div -->
<p> 4th Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 5th Div -->
<p> 5th Grid </p>
</div>
<div class="grid-2 dashboardIconsMod">
<!-- 6th Div -->
<p> 6th Grid </p>
</div>
</div>
</div>
</section>
</div>
</section>

How to get three div in one line

I would like to align three div in one line with a little space between first div and second div and last div using bootstrap as you see in the picture :
I try with this code :
<div class="row">
<div class="col-md-2">
<img src="img/emo_positif.png')}}">
</div>
<div class="col-md-7">
<div class="square1"></div>
</div>
<div class="col-md-3">
<img src="img/emo_negative.png')}}">
</div>
</div>
but it shows me a big space between the div
Using Bootstrap 3:
.row {
height: 24px;
}
.row > div {
height: 100%;
}
.square {
background: pink;
}
.square1 {
background: #01a8ff;
height: 100%;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" >
<div class="row">
<div class="col-xs-2 square">
</div>
<div class="col-xs-8">
<div class="square1"></div>
</div>
<div class="col-xs-2 square">
</div>
</div>
Check this Pen.
Read the docs.
For making the three division in same line . There are many ways. For better UX use display:flex in css for the parent division
Thanks

Non-wrapping CSS Flex with fixed widths

I'm building a Carousel-type component, but am having some difficulty getting it to work just right.
My basic approach is a div (wrapper) with lots of other divs (items) in it. I want to display 4 items on the carousel at any one time. The items have various content heights, but the heights of the items should be equal (to the largest required).
I can't work out the CSS combination I need to get this to work correctly.
With this setup (HTML + CSS at bottom of post), the width: 25%; on each item-container is ignored.
If I add a fixed with to .item, then the 25% kicks in, but the item width is unknown -- it depends on the browsers size. Setting it to 1000px means you lose content from the item. Setting it to ~210px works, but when you start shrinking your browser, you lose content. On a large browser, you have excessive spacing.
Curiously, if I add flex-wrap: wrap to the CSS, then the 25% width is applied correctly -- but I can't do that, because then it's not a carousel! Example
The scenario is simple:
An unknown amount of items in a div with overflow: auto, which are equal heights should be displayed, with 4 of the children divs on the screen at any one time.
My HTML is structured as follows:
<div id="container">
<div id="wrapper">
<div class="item-container">
<div class="item">
<p>
Carousel Item #1 with some quite long text.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #2.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #3.
</p>
</div>
</div>
...
</div>
</div>
My CSS:
#container {
width: 100%;
background: #0f0;
overflow: auto;
}
#wrapper {
display: flex;
}
.item-container {
border: 1px solid #f00;
width: 25%;
}
Note, this is my MCVE. On my real component, I have buttons for scrolling left and right, the content is significantly more complex and stuff like that.
All you need is to add flex: 0 0 auto to .item-container elements.
* {
box-sizing: border-box;
}
#container {
width: 100%;
background: #0f0;
overflow: auto;
}
#wrapper {
display: flex;
}
.item-container {
border: 1px solid #f00;
flex: 0 0 auto;
width: 25%;
}
<div id="container">
<div id="wrapper">
<div class="item-container">
<div class="item">
<p>
Carousel Item #1 with some quite long text.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #2.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #3.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #4.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #5.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #6.
</p>
</div>
</div>
<div class="item-container">
<div class="item">
<p>
Carousel Item #7.
</p>
</div>
</div>
</div>
</div>

How to clear z-index rule in the last div?

I have the following problem: I'm working on this site and using a picture as a background of 1 div. Inside this div I have another 2 dives - logo and navigation.
Last /outside of the div with the picture/ is the footer, whos background must be other color, not the picture. I set the css to the div with the picture like this :
#background{
position: absolute;
z-index: -1;
display: block;
}
Here is the html :
<div id="pic">
<img id="background" src="picture.jpg">
<div class="row" id="logo">
</div>
<div class="row" id="example-menu">
</div>
</div>
<div class="row" id="footer">
<div class="medium-3 columns medium-offset-1" id="worktime">РАБОТНО ВРЕМЕ: </br> ПОНЕДЕЛНИК-ПЕТЪК </br> 08:30-19:00ч.</div>
<div class="medium-8 columns"></div>
</div>
But the problem is that the footer also goes over the div with the picture and my design is not like this... I need to keep the whole picture to be seen.
Thanks in advance !
the issue is with the z-index of the div containing the image and the div containing the footer.
the easiest solution is to put the footer after the div with your content. The only time z-index should come into play is if you are trying to put items on top of the image for effect.
<div id="content-wrapper">
<div id="nav">
<span>NAV</span>
</div>
</div>
<div id="footer">
<span>FOOTER</span>
</div>
you can easily add the footer in the container, attach at the bottom as well.
see if this example helps:
https://plnkr.co/edit/GDfzul2qKApw6JGhQSbb?p=preview
Option 1
Instead of using <img>, put the background using CSS
HTML
<div id="pic">
<div class="row" id="logo"></div>
<div class="row" id="example-menu"></div>
</div>
<div class="row" id="footer">
The footer content
</div>
CSS
#pic{
background: url( 'picture.jpg' ) no-repeat;
}
Option 2
Use fixed height for the parent div, #pic in this case:
HTML
<div id="pic">
<img id="background" src="picture.jpg">
<div class="row" id="logo"></div>
<div class="row" id="example-menu"></div>
</div>
<div class="row" id="footer">
The footer content
</div>
CSS
#pic{
position: relative;
height: 100px; /* This should be the height of the background image */
}
#background{
position: absolute;
z-index: -1;
}
Option 3
You may use position:absolute for the .rows instead:
#pic{ position: relative; }
.row{ position: absolute; top: 0; z-index: 99; }
<div id="pic">
<img id="background" src="https://placehold.it/200x100">
<div class="row" id="logo">This is logo</div>
</div>
<div id="footer">
The footer content
</div>

even odd selection from nested divs

I am trying to select every other div with a class name. the issue is there are all in different parent div's. I've tried many things with sibling selection but have not yet found a solution. This is what I am looking for:
Add a margin of 30px to ever even div with the class name article
<div class="wrapper">
<div class="section">
<div class="article"><!--No Margin here-->
</div>
</div>
<div class="section">
<div class="article"><!--Add Margin here-->
</div>
</div>
<div class="section">
<div class="article"><!--No Margin here-->
</div>
</div>
<div class="section">
<div class="article"><!--Add Margin here-->
</div>
</div>
</div>
I tried something like this but did not work:
.section > .article:nth-child(even){
margin-right: 30px;
}
Rather than select the even/odd .article elements, you need to select the even/odd .section elements.
.section:nth-child(even) > .article
{
/* Your css here */
}
Fiddle: http://jsfiddle.net/jakelauer/4PMbS/

Resources