How to float divs without using tables [closed] - css

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi how do i have divs layout like this using CSS float and other methods. i am trying to avoid using tables and learn how to float divs properly.
Thanks

first id use a main div with display:block;
then inside that main div for your example create 4 sub divs with display:inline-block;

Maybe take a look at solutions like Singularity or Susy. Both capable grid frameworks giving you a helping hand accomplishing exactly what you intend to do and keep the necessity of math out of your head. ;)

Sigh those were a lot of boxes ;D. I couldn't exactly see the dimensions of each box but I did my best to replicate what you showed (no absolute or relative positioning used!).
Here's the jsFiddle: link The formatting is odd, jsFiddle's tidyUp is not working for me
Here's the codepen: link Use this one instead
The trick is to use float: left when you want things to stack horizontally. When you want them to stack vertically, just wrap those elements in a container and make sure each has display: block. You use these two things to accomplish this sort of thing.
HTML:
<div id="container">
<div id="left0">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="left1"></div>
<div id="left2"></div>
<div id="row1">
<div></div>
<div></div>
</div>
<div id="row2"></div>
<div id="row3">
<div></div>
<div></div>
</div>
</div>
CSS:
*{ /*includes the border in height&width*/
box-sizing: border-box;
}
div{ /*gives us some general spacing*/
margin: 5px;
background: lightblue;
}
#container{
width: 1200px;
margin: 1em auto;
background: lightgray;
}
/*gives a float-left property to first-level children*/
#container > div{
float: left;
}
#container > div > div{
border: 1px solid red;
}
#left0{
width: 50px;
}
#left0 > div{ /*styles for little boxes*/
height: 40px;
margin: 5px;
}
#left1{
width: 230px;
height: 230px;
}
#left2{
width: 150px;
height: 230px;
}
#row1{
width: 600px;
height: 50px;
}
#row1 div:nth-child(1){
float: left;
width: 350px;
height: 40px;
}
#row1 div:nth-child(2){
float: left;
width: 230px;
height: 40px;
}
#row2{
width: 600px;
height: 70px;
}
#row3{
width: 600px;
height:90px;
}
#row3 div:nth-child(1){
width: 200px;
height: 40px;
float: left;
}
#row3 div:nth-child(2){
width: 100px;
height: 80px;
float: right;
}
I'll comment explaining some of the css3 selectors I used in case you're not familiar with them.

Related

Setting up two rows on a left and right sides of a horizontal center using CSS

I am facing a same problem. I'm trying to create two separate rows (marked as red background color) to be aligned horizontally in the center. One of the row on the left side of center part, and second one on the right side of the center part.
Do I need to add something or change some values? I've been trying to do this for 2 hours now.
Any help will be appreciated. Thank you :)
.others {
position: relative;
vertical-align: middle;
width: 70%;
background-color: #d0d0d0;
height: 500px;
margin: auto;
padding: 40px 15% 20px 15%;
display: table;
}
.others p {
margin: 0px;
height: 300px;
float: left;
background-color: red;
}
<DIV CLASS="others">
<P ID="leftside">
News will be shown here as they appear.
</P>
<P ID="rightside">
Here you will be able to see our products.
</P>
</DIV>
.others {
position: relative;
vertical-align: middle;
width: 70%;
background-color: #d0d0d0;
height: 500px;
margin: auto;
padding: 40px 15% 20px 15%;
display: table;
}
.others p {
margin: 0px auto;
height: 300px;
width:50%;
display-inline-block;
text-align:center;
float: left;
background-color: red;
}
<DIV CLASS="others">
<P ID="leftside">
News will be shown here as they appear.
</P>
<P ID="rightside">
Here you will be able to see our products.
</P>
</DIV>
Worked for me just by removing float:left; and add display:table-cell; to .others p.
Fiddle
.others p {
margin: 0px;
height: 300px;
background-color: red;
display:table-cell;
}
.others p {
margin: 0px;
height: 300px;
background-color: red;
display:inline-block;
}
i think you shouldnt use <p> for positioning.
use <div> instead.
also using float:left or float:right might solve your problem.
Read up on using floating items here:
http://www.w3schools.com/cssref/pr_class_float.asp
Also, when using floats, browsers will assume there is nothing inside your 'container' <div>.
So i'd also suggest you read up on using css attribute overflow.
http://www.w3schools.com/cssref/pr_pos_overflow.asp
.others
{
position: relative;
vertical-align: middle;
width: 70%;
background-color: #d0d0d0;
height: 500px;
margin: auto;
padding: 40px 15% 20px 15%;
display: table;
}
#leftside
{
display:inline-block;
margin: 0px;
height: 300px;
width:50%;
float: left;
background-color: red;
}
#rightside
{
display:inline-block;
margin: 0px;
height: 300px;
width:50%;
float: right;
background-color: green;
}
<DIV CLASS="others">
<P ID="leftside">
News will be shown here as they appear.
</P>
<P ID="rightside">
Here you will be able to see our products.
</P>
</DIV>
You just need to provide to p a width value because you are floating the p elements to the left, every p element into the container will get out of the normal document flow and flow from left to right.
Just add width: 50% to every p element. like this:
.others p {
margin: 0px;
height: 300px;
float: left;
background-color: red;
width:50%;
}
Also provide a clearfix or overflow:hidden; to the .others in order to contain the floated elements within it's body.
Here is a demo to work with
Edit: Almost forgot. If you want to gain control onto your layout, provide also a min-width and a max-width value to the body container, so it doesn't strech to much on wide screens, nor it is contained to much on narrower screens. Also, try a css framework, like bootstrap. It will give you fine control onto your layout.
Cheers!

