HTML / CSS : Fixed Margin & Fluid Width - css

How should I make this with CSS:
I would like to have 2 divs or more and their width should be in percent, but the margin between the divs should be fixed, in this example 30px
The problem for me is the margin between the two divs because I can put the divs into a bigger div and set left and right padding to 30px and thats ok, but what should I do with the margin between the two divs?
If I try to add for example to the first div margin-right:30px; then the width of the div will be 70% + 30px what will be overall greater than 100% and the second div will fall down.
So what is the solution for this?

Is this close enough?
Live Demo
HTML:
<div id="container">
<div id="left"><div id="left2">leftgggg</div></div>
<div id="right">right</div>
</div>
CSS:
#container {
margin: 0 30px 0 30px;
overflow: hidden;
background: #f3c
}
#left {
float: left;
width: 70%;
position: relative;
left: -30px;
}
#left2 {
height: 200px;
margin: 0 0 0 30px;
background: #ccc
}
#right {
height: 200px;
float: right;
width: 30%;
background: #666
}

Calc support is decent now, so you can use that to mix and match units. Using that, you can come up with something that works pretty well:
http://jsfiddle.net/CYTTA/1/
#a {
margin-left: 30px;
width: calc(70% - 30px - 15px);
}
#b {
margin-left: 30px;
margin-right: 30px;
width: calc(30% - 30px - 15px);
}
You may have to prefix calc with -webkit or -moz.
The width of the first one is 70% minus the left margin (30px) and minus half the middle margin (15px). The second one is 30% minus the right margin (30px) and minus half the middle margin (15px).
While the widths won't be exactly equal to 70% and 30%, you'll have a fluid layout with fixed margins.

I found a way to do this keeping the ratio of the widths of the containers exactly 70% : 30%. Try this, works for me...
HTML:
<div id="page">
<div id="a"><div id="aWrap">This is 70% of the available space...</div></div>
<div id="b"><div id="bWrap">This is 30% of the available space...</div></div>
</div>
CSS:
*{
margin:0;
padding:0;
}
#page{
margin:30px;
}
#a{
width:70%;
float:left;
}
#aWrap{
margin-right:15px;
background:#aaa;
}
#b{
width:30%;
float:right;
}
#bWrap{
margin-left:15px;
background:#ddd;
}
Best of luck!

It may be obvious, and you've probably already twigged, but (70% + 30% + 30px) > 100%. Without some kind of calculative ability, this won't work, and CSS2 doesn't appear to have that ability. Javascript could do it, as another poster has suggested, and CSS 3 is due to add it, apparently.
Not that it's a solution to your original enquiry, but you can enforce a fixed width on the right hand container, and maintain fluidity on the left.
<div style="margin-left: 30px; float: right; width: 270px;">
<p>Content text ...</p>
</div>
<div style="margin-right: 300px;">
<p>Sidebar text ...</p>
</div>
The original commenter is correct though, your best bet is one or the other.

Here my solution.
html
<div id="wrapper">
<div id="left"></div>
<div id="rightWrapper">
<div id="right"></div>
</div>
</div>
css
#wrapper {
margin:0 30px;
}
#left {
width:70%;
height:200px;
background:black;
float:left;
}
#rightWrapper {
margin-left:99px;
}
#right {
width:30%;
height:200px;
float:right;
background:grey;
}
Demo: http://jsfiddle.net/GEkG7/1/

Yeah, my solution was similar to others. A #wrap has 30px padding, #main and #side have their percentages set and floated left and right respectively. #main has an extra <div> inside it with 30px of right margin.
http://jsfiddle.net/Marcel/FdMFh/embedded/result/
Works fine in all the modern browsers I have installed (Chrome, Firefox, Safari, Opera, IE9 RC), I'm sure it'll break down somewhere older but should be fixable.

Thirtydot has a nice answer (up vote from me) to adjust the left positioning of the div relative to its container, I would only suggest that it may need testing in certain browsers, such as older versions of Internet Explorer.
Alternatively you could consider that adjusting a left position by a fixed amount is more confusing than just applying a different width.
Your also asking for a fluid width and a fixed margin, overall this is no longer a fluid layout... your 30px will look the same in a large or small resolution.. but your widths will change, either fix the widths to pixels or set the margin to a percentage (Maybe try using max-width for some browsers too for bigger resolutions). Newer browsers also adjust a pixel layout when increasing the text/zoom size, older browsers require use of EMs for text size changes.
example with percentages and padding:
#container {
margin: 0 8% 0 8%;
overflow: hidden;
background: #f3c
}
#left {
float: left;
width: 62%;
position: relative;
padding-right: 8%;
}

