If I have a div which I want to center between two other divs, one floated left and one floated right, giving it a margin of exactly 10 pixels on either side (between it and the div on that side), how would I give it a fluid width, such that it would get wider or narrower as the window size is changed, but always maintain a distance of exactly 10 pixels between it and the divs on either side?
Try this fiddle. Solution works with pure CSS.
The trick is: width: 230px; /* 10 more width than column size */
http://jsfiddle.net/ngdS9/1/
Here's a fiddle of my answer:
http://jsfiddle.net/utThB/
If your left and right div elements are of variable witdh, the only way I can see this done is to dynamically get the width of the left and right divs via jQuery (or pure JavaScript, ask me for this version) and then adding the margin style to the centered div.
jQuery:
$(document).ready(setWidth);
function setWidth(){
var $left_width = $(".left").width() + 10;
var $right_width = $(".right").width() + 10;
$(".center").css("margin-left",$left_width + 'px');
$(".center").css("margin-right",$right_width + 'px');
}
HTML:
<div class="container">
<div class="left">Variable width DIV</div>
<div class="right">Another.</div>
<div class="center">Centered Div</div>
</div>
CSS:
.container{width:100%;}
.left{
float:left;
background-color:green;
}
.center{
text-align:center;
background-color:blue;
width:auto;
}
.right{
float:right;
background-color:red;
}
Try this. Here's a fiddle
HTML
<div id=wrapper>
<div id=leftdiv>left</div>
<div id=rightdiv>right</div>
<div id=main>
Lorum Ipsum
</div>
</div>
CSS
#main {
padding-right:60px;
padding-left:60px;
}
#leftdiv {
float:left;
width:50px;
background-color:#ddd;
}
#rightdiv {
float:right;
width:50px;
background-color:#ddd;
}
Related
I am trying to create a page footer with some text on the left, on the right, and centered on the page. I've been following examples such as this and I'm having the same problem with all of them: The content in the center div is centered between the borders of the left and right divs, not centered on the body. That is, if left/right are not the same width then the center is off-center.
I can't use fixed widths because I know neither the content nor the font size. I do know the content will be just a few words each.
I can't use explicit proportional widths either for similar reasons; I don't know the proportions of the content and e.g. the center may be short with a left or right side greater than 1/3 of the page width.
I don't actually have to use divs, I just am because that seems to be the way this is done... but anything that will get me a left + body centered + right aligned footer-style layout will work (as long as it works on all common browsers).
I don't care what happens when contents overlap; they can either overlap, or word-wrap, or do something else ugly.
Currently the closest I've gotten is this CSS:
#left { float:left; }
#right { float:right; }
#center { float:none; text-align:center; }
And this HTML:
<div id="container">
<div id="left">...</div>
<div id="right">...</div>
<div id="center">...</div>
</div>
But I am seeing this (an extreme example):
Here is a fiddle: http://jsfiddle.net/HCduT/
I've tried various combinations of float, display, overflow, and margin, but I can't seem to get this right.
Edit: I've also tried http://jsfiddle.net/nshMj/, recommended by somebody elsewhere, but it's got the same issue (with the disadvantage that I don't really understand what it does).
How do I make the content in the center div aligned to the page, rather than centered between the left and right divs (which have different sizes)?
I'm not 100% sure what you're after. Here's what I did get:
You want the left div on the left
You want the right div on the right
You want don't want to limit the width of those to 33%;
You want the center to always be dead center.
You don't care about overlapping content
If I got that right, then try this out: DEMO
CSS:(The color is just so you can distinguish the content, as it overlaps)
#content {
text-align:center;
}
#container {
position: relative;
}
#left {
float:left;
}
#right {
float:right;
color: #ccc;
}
#center {
text-align:center;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
color: red;
}
After some research i ended with this:
html
<body>
<div id="content">center point V center point</div>
<div id="container">
<div id="left">L</div>
<div id="center">C</div>
<div id="right">RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR</div>
<br style="clear: left;" />
</div>
</body>
css
#content {
text-align:center;
}
#left {
width:33%;
float:left;
}
#right {
width:33%;
float:left;
overflow:hidden !important;
}
#center {
float:left;
width:33%;
text-align:center;
}
#container{
width:100%;
}
fiddle
I am building a responsive page layout, but I have a question:
How can I manage width, padding, margin ETC. in percentage(%)?
For example, I want to have my page left side bar 700px and right side bar 300px so what property do I have to use in percentage(%).
Here is my code :
HTML:
<div class="main_div">
<div class="left_bar">content goes here</div>
<div class="right_bar">content goes here</div>
</div>
CSS:
.main_div {
width:1000px;
float:left;
}
.left_bar {
width:300px;
float:left;
background-color:green;
padding:15px 0;
}
.right_bar {
width:270px;
float:left;
background-color:red;
padding:15px;
}
I am assuming the issue you have is that your boxes don't stay on one line when you set percentages.
you need to be careful
width + left/right margin + left/right padding + left/right border = width of container.
like this : FIDDLE
I have page that loads data in dynamically. I have put the image on the left and some text on the right. In two column by using float:left;
This works fine but the height of the containing div does not change to match the height of the larger div.
I have soemthing like this:
<div class="container">
<div class="left">//some php to load image</div>
<div class="right">//loaded text</div>
</div>
.container{
width:800px;
height:auto;
}
.left,.right{
float:left;
height:auto;
}
.left{
width:300px;
}
.right{
width:500px;
}
The divs are next to eachother but the containing div only resizes to the height of the smallest div. Shouldn't it resize to the height of the largest div?
Add overflow:auto to the .container element..
I think this is a typical clearfix problem.
Read here about clearfix: What is a clearfix?
I am in need of some help with some divs.
I currently have
<body>
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
</body>
and I am trying to make it so the following occurs.
Left div aligns against left side of the screen, while right aligns to the right. The main then would be 1000px wide and be in the middle of the page
| Left Div | Main div 1000px wide | Right div |
I have seen this done using tables but I'd rather use a div to create my layout.
the main div will have other divs inside it for contents so I will also need the left and right divs height being the same as the main div.
Anyone able to help?
Regards,
Mason
You can do table-like layouts using divs. Check out the table-* values for CSS display.
http://jsfiddle.net/MVSjr/
HTML
<div class="table">
<div>
<div id="left">test</div>
<div id="center">test</div>
<div id="right">test</div>
</div>
</div>
CSS
#left {
background:#F00
}
#center {
background:#0F0;
width: 300px;
}
#right {
background:#00F
}
.table {
display:table;
width:100%;
}
.table > * {
display:table-row;
}
.table > * > * {
height: 200px;
display:table-cell
}
If you want a constant distance between the left / right divs to the main one, you'll have to specify it. My advise is to specify a page width, say 1300 px, and then split div's measures as you need (e.g: 150px for the left div, 1000px for the middle, 150px for the right. And then center the wrapper div to the page.
.wrapper{
width: 1300px;
margin: auto;
}
.left{
width:150px;
}
.main{
width:1000px;
}
.right{
width:150px;
}
You might also want to consider making it responsive either by specifying width in % if your content allows it or by specifying break-points in media-queries, but it's just a thought...
You are looking for three column layout for your web page. Three column layout can be either fixed or fluid.
In Fixed layout all the three columns would have the fixed width and when you re-size your browser screen, they will display with horizontal scroll bar.
In Fluid layout all the three columns would have width in percentage of screen width. Here is an example of three column fluid layout.
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
and CSS
div{
display:inline-block;
height:500px;
}
.left,.right{
background:red;
width:15%;
}
.main{
background:green;
width:70%;
}
Js Fiddle Demo
I have 3 divs, 'left div', 'right div' and 'bottom div'. I have been hopelessly trying to position them in a certain way but it just doesn't work. This is how I would ideally like them to be:
The main problem is the fact that 'left div' and 'right div' are of different heights so how would I sort this mess out?
EDIT: I would also like to add that the left and right divs need to be centered within the content area.
<div id="content">
<div class="left">
Left
</div>
<div class="right">
Right
</div>
<div class="bottom">
Bottom
</div>
</div>
css:
#content{
width:1000px;
margin:0 auto;
}
.left{
width:495px;
float:left;
margin-right:10px;
}
.right{
width:495px;
float:left;
}
.bottom{
clear:both;
margin-top:10px;
}
then if you want the left and right to match:
(using jquery)
$(document).ready(function(){
var leftdiv = $('.left').height();
var rightdiv = $('.right').height();
if(leftdiv > rightdiv)
$('.right').css('height', leftdiv + 'px');
else
$('.left').css('height', rightdiv + 'px');
});
Statically set their heights or use javascript to match them if the height is dynamic based on content.
Here's the HTML for the divs, remove the #container if you don't want to center them:
<div id="container">
<div class="left">Left</div>
<div class="right">Right</div>
<div class="bottom">Bottom</div>
</div>
Here is the CSS:
#container {
width: 200px;
margin: 0 auto;
}
.left {
float: left;
width: 100px;
background: silver;
}
.right {
float: right;
width: 100px;
background: red;
}
.bottom {
clear: both;
background: aqua;
}
I just used the colors to highlight the position. You can of course change the widths of the divs, but remember that the #container width has to be the width of .left + .right.
Read this to find out more about floats.
You can find a demo of the code here: http://jsfiddle.net/teaJb/
float the left and right div's to their respective directions. set identical padding right and left on the containing div. on the bottom div apply "clear:both" to clear the float and "margin:0 auto" to center it relative to the container.
if you don't match the heights, obviously the bottom div would appear after the higher div. you can match height using jquery's .height() method to compare the actual heights and set the higher value for both divs. Alternatively, you can just set a CSS rule for a constant height or a dynamic min-height.