Aligning divs horizontally inside a centered container - html

How can I align 4 divs, in css, inside a container like in this image: http://postimg.org/image/w0k7wgdfb/
Here's my html, I guess I need another container for DIV#2 and DIV#3.
<div id="container">
<div id="header"> DIV 1 </div>
<div id="wraper"> <!-- WRAPER -->
<div id="sidebar"> DIV 2 </div>
<div id="content"> DIV 3 </div>
</div> <!-- WRAPER -->
<div id="footer"> DIV4 </div>
</div>
Thank you for your help!

Solution 1 - Floats
After centre aligning the content, you could use a simple float trick for the two middle divs:
CSS
html, body {
height: 100%;
width:100%;
margin: 0;
padding: 20px;
box-sizing:border-box;
}
#container {
text-align:center;
width:500px;
margin: 0 auto;
height:100%;
background:black;
padding:20px;
box-sizing:border-box;
}
#header {
background:green;
height:20%;
}
#wraper {
height:60%;
overflow:hidden;
}
#sidebar {
width:20%;
float:left;
height:100%;
background:red;
}
#content {
overflow:hidden;
background:blue;
height:100%;
}
#footer {
background:orange;
height:20%;
}
Solution 2 - Display:Table
After centre aligning the content, you could apply a table layout to the middle divs
CSS
html, body {
height: 100%;
width:100%;
margin: 0;
padding: 20px;
box-sizing:border-box;
}
#container {
text-align:center;
width:500px;
margin: 0 auto;
height:100%;
background:black;
padding:20px;
box-sizing:border-box;
}
#header {
background:green;
height:20%;
}
#wraper {
height:60%;
display:table;
width:100%;
}
#sidebar {
width:20%;
display:table-cell;
background:red;
}
#content {
display:table-cell;
background:blue;
}
#footer {
background:orange;
height:20%;
}

Here there is a working fiddle.
HTML
CSS
#one{
width: 400px;
background: black;
margin: 0 auto;
height: 600px;
}
#two{
height: 100px;
width: 400px;
background: lime;
}
#three{
height: 400px;
width: 100px;
background: yellow;
float: left;
}
#four{
height: 400px;
width: 300px;
background: blue;
float: left;
}
#five{
height: 100px;
clear: both;
width: 400px;
background: silver;
}

Related

Changing layout of 3 div columns to 2 div columns and 3rd one below

I'm trying to rearrange 3 divs when device width is below 900px. They are arranged as three columns (2 floating divs and main one in the middle) and i don't know how to make them be 2 columns and third div below them (Image shows what i'm aiming at).
Thank you in advance :)
Adding code as you asked :) here is html
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<header></header>
<div id="left"></div>
<div id="right"></div>
<div id="middle"></div>
</div>
</body>
</html>
and here is css
#container{
width: 90%;
margin: 0px auto 0px auto ;
}
header{
width: 100%;
height: 200px;
background-color: blue;
}
#left{
float: left;
width: 20%;
height: 500px;
background-color: orange;
}
#right{
float: right;
width: 20%;
height: 500px;
background-color: green;
}
#middle{
width: 80%;
background-color: red;
height: 500px;
}
if i make right div float:none then it moves the middle div
You need to use media queries
https://css-tricks.com/snippets/css/media-queries-for-standard-devices/
Enjoy
With media queries and flex.
Here is a snippet, (click on run then full screen).
<div class="flex">
<div class="sub c">1</div>
<div class="sub c">2</div>
<div class="doge c">3</div>
</div>
.flex{
display: flex;
flex-wrap: wrap;
}
.c{
height:20px;
width:20px;
border: 1px solid green;
box-sizing: border-box;
}
#media(max-width:600px){
.sub{
width: 50%;
}
.doge{
width: 100%
}
}
<div class="flex">
<div class="sub c"></div>
<div class="sub c"></div>
<div class="doge c"></div>
</div>
Welcome to the world of {in an ominous voice} RESPONSIVE DESIGN ! ,
To perform what you are trying to do you will need to explore Media Queries.
Here is an example of what you are trying to do: JSFiddle
HTML
<div class="container">
<div class="left">
left content flexible width
</div>
<div class="right">
right content fixed width
</div>
<div class="bottom">
Bottom content flexible width
</div>
</div>
CSS
.container {
height: 100px;
overflow: hidden;
}
.left {
float: left;
background: #00FF00;
width: 25%;
overflow: hidden;
height: 100%;
}
.right {
display: inline-block;
width: 50%;
background: #0000ff;
height: 100%;
}
.bottom {
float: right;
background: #ff0000;
display: inline-block;
width: 25%;
height: 100%;
}
#media only screen and (max-width: 900px) {
.container {
height: auto;
overflow: hidden;
}
.left {
float: left;
background: #00ff00;
width: 25%;
overflow: hidden;
height: 100px;
}
.right {
float: none;
width: 75%;
background: #0000ff;
height: 100px;
}
.bottom {
position: relative;
float: none;
background: #ff0000;
width: auto;
overflow: hidden;
height: 50px;
display: inherit;
}
}
Good luck!
It would be helpful to see your sourcecode to tell you why it has not worked. At least you could describe it in more detail. Otherwise I would suspect that clear: both could maybe help you here by redefining a div-class in a media-query. At least this has worked for me.
As an example you could just attach float: left for the left column then the middle column would be following on the right side. By redefining the right-column (class) with clear: both the right-column would then be a footer. This is just an example and would not be the best solution indeed.
Here's my take on it.
/* Styles go here */
body,html{
margin: 0;
padding: 0;
height:100%;
}
.wrapper{
height:100%;
width:100%;
padding: 20px;
}
.div1{
height:100%;
width:30%;
float:left;
background-color:orange;
}
.div2{
height:100%;
width:30%;
float:left;
margin-left:2%;
background-color:red;
}
.div3{
height:100%;
width:30%;
margin-left:2%;
float:left;
background-color:green;
}
#media(max-width:900px){
.wrapper{
height:100%;
width:100%;
clear:both;
}
.div1{
height:70%;
width:49%;
float:left;
background-color:orange;
}
.div2{
height:70%;
width:49%;
float:left;
background-color:red;
}
.div3{
height:30%;
width:100%;
float:left;
margin:20px 0 20px 0;
background-color:green;
}
}
<div class="wrapper">
<div class="div1"><p></p></div>
<div class="div2"><p></p></div>
<div class="div3"><p></p></div>
</div>

