如何将3列放在一行中?

时间:2018-06-12 06:01:32

标签: css css3 flexbox css-float multiple-columns

我的页脚有问题:我希望div行中有3个居中的列。这是代码。



.col-1 {
    float: left;
    width: 20%;
    padding: 5px;
    height: 100px;
}

.col-2 {
    float: left;
    width: 40%;
    padding: 5px;
    height: 100px;
}

<footer>
    <div class="row bg-black">
        <div class="col-2">
            <div class="social-box">
                <div><p>Connect with us:</p></div>
                <div>
                    <div><img src="assets/twitter.png" alt=""></div>
                    <div><img src="assets/linkedin.png" alt=""></div>
                </div>
            </div>
        </div>
        <div class="col-1">
            <p>dsds</p>
        </div>
        <div class="col-1">
            <p>dsds</p>
        </div>
    </div>

    <div class="row"></div>
</footer> <!-- END FOOTER -->
&#13;
&#13;
&#13;

如果我将float: left放在列上,我的背景就不会显示为黑色。

我该如何解决这个问题?它也不适用于flex。

1 个答案:

答案 0 :(得分:1)

使用flexbox将col元素居中并添加一些小样式以使行背景颜色变黑(元素需要一个大小来显示背景)

.row {
  width: 100vmax;
  height: 100px;
  margin-left: -8px;
  display: flex;
  justify-content: center;
}

.bg-black {
  background-color: black;
}

.col-1 {
  color: white;
  /*float: left;*/
  width: 20%;
  padding: 5px;
  height: 100px;
}

.col-2 {
  color: white;
  /*float: left;*/
  width: 40%;
  padding: 5px;
  height: 100px;
}


<!DOCTYPE html>
<html lang="en">
  <header>
    <meta charset="UTF-8">
    <title>Black row</title>
    <link rel="stylesheet" href="style.css">
  </header>
  <body>
    <footer>

      <div class="row bg-black">

        <div class="col-2">
          <div class="social-box">
            <div>
              <p>Connect with us:</p>
            </div>
            <div>
              <div>
                <img src="assets/twitter.png" alt="">
              </div>
              <div>
                <img src="assets/linkedin.png" alt="">
              </div>
            </div>
          </div>
        </div>

        <div class="col-1">
          <p>dsds</p>
        </div>

        <div class="col-1">
          <p>dsds</p>
        </div>

      </div> <!-- Row black -->

      <div class="row"></div>

    </footer> <!-- END FOOTER -->
  </body>
</html>