Divs don't float how I would - html

I've 5 divs in a single div, that I would make it floats 2 at left and 3 at right:
.container {
height: 400px;
border: 1px solid black;
}
.first, .second, .third, .fourth, .fifth {
width: 50%;
}
.first {
height: 300px;
background-color: red;
float: left;
}
.second {
height: 100px;
background-color: blue;
float: left;
}
.third {
height: 100px;
background-color: green;
float: right;
}
.fourth {
height: 200px;
background-color: yellow;
float: right;
}
.fifth {
height: 100px;
background-color: aquamarine;
float: right;
}
<div class="container">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>
I would like that they were placed:
FIRST and SECOND at left
THIRD, FORTH and FIFTH at right.
Instead, they are placed like:
FIRST and FIFTH at left
SECOND, THIRD and FOURTH at right.
Do you know how fix it? Here there is my code: https://jsfiddle.net/82bkdbpn/

Since you have defined fixed height on container element you can use Flexbox to do this and define flex-direction: column.
.container {
height: 400px;
border: 1px solid black;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.first, .second, .third, .fourth, .fifth {
width: 50%;
height: 100px;
}
.first {
height: 300px;
background-color: red;
}
.fourth {
height: 200px;
background-color: yellow;
}
.second {background-color: blue;}
.third {background-color: green;}
.fifth {background-color: aquamarine;}
<div class="container">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>

You can wrap the divs in Columns, and float these two. Or you can use flexbox, there is a very good answer from Nenad Vracar about this.
Here is an example with two column wrapper div elements:
.container {
height: 400px;
border: 1px solid black;
}
.col {
width: 50%;
float: left;
}
.first {
height: 300px;
background-color: red;
}
.second {
height: 100px;
background-color: blue;
}
.third {
height: 100px;
background-color: green;
}
.fourth {
height: 200px;
background-color: yellow;
}
.fifth {
height: 100px;
background-color: aquamarine;
}
<div class="container">
<div class="col">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
</div>
<div class="col">
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>
</div>

Related

HTML - container is positioned in the same row as its brother

I want to implement a div which contains 2 divs:
A row with two sides, left and right.
A div below the first one
For that I am doing as follows:
<div id="main">
<div id="box1">
<div id="left" />
<div id="right" />
</div>
<div id="box2" />
</div>
And this is the css
.main {
margin-top: 75px;
}
#box1 {
display: flex;
flex-direction: row;
width: 100%;
height: 450px;
transition: ease all 0.5s;
}
#box1 #left {
width: 50%;
height: 100%;
background-color: lime;
}
#box1 #right {
width: 50%;
height: 100%;
background-color: red;
}
#box2 {
width: 100%;
height: 600px;
background-color: gold;
}
But for some reasons, I am getting the second div "box2" in the same row of "box1", next to it.
Why is that?
this seems to work (check full page view):
.left {
background: red;
width: 50%;
float: left;
}
.right {
background: green;
position: relative;
float: left;
width: 50%
}
.bottom {
background: blue;
position: relative;
width: 100%;
top: 9vh;
float: none;
}
<div id = "main">
<div id = "box2" class = "bottom" > bottom </div>
<div id = "box1">
<div class = "left"> left </div>
<div class = "right"> right </div>
</div>
</div>
You just missed the closing </div> tags
here is when it fixed, working perfectly fine
.main {
margin-top: 75px;
}
#box1 {
display: flex;
flex-direction: row;
width: 100%;
height: 450px;
transition: ease all 0.5s;
}
#box1 #left {
width: 50%;
height: 100%;
background-color: lime;
}
#box1 #right {
width: 50%;
height: 100%;
background-color: red;
}
#box2 {
width: 100%;
height: 600px;
background-color: gold;
}
<div id="main">
<div id="box1">
<div id="left">Left Contents Here</div>
<div id="right">Right Contents Here</div>
</div>
<div id="box2">Belowe Contents Here</div>
</div>

Make inner div width dynamic

