I have three divs in a column format that have widths as a percent of the window. Once the screen gets to 1300 px or less, I want the two rightmost divs to stop getting smaller and basically overtake the leftmost div (there will be content in the right and middle divs but nothing in the left one). I made a media query that says when the screen gets to 1300px, the left div's width becomes 0, and the middle and right have fixed widths and float right. However, when I do this, the middle and right divs always switch places so that the middle one is all the way to the right and the right one jumps to the left of the middle. How do I stop the two divs from switching places?
I have tried changing the display of the divs to block or inline but nothing has worked.
.column {
float: left;
padding: 10px;
height: 600px;
display: inline-block;
}
.left {
width: 20%;
background-color: red;
}
.middle {
width:60%;
background-color: orange;
}
.right {
width:20%;
background-color: yellow;
}
#media screen and (max-width: 1300px) {
.left {
width: 0px;
}
.middle {
width:765px;
float: right;
}
.right {
width: 255px;
float: right;
}
}
<div class="columns">
<div class="column left"></div>
<div class="column middle">
<h3>Lorem Ipsum </h3>
</div>
<div class="column right">
<div class="form-box"></div>
</div>
</div>
After the window becomes less than 1300px, I just want the middle and right columns to float right with the middle one to the left of the right one, but instead they always switch places.
You will be better suited using flexbox to do what you are trying to do. Also, you used the CSS class .column instead of the .columns definition you have in your css.
Try this:
.columns {
padding: 10px;
height: 600px;
display: flex;
flex-direction: row;
/*flex-wrap: wrap;*/
}
.left {
flex: 1 1 auto;
width: 20%;
background-color: red;
}
.middle {
flex: 1 1 auto;
width: 60%;
background-color: orange;
}
.right {
flex: 1 1 auto;
width: 20%;
background-color: yellow;
}
#media screen and (max-width: 1300px) {
.left {
display: none;
}
.middle {
width: 500px;
flex: 1 0 auto;
}
.right {
width: 500px;
flex: 1 0 auto;
}
}
/*#media screen and (max-width: 650px) {
.middle {
width: 100%;
}
.right {
width: 100%;
}
}*/
<div class="columns">
<div class="column left"></div>
<div class="column middle">
<h3>Lorem Ipsum </h3>
</div>
<div class="column right">
<div class="form-box"></div>
</div>
</div>
Related
I have divs that i want to wrap to the next line when the browser window gets smaller. I also want margin to be put in between the divs so that there's a gap between them. The problem I'm having is that the margin on the centre divs causes the divs to wrap incorrectly if the browser is set to a specific size. At a certain size you have 2 divs underneath one div. See my screenshot below as an example and this fiddle: http://jsfiddle.net/uhh2jwe2/ (change the width of the window)
This really needs to be dynamic as it will be a framework solution for laying out differently sized divs. The parent div will be fluid similar to the example. Any help would be great
#outer {
width: 90%;
height: 90%;
margin: 5%;
overflow: auto;
background-color: red;
}
.inner1 {
float: left;
width: 150px;
height: 150px;
margin-right: 20px;
background-color: blue;
}
.inner2 {
float: left;
width: 150px;
height: 150px;
margin-right: 20px;
background-color: blue;
}
.inner3 {
float: left;
width: 150px;
height: 150px;
background-color: blue;
}
<div id="outer">
<div class="inner1">1</div>
<div class="inner2">2</div>
<div class="inner3">3</div>
</div>
You can use media queries to alter the css on smaller screen.
#outer {
width: 90%;
height: 90%;
margin: 5%;
overflow: auto;
background-color: red;
}
.inner1 {
float: left;
width: 150px;
height: 150px;
margin-right: 20px;
background-color: blue;
}
.inner2 {
float: left;
width: 150px;
height: 150px;
margin-right: 20px;
background-color: blue;
}
.inner3 {
float: left;
width: 150px;
height: 150px;
background-color: blue;
}
#media (max-width: 435px) {
#outer > div {
margin-right:auto;
margin-left:auto;
margin-bottom:15px;
float:none;
}
}
<div id="outer">
<div class="inner1">1</div>
<div class="inner2">2</div>
<div class="inner3">3</div>
</div>
Use Media query like this:
#outer div:last-child {
margin-bottom: 0;
}
#media screen and (max-width:570px) {
.inner1, .inner2, .inner3 {
margin-bottom: 5px;
}
}
#media screen and (max-width:411px) {
.inner1, .inner2, .inner3 {
float: none;
margin: auto;
margin-bottom: 5px;
}
}
#outer {
width: 90%;
height: 90%;
margin: 5%;
overflow: auto;
background-color: red;
}
.inner1 {
float: left;
width: 150px;
height: 150px;
margin-right: 20px;
background-color: blue;
}
.inner2 {
float: left;
width: 150px;
height: 150px;
margin-right: 20px;
background-color: blue;
}
.inner3 {
float: left;
width: 150px;
height: 150px;
background-color: blue;
}
#outer div:last-child {
margin-bottom: 0;
}
#media screen and (max-width:570px) {
.inner1, .inner2, .inner3 {
margin-bottom: 5px;
}
}
#media screen and (max-width:411px) {
.inner1, .inner2, .inner3 {
float: none;
margin: auto;
margin-bottom: 5px;
}
}
<div id="outer">
<div class="inner1">1</div>
<div class="inner2">2</div>
<div class="inner3">3</div>
</div>
I would recommend a solution that extracts the grid-elements from the content-elements. Therefore you have a lot more control about your layout and you can be more flexible with content you want to place into it.
Use your .inner elements as grid-elements and wrap content inside them into .inner-content
Wrap all inners into a row to get rid of the outer-gutter
Give the .inner elements a percentage-width and a px-max-width. So the elments can take alwyay 33.33% of the avaiable width but never more then 150px.
I added some adjustments for small screens, so the .inner elements wrap below each other and take more then 33.33% of the .outer container width.
Inspect the code: http://jsfiddle.net/uhh2jwe2/5/
* {
box-sizing: border-box;
}
/* flexible outer container */
.outer {
width: 90%;
height: 90%;
margin: 5%;
overflow: hidden;
background-color: red;
}
/* remove outer gutter */
.row {
margin: 0 -10px;
}
/* .inner will take care of the width */
.inner {
width: 33.33%;
max-width: 150px;
float: left;
padding: 0 10px;
}
/* .inner-content take care of the height */
.inner-content {
height: 150px;
color: #fff;
background: blue;
}
#media (max-width: 435px) {
/* this wraps .inner elements below each other and extends width */
.outer .inner {
padding: 10px 0;
width: 100%;
max-width: 100%;
float:none;
}
}
<div class="outer">
<div class="row">
<div class="inner">
<div class="inner-content">1</div>
</div>
<div class="inner">
<div class="inner-content">2</div>
</div>
<div class="inner">
<div class="inner-content">3</div>
</div>
</div>
</div>
I would suggest to use bootstrap's technique for that. Have padding on both sides of your inner elements, and negate it with negative margin on the container.
This will require more markup tough. While .row and .container could be merge on the same element, the background-color would overflow to the left because of the negative margin.
.container {
background-color: green;
width: 510px;
}
.row {
font-size: 0;
margin: 0 -15px;
}
.block {
font-size: 12px;
padding: 0 15px;
display: inline-block;
}
.content {
width: 150px;
height: 150px;
background-color: red;
}
<div class="container">
<div class="row">
<div class="block">
<div class="content">
</div>
</div>
<div class="block">
<div class="content">
</div>
</div>
<div class="block">
<div class="content">
</div>
</div>
<div class="block">
<div class="content">
</div>
</div>
<div class="block">
<div class="content">
</div>
</div>
</div>
</div>
in your example, the first two divs are 170px wide (150+20), and the third is 150px wide because it doesn't have a margin, thats the problem.
avoid #media if you mant it to be fully responsive and not jumping from 4 items a line to 1 item a linefor example.
you can solve your issue by simply adding a margin-right:20 to your last element, but it is better to to like so :
.inner1, .inner2, .inner3{
float: left;
width: 150px;
height: 150px;
margin: 2px 10px; //left & right sides to half of 20px
background-color: blue;
}
because it will split the margin to the two sides, making it more symetrical.
For laying out differently sized divs.
if all your divs can change size but stay equal, it will work, but if the first div is 70 and the 2nd and 3rd are 50, there will always be two divs on the bottom line at some point.
I think I've found the simplest solution to what I'm trying to do without having to use media queries. I simply added the right margin to all fields including the last field rather than adding it to every field except the final field.
I then wrap all the fields in another div and add a minus margin (the same size as the gaps) so that the fields will wrap when they hit the side of the container. Here's a fiddle with the solution: http://jsfiddle.net/rahg1ky3/
#outer {
width: 90%;
height: 90%;
margin: 5%;
overflow: auto;
background-color: red;
}
#inner {
margin-right: -20px;
}
.cont {
float: left;
width: 150px;
height: 150px;
margin-right: 20px;
background-color: blue;
}
<div id="outer">
<div id = "inner">
<div class="cont">1</div>
<div class="cont">2</div>
<div class="cont">3</div>
</div>
</div>
JSFiddle
HTML:
<div class="leftwrapper">
<div class="left">left div</div>
<div class="middle">middle div</div>
</div>
<div class="right">right div</div>
CSS:
.leftwrapper{
float: left;
width: 75%;
}
.left{
float:left;
background:green;
width: 30%;
max-width: 75px;
}
.middle{
background: blue;
overflow: hidden;
}
.right{
float: right;
background:red;
width: 30%;
max-width: 75px;
}
In the above example, I would like to:
Keep all three divs on the same line when resizing the browser (currently, the right div moves beneath the left),
Make it so the blue/middle div resizes to fill the space between left and right. As it stands, when you expand the browser the margin grows. How do you keep it consistent?
As a note: the structure of the HTML divs is intentional ('leftwrapper' containing two divs, floating alongside the right div), so please no answers involving restructuring of the divs!
I would add a flex parent, and use flex-grow to make the elements grow to fill the available space where you want that to happen.
.flex {
display: flex;
}
.grow {
flex-grow: 1;
}
.middle {
background: blue;
}
.left {
background: green;
width: 30%;
max-width: 75px;
}
.right {
background: red;
width: 30%;
max-width: 75px;
}
<div class="flex">
<div class="leftwrapper flex grow">
<div class="left">left div</div>
<div class="middle grow">middle div</div>
</div>
<div class="right">right div</div>
</div>
Change your CSS to this and it'll work nicely.
.leftwrapper {
float: left;
width: 100%;
max-width: calc(100% - 75px);
}
.middle {
background: blue;
overflow: hidden;
}
.left {
float: left;
background: green;
width: 30%;
max-width: 75px;
}
.right {
float: left;
background: red;
width: 30%;
max-width: 75px;
}
What I've done is change the width of .leftwrapper to 100% but added in a max-width property and used calc() to ensure it's also 75px smaller than 100% of the space it can fill up. That way resizing the viewport ensures it stays on one line nicely.
MDN information about calc().
#j.winslow I just updated the JSFIDDLE http://jsfiddle.net/enRkR/1232/
I add this:
.leftwrapper{
float: left;
width: 100%;
max-width: calc(100% - 75px);
}
This is what you're looking for? Regards!
I want to achieve the following: A design with two columns of the same width, covering the whole width of the document, or a fixed width of pixels, whichever is smaller. When they are resized to a certain width, they should be moved underneath each other and take up at most 100% of the width of the screen (exactly 100% would be nice, but is not necessary).
I came up with the following code, but the max-width of 100% does not get applied.
Can i combine a width in percent and a max-width in percent like this?
Is this possible without another layer of divs?
Is this possible at all?
#head {
background-color: #00FF00;
}
body {
text-align: center;
}
#container {
margin: 0 auto;
width: 100%;
max-width: 500px;
text-align: center;
}
#left {
background-color: #FF0000;
float: left;
width: 50%;
min-width: 150px;
max-width: 100%;
}
#right {
background-color: #0000FF;
float: left;
width: 50%;
min-width: 150px;
max-width: 100%;
}
<div id="head">
foo
</div>
<div id="container">
<div id="left">
bar
</div>
<div id="right">
baz
</div>
</div>
I think you need media queries. You can set the width for different viewports.
#head {
background-color:#00FF00;
}
body {
text-align:center;
}
#container{
margin: 0 auto;
width:100%;
max-width:500px;
text-align:center;
}
#left {
background-color:#FF0000;
float: left;
width: 50%;
min-width: 150px;
max-width: 100%;
}
#right {
background-color:#0000FF;
float: left;
width: 50%;
min-width: 150px;
max-width: 100%;
}
#media (max-width: 500px) {
#left { width: 100%;}
#right { width: 100%;}
}
<html>
<head>
</head>
<body>
<div id="head">
foo
</div>
<div id="container">
<div id="left">
bar
</div>
<div id="right">
baz
</div>
</div>
</body>
</html>
you may use flex and min-width: http://codepen.io/anon/pen/JXNMgd
/* added */
#container {
display: flex;
flex-wrap: wrap;
}
#left,
#right {
flex: 1;
}
/* your css cleared a bit */
#head {
background-color: #00FF00;
}
body {
text-align: center;
}
#container {
margin: 0 auto;
max-width: 500px;
text-align: center;
}
#left,
#right {
background-color: #FF0000;
min-width: 150px;
}
#right {
background-color: #0000FF;
}
<div id="head">
foo
</div>
<div id="container">
<div id="left">
bar
</div>
<div id="right">
baz
</div>
</div>
Essentially I am looking for this:
Two Divs next to each other, that then stack with responsive change
However, the difficulty lies in that I need Div #2 (the right div) to stack on top of Div #1 (the left div), instead of being pushed below it.
What I have to put the divs side by side is as follows:
<div class="container" style="width: 1100px">
<div class="left-div" style="float: left; width: 700px;"> </div>
<div style="float: left; width: 300px;"> </div>
</div>
What I have tried is doing a css media query that simply pushes the left div down 300px, using the following:
#media screen and (max-width: 1100px) {
.left-div {
position: relative;
top: 350px;
}
}
However, this doesn't cause the right div to snap to the left and stack on top.. Any ideas?
Use float:right and change the order of your html.
.right-div {
float: right;
width: 30%;
background-color: red;
}
.left-div {
float: right;
width: 70%;
}
#media screen and (max-width: 1100px) {
.container > div {
width: 100%;
}
}
<div class="container" style="max-width: 1100px;">
<div class="right-div">RIGHT DIV</div>
<div class="left-div">LEFT DIV</div>
</div>
Try this https://jsfiddle.net/1aj7wvyz/
HTML
<div class="container">
<div class="divs div-2">Div 2 </div>
<div class="divs div-1">DIV 1 </div>
</div>
CSS
.divs {
display: inline-block;//<-Here you only need this
padding: 100px;
border: 1px solid black;
margin: 20px;
}
.div-2 {
float: right:
}
.div-1 {
float: left;
}
#media screen and (max-width: 400px) { //You will change width of course
.divs {
display: block;
margin: 0 auto;
}
}
I have this code:
#media screen and (max-width: 600px) {
.col {
max-width: 150px;
}
}
.wrapper {
max-width: 500px;
margin: 0 auto;
background: grey;
font-size: 0;
}
.col {
width: 200px;
margin-right: 100px;
display: inline-block;
}
.col:last-child {
margin-right: 0px;
}
img {
max-width: 100%;
}
<section class="wrapper clearfix">
<section class="col">
<img src="http://placehold.it/200x120" alt="">
</section>
<section class="col">
<img src="http://placehold.it/200x120" alt="">
</section>
</section>
DEMO
The container won't wrap around its elements when media query gets activated. The same thing happens with floated children (which is normal, I guess).
Demo
one option is to add an "inner" wrapper; just a wrapper within your wrapper. you can make it display inline-block and set text-align center on the parent wrapper. Finally, remove the grey background from the wrapper and apply to the inner wrapper.
just one drawback is that when you make the window really small the .col divs are not lined up on the left. not sure if that's an issue for you
html
<section class="wrapper clearfix">
<div class='inner-wrap'>
<section class="col">
<img src="http://placehold.it/200x120" alt="">
</section>
<section class="col">
<img src="http://placehold.it/200x120" alt="">
</section>
</div>
</section>
css
#media screen and (max-width: 600px) {
.col {
max-width: 150px;
}
}
.inner-wrap {
display: inline-block;
background: grey;
}
.wrapper {
text-align: center;
max-width: 500px;
margin: 0 auto;
font-size: 0;
}
.col {
width: 200px;
margin-right: 100px;
display: inline-block;
}
.col:last-child {
margin-right: 0px;
}
img {
max-width: 100%;
}