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

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>

Related

First div fixed, second full width. Cant change structure of html

Have problem. I have this code.
<div class="main">
<div class="sidebar"></div>
<div class="content"></div>
</div>
I need to make two colums.
"Sidebar" must have fixed width 200px;
And "content" all remaining width to fullscreen.
I cant change the structure of html code, just css.
if absolute position is ok, you can use it to say left:200px; right:0 and get all the space you need
fiddle : http://jsfiddle.net/h2udmqhn/
.main {
position: relative;
height: 300px;
width: 300px;
border: 1px solid black;
}
.sidebar {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 200px;
background: red;
}
.content {
position: absolute;
top: 0;
left: 200px;
right: 0;
height: 200px;
background: blue;
}
<div class="main">
<div class="sidebar"></div>
<div class="content"></div>
</div>
Use float: left for .sidebar and left margin for .content:
.sidebar {float: left; width: 200px; background: red;}
.content {background: green; margin: 0 0 0 200px;}
http://jsfiddle.net/orty5qtj/1/
Another option is to use calc, which is unsupported in IE8. The solution above works fine in all browsers.
Try this :
.sidebar {
float: left;
min-height: 50px;
background: red;
width: 200px;
}
.content {
background : yellow;
margin-left: 200px;
min-height: 50px;
}
https://jsfiddle.net/Saiyam/5krmkkkx/3/
There a couple of simple ways to do this without the need for calc, margins or absolute positioning. Both of the following ways have the added bonus of keeping the columns the same height as each other
Using display table (compatible to back ie8)
.main {
display: table;
width: 100%;
}
.main > div {
display: table-cell;
}
.sidebar {
width: 200px;
background: blue;
}
.content {
background: red;
}
<div class="main">
<div class="sidebar">200px</div>
<div class="content">the rest</div>
</div>
Using flex (for newer browsers only unless used with the browser prefix):
.main {
display: flex;
width:100%;
max-width:100%;
}
.sidebar {
width: 200px;
flex: 0 0 200px;
background-color:blue;
}
.content {
background-color:red;
flex: 1 0 auto;
}
<div class="main">
<div class="sidebar">200px</div>
<div class="content">the rest</div>
</div>

why the `img right` collapse?

JSFiddle.
<div class="content clearfix">
<div class="left">img Left</div>
<div class="center">Text Center</div>
<div class="right">img Right</div>
</div>
.content {
width: 100%;
height: 80px;
border:1px solid #333;
}
.center {
width: 400px;
margin:0 auto;
text-align: center;
}
.left {
float: left;
}
.right {
float: right;
}
I used float to layout the page. the left and the center element perform well. But the img Right was broken. I can't find the reason and what's more I used line-hegiht, the collapse is worse. Thanks for your advantage.
Your .center element is not floated and is centered. If you inspect it, you see its horizontal margins going on left and right (margin : 0 auto;).
For the .left element, it's ok, it comes before the .center element and it is out of the float, so the .center element just ignore it.
But the .right element comes after, and it has to consider the previous HTML element (here : .center). That's why .right element is going under .center element.
So, some solutions :
you can use absolute positioning for the .right element : position: absolute; top: 0; right: 0; (there : http://jsfiddle.net/pg7v4js3/2/ )
other solutions depend on what is the real layout you try to achieve
This is a new fiddle: Fiddle
.clearfix:after{
}
.content{
width: 100%;
height: 80px;
border:1px solid #333;
}
.center{width: 80%; float: left; text-align: center;}
.left{float: left; width:10%;}
.right{float: right; width:10%;}

Place three DIV boxes horizontally and centered

I'd like to place three DIV boxes horizontally and centered. If i resize (narrower) the browser boxes should take place vertically and centered.
----BrowserWide-----
______X X X______
----BrowserWide-----
----BrowserNarrow-----
________X_________
________X_________
________X_________
----BrowserNarrow-----
This is my html:
<div class="premium_features">
<div class="premium1">
<h2>Some Heading</h2>
<p>Some paragraph, Some paragraph, Some paragraph, Some paragraph, Some paragraph,
</p>
</div>
<div class="premium2">
<h2>Some Heading</h2>
<p>Some paragraph, Some paragraph, Some paragraph, Some paragraph, Some paragraph,
</p>
</div>
<div class="premium3">
<h2>Some Heading</h2>
<p>Some paragraph, Some paragraph, Some paragraph, Some paragraph, Some paragraph,
</p>
</div>
</div>
This is my css:
.premium1 {
background: url("4.png") no-repeat top center;
padding-top: 95px;
float:left;
width: 33%;
min-width: 300px;
max-width: 320px;
text-align:center;
margin-left: auto;
margin-right: auto;
margin-bottom: 20px;
}
.premium2 {
background: url("5.png") no-repeat top center;
padding-top: 95px;
float:left;
padding-top: 95px;
float:left;
width: 33%;
min-width: 300px;
max-width: 320px;
text-align:center;
margin-left: auto;
margin-right: auto;
margin-bottom: 20px;
}
.premium3 {
background: url("6.png") no-repeat top center;
padding-top: 95px;
float:left;
width: 33%;
height: 100px;
min-width: 300px;
max-width: 320px;
text-align:center;
margin-left: auto;
margin-right: auto;
margin-bottom: 20px;
}
.premium_features {
width: 75%;
margin-left: auto;
margin-right: auto;
}
With this code: It is OK when it is wide and when it is narrow.
But during resize 2 of 3 boxes are staying at the same block for a while. I need to sort them vertically when resizig starts.
Thanks for help.
There are many ways to centre elements:
Margin way:
With a set width or display: inline-block; you can use:
margin-left: auto;
margin-right: auto;
text-align way:
With a set width or display: inline-block; you can add this to the parent:
text-align: center;
Absolute way:
position: absolute;
left: 50%;
margin-left: width/2;
or
position: absolute;
left: 50%;
transform: translate(0, -50%);
Also don't worry too much about ie7 and below as the the majority of people use higher versions of ie or a different browser though this should work up until ie6
Place your three divs within one large div and style that div
#largeDiv{ margin : 0 auto 0;}
I'm assuming this is what you meant:
HTML:
<div id="longone">long one</div>
<center>
<div id="box"><img src="http://placehold.it/100x100"></div>
<div id="box"><img src="http://placehold.it/100x100"></div>
<div id="box"><img src="http://placehold.it/100x100"></div>
</center>
CSS:
#longone
{
display: block;
width: 100%;
border-bottom: 1px solid black;
}
#box
{
display: inline-block;
width: 100px;
}
http://jsfiddle.net/R9ENg/
Although, personally, I'd use <ul>s and <li>s.
Are you looking for something like this?
http://jsfiddle.net/a2cPu/
HTML
<div class="container-wrapper">
<div class="container-1"></div>
<div class="container-2"></div>
<div class="container-3"></div>
</div>
CSS
.container-wrapper {
text-align:center;
}
.container-1, .container-2, .container-3 {
width:200px;
height:200px;
display:inline-block;
margin-left:-4px;
}
.container-1 {
background:red;
margin-left:0;
}
.container-2 {
background:green;
}
.container-3 {
background:blue;
}
#media all and (max-width: 650px) {
.container-1, .container-2, .container-3 {
width:100%;
display:block;
margin:0;
}
}
you can use mediaqueries and test screenwidth to apply different style :
example mediaquerie + display:flex;
demo: http://codepen.io/anon/pen/qmFlt/
#container {
display:flex;
flex-direction:row;
justify-content:center;
}
#container div {
width:200px;
height:100px;
background:gray;
margin:auto 1em;
}
/* next block, #media, can be clone to set other media queries */
#media only screen
and (max-width : 800px) {/* under 800px width , this CSS is overriding precedent rules */
#container {
flex-direction:column;
}
#container div {
margin:1em auto;
}
<div id = "container">
<div>box
</div>
<div>box
</div>
<div>box
</div>
</div>
You can do it setting display:inline-block / block the #container div and drop the display flex rules for #container, keep margins for the 3 div .
demo: http://codepen.io/anon/pen/utflx/

