Two DIV's with auto-width [duplicate] - html

This question already has answers here:
CSS side by side div's auto equal widths
(5 answers)
Closed 8 years ago.
In my layout in the main part of site I need two flexible columns. The height is known and always the same. But the width should be auto-increases with the width of the browser.
I need this because in div#1 should be different content (float right I supposed) and background than in div#2 (float left I supposed). Whole layout is increasing their width with browser (width 100%).
It would be easy to make if the background of div 1 and 2 is the same (wrapper + background set on parent) but in this example backgrounds are different. I do not know how to auto-increase the width of these two divs.

adjust the width parameter of div#1 and div#2
div #header {
display: block;
position: absolute;
top: 0px;
height: 100px;
}
div #div1 {
display: block;
position: absolute;
top: 100px;
width: 50%;
left: 0px;
}
div #div2 {
display: block;
position: absolute;
top: 100px;
width: 50%;
right: 0px;
}

this is what you want:
<div class="container">
<div class="header">
This is header
</div>
</div>
<div class="container">
<div class="div1">
This is left div
</div>
<div class="div2">
This is right div
</div>
</div>
<div class="container">
<div class="footer">
This is footer
</div>
</div>
.container{
max-width:960px;
padding:0 15px;
height:auto;
position:relative;
clear:both;
}
.header{
position:relative;
height:100px;
background-color:yellow;
width:100%;
display:block;
}
.div1{
position:relative;
height:400px;
background-color:pink;
width:50%;
float:left;
}
.div2{
position:relative;
height:400px;
background-color:green;
width:50%;
float:left;
}
.footer{
position:relative;
height:100px;
background-color:cyan;
width:100%;
display:block;
}
I've created a jsfiddle demo

What about a good, simple, semantic layout? The below uses positioning to maintain a fixed footer, here is an example without
Demo Fiddle
HTML
<header></header>
<section></section>
<section></section>
<footer></footer>
CSS
html, body {
height:100%;
width:100%;
margin:0;
}
header, footer, section {
position:absolute;
width:100%;
}
header, footer {
background:green;
height:50px;
}
footer {
bottom:0;
}
section {
top:50px;
bottom:50px;
width:50%;
background:yellow;
overflow-x:auto;
}
section:last-of-type {
background:blue;
left:50%;
}

Try this
#container{
width:100%;
clear:both;
}
#header{
width:100%;
}
#div1{
height:400px;
width:50%;
float:left;
}
#div2{
height:400px;
width:50%;
float:right;
}
#footer{
width:100%;
}

Related

How to put a footer on bottom using css?