You can use the javascript onload and onresize functions. In each you first find the width of the container grid and then calculate the width of your 70pc and 30pc grids in pixels and set them via JS.
For example use the following code in your onload and onresize functions for the page:
container_width = document.getElementById('container_box').style.width
width_70 = (container_width - 90) * 0.7
width_30 = (container_width - 90) * 0.3
document.getElementById('seventy_pc_box').style.width = width_70
document.getElementById('thirty_pc_box').style.width = width_30

Related

How to size relative div which haven't a content?

I've a HTML markup, where the typographical content are inside of <p>-tags. Between these tags, there I want to place some images. These images are always the same size: 100% wide, 50% high.
To avoid some distortions, I set a <div>-tag with this size and set the image as a background-image with the cover-size.
This <div> doesn't contains any content, except the background-image. So my sizing won't work, because I can't set it to position: absolute / fixed;, beacuase it wouldn't fit anymore between the <p>-tags.
So how I'm able to size the empty div without losing the the fit?
The HTML markup:
<div class="container">
<section class="about">
<div class="content">
<p>Lorem ipsum dolor</p>
<div class="img"></div>
<p>Lorem ipsum dolor</p>
</div>
</section>
</div>
And the CSS style
.container,
.container > section{
position: fixed;
height: 100%;
width: 100%;
}
.container > section{
overflow:auto;
}
.container > section > .content > p{
padding: 5% 15% 5% 15%;
font-family: Arial, sans-serif;
font-size: 1.8em;
line-height: 1.5;
}
.container > section > .content > .img{
width:100%;
height:50%;
background: url(http://www.hdcarwallpapers.com/walls/widescreen_lamborghini_lp710-wide.jpg) no-repeat center center;
background-size:cover;
}
And a CODEPEN DEMO
I think the problem is the height. Try removing the 50% height and instead add padding of 50%
.container > section > .content > .img{
display: block;
width:100%;
padding: 0 0 50% 0;
background: url(http://www.hdcarwallpapers.com/walls/widescreen_lamborghini_lp710-wide.jpg) no-repeat center center;
background-size:cover;
}
Here's a demo.
I'm not sure what problems you're referring to when you say you want to avoid 'distortions', as by far the simplest solution here is to include the image(s) themselves actually in the markup, and then add some styling to make them responsive.
However, you can achieve what you want with your approach (only browser support suffers). You know the ratio that you want the image to display at. First off, remove the width: 100% from the .img div (it's a div, it will fill the horizontal space). Then, add your 50% as padding:
.img {
padding-bottom: 50%;
}
When you set a height (or vertical padding here) as a percentage, you're actually setting it as a percentage of the width of the parent. That means your .img div will always have padding equal to half of the .content div, which is what you're after.
This is the same approach that's necessary to get fluid videos and iframes working. Check out this CSS Tricks article for a good explanation of what's going on.

Positioning a div within a parent div using auto margin or %

I was under the impression that when using % or auto for margins on a div contained within another div the position would be calculated in respect to the parent div.
So if I have a div with height: 50%, margin-top: 25% and margin-bottom: 25% the box should centre vertically within the parent div.
When I do this though the div centres on the page not the parent div.
The CSS
div#header {
width: 100%;
height: 100px;
margin: 0px;
position: fixed;
}
div#leftnavigation {
height: 50%;
margin-top: 25%;
margin-bottom: 25%;
float: left;
}
And the HTML
<!--Title and navigation bar-->
<div id='header'>
<!--Left navigation container-->
<div id='leftnavigation'>
<p>efwfwgwegwegweg</p>
</div>
</div>
In my case there are other divs floated to the right of the one detailed above, but any one of them behaves the same way. I'm assuming I'm doing something daft but I've been over all the other questions I could find along these lines and still can't figure it out.
EDIT
Here's the JSFiddle as requested http://jsfiddle.net/ChtVv/
UPDATE
I've tried removing the margin constraints and setting the leftnavigation div to height: 100%, this works so the issue is with the margin attribute?
The reason it didn't work is that percentage-margins are percentages of the parent's width, not its height. You can tell this by using margin-top: 25px; margin-bottom: 25px;, and also by increasing the width of the right-panel in jsFiddle.
In all cases % (percentage) is a valid value, but needs to be used
with care; such values are calculated as a proportion of the parent
element’s width, and careless provision of values might have
unintended consequences.
W3 reference
CSS is tricky!! :D
This is a borrowed technique to centre vertically and horizontally, but it would involve changing your HTML and CSS. I am not sure how flexible you are with your code:
CSS:
#outer {width: 100%; border: 3px solid red;}
#middle {width: 100%; text-align: center;border: 3px solid green;}
#inner {width: 200px; margin-left: auto; margin-right: auto; text-align: left;border: 3px solid blue;}
/* Courtesy: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html */
HTML
<!--Title and navigation bar-->
<div id='outer'>
<!--Left navigation container-->
<div id='middle'>
<p id="inner">efwfwgwegwegweg</p>
</div>
</div>
You can build upon this to achieve whatever you are after!
fiddle: http://jsfiddle.net/pratik136/ChtVv/2/
Ok, so there are a lot of reasons why this would not work.
The main reason would be that your container has position:fixed;
When adding position:fixed; to a element, it no longer reserved it's space in the DOM and won't contain it's children.
I have made a example of the best way (in my Opinion) to center your child both Vertically & Horizontally
Here is a demo.
Demo
And here is the code.
<div id="container">
<div id="child"></div>
</div>
#container{
width:100%;
height:500px;
background:#CCC;
margin:0;
}
#child{
width:50%;
height:50%;
background:#EEE;
position:relative;
top:25%;
left:25%;
}