DIV POSITIONING on right and left side

Am new to html and css I want to build a page with 3layouts right side left side and middle part will be content
My question is how to place a divs on right and left side
I tried APdiv in dreamweaver but it is overlaping
plz give me a solution
Use
float:left;
for left,
float:right;
for right. And search before, ask you can find easily this questions answer on the net.
This is one way of doing 3 column web page:
HTML:
...
<body>
<div id="container">
<div id="left">LEFT</div>
<div id="center">CENTER</div>
<div id="right">RIGHT</div>
</div>
</body>
...
CSS:
#container{
width: 960px;
margin: 0 auto;
overflow: hidden; /* This is not to overlap */
}
#left{
width: 200px;
height: 800px;
background-color: red;
float: left;
}
#center{
width: 560px;
height: 800px;
background-color: blue;
float: left;
}
#right{
width: 200px;
height: 800px;
background-color: green;
float: left; /* you can do here float:right also */
}

CSS div float. push last div up

I have 5 <div> elements and they all float left.
How can I push UP my last div? (i cant use 2 more wrappers because they will be re-sized with jQuery, all 5 of them must be in same wrapper)
I don't know if I explain my problem in a right way so if you have question, please ask.
HTML:
<div id="ModeliSadrzajAir">
<div class="kocka220x140">1</div>
<div class="kocka220x140">2</div>
<div class="kocka220x300">3</div>
<div class="kocka220x300">4</div>
<div class="kocka460x140">5</div>
</div>
CSS:
#ModeliSadrzajAir {
width: 960px;
margin: -60px 0px 0px -10px;
min-height: 500px;
background-color: #00FFFF;
position: absolute;
z-index: 1000;
}
.kocka220x140 {
border-radius:5px;
width: 220px;
margin: 10px;
height: 140px;
float: left;
background-color: #FFFF00;
}
.kocka220x300 {
border-radius: 5px;
width: 220px;
margin: 10px;
height: 300px;
float: left;
background-color: #FF0000;
}
.kocka460x140 {
border-radius: 5px;
width: 460px;
margin: 10px;
height: 140px;
float: left;
background-color: #FF0000;
}
Fiddle
You've to set your .kocka220x300's float property from left to right
I also suggest you to change your html to this
<div id="ModeliSadrzajAir">
<div class="kocka220x140">1</div>
<div class="kocka220x140">2</div>
<div class="kocka220x300">4</div> <!-- This comes first -->
<div class="kocka220x300">3</div> <!-- This comes second -->
<div class="kocka460x140">5</div>
</div>
This way, your 3 is on the left side of 4, check the fiddle link for the update
You can try this http://jsfiddle.net/modaloda/czz2Z/9/
.kocka460x140
{
border-radius: 5px;
width: 460px;
margin: 10px;
height: 140px;
float: left;
background-color: #FF0000;
position: absolute;
top: 160px;
}
I tried to reproduce your example.
Basically I think you need one wrapper with position:relative; that contains all divs and make the 5th div position:absolute; and bottom:0px;. Also add overflow:auto; so that the max height you have contained in your parent div will push the parent div's height (read it again you will understand :P).
Check this fiddle:
http://jsfiddle.net/R8hJ3/1/
Have You Tried Some plugins like Grid-a-licious..
if not try it out.. Else if you need a pure Css you could have a look the link below..
jsfiddle.net/chermanarun/HaV29/