I've a simple web page that contains a <div> on background (a green rectangle on background) and a second <div> that is the "body" it contains paragraphs, table etc
And on bottom I need to to put a simple footer containing juste copyrights and some socials networks buttons. The problem is : the footer is not on bottom, there is a space under the footer, how to avoid this please ?
See my simple code please
On jsfiddle is better (to see the space under the footer) see here please
.bg-green{
width:100%;
height:100px;
background-color:green;
}
.content{
width:80%;
height:300px;
margin:-50px auto;
background-color:gold;
text-align:center;
}
footer{
width:100%;
height:65px;
background-color:red;
opacity:0.5;
}
<div class="bg-green">
</div>
<div class="content">
this is the "body" of my page (kind of a wrapper) it needs to be like this (with negative margin top)
</div>
<footer>this is the footer</footer>
You forgot to remove default margin of body.
Set in css:
body {
margin: 0;
}
Fiddle
body {
margin: 0;
}
.bg-green{
width:100%;
height:100px;
background-color:green;
}
.content{
width:80%;
height:300px;
margin: -50px auto;
background-color:gold;
text-align:center;
}
footer{
width:100%;
height:65px;
background-color:red;
opacity:0.5;
}
<div class="bg-green">
</div>
<div class="content">
this is the "body" of my page (kind of a wrapper) it needs to be like this (with negative margin top)
</div>
<footer>this is the footer</footer>
For best practice always set body and html 0 margin and 0 padding.
body,html{
margin:0;
padding:0;
}
Add this to .footer
margin-top:50px;
Perhaps you want to stick your footer to the bottom?
Clear the paddings and margins by:
html, body {
padding:0;
margin:0;
}
then
footer{
width:100%;
height:65px;
background-color:red;
opacity:0.5;
position:absolute;
bottom:0;
}
Working fiddle
Set the min height of body to 100% and set position to absolute.
.bg-green{
width:100%;
height:100px;
background-color:green;
}
.content{
width:80%;
height:300px;
margin:-50px auto;
background-color:gold;
text-align:center;
}
html, body{
padding: 0;
margin: 0;
}
html{
height: 100%;
}
body{
min-height: 100%;
}
footer{
width:100%;
height:65px;
background-color:red;
opacity:0.5;
position: absolute:
bottom: 0;
}
<div class="bg-green">
</div>
<div class="content">
this is the "body" of my page (kind of a wrapper) it needs to be like this (with negative margin top)
</div>
<footer>this is the footer</footer>
For that case you just need to set the margin and padding of body tag to 0.
body {
margin: 0;
padding: 0;
}
Or if your site have margins specified you can only set the bottom margin of body as.
body {
margin-top: 0;
margin-bottom: 0;
padding: 0;
}
body {
margin: 0;
}
.bg-green{
width:100%;
height:100px;
background-color:green;
}
.content{
width:80%;
height:300px;
margin:10px auto;
background-color:gold;
text-align:center;
}
footer{
width:100%;
height:65px;
background-color:red;
opacity:0.5;
}
<div class="bg-green">
</div>
<div class="content">
this is the "body" of my page (kind of a wrapper) it needs to be like this (with negative margin top)
</div>
<footer>this is the footer</footer>
Modify content css:
.content{
width:80%;
height:300px;
margin:10px auto;
background-color:gold;
text-align:center;
}
.bg-green{
width:100%;
height:100px;
background-color:green;
}
.content{
width:80%;
height:300px;
margin:-50px auto;
background-color:gold;
text-align:center;
padding-top:100px;
}
footer{
width:100%;
height:65px;
margin-top: -65px;
background-color:red;
opacity:0.5;
position: absolute;
bottom: 0
}
body {
margin: 0;
min-height: 100%;
padding-bottom: 100px;
position: relative;
box-sizing: border-box;
}
html {
height: 100%;
}
<div class="bg-green">
</div>
<div class="content">
this is the "body" of my page (kind of a wrapper) it needs to be like this (with negative margin top)
</div>
<footer>this is the footer</footer>
You can fix footer at bottom by position: absolute;
Updated fiddle
Use position: absolute;bottom: 0; Style in footer class
footer
{
width: 100%;
height: 65px;
background-color: red;
opacity: 0.5;
position: absolute;
bottom: 0;
}
Click Here Live Demo

Positioning A Circle In The Middle Of Two Divs

I have one DIV positioned right.
.right {
width:25%;
height:100%;
background-color:#000;
position:fixed;
right:0px;
z-index:1;
And left
.left {
width:25%;
height:100%;
background-color:#000;
position:fixed;
left:0px;
z-index:1;
And I'm trying to put this circle
.circle {
height:100px;
width:100px;
border-radius:50px;
background-color:#F00;
position:fixed;
left:45%;
z-index:99;
in the middle
this is my HTML
<div class="left">
</div>
<div class="centerc">
<div class="circle">
</div>
<div class="right">
</div>
What am I doing wrong and how do I fix it?
Your code seems to be working. However, the circle is off-center.
I suggest that you define the circle's position as 50% of the container's width minus 50% of the circle's width:
.circle {
...
width:100px;
left:50%;
margin-left:-50px;
}
Also, since everything is position:fixed, I don't see the purpose of div.centerc. I removed it.
<div class="left"></div>
<div class="circle"></div>
<div class="right"></div>
Working example (jsFiddle)
Try to put this inside .circle styling:
left:50%;
margin-left:-50px;
left:50%; will put the left side of the .circle in the middle of the screen, then margin-left:-50px; will put the .circle 50px to the left (half of its width).
Also, it's a good idea to remove the non-closed .centerc div.
Demo
*{margin:0;}
body{
background:#fff;
}
.left{
position:fixed;
height:100%;
width:25%;
left:0;
background:#222;
}
.circle{
z-index:1;
position:fixed;
width:100px;
height:100px;
left:50%; /* Left side of the circle centered */
margin-left:-50px; /* A half of circle width to the left */
border-radius:50px;
background:#F33;
}
.right{
position:fixed;
height:100%;
width:25%;
right:0;
background:#222;
}
<div class="left"></div>
<div class="circle"></div>
<div class="right"></div>
If you are meaning to have the left and right divs aligned and the circle div floating over them in dead center here's a quick fiddle to set you in that direction.
http://jsfiddle.net/Hastig/zLsbE/
I added a container div wrapped around all three (left, right and circle) and set it to position: relative
I then set the circle div to position: absolute and played with it's left and top alignment to center it.
Note - It's not a responsive solution.
.container {
width: 500px;
height: 250px;
position: relative;
}
.left {
float: left;
width: 250px;
height: 250px;
background-color: #000;
}
.right {
float: right;
width: 250px;
height: 250px;
background-color: #555;
}
.circle {
height: 100px;
width: 100px;
border-radius: 50px;
background-color: #F00;
position: absolute;
top: 75px;
left: 200px;
}
<div class="container">
<div class="left"></div>
<div class="circle"></div>
<div class="right"></div>
</div>
http://jsfiddle.net/R8YRh/1/ demonstrates using:
.centerc {
text-align:center;
}
and the addition of display: inline-block; to .circle. This required the addition of top: 0; to .right.
.left {
width:25%;
height:100%;
background-color:#000;
position:fixed;
left:0;
z-index:1;
}
.right {
width:25%;
height:100%;
background-color:#000;
position:fixed;
right:0;
top: 0;
z-index:1;
}
.centerc {
text-align:center;
}
.circle {
display: inline-block;
height:100px;
width:100px;
border-radius:50px;
background-color:#F00;
z-index:99;
}
<div class="left"></div>
<div class="centerc">
<div class="circle"></div>
</div>
<div class="right"></div>

How do you position or float one div next to an absolute div

I want a div to appear side-by-side with a google map. However, the google map HAS to be absolute positioned in order for it to take a percentage height.
<style>
#text{
height:40%;
width:40%;
float:left;
display:inline;
margin:10px;
background-color:white;
}
#map{
height:100%;
width:40%;
float:left;
position:absolute;
}
</style>
<div id="map1"> </div>
<div id="infoBox">text goes here </div>
While you are at it just go ahead with:
position: absolute;
left: 40%;
on the text element. You'll maintain your margin-left of 10px this way.
#infoBox{
height:40%;
width:40%;
margin:10px;
background-color:white;
position: absolute;
left: 40%;
}
#map{
height:100%;
width:40%;
float:left;
position: absolute;
}
http://jsfiddle.net/22Dhy/
#map {
left: 40%; /* width of #infoBox */
margin-left:10px; /* greater than or equal to left margin of #infoBox */
}

