Note that there are some explanatory texts on larger screens.

plurals
  1. POpercentages and CSS margins
    primarykey
    data
    text
    <p>I have create a jsFiddle to demonstrate my problem:</p> <blockquote> <p><a href="http://jsfiddle.net/MXt8d/1/" rel="nofollow">http://jsfiddle.net/MXt8d/1/</a></p> </blockquote> <pre><code>.outer { display: inline-block; vertical-align: top; overflow: visible; position: relative; width: 100%; height: 100%; background: red; } .inner { overflow: hidden; height: 50%; width: 100%; margin-top: 25%; margin-bottom: 25%; background: blue; opacity: 0.7; color: white; } &lt;div class="outer"&gt; &lt;div class="inner"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>The thing is that when i need to horizontally center a div inside another. I specify the height of the inner div in % (eg. 50%) and then the margin-top and margin-bottom to the remaining (eg. (100 - 50) / 2 = 25 %).</p> <p>But as you see in the jsFiddle it's not working as intended.</p> <p>Calculating the margins from the Parent works, but it's not possible for me, because I dont have access to the div's parent, as the elements-style object is bound to the object via knockout.js and it's not so simple as shown in the jsFiddle.</p> <p>Hope anyone could help me :-)</p> <p>bj99</p> <p><strong>Update:</strong></p> <p>Just found out why this is actually happening, so I'll post here for peaple with similar problems:</p> <blockquote> <p>From <a href="http://www.w3.org/TR/CSS2/box.html#propdef-margin-top" rel="nofollow">http://www.w3.org/TR/CSS2/box.html#propdef-margin-top</a> :</p> <p>'margin-top', 'margin-bottom' Percentages: refer to <em><strong>width</strong></em> of containing block</p> </blockquote> <p>And not as I tought to the <strong>height</strong> :-/</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload