【问题标题】:Why doesn't justify-content result in space between elements? [closed]为什么 justify-content 不导致元素之间的空间? [关闭]
【发布时间】:2020-09-17 00:04:23
【问题描述】:

我需要元素之间的空闲空间。为此我添加了justify-content: space-between;,但不知何故它不起作用。

.cards {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  text-align: center;
  font-family: arial, serif;
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  width: 40%;
  height: 40%;
}

.price {
  color: grey;
  font-size: 22px;
}

.cards button {
  border: none;
  outline: 0;
  padding: 12px;
  color: white;
  background-color: #000;
  text-align: center;
  cursor: pointer;
  width: 100%;
  font-size: 18px;
}

.cards button:hover {
  opacity: 0.7;
}
<div class="cards">
  <div class="card">
    <img id="coffeeImage1" src="https://via.placeholder.com/180" alt="Coffee" style="width:100%">
    <h1 id="name1">Название</h1>
    <p id="price1" class="price">price</p>
    <p id="weight1">weight</p>
    <p id="country1">country</p>
    <p><button>Добавить в корзину</button></p>
  </div>

  <div class="card">
    <img id="coffeeImage2" src="https://via.placeholder.com/180"" alt="Coffee" style="width:100%">
    <h1 id="name2">Название</h1>
    <p id="price2" class="price">price</p>
    <p id="weight2">weight</p>
    <p id="country2">country</p>
    <p><button>Добавить в корзину</button></p>
  </div>

  <div class="card">
    <img id="coffeeImage3" src="https://via.placeholder.com/180"" alt="Coffee" style="width:100%">
    <h1 id="name3">Название</h1>
    <p id="price3" class="price">price</p>
    <p id="weight3">weight</p>
    <p id="country3">country</p>
    <p><button>Добавить в корзину</button></p>
  </div>
</div>

它们太多了,我必须逐个写出来。该项目使用HTML + CSS + JAVASCRIPT(无框架或第三方库)。

【问题讨论】:

  • 似乎您将父级cards 宽度设置为 40%,这太窄而无法看到排水沟?尝试将宽度设置为 100%
  • 是的,为什么你的容器这么窄?只需删除该规则即可修复它。 jsfiddle.net/skqp18xe

标签: javascript html css flexbox


【解决方案1】:

你的类cards这里是容器(就像这里解释的https://css-tricks.com/snippets/css/a-guide-to-flexbox/)。

但你的类容器不应该被束缚

.cards {
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
}

然后你就可以上课card,意思是你孩子上的课。

.card {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    text-align: center;
    font-family: arial, serif;
}

【讨论】:

    【解决方案2】:

    这可能是因为弹性项目的宽度不受限制,并且由于其内部内容,它们试图填满所有可用空间。限制宽度起作用。

    .cards {
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        text-align: center;
        font-family: arial, serif;
        display: flex;
        flex-flow: row nowrap;
        justify-content: space-between;
        width: 100%;
        height: 40%;
    }
    
    .card{
    background-color: red;
    border: 1px solid black;
    width: 30%;
    }
    
    
    .price {
        color: grey;
        font-size: 22px;
    }
    
    .cards button {
        border: none;
        outline: 0;
        padding: 12px;
        color: white;
        background-color: #000;
        text-align: center;
        cursor: pointer;
        width: 100%;
        font-size: 18px;
    }
    
    .cards button:hover {
        opacity: 0.7;
    }
    <div class="cards">
        <div class="card">
            <img id="coffeeImage1" src="https://st.depositphotos.com/1318696/3726/v/450/depositphotos_37267091-stock-illustration-seamless-coffee-background.jpg" alt="Coffee" style="width:100%">
            <h1 id="name1">Название</h1>
            <p id="price1" class="price">price</p>
            <p id="weight1">weight</p>
            <p id="country1">country</p>
            <p><button>Добавить в корзину</button></p>
        </div>
    
    <div class="card">
        <img id="coffeeImage2" src="https://st.depositphotos.com/1318696/3726/v/450/depositphotos_37267091-stock-illustration-seamless-coffee-background.jpg" alt="Coffee" style="width:100%">
        <h1 id="name2">Название</h1>
        <p id="price2" class="price">price</p>
        <p id="weight2">weight</p>
        <p id="country2">country</p>
        <p><button>Добавить в корзину</button></p>
    </div>
    
    <div class="card">
        <img id="coffeeImage3" src="https://st.depositphotos.com/1318696/3726/v/450/depositphotos_37267091-stock-illustration-seamless-coffee-background.jpg" alt="Coffee" style="width:100%">
        <h1 id="name3">Название</h1>
        <p id="price3" class="price">price</p>
        <p id="weight3">weight</p>
        <p id="country3">country</p>
        <p><button>Добавить в корзину</button></p>
    </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-22
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2020-01-14
      • 1970-01-01
      相关资源
      最近更新 更多