Place Floating Div evenly and horizontally - css

I want to place 3 div's evenly inside another div. However, I can't get rid of the right margin for the last floating box. Also, the spaces between them do not look right to me.
<div class="page">
<div class="box">
<div class="b">b1</div>
<div class="b">b2</div>
<div class="b">b3</div>
<div style="clear:both;"></div>
</div>
</div>
.page{
background-color: green;
padding: 10px;
}
.box{
background-color: blue;
}
.b{
width: 30%;
margin-right: 3%;
background-color: #999;
float: left;
height: 100px;
}
The code is located at http://jsfiddle.net/u6KqK/
Is there a better solution for this?

You're using 99% (30+30+30+3+3+3) of the parent div, not 100%, thus why the right margin of the right-most div appears to be 4%. Here are a couple solutions:
1) set the margin-right to use the final percent:
.b{
width: 30%;
margin-right: 3.3333333333%;
margin-right: calc(10%/3);
background-color: #999;
float: left;
height: 100px;
}
Since older browsers don't support calc, I included a fallback that will be identical for essentially every scenario. Fiddle: http://jsfiddle.net/u6KqK/7/
2) Add a 1% margin to the left of the first div:
.b:first-of-type{
margin-left:1%
}
Fiddle: http://jsfiddle.net/u6KqK/1/

You can add a second class to the middle div and add the margins to that class. That way it only gets applied to the middle class.
<div class="page">
<div class="box">
<div class="b">b1</div>
<div class="b middle">b2</div>
<div class="b">b3</div>
<div style="clear:both;"></div>
</div>
</div>
.page{
background-color: green;
padding: 10px;
}
.box{
background-color: blue;
}
.b{
width: 30%;
background-color: #999;
float: left;
height: 100px;
}
.middle{
margin-right: 5%;
margin-left: 5%;
}
http://jsfiddle.net/u6KqK/9/

Since you have the left-most div on the actual left it would make sense to have the right-most also to the far right with zero margin
JSfiddle Demo
HTML
<div class="page">
<div class="box">
<div class="b">b1</div>
<div class="b">b2</div>
<div class="b">b3</div> /* cleafix div removed */
</div>
</div>
CSS
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.page{
background-color: green;
padding: 10px;
overflow:hidden; /* quick clearfix */
}
.box{
background-color: blue;
}
.b{
width: 30%;
margin-left: 5%; /* (100% - 3x30%)/2 */
background-color: #999;
float: left;
height: 100px;
}
.box div:first-child {
margin-left: 0;
}

Related

A simple CSS float example

Have a look at this example pen:
http://codepen.io/benpearson/pen/bHJke
Is it possible to get div Four and Five to move up beside div Two WITHOUT changing the HTML or using absolute positioning?
(I can't use contains as each div will be floated in different directions depending on screen size.)
.wrap {
background-color: #666;
max-width: 500px;
height: 200px;
margin: 0 auto;
}
.one {
background-color: #ddd;
height: 110px;
width: 25%;
float: left;
}
.two {
background-color: #ccc;
height: 55px;
width: 50%;
float: left;
}
.three {
background-color: #bbb;
height: 35px;
width: 50%;
float: left;
}
.four {
background-color: #aaa;
height: 20px;
width: 25%;
float: right;
}
.five {
background-color: #999;
height: 20px;
width: 25%;
float: right;
}
<div class="wrap">
<div class="one">
One
</div>
<div class="two">
Two
</div>
<div class="three">
Three
</div>
<div class="four">
Four
</div>
<div class="five">
Five
</div>
</div>
Does this work for you? http://codepen.io/anon/pen/bAzch
Just changed divs four and five to be display:inline-block; instead of float:right;
Sure, just put a container div around divs two and three and move their float and width properties to it instead.
HTML:
<div class="wrap">
<div class="one">
One
</div>
<div class="rowtwo">
<div class="two">
Two
</div>
<div class="three">
Three
</div>
</div>
<div class="four">
Four
</div>
<div class="five">
Five
</div>
</div>
CSS:
.rowtwo {
float: left;
width: 50%;
}
}
.two {
background-color: #ccc;
height: 55px;
}
.three {
background-color: #bbb;
height: 35px;
}
You can see it at: http://codepen.io/anon/pen/KABoC
Ben, the div's 4 and 5 will never start from the top 0 of wrapper, because the div 3 starts on the end of div 2. So 4 and 5 recognize the right-upper corner of div 3 as starting point. You must use position: absolute; .. no other choice as far as I know.

