im trying to build 3 divs. "left" "Center" and Right" all float to left. The "left" and "Right" div is fix in width (like side bars). The "center" div (content) is either auto or %.
When i re size my browser, In the case of (eg center div 60%), the right div is pushed to a lower position. If i use (eg center div auto) I get "left" div in place, "center" div takes all remaining space and the "right" div below.
What I need is, "left" and "right" div fix dimension 250px. The "Center" one fills the remaining space between the "left and right" div. And on resize only the "center div" width changes to fit.
(Note: Of course im using media query) This stuff will be helpful to me from 750px to 950px.)
<style type="text/css">
#left {
float:left;
height: 200px;
width: 250px;
border: medium dashed #03F;
}
#center {
float:left;
height: 200px;
width: 60%;
border: medium solid #C00;
}
#right {
float:left;
height: 200px;
width: 250px;
border: medium dashed #0C6;
}
</style>
<div id="left">
</div>
<div id="center">
</div>
<div id="right">
</div>
Check this: http://jsfiddle.net/87enE/
Create a wrapper and use display:table;. Then use dislay: table-cell for child elements:
HTML:
<div id="wrapper">
<div id="left"></div>
<div id="center"></div>
<div id="right"></div>
</div>
CSS:
#wrapper{
width: 100%;
display: table;
}
#left {
display: table-cell;
height: 200px;
width: 250px;
border: medium dashed #03F;
}
#center {
display: table-cell;
height: 200px;
border: medium solid #C00;
}
#right {
display: table-cell;
height: 200px;
width: 250px;
border: medium dashed #0C6;
}
For responsive concern, better use percentage instead of fixed width:
Fiddle: http://jsfiddle.net/87enE/3/
You should put all div under table tr td and give first and last td to fix size you want(in %) and center to 100%.
Try this:
<style type="text/css">
#left {
float:left;
height: 200px;
width: 100%;
border: medium dashed #03F;
}
#center {
float:left;
height: 200px;
width: 100%;
border: medium solid #C00;
}
#right {
float:left;
height: 200px;
width: 100%;
border: medium dashed #0C6;
}
</style>
<table style="width:100%;">
<tr>
<td style="width:20%;">
<div id="left">
</div>
</td>
<td style="width:60%;">
<div id="center">
</div>
</td>
<td style="width:20%;">
<div id="right">
</div>
</td>
</table>
here is a demo
Note: If you wish to keep it responsive, you need to give all width in % instead of px. Also you can get more details about about media queries here
It should help you!
Thanks.
In cases such as this, I believe FlexBox handles the situation the best. #center's width will change to take up the remaining space.
<style>
#container {
display:flex;
flex-flow: row nowrap;
}
#left {
height: 200px;
width: 250px;
order:0;
border: medium dashed #03F;
}
#center {
flex:1;
order:1;
height: 200px;
border: medium solid #C00;
}
#right {
height: 200px;
width: 250px;
order:2;
border: medium dashed #0C6;
}
</style>
You can try this:
Fiddle Here:
<div id="wrapper" class="clearfix">
<div class="div" id="left">
</div>
<div class="div" id="center">
</div>
<div class="div" id="right">
</div>
</div>
Good Luck..
Related
Just wondering if this is possible for css, i am try to have a layout where left hand column have a fixed width and right hand side have a flex width within a contain of fixed width.
here is the attachment for image
Thanks for any suggestion.
Check if this can help you
HTML
<div class="outer">
<div class="left">Test</div>
<div class="right">Test</div>
</div>
CSS
*{margin: 0}
.outer {
max-width: 1444px;
}
.left {
float: left;
width: 200px;
height: 600px;
background: red
}
.right {
margin-left: 200px;
background: yellow;
height: 600px;
}
You can do it with a table for sure. Not sure if anyone has a better/alternate method.
<html>
<head>
<style>
.container { width:400px; }
.left { width:100px; background-color:red;}
.right { width:100%; background-color:yellow;}
</style>
</head>
<body>
<table class="container">
<tr>
<td class="left">left section</td>
<td class="right">right section</td>
</tr>
</table>
</body>
</html>
You can achieve it by using the following code. Basically we are floating the left side div with fixed width and letting the right side div take up the rest.
HTML:
<div class='container'>
<div class='fixed-left'>abcd</div>
<div class='flexible'>12345</div>
</div>
CSS:
.container{
border: 1px solid black;
max-width: 440px;
}
.fixed-left{
float: left;
width: 200px;
border: 1px solid blue;
}
.flexible{
border: 1px solid red;
width: 100%;
}
Demo|Demo without Margin
You could use float for that:
HTML:
<div class="left-div">This is left DIV with lots of text text text text<br />and even more text</div>
<div class="right-div">
<div class="upper">This is upper right DIV</div>
<div class="lower">This is lower right DIV</div>
</div>
CSS:
.left-div {
width: 200px;
float: left;
}
.right-div {
float: right;
}
.upper {
max-width: 100%;
}
.lower {
max-width: 1444px;
}
Fiddle: http://jsfiddle.net/68u8N/
How do I align the red box with the gray box vertically?
http://jsfiddle.net/sLZzK/1/
I need several box combinations like that on my page, which is why I cannot simply push the red box up manually. A negative margin won't work either, since I do not know in advance how much content will be in the gray box. And the red box must overlap other page content, hence the absolute positioning. (http://jsfiddle.net/xMm82/)
CSS:
body {
padding: 0;
margin: 10px;
}
.left_div {
margin-bottom: 10px;
width: 300px;
height: 70px;
border: 1px solid gray;
}
.right_div {
position: absolute;
border: 1px solid red;
left: 311px;
width: 200px;
height: 200px;
}
HTML:
<div class="left_div">gray box
<div class="right_div">red box</div>
</div>
Why are you using absolute positioning for such structure? In the case the better solution is to use float: left for each div. If you want to have two divs aligned vertically use display: table-cell rule. Here it is:
FIDDLE
UPDATE: Try to use this:
FIDDLE
what I've understood is you want gray box on top of Red box:
first of all wrap them in a parent div.
set the width of wrapper to desirable width.
set width to 100%(both red and gray) and you are done !! (fiddle)
If you want to arrange them horizontally:
left_div will be wrapper
it will contain 2 child div's
left one will have content and right one will be red box.(fiddle)
I would do it this way:
HTML:
<div class="container">
<div class="left_div">gray box</div>
<div class="right_div yellow">red box</div>
<div class="clr"></div>
</div>
CSS:
.container:not(:last-child){margin-bottom: 10px;}
.left_div,.right_div{float:left;}
.clr{clear:both;}
Fiddle here.
use float to arrange vertically and clear:both to prevent any errors
here's the corrected one
.left{
float:left;
width: 300px;
}
.right{
float:left;
width: 200px;
}
.left_div {
width: 300px;
height: 70px;
border: 1px solid gray;
}
.right_div {
border: 1px solid red;
width: 200px;
height: 200px;
}
<div class="left">
<div class="left_div">
</div>
</div>
<div class="right">
<div class="right_div">
</div>
</div>
<div style="clear:both"></div>
http://jsfiddle.net/sLZzK/8/
There you go: http://jsfiddle.net/sLZzK/14/
HTML:
<div class="wrapper">
<div class="left_div">gray box</div>
<div class="right_div">red box</div>
</div>
CSS:
.wrapper {
border: 1px solid #369;
padding: 10px;
}
.wrapper > div {
display: inline-block;
vertical-align: middle;
}
You might also want to read about flexbox which will give you a similar and more consistent result, however it's not fully supported on various browsers yet.
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 ;)
I need the following in a header of fixed width:
A div of varying width floated left.
A div of varying width floated right.
An h2 centered between them that takes up any remaining space.
The floated divs contain content that may vary in size.
I've tried various approaches but they have all failed. I know one solution is to absolutely position the outer divs, then stretch the h2 out for the full width and center the text so it sits centrally, but there must be a nicer way to do this.
A basic jsFiddle example with minimal markup.
HTML
<div id="container">
<div id="left">left</div>
<div id="right">right</div>
<h2>H2</h2>
</div>
CSS
#container {
border:1px solid #999;
}
#left {
float:left;
}
#right {
float:right;
}
h2 {
text-align:center;
margin:0;
}
You could use display: inline-block instead of float, and then use CSS calc to get the right width for the middle div:
HTML:
<div id="wrapper">
<div id="one"></div><div id="two"></div><div id="three"></div>
</div>
CSS:
#wrapper {
min-width: 300px;
}
#one, #two, #three {
display: inline-block;
height: 300px;
}
#one {
background: lightgreen;
width: 100px;
}
#two {
background: lightblue;
width: 100%;
width: calc(100% - 300px);
width: -webkit-calc(100% - 300px);
width: -moz-calc(100% - 300px);
}
#three {
background: lightgreen;
width: 200px;
}
jsFiddle Demo
You can then put the h2 inside the the middle div, in this case #two.
Considering the following HTML:
<div id="parent">
<div id="left">Left</div>
<h2>Heading</h2>
<div id="right">Right</div>
</div>
CSS Code:
#parent {
width: 80%;
margin: auto;
display: table;
}
#parent div, #parent h2 {
display: table-cell;
}
#left, #right {
width: 50px;
}
Demo: http://jsfiddle.net/MAhmadZ/pMfLx/
try this out
i think it may solve your problem
<style type="text/css">
div{
display: inline-block;
vertical-align: top;
height: 50px;
border: 1px solid red;
position: static;
}
#one{
float: left;
width: 100px;
}
#three{
float: right;
width: 100px;
}
</style>
<div id="outerDiv" style="width: 500px;height: 500px;border: 1px solid red;">
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
</div>
<script type="text/javascript">
var spaceLeft = document.getElementById("one").offsetWidth;
var spaceRight = document.getElementById("three").offsetWidth;
var totalSpace = document.getElementById("outerDiv").offsetWidth;
document.getElementById("two").style.width = totalSpace-(spaceLeft+spaceRight+4) + "px";
</script>
I have two divs like this
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
I want them to display on the same row, so I used float:left.
I want both of them to be at center of the page as well, so I tried to wrap them with another div like this
<div style="width:100%; margin:0px auto;">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
But it doesn't work. If I change the code to this
<div style="width:100%; margin-left:50%; margin-right:50%">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
then it's going to the center, but the horizontal scrollbar is there and it seems like it's not really centered as well.
Can you please kindly suggest to me how can I achieve this? Thanks.
Edit: I want the inner div (Div 1 and Div 2) to be center align as well.
You could do this
<div style="text-align:center;">
<div style="border:1px solid #000; display:inline-block;">Div 1</div>
<div style="border:1px solid red; display:inline-block;">Div 2</div>
</div>
http://jsfiddle.net/jasongennaro/MZrym/
wrap it in a div with text-align:center;
give the innder divs a display:inline-block; instead of a float
Best also to put that css in a stylesheet.
Could this do for you? Check my JSFiddle
And the code:
HTML
<div class="container">
<div class="div1">Div 1</div>
<div class="div2">Div 2</div>
</div>
CSS
div.container {
background-color: #FF0000;
margin: auto;
width: 304px;
}
div.div1 {
border: 1px solid #000;
float: left;
width: 150px;
}
div.div2 {
border: 1px solid red;
float: left;
width: 150px;
}
both floated divs need to have a width!
set 50% of width to both and it works.
BTW, the outer div, with its margin: 0 auto will only center itself not the ones inside.
Align to the center, using display: inline-block and text-align: center.
.outerdiv
{
height:100px;
width:500px;
background: red;
margin: 0 auto;
text-align: center;
}
.innerdiv
{
height:40px;
width: 100px;
margin: 2px;
box-sizing: border-box;
background: green;
display: inline-block;
}
<div class="outerdiv">
<div class="innerdiv"></div>
<div class="innerdiv"></div>
</div>
Align to the center using display: flex and justify-content: center
.outerdiv
{
height:100px;
width:500px;
background: red;
display: flex;
flex-direction: row;
justify-content: center;
}
.innerdiv
{
height:40px;
width: 100px;
margin: 2px;
box-sizing: border-box;
background: green;
}
<div class="outerdiv">
<div class="innerdiv"></div>
<div class="innerdiv"></div>
</div>
Align to the center vertically and horizontally using display: flex, justify-content: center and align-items:center.
.outerdiv
{
height:100px;
width:500px;
background: red;
display: flex;
flex-direction: row;
justify-content: center;
align-items:center;
}
.innerdiv
{
height:40px;
width: 100px;
margin: 2px;
box-sizing: border-box;
background: green;
}
<div class="outerdiv">
<div class="innerdiv"></div>
<div class="innerdiv"></div>
</div>
I would vote against display: inline-block since its not supported across browsers, IE < 8 specifically.
.wrapper {
width:500px; /* Adjust to a total width of both .left and .right */
margin: 0 auto;
}
.left {
float: left;
width: 49%; /* Not 50% because of 1px border. */
border: 1px solid #000;
}
.right {
float: right;
width: 49%; /* Not 50% because of 1px border. */
border: 1px solid #F00;
}
<div class="wrapper">
<div class="left">Div 1</div>
<div class="right">Div 2</div>
</div>
EDIT: If no spacing between the cells is desired just change both .left and .right to use float: left;
Better way till now:
If you give display:inline-block; to inner divs then child elements of inner divs will also get this property and disturb alignment of inner divs.
Better way is to use two different classes for inner divs with width, margin and float.
Best way till now:
Use flexbox.
http://css-tricks.com/snippets/css/a-guide-to-flexbox/
Please take a look on flex it will help you make things right,
on the main div set css display :flex
the div's that inside set css: flex:1 1 auto;
attached jsfiddle link as example enjoy :)
https://jsfiddle.net/hodca/v1uLsxbg/