[英]How to center elements together <div> and <h1>, top to bottom?
I'm trying to mimic the following site: http://weareundefined.be/ and once you get passed the first page by clicking it on it, there is a computer and a short paragraph below it. 我正在尝试模仿以下站点: http : //weareundefined.be/ ,一旦您通过单击它来通过第一页,就会在其下有一台计算机和一小段内容。
After analyzing the site using dev webtool, I still am not able to center the elements properly. 使用dev webtool分析网站后,我仍然无法正确确定元素的中心。 I attempted the top: 50%
with position: relative
, yet it is not centered correctly. 我尝试了top: 50%
, position: relative
,但未正确居中。
I tried to break down to the necessary CSS, but still not able to recreate it. 我试图分解为必要的CSS,但仍然无法重新创建它。
Code: 码:
<div style={{height: '100%’}}>
<div className="container">
<div id="rotate-container">
<div>
Center Me
</div>
</div>
<h1> We are undefined</h1>
<p>We're a creative agency with a focus on digital.</p>
</div>
</div>
CSS (SCSS): CSS(SCSS):
.container {
height: 100%;
position: relative;
padding: .5em;
margin: 0 auto;
max-width: 400px;
text-align: center;
top: 50%;
}
#rotate-container {
div {
color: #fb3131;
font-size: 40px;
font-weight: bold;
display: block;
}
}
What could I be missing or doing incorrectly? 我会丢失什么或做错什么? And how are they handling the resizing of elements? 他们如何处理元素的大小调整? Any suggestions or guidance would be greatly appreciated it. 任何建议或指导将不胜感激。
Thank you in advance and will be sure to accept and upvote answer. 预先谢谢您,一定会接受并支持答案。
You're close. 你近了 both html
and body
need to be height: 100%;
html
和body
必须是height: 100%;
, too, otherwise it's children won't be 100% of the viewport. ,否则,它的孩子将不会是100%的视口。
.container
doesn't need height: 100%;
.container
不需要height: 100%;
. 。 Since you already have .container
at top: 50%;
因为您已经有.container
在top: 50%;
, just use transform: translateY(-50%);
,只需使用transform: translateY(-50%);
to shift it back up 50% of it's own width so the center of it is in the center of the browser. 将其移回其自身宽度的50%,因此其中心位于浏览器的中心。
body, html { height: 100%; } .container { position: relative; padding: .5em; margin: 0 auto; max-width: 400px; text-align: center; top: 50%; transform: translateY(-50%); } #rotate-container div { color: #fb3131; font-size: 40px; font-weight: bold; display: block; }
<div style="height:100%;"> <div class="container"> <div id="rotate-container"> <div> Center Me </div> </div> <h1> We are undefined</h1> <p>We're a creative agency with a focus on digital.</p> </div> </div>
You can also use flexbox with align-items: center;
您还可以将flexbox与align-items: center;
body, html { height: 100%; } .container { position: relative; padding: .5em; margin: 0 auto; max-width: 400px; text-align: center; } #rotate-container div { color: #fb3131; font-size: 40px; font-weight: bold; display: block; }
<div style="height:100%; display: flex; align-items: center;"> <div class="container"> <div id="rotate-container"> <div> Center Me </div> </div> <h1> We are undefined</h1> <p>We're a creative agency with a focus on digital.</p> </div> </div>
Try: 尝试:
body {
min-width: 970px;
padding-top: 70px;
padding-bottom: 30px;
}
.container {
width: 970px;
max-width: none !important;
padding-right: 10px;
padding-left: 10px;
margin-right: auto;
margin-left: auto;
text-align: center;
}
And adjust accordingly 并据此进行调整
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.