Positioning a div between two others - css

I have 3 divs vertically. The first should have 100% width, the second should have an image with width 283px, and third div should have 100% width.
Does anyone know how to position the image div in the middle of two others divs 100%?
I've tried this, but dont works for me
<div class="content">
<div class="first">1</div>
<div class="third">3</div>
<div class="second">2</div>
</div>
.first{
width:100%;
float:left;
background:yellow;
}
.third{
width:100%
float:right;
background:pink;
}
.second{
width:283px;
overflow:hidden;
background:blue;
}​enter code here

If your intention is to position the divs next to each other horizontally than you can't have any of them set to a width of 100% as the total of all elements next to each other can only total 100%.
If your website will be fixed width than your easiest solution would be to set the width of the left and right div in pixels to the (width of the site - 283) / 2. Then they would float next to each other. You could also do this with %.
However if your site is fluid width, then you would need to work out a percentage for all 3 divs i.e 33% each but this would mean the middle won't be exactly 283px.
The only way I can think to make this work exactly as you want would be to use Javascript to resize the elements after the page load which could then get the browser dimensions and work it all out.

Having read it a few times i think i get what you want to do.
You want he middle div to be centred between the two other divs.
you need to give the div a class something like this:
.middlediv
{
width: 283px;
margin-left: auto;
margin-right: auto;
}
which can also be written like this:
.middlediv
{
width: 283px;
margin: 0px auto;
}

Your question isn't clear, so I've done both possible interpretations: http://jsfiddle.net/EbBzY/
HTML
<h1>Option 1</h1>
<div class="main">
<div class="content">
<div class="first">1</div>
<div class="second">2</div>
<div class="third">3</div>
</div>
</div>
<h1>Option 2</h1>
<div class="content">
<div class="first">1</div>
<div class="second">2</div>
<div class="third">3</div>
</div>
CSS
.main{
display:table;
width:100%;
margin-bottom:100px;
}
.main .content{
display:table-row;
}
.main .content div{
display:table-cell;
}
.first{
background:yellow;
}
.third{
background:pink;
}
.second{
background:blue;
width:283px;
margin:auto;
}

Related

How do I make <div> stretch vertically with the amount of content?

I have a vertical div with five little divs inside. Now, I want the first two and the last two to have specific heights, and the one in the middle to be changeable, depending on the amount of content inside of it. Also, there should probably be a minimum height to the entire div, so it can fit the page nicely, and the bottom two divs are supposed to be fixed to the bottom of the page. I am only allowed to use CSS. Since I don't have any code for this, let's say those four fixed divs have the height of 100px, and they are named #one, #two, etc.
Thanks in advance.
Can you just use:
.two {
min-height: 100px;
height: auto;
}
https://jsfiddle.net/5cLd2ybv/
You can do the following, all you have to do is set the elements
display:block;
here's an example:
#container{
border:1px solid black;
}
.element{
display: block;
height:100px;
width:100px;
margin:0px;
border:1px solid;
}
<div id="container">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>

CSS layout width fixed width and fluid right column

I have a fixed with container, lets say 1120px. Inside this container, i have a left sidebar which is 400px, and i need a right sidebar which is expanding from the container and touching the right side of the screen. Here is an image explaining the layout i want:
This is the progress i made so far: http://jsfiddle.net/UvxK8/
#wrapper {
background:#f0f0f0;
margin:0 auto;
width:400px;
overflow:hidden;
}
#wrapper .left {
width:100px;
float:left;
height:600px;
background:#333;
}
#wrapper .right {
position:absolute;
margin-left:100px;
height: 650px;
background: green;
min-width:100%;
}
Obviously its not good, because the right sidebar is too wide and a horizontal scrollbar appears.
The image you've used doesn't match your desicription of what you want; I think maybe you're looking at this the wrong way. If the right sidebar spills beyond the container, what's the point of the fixed width container at all?
From your image, it looks like what you want is...
HTML
<div id="header"><h1>Content here.</h1></div>
<div id="container">
<div id="container-left">
<p>Content here.</p>
</div>
<div id="container-right">
<p>Content here.</p>
</div>
</div>
CSS
#header {width:1120px;}
#container {width:100%; min-width:100%;}
#container-left {width:400px; float:left; }
#container-right {width:100%; min-width:100%;}
This will create the image illustration you've used. jFiddle here: http://jsfiddle.net/4JNX2/
Add position relative to your wrapper. try this
HTML
<div id="wrapper">
<div id="left">
</div>
<div id="right">
</div>
</div>
CSS
#wrapper
{width:1120px;
background:#096;
margin:0 auto;
position:relative;
}
#wrapper #left {
width:10%;
float:left;
height:600px;
background:#333;
}
#wrapper #right {
position:absolute;
margin-left:10%;
height: 650px;
background: green;
min-width:95%;
}