Packing fixed width div's into fluid container

I have simple structure with container and inside boxes:
<div id="container">
<div class="block"></div>
// more blocks
<div class="block"></div>
</div>
What I would like to achieve is to center boxes inside this container but to pack them as much as possible in a one line. The same I can do using JS: http://jsfiddle.net/JhxSd/ but I would like to avoid that, and use only CSS. Is that possible?
#media queries
Use a set of #media queries to define different layouts for the grid based on the current screen size. The only part of the layout that needs to vary is the width of the grid wrapper.
For all practical purposes, this is the only CSS solution available at present. For an explanation of why #media queries are appropriate, and why other available CSS options won't work, see this answer.
JSFiddle Demo
The above demo has #media queries for screen sizes up to 1200px wide (more can be added as needed), and does not use JavaScript. The rendered width of #container is always 75% (not counting the border), and the grid is centered within #container.
Note: This solution requires adding a wrapper div around the blocks. In each #media query, the width of the wrapper is just enough to fit the number of columns appropriate for the current screen size. The fixed wrapper width is what allows the grid as a whole to be centered within #container. If editing the static HTML isn't an option, the wrapper div can be added when the page loads using jQuery.
HTML
<div id="container">
<div class="grid-wrapper">
<div class="block"></div>
...
</div>
</div>
CSS
#container {
width: 75%;
...
}
.grid-wrapper {
margin: 0 auto;
width: 70px; /* Default: 1 column */
}
#media (min-width: 200px) {
.grid-wrapper {width: 140px;} /* 2 columns */
}
#media (min-width: 290px) {
.grid-wrapper {width: 210px;} /* 3 columns */
}
...
I hope this will do the trick:
http://jsfiddle.net/CnjZR/1/
<div id="container">
<div id="wrap">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
CSS:
#container {
width: 100%;
overflow: hidden;
background: red;
text-align: center;
}
.block {
width: 20px;
height: 20px;
background: blue;
float: left;
margin: 5px;
}
#wrap {
background: green;
overflow: hidden;
display: inline-block;
}
Not too sure if you where looking for something like 'flex-justify' , I added in the demo a turn around based on inline-boxes behavior and text-align values.
edit : point cleared: text-align:center ; is it.
http://jsfiddle.net/JhxSd/10/
The point is you should not use float, but display.
Float is not friendly with centering , nor vertical nor horizontal, since it is not standing in the natural flow of the document.
#container {
width: 75%;
border: 1px solid;
text-align:center;
overflow:hidden;
padding:1em 1em 0;
box-sizing:border-box;
float:left;
}
#container .block {
width: 50px;
height: 50px;
background-color: blue;
display:inline-block;
margin: 10px;
}
I think, everything you have almost done already.
#container {
width: 75%;
border: 1px solid;
float: left;
}
#container .block {
width: 50px;
height: 50px;
background-color: blue;
float: left;
margin: 10px;
overflow: hidden;
}
http://jsfiddle.net/JhxSd/3/
Try this:
#container {
width: 75%;
border: 1px solid;
float: left;
overflow: hidden;
text-align: center;
}
#container .block {
display: inline-block;
width: 50px;
height: 50px;
background-color: blue;
margin: 10px;
}
If you truly need everything left-aligned then I think you're out of luck with just CSS.
You can use the text-align:justify for the container and use the display:inline-block for the div.block. but you need add some placeholder tag at the last.Like this:
HTML
<div class="wrapper">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block">4</div>
<div class="block">5</div>
<div class="block">6</div>
<div class="block">7</div>
<div class="block">8</div>
<div class="block">9</div>
<div class="block">10</div>
<div class="block">11</div>
<div class="block">12</div>
<div class="block">13</div>
<div class="block">14</div>
<div class="block">15</div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
CSS
.wrapper {
width: 75%;
border: 1px solid;
font-size: 0.1px;
text-align: justify;
}
.wrapper:after {
content:"";
display:inline-block;
width: 100%;
}
.wrapper div{
font-size: 16px;
display:inline-block;
*display: inline;
zoom:1;
color: #fff;
background-color:blue;
width: 50px;
height: 50px;
margin: 10px;
}
.wrapper .placeholder {
width: 50px;
height: 0px;
background:none;
}
Please view the demo. A detailed tutorial, please click here.

