【问题标题】:How to center three divs horizontally within a parent div? [duplicate]如何在父 div 中水平居中三个 div? [复制]
【发布时间】:2016-10-16 07:34:58
【问题描述】:

我知道这个问题经常被问到,但我似乎永远无法让它发挥作用。你能告诉我有什么问题吗?

我在 #container div 中有三个 div,我想将它们并排居中。 #container 是 1000px 宽(我想保持这种状态)。这是我的代码:

#container {
  margin-top: 500px;
  position: absolute;
  width: 1000px;
}
.related-article {
  background-color: #D6A400;
  display: inline-block;
  width: 230px;
  height: 300px;
  border-radius: 30px;
  margin-bottom: 0px;
}
.related-article > img {
  width: 200px;
  height: 150px;
  border-radius: 15px;
  margin-top: 15px;
}
.related-article > h3 {
  font-size: 15px;
  width: 180px;
  text-align: justify;
  margin-left: auto;
  margin-right: auto;
  color: #f1f1f1;
  font-family: Abel, serif;
  margin-bottom: none;
}
a {
  text-decoration: none;
}
#right {
  float: right;
}
#left {
  float: left;
}
#center {
  margin-left: 385px;
  margin-right: 385px;
}
<div id="container">
  <h2>Here's what you'll do!</h2>
  <div id="left">
    <a href="#" class="related-article first" align="middle">
      <img src="download.jpeg" alt="T-rex">
      <h3>Constructing amazing fossils you never even dreamed of</h3>
    </a>
  </div>
  <div id="center">
    <a href="#" class="related-article second" align="middle">
      <img src="fossil-fish-10-lg.jpg" alt="Fish">
      <h3>Studying ancient marine biology</h3>
    </a>
  </div>
  <div id="right">
    <a href="#" class="related-article third" align="middle">
      <img src="fossil.turtle2.jpg" alt="Turtle">
      <h3>Uncovering the world's greatest mysteries</h3>
    </a>
  </div>
</div>