Need div to fill gap between two divs

Given the following HTML:
<div id="wrapper">
<div id="header">*header*</div>
<div id="content">*content*</div>
<div id="footer">*footer*</div>
</div>
And the following CSS:
#wrapper {
min-height:100%;
position:relative;
}
* {margin:0;padding:0;height:100%;}
#header {
width:100%;
height:auto;
margin-top: 0;
}
#content {
width:100%;
height: auto;
margin:0;
margin-top:0;
margin-bottom:0;
}
#footer {
width:100%;
height:auto;
position:absolute;
margin-top: 0;
margin-bottom: 0;
bottom:0;
left:0;
}
There is a gap between the content and the footer's div. How can I set that the content's height must be all the space between the header and the footer?
The footer has to have the 'absolute' position to position is at the bottom of the page.
Try using display:table, table-row options
display:table to #wrapper
display:table-row to #header, #content (width and height should be 100% here) and #footer
#wrapper {
min-height:100%;
position:relative;
display: table;
width:100%
}
#header {
width:100%;
height:auto;
margin-top: 0;
background:#dbfcd6; display: table-row;
}
#content {
width:100%; display:table-row; background:green; height:100%
}
#footer {
width:100%;
height:auto;
position:absolute;
margin-top: 0;
margin-bottom: 0;
bottom:0;
left:0; background:yellow;
display: table-row;
}
DEMO
It is because your footer has a bottom:0 and its position is absolute.This will make it stuck at the bottom.
You should give your content a min height-and max-height like this:
#content {
background-color:red;
width:100%;
min-height:450px;
max-height: auto;
margin:0;
margin-top:0;
margin-bottom:0;
}
Then change the position of the footer to relative
Check out this fiddle : )Check me!

How to use the width that is left

I am having a left div width a fixed width of 200 px, then I want the content area to take the space that is left. How can I solve that? I have done this...but it doesn't work.
#sidebar {
float:left;
width:200px;
height:100%;
background-color: blue;
}
#mainContent {
float:left;
width: // USE WHATEVER SPACE IS LEFT;
height:100%;
background-color: red;
}
Have a look at this code:
http://jsfiddle.net/Ffx8R/
CSS:
#sidebar {
float:left;
width:200px;
height:100%;
}
#mainContent {
padding-left:200px;
height:100%;
border:1px blue solid;
}
#container
{
height:200px;
clear:both;
}
HTML:
<div id="container">
<div id="sidebar">here is sidebar info</div>
<div id="mainContent">main Content info</div>
</div>