I want to give remaining width to .inner div. At the Same time it's siblings (a & span tags) can be of dynamic width.
Any idea?
Code below & at https://jsfiddle.net/pge8rqw0/
.top {} .inner {
border-bottom: 1px solid black;
background-color: green;
width: auto;
float: left;
width: 50px;
}
a {
float: left;
}
span {
float: right;
background-color: red;
}
<div class="top">
Visit W3Schools.com!
<div class="inner"></div>
<span>Html is good</span>
</div>
Thanks
Solution using display: flex.
.top {
display: flex;
}
.inner {
border-bottom: 1px solid yellow;
background-color: green;
min-width: 50px;
flex: 1;
}
a {
background-color: #f5f5f5;
}
span {
background-color: red;
}
<div class="top">
Visit W3Schools.com!
<div class="inner"></div>
<span>Html is good</span>
</div>
If you can change the order of elements in your code then you can achieve it without flex as follows:
.top {overflow: hidden;}
a {
float: left;
}
span {
float: right;
background-color: red;
}
.inner {
border-bottom: 1px solid black;
background-color: green;
overflow: hidden;
}
<div class="top">
Visit W3Schools.com!
<span>Html is good</span>
<div class="inner">Inner Content</div>
</div>

Align divs horizontally to the center

I've got this short code:
#div1 div {
margin: 0% 0,5%;
display: inline-block;
color: white;
border: 1px dotted yellow;
align: center;
}
#div1 {
margin: 0px auto;
width: 620px;
height: 100px;
background-color: black;
overflow: hidden;
text-align: center;
}
#div2, #div10 {
width: 21px;
height: 100px;
}
#div3, #div9 {
width: 60px;
height: 60px;
}
#div4, #div8 {
width: 70px;
height: 70px;
}
#div5, #div7 {
width: 77px;
height: 77px;
}
#div6 {
width: 85px;
height: 85px;
}
<div id="div1">
<div id="div2">Content2</div>
<div id="div3">Content3</div>
<div id="div4">Content4</div>
<div id="div5">Content5</div>
<div id="div6">Content6</div>
<div id="div7">Content7</div>
<div id="div8">Content8</div>
<div id="div9">Content9</div>
<div id="div10">Content10</div>
</div>
I would like to be able to horizontally align these divs so they are not aligned to the top of my main div but to the center.
I tried it many different ways, such as padding, margin, but i wasn't able to figure out how to do it.
Do you have any idea?
Just add vertical-align:middle; on the rule above:
CSS
#div1 div {
margin: 0% 0,5%;
display: inline-block;
color: white;
border: 1px dotted yellow;
align: center;
vertical-align: middle;
}
DEMO HERE
Hey if you are having some confusion or problem of using vertical-align:middle you can go through below example
I have added a new div inside of div with id div2 to div10 and updated css
#div1 > div {
display: inline-block;
align: center;
margin: 0% 0, 5%;
position: relative;
top: 50%;
}
#div1 > div[id] > div {
transform: translateY(-50%);
color: white;
border: 1px dotted yellow;
}
#div1 {
margin: 0px auto;
width: 620px;
height: 100px;
background-color: black;
overflow: hidden;
text-align: center;
}
#div2 > div, #div10 > div {
width: 21px;
height: 100px;
}
#div3 > div, #div9 > div {
width: 60px;
height: 60px;
}
#div4 > div, #div8 > div {
width: 70px;
height: 70px;
}
#div5 > div, #div7 > div {
width: 77px;
height: 77px;
}
#div6 > div {
width: 85px;
height: 85px;
}
<div id="div1">
<div id="div2">
<div>
Content2
</div>
</div>
<div id="div3">
<div>
Content3
</div>
</div>
<div id="div4">
<div>
Content4
</div>
</div>
<div id="div5">
<div>
Content5
</div>
</div>
<div id="div6">
<div>
Content6
</div>
</div>
<div id="div7">
<div>
Content7
</div>
</div>
<div id="div8">
<div>
Content8
</div>
</div>
<div id="div9">
<div>
Content9
</div>
</div>
<div id="div10">
<div>
Content10
</div>
</div>
</div>
JSFIDDLE: https://jsfiddle.net/9tdzqvot/

Div width equal to gap between two other divs