How to make a display:table-cell div cover adjoining table-cell divs?

I am trying to create two same-height columns using display:table-cell, and then place a third same-height overlay div over the top of the first two columns to hide them. The two same-height columns work. But I can't figure out how to make the third div the same height as the first two, AND be on top of them.
Goals:
Column 1 and 2 must be the same height at all times. The height
cannot be explicitly set, they must both take the height of whichever
column is taller based on the column's contents.
The cover must be the exact height and width of the row it covers, not explicitly set.
I am looking for a solution that does not use JavaScript.
Please see the following fiddle: http://jsfiddle.net/rEAYb/1/
HTML
Some filler content that should not be covered.
<div class="Table">
<div class="Row">
<div class="Cell Left">
Left<br/>
sdfgsdfg<br/>
sdfgsd<br/>
fgsdfg<br/>
sdfg<br/>
dsfgsdfg<br/>
sdfgsdfgdsfg<br/>
</div>
<div class="Cell Right">Right</div>
<div class="Cell Cover"> </div>
</div>
</div>
Some filler content that should not be covered.
CSS
.Table{
display:table;
}
.Row{
display: table-row;
position:relative;
}
div.Cell{
padding:18px;
padding-bottom:60px;
padding-top:40px;
width:480px;
display: table-cell;
}
div.Cover{
background:#444;
opacity:.5;
position:absolute;
top:0px;
left:0px;
}
div.Left{
background:green;
}
div.Right{
background:blue;
}
You can get the effect that you want as follows:
First, alter the HTML as follows:
<div class="Cover"></div>
The overlay can be a simple block element, so remove the .Cell class. Note that the .Cover element can be left empty.
The CSS needs to be adjusted as follows:
.Table {
display:table;
position:relative;
}
.Row {
display: table-row;
}
div.Cover {
background:#444;
opacity: 0.9;
position:absolute;
top:0;
bottom: 0;
left:0;
right: 0;
}
Apply position: relative to .Table instead of .Row.
On div.cover, add the additional box offsets for bottom and right.
Fiddle: http://jsfiddle.net/audetwebdesign/pyxaN/
This positioning relies on CSS 2.1 so it should pretty much in most browsers.

Div position - css

I'm trying to achieve, that the div's will behave like an example on picture, using css:
Is there any clean way to do this? I achieve this using javascript to calculate "left" div height and "main" div width and height. But i dont like this solution...is there any way to do this using css only?
Edit:
Page must not have scrollbar...so page's height is always max 100%, and no more...
thanks
If the sidebar (or any other div) is 100% height, and on top you have a 30px header, so that causes your container to be 100% + 30px height.
In the future you will have in css3 calc():
http://hacks.mozilla.org/2010/06/css3-calc/
This will solve your problem.
But for now you can add overflow: hidden; to the html and body section, but I recommend calculate the height of the sidebar ( container height - header height) using Javascript.
Check fiddle here
If you mean the two-column layout, you do it with pure CSS like this:
.container {
background-color: #aaaaaa;
}
.left {
float: left;
width: 100px;
clear: left;
}
.right {
margin-left: 100px;
background-color: #888888;
}
and HTML:
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
</div>
Live demo: jsFiddle
The div on top can be achieved without any special CSS. To place something below (a footer for example), you'll need to use clear: both.
Without any code it is hard to determine what you want. Here is a extremely simple version of what I believe you want.
HTML:
<div id="header">
</div>
<div id="side">
</div>
<div id="content">
</div>
CSS:
#header {
width:100%;
height:50px;
}
#side {
width:300px;
height:100%;
float:left;
}
#content {
width:660px;
height:100%;
float:left;
}
jsFiddle

A div between 2 divs width 100%

On my page I have div structure like this.
-On body there are 2 divs. First 20% width, second 80% width.
-In first div there are 3 divs alongside. First floats left: 11px width, Third floats right: 22px width. I wanna place 2nd div between 1st and 3rd divs covers 100% of the remaining width.
I cannot make the 2nd div like this. How can I do it?
Write like this:
HTML
<div class="firstdiv">
<div class="first">1</div>
<div class="third">3</div>
<div class="second">2</div>
</div>
<div class="secdiv">80%</div>
CSS
.firstdiv{
width:20%;
float:left;
background:red;
}
.secdiv{
overflow:hidden;
background:green;
}
.first{
float:left;
width:11px;
background:yellow;
}
.third{
float:right;
width:22px;
background:pink;
}
.second{
overflow:hidden;
background:blue;
}
Check this fiddle

Resources