Align all content with the bottom of the page?

I'm trying to align a html-page with the bottom of the browser-window. This is my apporach:
<body>
<div class="outer-wrapper">
</div>
</body>
.outer-wrapper{
min-height: 950px;
width: 100%;
position: absolute;
bottom: 0;
}
The problem with this solution is that when the screen is smaller than 950px high, the top of the outer-wrapper disapears above the screen and no scroll is added. Both the body and the outer-wrapper has a background-image.
Here is a sample, as you can see, the top of the red box is above the body.
http://jsfiddle.net/C5Nce/1/
The following demo should work, if I understand what you want correctly:
http://jsfiddle.net/C5Nce/10/show/
I just used a media query to detect when the page is less than 550px and set the element to be pinned to the top instead:
#media screen and (max-height: 550px) {
.outer_wrapper {
top: 0;
}
}
I've coloured it green so you can tell when the query fires.
.outer {
position:absolute;
height:100%;
width:100%;
background-color:#aaaaaa;
margin:0;
padding:0;
}
.content {
position:relative;
width:90%;
height:90%;
background-color:#444444;
margin:5%;
}
.inner {
position:absolute;
height:20%;
width:100%;
background-color:#eeeeee;
bottom:0;
margin-bottom:10%;
}
<div class="outer">
<div class="content">
<div class="inner"></div>
</div>
</div>
http://jsfiddle.net/L8H9J/
1) Remove the margin-bottom style from the inner class
2) All the content you add inside the inner class will be aligned with the bottom
3) Because of the flow of the document in HTML, you cannot explicitly align them with the
bottom
4) You can use this trick to do so, but again all elements inside the inner class will be
with flow of position:static
5) There comes the use of JavaScript to determine suitable margins for each element inside
the inner class
Tip: Use percentages; although you want the wrapper to be of height ~950px, but if you can use percentages for the dimensions, you would really love watching your web applications scale with the browsers:
I would just give your outer-wrapper a height of 100% (along with html, body):
html, body {
height: 100%;
padding: 0;
margin: 0;
}
.outer-wrapper {
height: 100%;
width: 100%;
position: absolute;
bottom: 0;
overflow-y: auto;
background-position:bottom; //Edit
}
Then the outer-wrapper will always keep the body's height. There’s no need for the 950px height min because in the case that the viewport is too small you wanted for this to scroll and in the other case the viewport is bigger than 950px - well, it's bigger than 950px - that's a good thing.
Edit section from your code here
.outer_wrapper
{
background-color:red;
/*min-height: 550px;*/
margin-left: -75px;
top:auto;
bottom: 0;
position: absolute;
left:50%;
}
and you are specifying your red box is above the body, if you put it inside body it supposed to be placed like it as you also have specify min-height of container.

Unable to align two floating divs horizontally

