【问题标题】:Bootstrap-4 justify-content-around not working for flex-columnBootstrap-4 justify-content-around 不适用于 flex-column
【发布时间】:2020-11-15 23:57:39
【问题描述】:

我正在使用 bootstrap-4,我不明白为什么 justify-content-around 适用于 flex-row 而不是 flex-column? 我为div.item2 申请了flex-column justify-content-around,但它不起作用。但是当我将flex-row justify-content-around 用于div.main 时,它可以工作! 我怎样才能让它工作? 我仅将 css 用于颜色目的。这是我的代码:

.main{background-color:pink;}
.item1{background-color:red;}
.a{background-color:green;} 
.b{background-color:yellow;} 
.c{background-color:blue;}
<head>
  <!-- CSS Stylesheets  -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <link rel="stylesheet" href="css/styles.css">

  <!-- Bootstrap Scripts -->
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</head>

<body>
  <div class="main d-flex flex-row justify-content-around bd-highlight">
    <div class="item1 d-flex p-2 bd-highlight">Flex item 1</div>
    <div class="item2 d-flex flex-column justify-content-around bd-highlight">
      <div class="a p-2 bd-highlight">Flex item 2.1</div>
      <div class="b p-2 bd-highlight">
        <h2 class="price-text">Flex item 2.2</h2>
        <p>fermentum iaculis eu</p>
        <p>lectus vestibulum mattis</p>
        <p>cursus in hac</p>
        <button class="btn btn-lg btn-block btn-outline-dark" type="button">Sign Up</button>
     </div>
      <div class="c p-2 bd-highlight">Flex item 2.3</div>
   </div>
  </div>
</body>

【问题讨论】:

    标签: html twitter-bootstrap bootstrap-4 flexbox


    【解决方案1】:

    justify-content 属性对齐灵活容器的项目 当项目使用主轴上的所有可用空间时 https://www.w3schools.com/cssref/css3_pr_justify-content.asp

    在你的情况下the items use all available space.item2 的高度 = 内部内容的高度 = 没有可用空间 = 没有视觉变化)。

    非常简单的代码示例来展示这一点:为.item2 添加一些高度(例如600px):

    .main{background-color:pink;}
    .item1{background-color:red;}
    .a{background-color:green;} 
    .b{background-color:yellow;} 
    .c{background-color:blue;}
    
    aside{
      border: 5px solid green; 
      height: 600px;
    }
    #stroke{
      border: 5px solid red;
    }
    <head>
      <!-- CSS Stylesheets  -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
      <link rel="stylesheet" href="css/styles.css">
    
      <!-- Bootstrap Scripts -->
      <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
      <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    </head>
    
    <body>
      <div class="main d-flex flex-row justify-content-around">
        <main class="item1 d-flex p-2 bd-highlight">Flex item 1</main>
        <aside class="item2 d-flex flex-column justify-content-around">
          <div class="a p-2 bd-highlight">Flex item 2.1</div>
          <div id="stroke" class="b p-2 bd-highlight">
            <h2 class="price-text">Flex item 2.2</h2>
            <p>Nested item 1</p>
            <p>Nested item 2</p>
            <p>Nested item 3</p>
            <button class="btn btn-lg btn-block btn-outline-dark" type="button">Sign Up</button>
          </div>
          <div class="c p-2 bd-highlight">Flex item 2.3</div>
        </aside>
        </>
        </body>

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 2023-02-06
      • 2021-06-11
      • 1970-01-01
      • 2020-09-23
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      相关资源
      最近更新 更多