Splitting content into 2 columns CSS

I am trying to split the content into 2 columns
using the below CSS
.container2 {width: 320px; height: 100px; display: block;}
.image2 {border: 1px solid #F2F3F4;
float: left;
height: 50px;
margin-right: 6px;
padding: 1px;
width: 50px;}
.image2 img { height: 50px; width: 50px;}
.text2 {width: 230px; height: 100px; float:left; font-size: 13px;}
.clear2 {clear: both;}
here is my page http://www.applicationcatalyst.com/columns/
(but the content is in single column)
Now I would like to know what extra I need to add in the CSS code to make the content
split into 2 columns
Thanks
You should do the following:
Put the following div in one div with class 'ColumnDiv' or something.
(image2 & text2)
so you have this:
<div class="ColumnDiv">
<div class="image2">
<img src="http://www.applicationcatalyst.com/storage/other/cakehealth.png"/>
</div>
<div class="text2">Track and Optimize Your Healthcare.</div>
</div>
<div class="ColumnDiv">
<div class="image2">
<img src="http://www.applicationcatalyst.com/storage/other/greengoose.png"/>
</div>
<div class="text2">Farmville for real life, with wireless sensors.</div>
</div>
<div class="clear2"/>
and in css you need to put the following:
.ColumnDiv
{
float:left;
}
Now your divs will show next to each other.
And i guess .clear2 is for evening out the length of both columns.
If you don't want to make any changes to the HTML, you can do it by doing the following:
Increasing the width of .container2 to hold both content blocks side by side.
Removing the clear: both from .clear2. This allows the content blocks to float next to each other.
The changed rules are below:
.container2 {
width: 640px;
height: 100px;
display: block;
}
.clear2{
clear: none;
}
What you'll then see is that your 2 columns aren't properly centered in the main content area. To fix this, you'll need to update the following 2 CSS rules to give them space:
#sidebar2Wrapper {
display: none;
}
#contentWrapper {
width: 800px;
}

Two column layout with one having a fixed width in CSS

I want a nice 2 column layout using CSS float's.
Column#1 160 px
Column#2 100% (i.e. the rest of the space).
I want to place the Col#2's div first, so my layout looks like:
<div id="header"></div>
<div id="content">
<div id="col2"></div>
<div id="col1"></div>
</div>
<div id="footer"></div>
What has to be get this effect?
Neither of the above will work.
div#col2 {
width: 160px;
float: left;
position: relative;
}
div#col1 {
width:100%;
margin-left: 160px;
}
That's assuming that Column 2 should appear as a left sidebar, with col 1 as the main content.
You should use the "float" CSS property for doing this. Check out for a simple implementation here. And you can find a bit more detailed article here
You have to use float:left on first column and float:right on the second column
I think you could do something like this.
div#col2 {
padding-left: 160px;
width: 100%;
}
div#col1 {
float: left;
width: 160px;
}
This relies on #col1 coming before #col2, which might make it unusable.
This will not, but relies on #col1 being the longer:
#content {
position: relative;
}
div#col2 {
width: 160px;
position: absolute;
}
div#col1 {
width: 100%;
margin-left: 160px;
}
This'll keep the footer in place.
div#footer {
clear: both;
}
Although the question is for years ago, I provide this useful answer for any future reference and similar cases.
Putting #col1 before #col2 in markup, you may float it to the right, in case you have LTR lauout (if you have an RTL layout then float to the left) and give the other col overflow: hidden.
Note that the parent ( #content ) should have the overflow: hidden too:
#content{
overflow: hidden;
padding: 20px 0;
height: 100px;
background-color: #cdeecd;
}
#content #col1{
float: right;
width: 160px;
height: 100px;
background-color: #eecdcd;
}
#content #col2{
height: 100px;
overflow: hidden;
background-color: #cdcdee;
}
<div id="content">
<div id="col1"></div>
<div id="col2"></div>
</div>

Resources