How to get a height:100% div in a height:auto div?

Here is my HTML :
<div id="container">
<div id="red"></div>
<div id="yellow">
<div id="green"></div>
</div>
</div>
Here is my CSS :
#container { height:auto; width:100%; background:orange; float:left; }
#red { height:100%; width:200px; background:red; float:left; position:relative; }
#yellow { height:100%; width:calc(100% - 210px); background:yellow; float:right; position:relative; padding:5px; }
#green { height:300px; width:100%; background:green; }
Here is a sample : https://jsfiddle.net/cc5xL660/
As it is in the jsfiddle, the #red div is invisible. I'm looking for a way to make the #red div visible without a specific height dimension. Of course, I could give a height:300px to the #red div but the #green div is supposed to be dynamic. I would like the #red div to have the same height.
You have to give
position: relative to #container
position: absolute to #red
Your JSFiddle edited: https://jsfiddle.net/cc5xL660/4/
You can use display:flex to do that. Have a look at here
#container {
height: auto;
width: 100%;
background: orange;
float: left;
display:flex;
flex-direction:row;
align-items:stretch;
}
#red {
/*height: 100%;*/
width: 200px;
background: red;
float: left;
position: relative;
}
#yellow {
/*height: 100%;
width: calc(100% - 210px);*/
background: yellow;
float: right;
position: relative;
padding: 5px;
flex:1 0;
}
#green {
height: 300px;
width: 100%;
background: green;
}
<div id="container">
<div id="red"></div>
<div id="yellow">
<div id="green"></div>
</div>
</div>
Fiddle

Positionning two div elements with one centered

I want to center a div element and to place another div element just on the right with the same vertical alignment. I don't know how to proceed without centering both elements.
Here is my code.
<div class="container">
<div class="center"></div>
<div class="right"></div>
</div>
.center {
width: 100px;
height: 100px;
margin: auto;
background-color: red;
}
.right {
width: 100px;
height: 100px;
background-color: blue;
}
http://jsfiddle.net/KWsnh/
You could use calc to achieve this:
FIDDLE
.container{
text-align:center;
position: relative;
}
.center {
width: 100px;
height: 100px;
background-color: red;
display: inline-block;
}
.right {
width: 100px;
height: 100px;
background-color: blue;
position:absolute;
top:0;
left: calc(50% + 50px); // (100% - 100px)/2 + 100px (offset) = 50% + 50px
}
PS: Browser support for calc is quite good these days.
Demo Fiddle
HTML
<div class="container">
<div class='vcenter'>
<div class="center"></div>
<div class="right"></div>
</div>
</div>
CSS
html, body {
margin:0;
padding:0;
height:100%;
width:100%;
}
body {
display:table;
}
.container {
display:table-cell;
vertical-align:middle;
}
.center {
width: 100px;
height: 100px;
margin: 0 auto;
background-color: red;
}
.vcenter {
display:block;
width:100%;
position:relative;
}
.right {
width: 100px;
height: 100px;
background-color: blue;
position:absolute;
right:0;
top:0;
}

