简体   繁体   English

垂直对齐h1并将宽度保持在100%

[英]Vertically aligning h1 and keeping width at 100%

I have the following HTML: 我有以下HTML:

<div class="section-cover dark-cyan" ng-style="{height: getCoverHeight()}">
    <div class="container">
        <h1 class="intro">Some intro text.</h1>
    </div>

    <div class="arrow-holder" ng-click="scrollTo('video-container');">
        <span class="arrow"></span>
    </div>
</div>

The .section-cover div changes height dynamically based on viewport size whenever the browser is resized. 每当调整浏览器大小时, .section-cover div都会根据视口大小动态更改高度。 I need to align the <h1> element vertically inside the section-cover div. 我需要在section-cover div中垂直对齐<h1>元素。 So far I've achieved that using display: table-cell however now I can't get the width to stick at 100% . 到目前为止,我已经使用display: table-cell实现了这一点,但是现在我无法将宽度保持在100% Here's a JSFiddle example: 这是一个JSFiddle示例:

http://jsfiddle.net/AvrrM/ http://jsfiddle.net/AvrrM/

How can I modify this to vertically align the <h1> and keep the width at 100%? 如何修改此值以垂直对齐<h1>并将宽度保持在100%?

Maybe something like this? 也许是这样的吗?

http://jsfiddle.net/AvrrM/1/ http://jsfiddle.net/AvrrM/1/

.section-cover {
    position: relative;
    display: table;
    vertical-align: middle;
    width: 100%;
}

.container{
    display: table-cell;
    background: red;
    text-align: center;
    vertical-align: middle;
}

Why is itsn't working?? 为什么不起作用? because table-cell is similar to html table, you need to provide a container which is table type before you can make the table-cell occupy the full width!! 因为table-cell类似于html table,所以您需要提供一个table类型的容器,然后才能使table-cell占据整个宽度!

working fiddle 工作小提琴

add this CSS in your markup : 将此CSS添加到您的标记中:

html, body {
    width: 100%;
    height:100%;
    margin:0;
    padding:0;
}
#table {
    width: 100%;
    display:table;
    text-align:center;
}

HTML HTML

<div id="table">
<div class="section-cover dark-cyan" style="background: blue; color: white; height: 200px">

<!-- all inner html here -->

</div>
</div>

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

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