所有帮助将不胜感激。

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以使用 flexbox 非常简单地做到这一点:

    #container {
    /*     margin-top: 500px; */
      width: 1000px;
      margin: 0 auto;
    }
    .related-article {
      background-color: #D6A400;
      display: inline-block;
      border-radius: 30px;
      margin-bottom: 0px;
    }
    .related-article > img {
        width: 200px;
        height: 150px;
        border-radius: 15px;
        margin-top: 15px;
    }
    .related-article > h3 {
      font-size: 15px;
      width: 180px;
      text-align: justify;
      margin-left: auto;
      margin-right: auto;
      color: #f1f1f1;
      font-family: Abel, serif;
      margin-bottom: none;
    }
    a {
        text-decoration: none;
    }
    
    }
    #container {
        width: 1000px;
    }
    
    .related-article {
      background-color: #D6A400;
      display: inline-block;
      width: 230px;
      height: 300px;
      border-radius: 30px;
      margin-bottom: 0px;
    }
    .related-article > img {
        width: 200px;
        height: 150px;
        border-radius: 15px;
        margin-top: 15px;
    }
    .related-article > h3 {
      font-size: 15px;
      width: 180px;
      text-align: justify;
      margin-left: auto;
      margin-right: auto;
      color: #f1f1f1;
      font-family: Abel, serif;
      margin-bottom: none;
    }
    a {
        text-decoration: none;
    }
    .box {
      margin-right: 10px;
        width: 230px;
      height: 300px;
      }
    
    .flex-container {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    <div id="container">
            <h2>Here's what you'll do!</h2>
            <div class="flex-container">
            <div id="left" class="box">
              <a href="#" class="related-article first" align="middle">
                <img src="download.jpeg" alt="T-rex">
                <h3>Constructing amazing fossils you never even dreamed of</h3>
              </a>
              </div>
              <div id="center"  class="box">
              <a href="#" class="related-article second" align="middle">
                <img src="fossil-fish-10-lg.jpg" alt="Fish">
                <h3>Studying ancient marine biology</h3>
              </a>
              </div>
              <div id="right"  class="box">
              <a href="#" class="related-article third" align="middle">
                <img src="fossil.turtle2.jpg" alt="Turtle">
                <h3>Uncovering the world's greatest mysteries</h3>
              </a>
              </div>
              </div>
            </div>

    【讨论】:

      【解决方案2】:

      使用display: flex 的解决方案。阅读更多关于 flexbox 的信息here

      1. display: flex 应用于容器
      2. flex: 1 添加到所有要水平居中的孩子。

      h2 {
        margin-top: 500px;
      }
      #container {
        position: absolute;
        width: 1000px;
        display: flex;
        text-align: center;
      }
      #container div {
        flex: 1;
      }
      .related-article {
        background-color: #D6A400;
        display: inline-block;
        width: 230px;
        height: 300px;
        border-radius: 30px;
        margin-bottom: 0px;
      }
      .related-article > img {
        width: 200px;
        height: 150px;
        border-radius: 15px;
        margin-top: 15px;
      }
      .related-article > h3 {
        font-size: 15px;
        width: 180px;
        text-align: justify;
        margin-left: auto;
        margin-right: auto;
        color: #f1f1f1;
        font-family: Abel, serif;
        margin-bottom: none;
      }
      a {
        text-decoration: none;
      }
      <h2>Here's what you'll do!</h2>
      <div id="container">
        <div id="left">
          <a href="#" class="related-article first" align="middle">
            <img src="download.jpeg" alt="T-rex">
            <h3>Constructing amazing fossils you never even dreamed of</h3>
          </a>
        </div>
        <div id="center">
          <a href="#" class="related-article second" align="middle">
            <img src="fossil-fish-10-lg.jpg" alt="Fish">
            <h3>Studying ancient marine biology</h3>
          </a>
        </div>
        <div id="right">
          <a href="#" class="related-article third" align="middle">
            <img src="fossil.turtle2.jpg" alt="Turtle">
            <h3>Uncovering the world's greatest mysteries</h3>
          </a>
        </div>
      </div>

      【讨论】:

        【解决方案3】:

        删除所有的浮点数并替换为:

        display: inline-block;
        

        此外,正如我所尝试的那样,由于 div 之间的间距,您将无法以正确的方式显示它们。使#left、#center 和#right div 之间的空间小于50px,或者使用百分比(%)。

        【讨论】:

          【解决方案4】:

          您可以为此使用display:table..

          你可以拥有带有样式的父 div

          display:table
          

          和你的 3 个子 div 为

          display:table-cell 
          

          #container {
            margin-top: 500px;
            position: absolute;
            width: 1000px;
          }
          .related-article {
            background-color: #D6A400;
            display: inline-block;
            width: 230px;
            height: 300px;
            border-radius: 30px;
            margin-bottom: 0px;
          }
          .related-article > img {
            width: 200px;
            height: 150px;
            border-radius: 15px;
            margin-top: 15px;
          }
          .related-article > h3 {
            font-size: 15px;
            width: 180px;
            text-align: justify;
            margin-left: auto;
            margin-right: auto;
            color: #f1f1f1;
            font-family: Abel, serif;
            margin-bottom: none;
          }
          a {
            text-decoration: none;
          }
          #container {
            margin-top: 500px;
            position: absolute;
            width: 1000px;
            display: table;
          }
          .related-article {
            background-color: #D6A400;
            display: inline-block;
            width: 230px;
            height: 300px;
            border-radius: 30px;
            margin-bottom: 0px;
          }
          .related-article > img {
            width: 200px;
            height: 150px;
            border-radius: 15px;
            margin-top: 15px;
          }
          .related-article > h3 {
            font-size: 15px;
            width: 180px;
            text-align: justify;
            margin-left: auto;
            margin-right: auto;
            color: #f1f1f1;
            font-family: Abel, serif;
            margin-bottom: none;
          }
          a {
            text-decoration: none;
          }
          #left,
          #right,
          #center {
            display: table-cell;
          }
          #center {
            margin-left: 385px;
            margin-right: 385px;
          }
          h2 {
            display: table-row;
          }
          <div id="container">
            <h2>Here's what you'll do!</h2>
            <div id="left">
              <a href="#" class="related-article first" align="middle">
                <img src="download.jpeg" alt="T-rex">
                <h3>Constructing amazing fossils you never even dreamed of</h3>
              </a>
            </div>
            <div id="center">
              <a href="#" class="related-article second" align="middle">
                <img src="fossil-fish-10-lg.jpg" alt="Fish">
                <h3>Studying ancient marine biology</h3>
              </a>
            </div>
            <div id="right">
              <a href="#" class="related-article third" align="middle">
                <img src="fossil.turtle2.jpg" alt="Turtle">
                <h3>Uncovering the world's greatest mysteries</h3>
              </a>
            </div>
          </div>

          【讨论】:

            【解决方案5】:

            从三个中删除float并将display: inline-block添加到所有三个中,然后将text-align: center;添加到容器中。

            【讨论】:

            【解决方案6】:

            试试这个,将 float:left 添加到您的 right、left 和 center div 并减少您的 ma​​rgin left 和 right 中心 div。

            #right {
            float: left;
            }
            #left {
            float: left;
            }
            #center {
            margin-left: 85px;
            margin-right: 85px;
            float:left;
            }
            

            【讨论】:

              【解决方案7】:

              而不是添加中心、左侧和右侧。使用 ul 并以百分比设置 li 的宽度。它将改进代码并减少 css 代码。

              codepen 网址是http://codepen.io/SESN/pen/pbbjrb

              CSS

              #container { width: 100%; }
              .ulContainer { margin: 0px auto; list-style: none; width: 80%; }
              .ulContainer li { width: 33.33%; float: left; }
              

              HTML

              <div id="container">
                      <h2>Here's what you'll do!</h2>
                <ul class="ulContainer">
                <li>
                  <a href="#" class="related-article first" align="middle">
                          <img src="download.jpeg" alt="T-rex">
                          <h3>Constructing amazing fossils you never even dreamed of</h3>
                        </a>
                </li>
                  <li>
                  <a href="#" class="related-article second" align="middle">
                          <img src="fossil-fish-10-lg.jpg" alt="Fish">
                          <h3>Studying ancient marine biology</h3>
                        </a>
                  </li>
                  <li>
                  <a href="#" class="related-article third" align="middle">
                          <img src="fossil.turtle2.jpg" alt="Turtle">
                          <h3>Uncovering the world's greatest mysteries</h3>
                        </a>
                  </li>
                </ul>
                      </div>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-08-30
                • 2013-03-30
                • 2014-05-22
                相关资源
                最近更新 更多