Aligning five divs next to each other

I'm currently making a website where you can find results of Formula One races. To do so, I want to make a result page for each Grand Prix, where the results are being shown in 5 boxes next to each other. Like this:
1 2 3 4 5
But right now it looks like this
1 2
3
4 5
This is the HTML code I use:
<div id="wrap">
<div id="fp1">FP1</div>
<div id="fp2">FP2</div>
<div id="fp3">FP3</div>
<div id="qual">Qual</div>
<div id="race">Race</div>
</div> <!--End wrap div-->
And this the CSS I use:
#wrap{
width: 100%;
height: 600px;
background-color: #000;
border: 1px solid white;
}
#fp1{
width: 20%;
height: 600px;
background-color: #333;
float: left;
}
#fp2{
margin-left: 20%;
width: 20%;
height: 600px;
background-color: #666;
}
#fp3{
margin-left: 40%;
width: 20%;
height: 600px;
background-color: #333;
}
#qual{
margin-left: 60%;
width: 20%;
height: 600px;
background-color: #666;
float: right;
}
#race{
width: 20%;
height: 600px;
background-color: #333;
float: right;
}
Anybody who knows how to fix it?
please check this: http://jsfiddle.net/itz2k13/KAwEz/
#fp1{
width: 20%;
height: 600px;
background-color: #333;
float: left;
}
.....
You can use a generic class, since styles are repetitive. see this for efficient one: http://jsfiddle.net/itz2k13/KAwEz/1/
else you can follow inline-block method, and further in time column and display:flex will be usefull:
http://codepen.io/seraphzz/pen/IosFk
#wrap {
white-space:nowrap;
}
#wrap, .wrap {
/* for test */
height:200px;
overflow:auto;}
.wrap {
-moz-column-width:300px;
-webkit-column-width:300px;
column-width:300px;
}
#wrap div {
white-space:normal;
display:inline-block;
}
#wrap div , .wrap div {
/* for test */
width:300px;
height:100%;
background:#999;
}
<div id="wrap">
<div id="fp1">FP1</div>
<div id="fp2">FP2</div>
<div id="fp3">FP3</div>
<div id="qual">Qual</div>
<div id="race">Race</div>
</div> <!--End wrap div-->
<div class="wrap" >
<div id="fp1">FP1</div>
<div id="fp2">FP2</div>
<div id="fp3">FP3</div>
<div id="qual">Qual</div>
<div id="race">Race</div>
</div> <!--End wrap div-->
If you want no scroll, divide 100%/numbers of boxes (fine if window not too small :) )
cheers
One more thing i noticed other than float:left regarding structure is you can use margin-left without % and give common margin-left like 20px

Weird three divs side by side

I'm a tables guy, but I'll need to drag and drop some divs, so I tried doing it tabeless (the right way).
This is what I want to do:
The space between all elements should be 24px. My main problem is having the divs (1,2,3) occupying 100% of available space. The width: 100% its sending them beyond the main container.
This is my code so far:
html
<div id="mainContainer">
<div id="topContainer">Just the top one
</div>
<div id="table">
<div id="Line1Container">
<div id="container1" class="container">1
</div>
<div id="container2" class="container">2
</div>
<div id="container3" class="container">3
</div>
</div>
<div id="Line2Container">
<div id="container4" class="container">4
</div>
<div id="container5" class="container">5
</div>
<div id="container6" class="container">6
</div>
</div>
</div>
</div>
And my css
#mainContainer {
border: 1px solid lightgray;
position:fixed;
top: 80px;
bottom:20px;
left:80px;
right:80px;
overflow-y: scroll;
overflow-x: hidden;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
#topContainer {
border: 1px solid lightgray;
border-radius: 10px;
margin-left: 24px;
margin-right: 24px;
margin-top: 24px;
}
#table {
display: table;
margin: 24px;
width: 95%;
}
#Line1Container, #Line2Container {
display: table-row;
}
.container {
border: 1px solid lightgray;
display: table-cell;
width: 33%;
border-radius: 10px;
}
As you see I tried the table-cell approach, but before I have tried the float: left approach.
Thanks
Fiddle
You can't properly use px values with % values together with dynamic sizes.
You should use x% instead of 24px.
And you can use float: left on the "cells"
How about using a table for separating the divs? that way with the td padding there will always be 24px between them
check out this fiddle:
http://jsfiddle.net/5zfEq/
added:
#Line1Container {
padding:12px;
}
#inner-table {
width: 100%;
}
#inner-table td {
padding: 12px;
}
based on #Edifice fiddle .... thanks ;)