I have several divs in a container, organized in a row.
$(document).ready(function() {
$('#swap').click(function() {
$('#container').find('div.blue').each(function() {
$(this).removeClass('blue');
$(this).addClass('green');
});
});
});
#container {
border: 1px solid black;
border-right: 0px;
width: 500px;
min-height: 40px;
}
#left {
float: left;
}
#right {
float: right;
}
.purple {
background-color: #9471AB;
width: 70px;
}
.red {
background-color: #D48A8A;
width: 40px;
}
.green {
background-color: #A4B995;
width: 50px;
}
.blue {
background-color: #95AEB9;
width: 75px;
}
.red,.green,.blue,.purple {
height: 40px;
float: left;
box-sizing: border-box;
border-right: 1px black solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
<div id="left">
<div class="purple">
</div>
<div class="purple">
</div>
<div class="purple">
</div>
</div>
<div id="middle">
<div class="red">
</div>
</div>
<div id="right">
<div class="blue">
</div>
<div class="blue">
</div>
<div class="green">
</div>
</div>
</div>
<br/>
<button id="swap">Swap</button>
.purple, .blue, .green are defined widths, but not .red. For this example, I've given it a width.
I'd like .red to have a width equal to the gap between #left and #right. I could put it underneath all of them and make the width equal to container width, but I'm looking for something that's friendly with text.
I've put a button that changes all .blue to .green. .red should stretch its width accordingly so there's no gap. Some scenarios might have two .green and one .blue on the right, some might be three .blue or three .green., etc. It should be dynamic and not calculated against the width of other classes.
flexbox solution
$(document).ready(function() {
$('#swap').click(function() {
$('#container').find('div.blue').each(function() {
$(this).removeClass('blue');
$(this).addClass('green');
});
});
});
* {
box-sizing: border-box;
}
#container {
border: 1px solid black;
border-right: 0px;
width: 500px;
margin: 10p auto;
display: flex;
justify-content: space-between;
}
#left {
display: flex;
}
#middle {
display: flex;
flex:1;
}
#right {
display: flex;
}
.purple {
background-color: #9471AB;
width: 70px;
}
.red {
background-color: #D48A8A;
flex:1;
}
.green {
background-color: #A4B995;
width: 50px;
}
.blue {
background-color: #95AEB9;
width: 75px;
}
.red,.green,.blue,.purple {
height: 40px;
border-right: 1px black solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="left">
<div class="purple">
</div>
<div class="purple">
</div>
<div class="purple">
</div>
</div>
<div id="middle">
<div class="red">
</div>
</div>
<div id="right">
<div class="blue">
</div>
<div class="blue">
</div>
<div class="green">
</div>
</div>
</div>
<br/>
<button id="swap">Swap</button>
Update
http://jsfiddle.net/w4rxg7v3/1/
This approach lets you have dynamic widths of left and right, and middle fills in the rest.
<div class="middle">
<div class="left"></div>
<div class="right"></div>
<p>Content for Middle: cupiditate blanditiis dolorum natus!</p>
</div>
============================================================
http://jsfiddle.net/w4rxg7v3/
You can accomplish this using float and calc().
Make 1st div float:left, right div to float:right, then make middle one float either left or right. Then set its width to calc( 100% - sumofwidthothertwodivs)
body {
min-width: 500px;
}
.left,.middle,.right {
height: 80px;
}
.left {
background-color: blue;
width: 200px;
float: left;
}
.middle {
width: calc( 100% - 400px ) ;
float: right;
background-color: red;
}
.right {
background-color: green;
float: right;
width: 200px;
}

Centering multiple side-by-side divs

I am trying to make multiple divs, specifically five and center them all. I have used the display:inline-block to get them to be side by side but then when I use margin: 0 auto, the display:inline-block seems to get negated and then it's a vertical strip going down the page.
Below is my code:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
I tried looking at the other relevant posts on SO but they don't do it with as many divs or they use static positioning which I don't want to use.
Can someone point me in the right direction?
This happens cause the width of the container is 50px. One quick solution is to set width of container to 100%:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 100%;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
You can align to center using text-align center to container:
div {
width: 50px;
height: 300px;
border-radius: 0;
display: inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 100%;
text-align: center;
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
To achieve both and vertical and horizontal align you can use position: absolute to the container top: 50% left: 50% and margin-top: -150px; /* Half the height */ margin-left: -135px; /* Half the width */:
div {
width: 50px;
height: 300px;
border-radius: 0;
display:inline-block;
}
#red {
background-color: red;
}
#orange {
background-color: orange;
}
#yellow {
background-color: yellow;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
.container {
width: 270px;
position: absolute;
top: 50%;
left:50%;
margin-top: -150px; /* Half the height */
margin-left: -135px; /* Half the width */
}
<div class="container">
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
</div>
You can set text-align: center on .container. Updated you code:
.container {
width: 100%;
text-align: center;
}
.container > div{
width: 50px;
height: 300px;
border-radius: 0;
display:inline-block;
}
http://jsfiddle.net/jermund/wzdLrs0m/