text inside the block displaying incorrectly

I have 3 blocks - block1, block2, block3..block1 & 2 are left floated
http://jsfiddle.net/MTSg4/1/
The text inside block3 needs to be displayed inside the block but for some reason its displaying outside.
css
html, body{
height: 100%;
width:100%;
}
#block1{
height:10%;
width:50%;
text-align:center;
float:left;
background-color:red;
}
#block2{
height:90%;
width:50%;
background-color:green;
float:left;
}
#block3{
height:90%;
width:50%;
background-color:yellow;
}
html
<div id="block1">
Block 1
</div>
<div id="block2">
Block 2
</div>
<div id="block3">
Block 3
</div>
You need to clear the floats in blocks 1 and 2. Try this for block3:
#block3{
height:90%;
width:50%;
background-color:yellow;
clear:both;
}
Your usage of floats is the problem. You see, block 2 is just a trial and error. It does not float left. Check this FIDDLE.
#block2{
height:90%;
width:50%;
background-color:green;
float:right;
}
#block3{
height:90%;
width:50%;
background-color:yellow;
float: left;
}
Hope this helps!
Use this css
#block1 {
height: 10%;
width: 50%;
text-align: center;
background-color: red;
display: block;
float: left;
}
#block2 {
height: 90%;
width: 50%;
text-align: center;
background-color: green;
display: block;
float: left;
}
#block3 {
height: 90%;
width: 50%;
text-align: center;
background-color: yellow;
display: block;
clear: both;
}
I think you need to rethink the HTML to achieve this.
Perhaps a better solution is to split this into two columns, with block 1 and 2 in the first column, and block 3 in the second?
HTML
<div id="col1">
<div id="block1">
Block 1
</div>
<div id="block2">
Block 2
</div>
</div>
<div id="col2">
<div id="block3">
Block 3
</div>
</div>
CSS
html, body{
height: 100%;
width:100%;
}
body {
margin: 0;
}
#col1, #col2 {
float: left;
width: 50%;
height: 100%;
}
#block1{
height:10%;
text-align:center;
background-color:red;
}
#block2{
height:90%;
background-color:green;
}
#block3{
height:100%;
background-color:yellow;
}
Demo
Try this CSS:
html, body{
height: 100%;
width:100%;
}
#block1{
height:10%;
width:50%;
text-align:center;
float:left;
background-color:red;
}
#block2{
height:90%;
width:50%;
background-color:green;
float:right;
}
#block3{
height:90%;
width:50%;
background-color:yellow;
float: left;
}
.clear {
clear: both;
content: ' ';
*zoom: 1;
}
}
in your HTML:
<div id="block1">
Block 1
</div>
<div id="block2">
Block 2
</div>
<div id="block3">
Block 3
</div>
<div class="clear"></div>
This happens because the "block3" is floating around and not in a position while the other boxes are being called in left (including the box2 [It should be float: right]).
I've fixed and and added a debug class (clear).
Jsfiddle: Demostration
Hope it helps!

chrome: "ghost" width of floated elements inside inline block

test case http://codepen.io/anon/pen/hFumw.
Works fine in all browsers except chrome. In chrome width of .container is calculated like .child.one elements are not floated. Is there any way to fix this behaviour?
HTML:
<div class="container">
<div class="header">
header
</div>
<div class="child one">
</div>
<div class="child one">
</div>
<div class="child one">
</div>
</div>
CSS:
.container {
background:red;
padding:10px;
display: inline-block;
overflow:hidden;
.child {
width:100px;
height: 100px;
background: green;
float:left;
clear:left;
border: 1px solid blue;
}
.one {
float: left;
clear:left;
background:yellow;
}
.header {
background:blue;
}
}
UPD:
.header {
float: left;
width: 100%;
}
is not acceptable in my particular case.
Following CSS seems to work fine in Chrome and FF. See header declaration.
body {
text-align: center;
}
.container {
margin-left:auto;
margin-right:auto;
background:red;
padding:10px;
display: inline-block;
overflow:hidden;
.child {
width:100px;
height: 100px;
background: green;
float:left;
clear:left;
border: 1px solid blue;
}
.one {
float: left;
clear:left;
background:yellow;
}
.two {
float: right;
margin-left:0px;
clear: right;
}
.header {
background:blue;
display:block;
float:left;
width:100%;
}
}
Try adding this css to .header
clear:both;
float:left;
width:100%