简体   繁体   English

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

[英]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%; htmlbody必须是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%; 因为您已经有.containertop: 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 并据此进行调整

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.

相关问题 如何使 div 组件覆盖在任何其他组件(h1 或 p)之上 - How to make div component go over on top of any other component (h1 or p) h1 元素没有出现 - h1 elements not appearing 如何向此基本代码添加格式(粗体、H1 和中心) - How to add formatting to this basic code (bold, H1, and center) 如何编辑不包含H1标签的div的CSS? - How to edit the CSS of a div that does not contain a H1 tag? 如何使用javascript在div中添加h1 - how to add a h1 inside a div using javascript 如何使用 <h1> 在c3.js div中? - how to use <h1> in c3.js div? 如何在javascript中将多个h1元素替换为h1,h2,h3,h4,h5,h6? - How can I do multiple h1 elements replace to h1,h2,h3,h4,h5,h6 in javascript? 如何在javascript中获取所有h1,h2,h3等元素? - How do I get all h1,h2,h3 etc elements in javascript? 如何根据底部div调整顶部div的大小 - How to resize the top div according to the bottom div 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM