I have 6 Divs within a container Div. These divs have to be aligned a certain way for a clients needs. I've taken the basic layout for the customer website I'm working on and created a sample to be posted here. I have a regular CSS file and a mobile CSS file. When in mobile view the Divs will be aligned in sequential order 1-6. When in the regular view, Divs 1, 3, 4, and 5 will be aligned to the right. Divs 2 and 6 will be aligned to the left. I need div 6 to hug the bottom of div 2 with the exception of about 10px. With my current code the Divs have a huge gap between them and 6 gets pushed further down the page with the more content the right side Divs have. All of these Divs have dynamic content that can make them increase or decrease in the amount of shown content. How can I get Div 6 to ignore the right side Divs and hug Div 2 without screwing up how these condense and appear in mobile? (You can just resize your browser window for the mobile version to appear with this code.)
Here is my HTML and CSS:
/*
exampleCSS.css
*/
#containerDiv {
width: 500px;
margin-left: auto;
margin-right: auto;
padding: 10px;
}
#div1 {
border: 1px solid red;
background-color: red;
margin-bottom: 10px;
width: 25%;
float: right;
}
#div2 {
border: 1px solid blue;
background-color: blue;
margin-bottom: 10px;
width: 65%;
float: left;
}
#div3 {
border: 1px solid green;
background-color: green;
margin-bottom: 10px;
width: 25%;
float: right;
}
#div4 {
border: 1px solid orange;
background-color: orange;
margin-bottom: 10px;
width: 25%;
float: right;
clear: right;
}
#div5 {
border: 1px solid purple;
background-color: purple;
margin-bottom: 10px;
width: 25%;
float: right;
clear: right;
}
#div6 {
border: 1px solid pink;
background-color: pink;
margin-bottom: 10px;
width: 65%;
float: left;
}
/*
exampleCSSMobile.css
*/
#containerDiv {
width: 100%;
}
#div1 {
border: 1px solid red;
background-color: red;
margin-bottom: 10px;
width: 100%;
}
#div2 {
border: 1px solid blue;
background-color: blue;
margin-bottom: 10px;
width: 100%;
}
#div3 {
border: 1px solid green;
background-color: green;
margin-bottom: 10px;
width: 100%;
}
#div4 {
border: 1px solid orange;
background-color: orange;
margin-bottom: 10px;
width: 100%;
}
#div5 {
border: 1px solid purple;
background-color: purple;
margin-bottom: 10px;
width: 100%;
}
#div6 {
border: 1px solid pink;
background-color: pink;
margin-bottom: 10px;
width: 100%;
}
<!DOCTYPE html>
<html>
<head>
<title>Example HTML</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="exampleCSSMobile.css" media="(max-width: 940px)" rel="stylesheet" type="text/css" />
<link href="exampleCSS.css" media="(min-width: 940px)" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="containerDiv">
<div id="div1"><p>Test 1 Lorem Ipsum.</p></div>
<div id="div2"><p>Test 2 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
<div id="div3"><p>Test 3 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. </p></div>
<div id="div4"><p>Test 4 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
<div id="div5"><p>Test 5 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
<div id="div6"><p>Test 6 Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim. Lorem Ipsum. Prion gravida nibh vel velit auctor
aliquet. Aenean solicitudin, lorem quis bibendum
auctor, nisi elit consquat ipsum, nec sagittas sem
nibh id elit. Duis sed odio sit amet nibh vulputate
cursus a sit neque. Suspendisse in orci enim.</p></div>
</div>
</body>
</html>
Image of the example with the space between 2 and 6:
Another simple way to do this is to simply add float: left , which floats the div next of elements near it. A float also makes it so a div is only as wide as it's content (Unless otherwise specified).
Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.
HTML | <div> align Attribute left: It sets the content to the left-align. right: It sets the content to the right-align. center: I sets the div element to the center. By default, it is set to center.
A floated element will move as far to the left or right as it can in the position where it was originally(this is important #1).
So put in this way:
We have 2 div
<div class="div5">div5</div>
<div class="div6">div6</div>
.div-blue{
width:100px;
height:100px;
background: blue;
}
.div-red{
width:50px;
height:50px;
background: red;
}
without float
they'll be one below the other
If we float: right
the div5
, the div6
be positioned on the line when it was the div5
,
/*the lines are just for illustrate*/
So if now we float: left
the div6
it will move as far to the left as it can, "in this line" (see #1 above), so if div5
change its line, div6
will follow it.
Now let's add other div into the equation
<div class="div4">div4</div>
<div class="div5">div5</div>
<div class="div6">div6</div>
.div-gree{
width:150px;
height:150px;
background: green;
float:right;
}
We have this
If we set clear: right
to the div5
, we are force it to take the line bellow div4
So there you have why happens. Here the jsfiddle where I code this
NOW, HOW TO FIX IT
Just remove the float for the <div id="div6">
and set display: inline-block
like this:
#div6 {
border: 1px solid pink;
background-color: pink;
margin-bottom: 10px;
width: 65%;
/*float: left;*/ /*removed*/
display: inline-block; /*added*/
}
This will keep the normal behavior for elements without float
(The elements after the floating element will flow around it.). The display: inline-block
is for your case to maintain the margin
of div2
.
HERE A JSFIDDLE EXAMPLE WORKING FOR YOUR CASE
I hope this helps now and in the future :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With