Note that there are some explanatory texts on larger screens.

plurals
  1. POVertically centered fluid layout with floats but can't add visible margins
    primarykey
    data
    text
    <p>I've been battling with this for a while now, always seeming to get somewhere with one approach, only to then find a problem lurking when I think I'm almost done.</p> <p>In a nutshell, I'm trying to vertically center content in a div that has a height equal to a percentage of the window height. If the content is taller than that of the containing div, then only some portions of the content should scroll.</p> <p>Please see <a href="http://fiddle.jshell.net/verism/G74F9/show/" rel="nofollow">this fiddle result</a> to get an idea of what I'm trying to do; (actual fiddle is <a href="http://jsfiddle.net/verism/G74F9/" rel="nofollow">here</a>).</p> <p>The final (I hope) hurdle is to somehow add a small, fixed amount of space (say, 30px) between the main text and the red "button", when there is enough content to force scrolling. However, everything I've tried - i.e. various combinations of margins and/or padding - just doesn't work. Can somebody please help?</p> <hr> <p><strong>Mandatory Code</strong></p> <p>HTML:</p> <pre><code>&lt;div id="main"&gt; &lt;div id="box"&gt; &lt;div id="content"&gt; &lt;h1 id="heading"&gt;HEADING&lt;/h1&gt; &lt;div id="left"&gt; &lt;div id="leftContents"&gt; &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget diam in mi facilisis condimentum. Nullam at arcu erat. Nullam elit libero, sodales sit amet pellentesque et.&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="button"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>CSS:</p> <pre><code>*{ padding:0; margin:0; } #main { position:absolute; top:0; left:0; right:0; bottom:0; background:lightblue; text-align:center; font-family:arial; } #box{ position:absolute; top:15%; bottom:15%; background:white; left:50%; } #content{ position:absolute; width:100%; top:50%; background:lightgrey; } #left{ float:left; width:50%; overflow:auto; background:lightyellow; margin-bottom:30px; } #leftContents{ /*clear:left;*/ } #button{ height:30px; background:red; position:absolute; bottom:0; left:25%; right:25%; } </code></pre> <p>jQuery:</p> <pre><code>function alignContent() { // Position containing box $box = $('#box'); $box.css({ 'width': $box.height() + 'px', 'margin-left': '-' + $box.height() / 2 + 'px' }); // Size &amp; position content area $content = $('#content'); $content.css({ 'max-height': $box.height() - 72 + 'px', 'margin-top': '-' + ($content.outerHeight() / 2) + 'px' }); // Left panel sizing $left = $('#left'); $max = ($box.height() - 72 - 30 - $('#heading').outerHeight()); $left.css({ 'max-height': $max }); } alignContent(); // Update when resized $(window).resize(function () { alignContent(); }); $(window).load(function () { alignContent(); }); </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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