Resize before switching place? - html

I have a simple setup like this :
<div id="div0">
<div id="div1">Content</div>
<div id="div2">Content</div>
</div>
The two middle divs(1,2) have width:100% and max-width:390px plus floatLeft. When resizing the browser div2 will jump a row down and when getting less then width 390 thay will both start to resize.
What I need is to resize to a min-width first and then jump down to the second line.
How do I do that?
Edit1 : example : http://jsfiddle.net/dwDZx/

Here's a responsive example of what you're asking about. I changed some widths to make it easier to follow the example and see where the numbers come from. http://jsfiddle.net/dwDZx/4/
I change background colors in the different responsive layouts to show you which section is active at which point in resizing the browser.
The only change I made to the markup was to create a "content" div inside div1 and div2. This allowed me to set a border. If I set width of div1 and div2 to 50% AND set a border, then the total width would be 50%+2px (1px left + 1px right) which would cause the floats to wrap. By putting the border on the content div, it puts the borders inside the 50% instead of outside.
CSS:
* { margin: 0; padding: 0; }
.content { border: 1px solid black; }
#div1, #div2
{
float:left;
}
#media (min-width: 801px)
{
#div1, #div2
{
width: 400px;
background: green;
}
}
#media (min-width: 400px) and (max-width: 800px)
{
#div1, #div2
{
width: 49.9%;
background: red;
}
}
#media (max-width: 399px)
{
#div1, #div2
{
float: none;
width: 100%;
}
}
EDIT: I thought about it and simplified things a bit. See http://jsfiddle.net/dwDZx/5/ The CSS changes as follows: set a max-width on the parent div to be the max width of div1+div2. Then you only need one media state: for when it's < 400px and should be on one line.
CSS:
* { margin: 0; padding: 0; }
.content { border: 1px solid black; }
#container { max-width: 800px; }
#div1, #div2
{
float:left;
width: 50%;
background: green;
}
#media (min-width: 400px) and (max-width: 800px)
{
#div1, #div2
{
background: red;
}
}
#media (max-width: 399px)
{
#div1, #div2
{
float: none;
width: 100%;
background: blue;
}
}

Related

Bootstrap - Change columns height on stack

Using Bootstrap, I have a row and 3 column divs in my html and I have the css set to give the columns a height of 100%.
HTML:
<div class="row">
<div id="one" class="col-sm-4">one</div>
<div id="two" class="col-sm-4">two</div>
<div id= "three" class="col-sm-4">three</div>
</div>
CSS:
html,body,{
height:100%;
}
.col-sm-4{
border: 1px solid black;
height: 100%;
}
#one{
background-color: red;
}
#two{
background-color: blue;
}
#three{
background-color: green;
}
When the screen get small enough and the columns stack, I want to essentially change the height of the columns from 100% to 33.3333% so they don't exceed the height of the body. Then when the screen gets big and the columns unstack I want to change their height back to 100%. How do I do this?
Use a media query specifically for Bootstrap's xs breakpoint of <768px..
html, body, .container-fluid, .row {
height:100%;
}
.col-sm-4 {
border: 1px solid black;
min-height: 100vh;
}
#one{
background-color: red;
}
#two{
background-color: blue;
}
#three{
background-color: green;
}
#media (max-width: 767px) {
.col-sm-4 {
min-height: 33.3333333%;
height: 33.3333333%;
}
}
http://www.bootply.com/uXe60OvI4I
Also, remember the .row should always be used inside a container.
Add a new class called .whatever (I used .mydivs) in your css. You don't want to be attaching media queries to bootstrap defined selectors. Add the height values to the ".mydivs" class instead.
If you attach values to a bootstrap class if you use it again later on your page it will also be changing anywhere else you end up using it.
1.) Add
<div class="row">
<div id="one" class="mydivs col-sm-4 col-xs-12">one</div>
<div id="two" class="mydivs col-sm-4 col-xs-12">two</div>
<div id="three" class="mydivs col-sm-4 col-xs-12">three</div>
</div>
2.) Then Use Breakpoints
#media (max-width: 543px) {
.mydivs {
height: 33.3%;
}
}
#media (min-width: 544px) {
.mydivs{ height: 100%; }
}
Sounds like you want to use a media query.
#media(max-width: 500px) {
.col-sm-4 {
height: 33px;
}
}
This will change the height of .col-sm-4 whenever the viewport width is < 500px;
html,
body,
{
height: 100%;
}
.col-sm-4 {
border: 1px solid black;
height: 100px;
}
#one {
background-color: red;
}
#two {
background-color: blue;
}
#three {
background-color: green;
}
#media(max-width: 500px) {
.col-sm-4 {
height: 33px;
}
}
<div class="row">
<div id="one" class="col-sm-4">one</div>
<div id="two" class="col-sm-4">two</div>
<div id="three" class="col-sm-4">three</div>
</div>
(note that this is really hard to see here because this website has a fixed min width)

CSS - Media Queries not working properly

I'm new to media queries, and I've watched a few tutorials on the best practices, but it seems i can't get mine to work..
I created a simple text div to make sure it even works, and I'm trying to have the background-color of the div change to blue once the width of the browser is smaller than 500px.
Does anybody know what I'm missing?
#text_box {
height: 100px;
width: 100px;
background-color: red;
}
#media screen and (max-width: 500px) {
#test_box {
height: 100px;
width: 100px;
background-color: blue;
}
}
<div id="text_box">Test</div>
Here is my demo
you have a typo inside your media query in your id,it is not test_box, but text_box.
plus you don't need to repeat properties already set before, if they have the same value.
#text_box {
height: 100px;
width: 100px;
background-color: red;
}
#media screen and (max-width: 500px) {
#text_box {
background-color: blue;
}
}
<div id="text_box">Test</div>

Media Query: Doesn't Change Style when i resize the browser

I have this code . When i resize the browser to min-width: 480px it doesn't change the background color to blue and width to 100px
this is my code so far:
#media screen and (min-width: 480px) {
.boxcontainer {
background-color: blue;
width: 100px;
}
}
.boxcontainer{
width: 1300px;
background-color: green;
height: 200px;
}
<div class="boxcontainer">
</div>
Thank you
Switch the order of the CSS rule, Change your CSS into
.boxcontainer {
width: 1300px;
background-color: green;
height: 200px;
}
#media screen and (min-width: 480px) {
.boxcontainer {
background-color: blue;
width: 100px;
}
}
As in this JSFiddle example. the background is blue as long as the width is not less than 480px, otherwise it turns green.
IF by any chance you meant to do the opposite, because .boxcontainer{width:1300px} makes me think you want that , then just change the media query break point to #media screen and (max-width: 480px) instead of #media screen and (min-width: 480px).
You can see the second option in this JSFiddle

CSS vary div between two width values

I am trying style a div so that its width decreases after a certain point wrt to the width of the viewport.
Here is my code:
<div data-category="budget" class="hotels-block active ">
<img src="images/budget1.jpg">
<div class="info-block">
<h2>BUDGET HOTEL 1</h2>
<p>fist line, second line, USA, third line comes over here</p>
<hr/>
BOOK NOW
</div>
</div>
CSS :
.hotels-block {
overflow: hidden;
margin-bottom: 30px;
}
.hotels-block img {
float:left;
}
.info-block {
width: 320px;
float: left;
display: inline-block;
background-color: #62bcb1;
text-align: center;
color: #FFF;
margin-bottom: -9999px;
padding-bottom: 9999px;
}
#media screen and (min-width: 961px) and (max-width: 1050px)
{
.info-block
{
width: 30%;
}
}
I want the width of the .info-block div to reduce until the viewport width hits 961px.
Currently, .info-block shrinks until 977px and then falls to the next line.
How do I prevent .info-block from going to the next line? No JS/JQuery please.
you want to use min-width and max-width properties? usually as absolutes and then set the standard width to a percentage. Also set the width in the media query to !important to supersede the pre-set arrangement.
#media screen and (min-width: 961px) and (max-width: 1050px)
{
.info-block
{
min-width:200px;
width: 30% !important;
max-width:400px;
}
}

CSS Div Block re-arranging for multiple screens

So what I'm trying to do is design a page that has two sidebars and a main article area. As it shrinks down for tablet or phone use though, I want the two sidebars to move so they are stacked vertically beside the main area at first, and then eventually both move below it. The Problem I am having is I can't get them to re-order correctly upon doing so.
The three sections on the left (desktop/tablet/phone) is how I want it. The pages on the right are how it's appearing now. Basically I need 5 to move up under 4, and 3 to move beside 4/5 for tablet view, and then 4 to move down under 3 for phone view.
I hope that makes sense? I'm using Dreamweaver if it helps/matters. Thanks!
Clicky to see what I mean!
It can be done if certain heights are known.
Fiddle: http://jsfiddle.net/BramVanroy/Pt7sS/
The height of #contentWrap and #sideRight need to be known to make this work. If these are not known, you can fetch the heights through jQuery.
html, body, #header1, #header2, #contentWrap {
width: 100%
}
#header1 {
background: red;
height: 50px
}
#header2 {
background: green;
height: 40px
}
#sideLeft, #main, #sideRight {
box-sizing: border-box;
float: left;
height: 200px
}
#sideLeft, #sideRight {
width: 20%;
background: #333
}
#main {
background: grey;
width: 60%
}
#media screen and (max-width: 768px) {
#main {
float: right;
width: 65%
}
#sideRight {
clear: left
}
#sideRight, #sideLeft {
width: 35%;
height: 100px
}
}
#media screen and (max-width: 480px) {
#sideLeft, #main, #sideRight {
float: none;
width: 100%;
position: absolute
}
#contentWrap {
position: relative;
height: 400px
}
#main {
top: 0
}
#sideLeft {
bottom: 100px
}
#sideRight {
bottom: 0
}
}
​