I'm very new to XHTML and CSS and I can't get the floats to work. Somehow I keep ending up with "fondo_header" below and on the right hand side of "wrapper".
This is the HTML code:
<div id="header">
<div id="wrapper">
<div id="figure">
<img src="images/logo_2nd_225x1182_forWeb.jpg" alt="Logo" width="225" height="118">
</div>
</div>
<div id="fondo_header">
<div id="nav">
<ul>
<li>Inicio</li>
<li>Servicios</li>
<li>Contacto</li>
</ul>
</div>
</div>
</div>
And the CSS:
#header{
margin-top: 20px;
width: 70%;
margin-left: auto;
margin-right: auto;
overflow: hidden;
}
#fondo_header{
width: 74%;
float: right;
background: url(images/header.jpg);
}
#wrapper{
width: 250px;
/*float:left;*/
height:auto;
background-color:#f2dfce;
}
I need to put "wrapper" and "fondo_header" right next to each other. The width for all divs should be correct, I've also tried pixels, different widths, adding margins, padding, different floating styles and way too many things but nothing works.
I've tried IE9, Chrome and FF with the same results. Margins and paddings have been reset.
I'm pretty sure I'm overlooking something pretty obvious at this this point.
Any help much appreciated.
in case ur using floating
the floating element's width should not exceed width of container else one will go on the second line
in ur case 74% for fondo_header + 240px for wrapper is greater then width of header
u can fix that by setting width of wrapper to 26%
or setting static values for both
You have to float both #fondo_header and #wrapper to the left. And when you float elements you have to set overflow: hidden on the parent element, so it fits its content's height.
Take a look at this, it will help you understand floats better: All About Floats.
Edit: Floating #wrapper to the left and #fondo_header to the right also works.
Seems to work alright for me as long as the header is small enough to fit. Are you sure you're giving each element enough room? Using percentages for width of one element and pixels for the other are going to give you different results depending on your screen width.
#header{
margin-top: 20px;
width: 70%;
margin-left: auto;
margin-right: auto;
overflow: hidden;
}
#fondo_header{
width: 30%;
float: right;
background: url(images/header.jpg);
}
#wrapper{
width: 250px;
float:left;
height:auto;
background-color:#f2dfce;
}​
http://jsfiddle.net/ARvw3/

HTML/CSS Div placing

Yo. There's a tendency in placing divs to follow each other vertically, but what i'm trying to accomplish right now is to is basically to place a number of divs (two) inside a parent div like so:
<div id='parent'><div id='onediv'></div> <div id='anotherone'></div> </div>
And i'd like to place 'anotherone' just to the right of 'onediv'. Sadly, float:right is pretty much ruining the layout with the divs popping out of their parent divs and whatnot. Any suggestions are welcome.
Edit: It might be worth noting that the parent div and 'anotherone' has no height elements at all, with 'onediv' planned to be thought as the "height support" div, allowing the contents of 'anotherone' to make the parent div larger at will.
Edit again: Here's the CSS for the specified stuff:
.parent
{
width: 90%;
margin: 0 auto;
border:solid black 1px;
}
.firstchild
{
width: 20%;
margin: 5px;
border: solid black 1px;
height: 180px;
}
.secondchild
{
width: 60%;
border:solid black 1px;
margin: 5px;
}
You can float both inner divs and give the outer div an overflow so that it grows with the inner divs.
Example:
#parent {
overflow: hidden;
}
#parent div {
width: 50%;
float: left;
}
Try this:
<div id="parent">
<div id="onediv" style="float:left;"></div>
<div id="anotherone" style="float:left;"></div>
<div style="clear:both;"></div>
</div>
I think this is what you want (note the re-ordering of DOM elements):
<div id="parent">
<div id="anotherone"></div>
<div id="onediv"></div>
</div>
/*CSS*/
#anotherone{
float:right;
width:50%;
}
#onediv{
float:left;
width:50%;
}
Note, if this is what you want, IE6 will still mess it up. ;-)
You certainly need to specify a width as indicated in #Kevin's answer to get the layout you described, simply specifying float left/right will not have the desired effect. Try specifying the width in pixels rather than a percentage. Failing that or if that's not appropriate for you, I think you possibly need to specify the width of the outer div (through css if you like).
#onediv { float: left; width: 50%; } #anotherone { float: right; width: 50%; }
Just use the <span> tag. Its the equivalent of except it doesn't start a new row.

Resources