【问题标题】:How to center elements together <div> and <h1>, top to bottom?如何从上到下将元素居中 <div> 和 <h1>?
【发布时间】:2017-02-14 07:05:03
【问题描述】:

我正在尝试模仿以下站点:http://weareundefined.be/,一旦您通过点击它通过第一页,就会出现一台计算机和它下面的一小段。

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

我尝试分解为必要的 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;
  }
}

我可能遗漏了什么或做错了什么?他们如何处理元素的大小调整?任何建议或指导将不胜感激。

在此先感谢您,我们一定会接受并点赞回答。

【问题讨论】:

    标签: javascript html css sass


    【解决方案1】:

    你已经接近了。 html 和 body 也必须是 height: 100%;,否则它的子元素不会是 100% 的视口。

    .container 不需要height: 100%;。由于您已经在top: 50%; 拥有.container,因此只需使用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>

    【讨论】:

    • @MichelCoker 非常感谢您的回复!不介意的话就问几个问题。为什么class="container" 的父级也必须是relative?这样子class="container" 相对于父移动(top: 50%;)其内容?您能否详细说明translateY(-50%); 到底在做什么?为什么是.container doesn't need height: 100%;?并且 flexbox 比正式的更受欢迎,为什么html 也必须是 100%?最后,该特定站点如何调整大小以响应?
    • @JoKo 1) 我的错,我在查看您的整个布局之前将其放入,除非孩子是 position: absolute;,否则不需要父级上的position: relative;。 2) developer.mozilla.org/en-US/docs/Web/CSS/transform-function/…, 3) 因为您要使该元素居中,而不是其中的内容,所以它不需要是 100% 的高度。 4) 意见问题,但可能,5) stackoverflow.com/questions/6654958/… 6) CSS 媒体查询
    【解决方案2】:

    试试:

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

    并相应调整

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 2018-06-06
      • 1970-01-01
      • 2014-01-01
      • 2023-03-29
      • 2018-03-14
      • 1970-01-01
      • 2021-01-20
      • 2012-09-26
      相关资源
      最近更新 更多