代码之家  ›  专栏  ›  技术社区  ›  user3541631

flexbox容器问题,忽略宽度和高度[重复]

  •  -1
  • user3541631  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我有以下代码和以下问题:

    图像容器应为方形,并具有:

    height: 5.5rem;
    width: 5.5rem;
    

    但这一维度并没有得到尊重。

    .slider {
      align-items: flex-start;
      display: flex;
      flex-direction: column;
    }
    
    .items {
      justify-content: center;
      margin: 0 auto;
      position: relative;
      display: flex;
    }
    
    .item {
      display: flex;
      flex-direction: row;
      left: 0;
      outline: 0;
      top: 0;
      opacity: 0;
      position: absolute;
      visibility: hidden;
      z-index: -1;
    }
    
    .item.show {
      position: relative;
      opacity: 1;
      visibility: visible;
      z-index: 9;
    }
    
    .image {
      height: 5.5rem;
      margin: 0 0.5rem 0 0;
      width: 5.5rem;
      align-items: center;
      padding: .5rem;
      display: flex;
      border: 1px solid black;
    }
    
    .image img {
      display: block;
      margin-left: auto;
      margin-right: auto;
      max-height: 100%;
      max-width: 100%;
    }
    
    
    }
    <div class="slide">
      <div class="items">
        <div class="item show">
          <div class="image">
            <img src="https://via.placeholder.com/150">
          </div>
          <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of</div>
        </div>
        <div class="item">
          <div class="image">
            <img src="https://via.placeholder.com/150">
          </div>
          <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a software like Aldus
            PageMaker including versions of Lorem Ipsu</div>
        </div>
      </div>
    </div>
    </div>
    2 回复  |  直到 6 年前
        1
  •  1
  •   mdubus    6 年前

    我只是添加了一个 flex-shrink:0; 到.图像,它工作了:)

    .slider {
      align-items: flex-start;
      display: flex;
      flex-direction: column;
      }
      
    .items { 
      justify-content: center;
      margin: 0 auto;
      position: relative;
      display: flex;
    }
    
    .item {
    display: flex;
    flex-direction: row;
    left: 0;
    outline: 0;
    top: 0;
    opacity: 0;
    position: absolute;
    visibility: hidden;
    z-index: -1;
    }
    
    .item.show {
        position: relative;
        opacity: 1;
        visibility: visible;
        z-index: 9;
    }
    
    .image { 
    height: 5.5rem;
    margin: 0 0.5rem 0 0;
    width: 5.5rem;
    align-items: center;
    padding: .5rem;
    display:flex;
    border: 1px solid black;
    flex-shrink: 0;
    }
    
    .image img { 
        display: block;
        margin-left: auto;
        margin-right: auto;
        max-height: 100%;
        max-width: 100%;
    }
    
    }
    <div class="slide">
      <div class="items">
        <div class="item show">
          <div class="image">
             <img src="https://via.placeholder.com/150">
           </div>
           <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of</div>
           </div>
     <div class="item">
          <div class="image">
             <img src="https://via.placeholder.com/150">
           </div>
           <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a  software like Aldus PageMaker including versions of Lorem Ipsu</div>
           </div>      
     </div>
     </div>
     </div>
        2
  •  0
  •   Αntonis Papadakis    6 年前

    如果需要静态宽度和高度,可以设置 和A 最大值 宽度和高度,因此:

    .image {
      min-height: 5.5rem;
      min-width: 5.5rem;
      max-height: 5.5rem;
      max-width: 5.5rem;
    }