繁体   English   中英

如何将元素居中 <div> 和 <h1> , 从上到下?

[英]How to center elements together <div> and <h1>, top to bottom?

我正在尝试模仿以下站点: http : //weareundefined.be/ ,一旦您通过单击它来通过第一页,就会在其下有一台计算机和一小段内容。

使用dev webtool分析网站后,我仍然无法正确确定元素的中心。 我尝试了top: 50%position: relative ,但未正确居中。

我试图分解为必要的CSS,但仍然无法重新创建它。

码:

  <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):

.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;
  }
}

我会丢失什么或做错什么? 他们如何处理元素的大小调整? 任何建议或指导将不胜感激。

预先谢谢您,一定会接受并支持答案。

你近了 htmlbody必须是height: 100%; ,否则,它的孩子将不会是100%的视口。

.container不需要height: 100%; 因为您已经有.containertop: 50%; ,只需使用transform: translateY(-50%); 将其移回其自身宽度的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> 

您还可以将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> 

尝试:

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;
}

并据此进行调整

CSS - 如何堆叠两个<div>元素相互叠加在一起调整大小?</div><div id="text_translate"><p> 我正在尝试将两个&lt;div&gt;组件堆叠在一起,它们也可以根据 window 大小自动调整大小 - 这意味着两个&lt;div&gt;之一将始终位于另一个之上。</p><p> 我想知道这是否可能,有两个不同的 div 组件是position: relative和自动调整大小? 我相信可以用一个position: absolute参与( <a href="https://stackoverflow.com/questions/1834831/how-to-position-two-divs-above-each-over/1834868" rel="nofollow noreferrer">如何 position 两个 div 在每个上方</a>)。</p></div>

[英]CSS - how to stack two <div> elements on top of one another that resize together?

暂无
暂无

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

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