简体   繁体   English

CSS扩展部分,最小高度为100%,垂直居中

[英]CSS expanding section with min-height 100% with vertical centre content

I am looking for an elegant solution to be able to have sections with min-height 100% (window height) where the div expands to fit the content IF the content is longer than 100% OR the content should vertically centre in the div if the content is smaller. 我正在寻找一种优雅的解决方案,以使最小高度为100%(窗口高度)的部分能够在div扩展以适合内容(如果内容长于100%,或者如果内容应垂直于div居中)内容较小。

I have found a simple solution for the 100% and expanding problem which works well, and I also have a solution to vertically centre the content in a div, but this makes the expanding-to-content not work as it involves an position absolute inner wrapper. 我找到了一个简单的解决100%扩展问题的方法,该方法很好用,而且我也有一个解决方案,可将内容垂直放置在div中,但这使扩展到内容的方法不起作用,因为它涉及到一个绝对内部的位置包装纸。

Ideally I would like (but maybe no need for contentWrapper...): 理想情况下,我想要(但可能不需要contentWrapper ...):

<section> (100% height of window or height of child, whichever is larger)
    <div> (content wrapper, 100% height of parent section or stretching to content)
      <p> (whatever content, image, div, etc, which stretches the size of the content wrapper and section, OR vertically centres if small)
      </p>
    </div>
</section>

Here is my current html... 这是我目前的html ...

<section id='a'>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tristique dui tellus, sit amet laoreet est rhoncus sit amet. Donec ac fringilla enim, at molestie orci.
    </p>
    <p>
        Vivamus accumsan id dui vitae laoreet. Donec rutrum magna et magna pulvinar lobortis. Vestibulum non lacinia augue. Nullam scelerisque venenatis enim suscipit vulputate. Vivamus magna ipsum, rhoncus ac laoreet auctor, tristique eget mi. Nam ultricies dui vel fringilla facilisis.
    </p>
    <p>
        Aenean non ultrices risus, varius porta nunc. Morbi eu suscipit purus, eu posuere elit. Vivamus dictum rhoncus odio, id pharetra mi dapibus at.
    </p>
    <p>
        Aenean non ultrices risus, varius porta nunc. Morbi eu suscipit purus, eu posuere elit. Vivamus dictum rhoncus odio, id pharetra mi dapibus at.
    </p>
</section>
<section id='b'>
    ...content
</section>
<section id='c'>
    ...content
</section>
<section id='d'>
    ...content
</section>
...more sections

...and my Stylus... ...还有我的手写笔...

html, body
    width 100%
    height 100%
    padding 0
    margin 0

section
    width 85%
    min-height 100%
    border 1px solid black
    display inline-block
    margin 0
    padding 0
    position relative
    float right

p
    font-size 2em
    font-family 'Helvetica'
    font-weight bold
    width 50%
    margin-left auto
    margin-right auto

And finally a JSFiddle of my current solution: http://jsfiddle.net/L265z/ 最后是我当前解决方案的JSFiddle: http : //jsfiddle.net/L265z/

Many thanks. 非常感谢。

I have come up with a solution using a tiny bit of JQuery, which works well. 我想出了一个使用少量JQuery的解决方案,效果很好。 Although I would still ideally like to do this without JQuery for the sake of keeping the styling separate from the logic, so other solutions are welcome. 尽管我理想情况下仍希望在没有JQuery的情况下执行此操作,以使样式与逻辑保持分离,所以欢迎使用其他解决方案。

HTML HTML

<section>
    <div class='contentWrap'>
        ...content goes here...
    </div>
</section>

CSS CSS

section
    width wrapperWidth
    min-height 100%
    display inline-block
    margin 0
    padding 0
    position relative
    float right
    border-bottom 1px solid black
    .contentWrap
        position absolute
        z-index 1
        display inline-block
        top 50%
        width 100%
        min-height 50%
        height auto
        transform translateY(-50%)
        -webkit-transform translateY(-50%)
        -moz-transform translateY(-50%)
        -ms-transform translateY(-50%)
        -o-transform translateY(-50%)

Coffee/JS (called on load and on resize) Coffee / JS(在加载和调整大小时调用)

heightFix = ->
    $('section').each ->
        if this.children[0].clientHeight > $(window).innerHeight()
            childHeight = this.children[0].clientHeight
            $(this).height(childHeight)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM