使用flexbox将文本居中对齐

问题描述 投票:-1回答:1

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

.none {
  width: 200px;
  background-color: lightgreen;
  height: 80px;
  margin: 3px;
  border-radius: 10px;
  line-height: 1em;
}

.none a {
  text-align: center;
  text-decoration: none;
  color: red;
}
<div class="lol1">

  <div class="none"><a href="#">A</a></div>

  <div class="none"><a href="#">B</a></div>

  <div class="none"><a href="#">C</a></div>

  <div class="none"><a href="#">D</a></div>

</div>
css flexbox anchor responsive
1个回答
0
投票

将这3个属性添加到.none

  display: flex;
  align-items: center;
  justify-content: center;

[justify-content: center会将其水平对准中心,align-items: center使其垂直对准。

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

.none {
  width: 200px;
  background-color: lightgreen;
  height: 80px;
  margin: 3px;
  border-radius: 10px;
  line-height: 1em;
  display: flex;
  align-items: center;
  justify-content: center;
}

.none a {
  text-align: center;
  text-decoration: none;
  color: red;
}
<div class="lol1">

  <div class="none"><a href="#">A</a></div>

  <div class="none"><a href="#">B</a></div>

  <div class="none"><a href="#">C</a></div>

  <div class="none"><a href="#">D</a></div>

</div>

另一种解决方法是将margin: auto放在a标签上。.none必须为display: flex才能工作。

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

    .none {
      width: 200px;
      background-color: lightgreen;
      height: 80px;
      margin: 3px;
      border-radius: 10px;
      line-height: 1em;
      display: flex;
    }

    .none a {
      margin: auto;
      text-align: center;
      text-decoration: none;
      color: red;
    }
    <div class="lol1">

      <div class="none"><a href="#">A</a></div>

      <div class="none"><a href="#">B</a></div>

      <div class="none"><a href="#">C</a></div>

      <div class="none"><a href="#">D</a></div>

    </div>
© www.soinside.com 2019 - 2024. All rights reserved.