2017-06-22 16 views
0

我有一個div使命陳述應該是整個可查看瀏覽器的寬度和高度如何在居中文本內部時繼承父div?

<div id="mission-statement"> 
    <video playsinline autoplay muted loop poster="{{ url_for('static',filename='images/cclc-background-image.png') }}" id="bgvid"> 
     <source src="{{ url_for('static',filename='videos/cclc-background-video.mov') }}" type="video/webm"> 
    </video> 
    <div id="mission-statement-text"> 
    <h1>Map-Based Operations Management for Enterprise</h1> 
    </div> 
</div> 

#mission-statement{ 
    margin-top:auto; 
    margin-bottom:auto; 
    height: 100vh; 
    width:100vw; 
} 

這部分不會顯示爲白色與黑色的邊框

#mission-statement h1{ 
    color: white; 
    text-shadow: -3px 0 black, 0 3px black, 3px 0 black, 0 -3px black; 

} 

這是沒有按部分似乎沒有工作

#mission-statement-text{ 
position: relative; 
top: 50%; 
transform: translateY(-50%); 
} 

它基本上是整個網頁的50%。我希望它的下降50%的方式(垂直居中)

回答

1

你需要position: absolute如果該文本被認爲上面出現/母體內。

#mission-statement { 
 
    margin-top: auto; 
 
    margin-bottom: auto; 
 
    height: 100vh; 
 
    width: 100vw; 
 
} 
 

 
#mission-statement h1 { 
 
    color: white; 
 
    text-shadow: -3px 0 black, 0 3px black, 3px 0 black, 0 -3px black; 
 
} 
 

 
#mission-statement-text { 
 
    position: absolute; 
 
    width: 100%; 
 
    text-align: center; 
 
    top: 50%; 
 
    transform: translateY(-50%); 
 
}
<div id="mission-statement"> 
 
    <video playsinline autoplay muted loop poster="{{ url_for('static',filename='images/cclc-background-image.png') }}" id="bgvid"> 
 
     <source src="{{ url_for('static',filename='videos/cclc-background-video.mov') }}" type="video/webm"> 
 
    </video> 
 
    <div id="mission-statement-text"> 
 
    <h1>Map-Based Operations Management for Enterprise</h1> 
 
    </div> 
 
</div>

+0

真棒。出於某種原因,當我嘗試水平居中文本時,通過添加transform:translateX(-50%);它不認爲這是適用的。有沒有關於這個位置的東西:絕對會阻止這樣的其他事情發揮作用? – BigBoy1337

+0

只需添加'width:100%'和'text-align:center'。 (我在我的編輯版本中做過)。有寬度和/或高度設置時,絕對位置更可靠 – Johannes