Two divs, one fixed width, the other, the rest

I've got two div containers.
Whilst one needs to be a specific width, I need to adjust it, so that, the other div takes up the rest of the space. Is there any way I can do this?
.left {
float: left;
width: 83%;
display: table-cell;
vertical-align: middle;
min-height: 50px;
margin-right: 10px;
overflow: auto;
}
.right {
float: right;
width: 16%;
text-align: right;
display: table-cell;
vertical-align: middle;
min-height: 50px;
height: 100%;
overflow: auto;
}
<div class="left"></div>
<div class="right"></div> <!-- needs to be 250px -->
See: http://jsfiddle.net/SpSjL/ (adjust the browser's width)
HTML:
<div class="right"></div>
<div class="left"></div>
CSS:
.left {
overflow: hidden;
min-height: 50px;
border: 2px dashed #f0f;
}
.right {
float: right;
width: 250px;
min-height: 50px;
margin-left: 10px;
border: 2px dashed #00f;
}
You can also do it with display: table, which is usually a better approach: How can I put an input element on the same line as its label?
It's 2017 and the best way to do it is by using flexbox, which is IE10+ compatible.
.box {
display: flex;
}
.left {
flex: 1; /* grow */
border: 1px dashed #f0f;
}
.right {
flex: 0 0 250px; /* do not grow, do not shrink, start at 250px */
border: 1px dashed #00f;
}
<div class="box">
<div class="left">Left</div>
<div class="right">Right 250px</div>
</div>
You can use calc() Function of CSS.
Demo: http://jsfiddle.net/A8zLY/543/
<div class="left"></div>
<div class="right"></div>
.left {
height:200px;
width:calc(100% - 200px);
background:blue;
float:left;
}
.right {
width:200px;
height:200px;
background:red;
float:right;
}
Hope this will help you!!
If you can flip the order in the source code, you can do it like this:
HTML:
<div class="right"></div> // needs to be 250px
<div class="left"></div>
CSS:
.right {
width: 250px;
float: right;
}
An example: http://jsfiddle.net/blineberry/VHcPT/
Add a container and you can do it with your current source code order and absolute positioning:
HTML:
<div id="container">
<div class="left"></div>
<div class="right"></div>
</div>
CSS:
#container {
/* set a width %, ems, px, whatever */
position: relative;
}
.left {
position: absolute;
top: 0;
left: 0;
right: 250px;
}
.right {
position: absolute;
background: red;
width: 250px;
top: 0;
right: 0;
}
Here, the .left div gets an implicitly set width from the top, left, and right styles that allows it to fill the remaining space in #container.
Example: http://jsfiddle.net/blineberry/VHcPT/3/
If you can wrap them in a container <div> you could use positioning to make the left <div> anchored at left:0;right:250px, see this demo. I'll say now that this will not work in IE6 as only one corner of a <div> can be absolutely positioned on a page (see here for full explanation).
1- Have a wrapper div, set the padding and margin as you like
2- Make the left side div the width you need and make it float left
3- Set the right side div margin equal to the left side width
.left
{
***width:300px;***
float: left;
overflow:hidden;
}
.right
{
overflow: visible;
***margin-left:300px;***
}
<div class="wrapper">
<div class="left">
...
</div>
<div class="right" >
...
</div>
</div>
Hope this works for you!
There are quite a few ways to accomplish, negative margins is one of my favorites:
http://www.alistapart.com/articles/negativemargins/
Good luck!
set your right to the specific width and float it, on your left just set the margin-right to 250px
.left {
vertical-align: middle;
min-height: 50px;
margin-right: 250px;
overflow: auto
}
.right {
width:250px;
text-align: right;
display: table-cell;
vertical-align: middle;
min-height: 50px;
height: 100%;
overflow: auto
}
If you need a cross browser solution, you can use my approach, clear and easy.
.left{
position: absolute;
height: 150px;
width:150px;
background: red;
overflow: hidden;
float:left;
}
.right{
position:relative;
height: 150px;
width:100%;
background: red;
margin-left:150px;
background: green;
float:right;
}
Use the simple this can help you
<table width="100%">
<tr>
<td width="200">fix width</td>
<td><div>ha ha, this is the rest!</div></td>
</tr>
</table>

Resources