【问题标题】:How do you remove the margin automatically created by display: flex?如何删除由 display: flex 自动创建的边距?
【发布时间】:2020-04-28 17:13:12
【问题描述】:

我有一个 div,其中包含带有显示的收藏夹的整个列表:带有一堆 div 的 flex(带有包含收藏夹框本身和旁边的图像的 favourites-entry 类)。 favourites-entry 除了字体之外没有 css。图像有浮动:对。我将 favourites-box 宽度设置为 90%,以为图像会紧挨着它,但图像位于 favourites-box div 下方。我将收藏夹框的左右边距设置为 0,但由于某种原因,它不断在右侧创建额外的边距。

我尝试将收藏夹框的宽度设置为较低的值,但图像没有移动。我还尝试在 favourites-box div 上设置不同的位置和显示属性,但似乎没有任何需要。

.favourites-container {
  display: flex;
  flex-direction: column;
  width: 70%;
  margin: 100px auto;
}

.favourites-entry {
  font-family: 'Open Sans', sans-serif;
  /*Sets fonts for the placeholder text inside inputs*/
}

.favourites-box {
  box-shadow: 0 2px 6px rgba(0, 0, 0, .2);
  /*Adds a shadow around the whole container*/
  border-radius: 5px;
  /*Makes corners of container rounded*/
  font-family: 'Open Sans', sans-serif;
  /*Sets fonts for the placeholder text inside inputs*/
  border: 1px solid #929292;
  /*Adds a border around the inputs*/
  margin: 20px 0;
  padding: 20px;
  width: 90%;
}

img.favourites {
  height: 80px;
  width: 80px;
  float: right;
}
<div class="favourites-entry">
  <div class="favourites-box">
    <h2 class="favourites">Vegetable Kofta - Starter</h2>
    Fried balls mixed with spices and besan, deep fried till crisp
    <button class="x-button">x</button>
  </div>
  <img src="image src" class="favourites">
</div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    也许你想要这样

    .favourites-entry {
      font-family: 'Open Sans', sans-serif;
      display: flex;
      align-items: center;
    }
    
    .favourites-box {
      box-shadow: 0 2px 6px rgba(0, 0, 0, .2);
      /*Adds a shadow around the whole container*/
      border-radius: 5px;
      /*Makes corners of container rounded*/
      font-family: 'Open Sans', sans-serif;
      /*Sets fonts for the placeholder text inside inputs*/
      border: 1px solid #929292;
      /*Adds a border around the inputs*/
      margin: 20px 0;
      padding: 20px;
      width: 90%;
    }
    
    img.favourites {
      height: 80px;
      width: 80px;
    }
    <div class="favourites-entry">
      <div class="favourites-box">
        <h2 class="favourites">Vegetable Kofta - Starter</h2>
        Fried balls mixed with spices and besan, deep fried till crisp
        <button class="x-button">x</button>
      </div>
      <img src="https://picsum.photos/200/300" class="favourites">
    </div>

    【讨论】:

      【解决方案2】:

      我的解决方案使用 flex-wrap 并将所有元素保持在顶部(而不是对齐中心)
      另外 - 我的一个建议 - 你为什么不开始使用 Bootstrap 的 12 点网格系统 - 你会喜欢它的。
      链接-https://getbootstrap.com/docs/4.0/layout/grid/

      当前解决方案:

      .favourites-container {
        display: flex;
        flex-direction: column;
        width: 70%;
        margin: 100px auto;      
      }
      
      .favourites-entry {
        display: flex;
        flex-wrap: wrap;      
        font-family: 'Open Sans', sans-serif;
        /*Sets fonts for the placeholder text inside inputs*/
      }
      
      .favourites-box {
        flex: 0 50%;
        box-shadow: 0 2px 6px rgba(0, 0, 0, .2);
        /*Adds a shadow around the whole container*/
        border-radius: 5px;
        /*Makes corners of container rounded*/
        font-family: 'Open Sans', sans-serif;
        /*Sets fonts for the placeholder text inside inputs*/
        border: 1px solid #929292;
        /*Adds a border around the inputs*/
        /*margin: 20px 0;*/
        padding: 20px;
        width: 90%;
      }
      
      img.favourites {
        flex: 0 40%;
        height: 80px;
        width: 80px;
        float: right;
      }
      <div class="favourites-entry">
        <div class="favourites-box">
          <h2 class="favourites">Vegetable Kofta - Starter</h2>
          Fried balls mixed with spices and besan, deep fried till crisp
          <button class="x-button">x</button>
        </div>
        <img src="image url" class="favourites">
      </div>

      【讨论】:

      • 我无法使用 bootstap,因为这是一个学校项目,但感谢您的帮助 :)
      猜你喜欢
      • 2011-06-19
      • 2019-06-13
      • 2012-12-15
      • 1970-01-01
      • 2022-01-21
      • 2011-01-12
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多