【问题标题】:Trouble centering CSS columns无法将 CSS 列居中
【发布时间】:2021-06-27 06:18:06
【问题描述】:

我在 css 中有一个三列框,我需要它在桌面的页面上居中,然后在 600 像素或更小的范围内将列堆叠在一起。除了以桌面视图为中心之外,我一切正常。

我尝试添加 justify-content: relative,添加包装器 align: center 和其他几行不起作用的代码。非常感谢任何帮助。

这是我目前拥有的代码:

* {
  box-sizing: border-box;
}


/* Create three equal columns that floats next to each other */

.column {
  float: left;
  width: 178px;
}


/* Clear floats after the columns */

.row:after {
  content: "";
  display: table;
  clear: both;
}


/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */

@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  }
}
<p align="center">
  <font size="5" color="336699"><strong>Great American Song Contest</strong></font>
</p>
<p align="center">
  <font size="3" color="a91e21"><strong>3 Easy Ways To Enter Your Songs</strong></font>
</p>

<div class="wrapper">
  <div class="row">
    <div class="column">

      <p>
        <a href="https://www.greatamericansong.com/newsite-backup/Form-Prepage.php/"><img src="https://www.greatamericansong.com/newsite-backup/images/submit-online.gif" alt="" width="178" height="158"></p>
    </div>
    <div class="column">

      <p>
        <a href="https://www.greatamericansong.com/newsite-backup/Entry-Direct.php/"><img src="https://www.greatamericansong.com/newsite-backup/images/submit-email.gif" alt="" width="178" height="158"></p>
    </div>
    <div class="column">

      <p>
        <a href="https://www.greatamericansong.com/newsite-backup/entry-mail.php/"><img src="https://www.greatamericansong.com/newsite-backup/images/submit-mail.gif" alt="" width="178" height="158"></a>
      </p>
    </div>
  </div>
</div>

<div align="center"> <img src="//shield.sitelock.com/shield/greatamericansong.com" id="sl_shield_image" style="cursor: pointer;" alt="SiteLock" align="middle" />
  <script id="sl_shield" type="text/javascript" src="//shield.sitelock.com/sitelock.js" language="javascript"></script>
</div>
<p align="center">


  <p></p>
  <p align="center"><strong>* WRITERS RETAIN ALL RIGHTS TO THEIR SONGS, LYRICS & COMPOSITIONS *</strong></p>

  <p>
    <font size="3" color="a91e21"><strong>2021 Rules & Entry:</strong></font>
  </p>

  <p>The Great American Song Contest is open to songwriters, lyricists & music composers worldwide.</p>

  <p align="center"><strong>* WRITERS RETAIN ALL RIGHTS TO THEIR SONGS, LYRICS & COMPOSITIONS *</strong></p>

【问题讨论】:

  • justify-content: center
  • @zergski 我不确定在我的代码中放置该行的位置。你能告诉我它应该去哪里吗?

标签: css css-multicolumn-layout


【解决方案1】:

将专用类应用于这些列的父级(在我的示例中:class="x"),在其上使用display: flex;justify-content: center,然后在媒体查询中将其更改为flex-direction: column(将它们放在下面彼此)和align-items: center;(使它们居中)。忘记花车......

* {
  box-sizing: border-box;
}
.column {
  width: 178px;
}

.x {
display: flex;
justify-content: center;
}

@media screen and (max-width: 600px) {
  .x {
    flex-direction: column;
    align-items: center;
  }
}
<p align="center">
  <font size="5" color="336699"><strong>Great American Song Contest</strong></font>
</p>
<p align="center">
  <font size="3" color="a91e21"><strong>3 Easy Ways To Enter Your Songs</strong></font>
</p>

<div class="wrapper">
  <div class="x">
    <div class="column">

      <p>
        <a href="https://www.greatamericansong.com/newsite-backup/Form-Prepage.php/"><img src="https://www.greatamericansong.com/newsite-backup/images/submit-online.gif" alt="" width="178" height="158"></p>
    </div>
    <div class="column">

      <p>
        <a href="https://www.greatamericansong.com/newsite-backup/Entry-Direct.php/"><img src="https://www.greatamericansong.com/newsite-backup/images/submit-email.gif" alt="" width="178" height="158"></p>
    </div>
    <div class="column">

      <p>
        <a href="https://www.greatamericansong.com/newsite-backup/entry-mail.php/"><img src="https://www.greatamericansong.com/newsite-backup/images/submit-mail.gif" alt="" width="178" height="158"></a>
      </p>
    </div>
  </div>
</div>

<div align="center"> <img src="//shield.sitelock.com/shield/greatamericansong.com" id="sl_shield_image" style="cursor: pointer;" alt="SiteLock" align="middle" />
  <script id="sl_shield" type="text/javascript" src="//shield.sitelock.com/sitelock.js" language="javascript"></script>
</div>
<p align="center">


  <p></p>
  <p align="center"><strong>* WRITERS RETAIN ALL RIGHTS TO THEIR SONGS, LYRICS & COMPOSITIONS *</strong></p>

  <p>
    <font size="3" color="a91e21"><strong>2021 Rules & Entry:</strong></font>
  </p>

  <p>The Great American Song Contest is open to songwriters, lyricists & music composers worldwide.</p>

  <p align="center"><strong>* WRITERS RETAIN ALL RIGHTS TO THEIR SONGS, LYRICS & COMPOSITIONS *</strong></p>

【讨论】:

  • 进行上述调整会一直将我的三列行移动到彼此之上。这是它的外观链接,greatamericansong.com/newsite-backup/rules-international.php 这三个图形现在在彼此上方,而不是在桌面视图中并排对齐。
  • 这不是您在问题中发布的 HTML:在您的网站上,您将三个 .column 元素包装在另一个 column 元素中,因此该元素是 .x div 的唯一子元素.删除额外的.column 元素,使.x 包含三个.column 元素,就像你上面的问题一样。
  • 谢谢,看不到我的错误。真的很感激,我会做出调整的。
猜你喜欢
  • 2018-01-15
  • 1970-01-01
  • 2016-11-04
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 2012-01-27
  • 2021-04-22
相关资源
最近更新 更多