min-width for wrapper DIV not working

I've two floated DIVs (two columns) which are nested in an "clear-float"-DIV, which itself is nested in an centered DIV ("wrapper" DIV).
<div id="content">
<div class="block2">
<div id="slot_left">
CONTENT-LEFT
</div>
<div id="slot_right">
*CONTENT-RIGHT*
</div>
</div>
</div>
The right column has min-width and max-width CSS option set. But the wrapper DIV, which has min-width and max-width also, is always expanded to max width.
#content {
min-width: 300px;
min-height: 80px;
max-width: 350px;
margin: 0 auto;
background: #c00;
}
.block {
overflow: hidden;
_overflow: visible;
_overflow-x: hidden;
_height: 0;
}
#slot_left {
width: 200px;
background: #ff0;
float: left;
position: relative;
}
#slot_right {
float: left;
background: #cc0;
min-width: 100px;
max-width: 150px;
position: relative;
}
What's the reason for that? I want the wrapper DIV to has minimum width required but to be centered on screen.
Here is an fiddle.
use display:inline-block
why this is happening?? div is by default block level element, so when you have given max-width, it will always obey it to occupy max area possible....
http://jsfiddle.net/sHB7g/3/
CSS
#content {
min-width: 300px;
min-height: 80px;
max-width: 350px;
margin: 0 auto;
background: #c00;
display:inline-block
}
.block {
display:inline-block;
overflow: hidden;
border:1px solid #000;
}
http://jsfiddle.net/sHB7g/1/
#content {
display: inline-block;
}
and then added a content wrapper
#contentwrapper {
text-align: center;
}
the html then is like this
<div id="contentwrapper">
<div id="content">
<div class="block">
<div id="slot_left">
CONTENT-LEFT
</div>
<div id="slot_right">
*CONTENT-RIGHT*
</div>
</div>
</div>

Div containing other divs does not have proper height

I have a 'frame' containing two divs which are respectively aligned on the left and on the right. Unfortunately, the main div does not have the proper height to englobe the inner divs.
Here is the HTML:
<div id="frm">
<div id="a">aaa<br>aaa</div>
<div id="b">bbb</div>
</div>
and the CSS:
#frm {
background: red;
width: 100%;
height: auto;
}
#a {
background: blue;
float: left;
}
#b {
background: green;
float: right;
}
Here is the JsFiddle: http://jsfiddle.net/mPH4H/
I should see a red frame, but there is none.
The floated elements are removed from the flow of the document, so the parent container thinks that it has nothing inside of it. You can add overflow:auto to your CSS rules for #frm to bring the background back and "contain" the floated children:
#frm {
background: red;
width: 100%;
height: auto;
overflow:auto;
}
jsFiddle example
overflow:hidden; will give height to #frm
Try:
#frm {
background: red;
width: 100%;
height: auto;
overflow:hidden;
}
DEMO here.
OR
Clear floats:
HTML:
<div id="frm">
<div id="a">aaa<br>aaa</div>
<div id="b">bbb</div>
<div class="clr"></div>
</div>
CSS:
.clr{clear:both;}
DEMO here.
i think this is worked as fine:
#frm {
background: red;
width: 100%;
height: auto;
}
#a {
background: blue;
width: 50%;
float: left;
}
#b {
background: green;
width: 50%;
float: right;
}

Resources