html - Flexbox 父属性不起作用(对齐内​​容、对齐内容、对齐元素)

标签 html css flexbox

我在我的网络元素中使用 Flexbox。我之前已经能够成功地使用它,但在这种情况下,我显然有某种样式或结构妨碍了 Flexbox 的工作。

似乎 justify-contentalign-itemsalign-content 属性都没有做任何事情。但是 flex-directionflex-wrap 确实有效。

相关的 HTML:

 <div class="category-container">
       <!-- Repeat this section for all the blocks -->
       <div class="flexy">
           <a href="#">
               <div class="categories">
                   <h2>Title</h2>
                   <img src="insertimghere.jpg" />
                   <p>
                      This is an example description. Look at all
                      this information. Wow, such info, very description.
                   </p>
                </div>
           </a>
       </div>
       <!-- Repeat this section for all of the blocks -->
</div>

相关 CSS:

 /* Parent Div - Flexbox */
    .category-container {
       padding:0 2% 0 2%;
       display:flex;
       flex-direction:row;
       flex-wrap:wrap;
       /* Next three lines have no effect when changed */
       justify-content:flex-start;
       align-items:flex-start;
       align-content:flex-start;
   }

  /* Child Div - Flexbox */
    .flexy {
       margin:auto;
       padding-bottom:30px;
   }

  /* Div inside Flexy */
    .categories {
       width:350px;
       height:420px;
       margin:auto;
       background-color:#fff;
       padding:30px 15px 10px 15px;
       box-shadow:0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
     }

我有什么: What I Have

我想要的: What I Want

最佳答案

此 CSS 的失败是 margin:auto;.flexy 上。删除它并用 margin:5px 10px; 代替。

试试下面的例子(https://jsfiddle.net/sebastianbrosch/63fe9t5n/3/):

/* Parent Div - Flexbox */
.category-container {
  padding:0 2% 0 2%;
  display:flex;
  flex-direction:row;
  flex-wrap:wrap;
  /* Next three lines have no effect when changed */
  justify-content:flex-start;
  align-items:flex-start;
  align-content:flex-start;
}

/* Child Div - Flexbox */
.flexy {
  margin:5px 10px;
  padding-bottom:30px;
}

/* Div inside Flexy */
.categories {
  width:350px;
  height:420px;
  margin:auto;
  background-color:#fff;
  padding:30px 15px 10px 15px;
  box-shadow:0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
<div class="category-container">
  <!-- Repeat this section for all the blocks -->
  <div class="flexy">
    <a href="#">
      <div class="categories">
        <h2>Title</h2>
        <img src="insertimghere.jpg" />
        <p>
          This is an example description. Look at all
          this information. Wow, such info, very description.
        </p>
      </div>
    </a>
  </div>
  <div class="flexy">
    <a href="#">
      <div class="categories">
        <h2>Title</h2>
        <img src="insertimghere.jpg" />
        <p>
          This is an example description. Look at all
          this information. Wow, such info, very description.
        </p>
      </div>
    </a>
  </div>
  <div class="flexy">
    <a href="#">
      <div class="categories">
        <h2>Title</h2>
        <img src="insertimghere.jpg" />
        <p>
          This is an example description. Look at all
          this information. Wow, such info, very description.
        </p>
      </div>
    </a>
  </div>
  <div class="flexy">
    <a href="#">
      <div class="categories">
        <h2>Title</h2>
        <img src="insertimghere.jpg" />
        <p>
          This is an example description. Look at all
          this information. Wow, such info, very description.
        </p>
      </div>
    </a>
  </div>
  <!-- Repeat this section for all of the blocks -->
</div>

关于html - Flexbox 父属性不起作用(对齐内​​容、对齐内容、对齐元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318853/

相关文章:

javascript - 通过javascript在标签音频中播放mp3

ios - 如何将 View 样式设置为 16 :9 in React Native? 之类的比例

css - Flex 导航栏失去居中

javascript - Scala.js CSS 依赖项

php - mod_pagespeed 破坏 CSS 链接

html - Flex Div 不会对齐,即使它们具有相同的高度

html - 删除绝对定位 div 周围的空白空间

javascript - 互联网浏览器 : issue with dynamically setting polymer paper-tooltip innerHTML

javascript - 尝试将 jquery .slideDown() 与多个类一起使用

Selenium 中特定类表的 CSS 选择器