30

For example, I have a div that has a height of 100px (I don't know the height, but let's suppose I did). I want to set the margin-bottom to a percent, so 25% would be 25px assuming the previous height. However, the percent seems to be of the document, not the element:

<div style="height:100px;margin-bottom:100%"></div>

The margin should be 100px but it isn't, it is 100% of the height of the page.

The element is just a line of text that has no background, so using height:150% theoretically could also work.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
JCOC611
  • 19,111
  • 14
  • 69
  • 90

4 Answers4

51

How about a CSS3 solution:

div {        
   -webkit-transform: translateY(-50%);
   -moz-transform: translateY(-50%);
}
ShibbyUK
  • 1,501
  • 9
  • 12
6

http://www.w3.org/TR/CSS21/box.html#margin-properties

Percentages: refer to width of containing block

If your DIV is in the BODY element, then the containing block is the BODY element, so the percentage will be based on the BODY width - which is in most cases the same as the width of the viewport.

Demo: http://jsfiddle.net/jghsF/1/

(Try resizing the width of the browser window and you will see that the margin-bottom changes)

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
5

As others note, I don't know you can use CSS to do this. jQuery could help:

http://jsfiddle.net/userdude/PZAvm/

<div id="margin">Foo</div>

div#margin {
    background-color:red;
    height:100px;
}

$(document).ready(function(){
    alert($('#margin').height());
    var margin = $('#margin').height()/4;
    $('#margin').css('margin-bottom',margin);
    alert($('#margin').css('margin-bottom'));
});

EDIT - This could possibly be done using em's.

EDIT 2 - em's are keying off font size, not a calculated box model size. So it won't work.

EDIT 3 - JCOC611 was able to use the em approach after all.

Original: http://jsfiddle.net/userdude/xN9V7/3/

JCOC611's Demo: http://jsfiddle.net/BCTg2/

The code:

<div id="foo">
    Foo
</div>
lol

div#foo {
    background-color: #fcc;
    margin-bottom: 1.5em;
    font-size:20px
}
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • The problem is that the height is going to be changing a lot, so I was hoping for a CSS answer. (of course I could update the margin w/ javascript but that would just take too much code) – JCOC611 Feb 05 '11 at 21:47
  • 1
    Who said the height would be changing by text wrapping? – JCOC611 Feb 05 '11 at 21:49
  • I think that deriving the height for an element from a calculation that relies on width is not going to work (as Sime Vidas points out with his reference to the docs). If you could post an example of your usage with the html and your css, that might help device a solution with your specific needs in mind. Btw, I personally think it silly that margin-top and margin-bottom rely on the containing width. :\ – Jared Farrish Feb 05 '11 at 21:53
  • Oop, nevermind. em's are keying off the font size, not margin, so when I updated the height, it still gave the same margin height. – Jared Farrish Feb 05 '11 at 22:14
  • It was your idea to use EM's, so if you update your answer to fit the fiddle I just posted, I'll mark it as accepted. – JCOC611 Feb 05 '11 at 22:22
  • yes, because the margin will change depending on the font size – JCOC611 Feb 05 '11 at 22:23
  • Ok, I updated it. Interesting how things work out sometimes. :) – Jared Farrish Feb 05 '11 at 22:27
  • This, however, will only work for one-line texts. Fortunately, that's my scenario. For anyone else, beware! PS: Yes it's pretty interesting! Thanks a lot! – JCOC611 Feb 05 '11 at 22:34
1

This question is a great deal more fascinating than I'd expected (+1).

I'm working with the following html structure:

<div id="someContent">
    <p>Lorem ipsum.</p>
</div>

Originally I tried to use padding to simulate a 'border' (JS Fiddle demo):

#someContent {
    background-color: #000;
    border-radius: 1em;
    padding: 10%;
}

p {
    background-color: #fff;
}

This on the assumption that the padding would be derived from the height of the element itself, which turned out to be a wrong-assumption.

After that clearly failed I tried to use a margin on the contained elements, on the assumption that if the margin of the containing element is based on its parents, so too should the margin of the contained element, giving the following CSS:

#someContent {
    background-color: #000;
    border-radius: 1em;
}

p {
    margin: 10%;
    background-color: #fff;
}

JS Fiddle demo. And this failed, too.

I suspect that it's impossible without defining a height for the parent element, which might require a JS solution.

David Thomas
  • 249,100
  • 51
  • 377
  • 410