【问题标题】:Flexbox image gallery responsiveness issueFlexbox 图片库响应问题
【发布时间】:2021-09-13 16:00:46
【问题描述】:

我正在使用 flexbox 制作响应式图片库。为此,我的html结构如下:

<main class="image-container">
    <div class="image-wrapper">
        <img src="#">
    </div>
    <div class="image-wrapper">
        <img src="#">
    </div>
    ....so on many 
</main>

下面是我的css sn-p:

.image-container {
        width: 90%;
       height: 2480px;
        margin: 20px auto 0px;
        background: rgba(209, 217, 223, 0.705);
        display: flex;
        flex-wrap: wrap;
        flex-direction: column;
        justify-content: space-evenly;
        align-content: space-evenly;
        position: relative;            
   }
  .image-wrapper {
       width: 30%;
       margin: 0px auto 8px;
       flex-shrink: 0;
   }
    img{
        width: 100%;  
        height: 100%;
   }

现在为了使这个响应,我必须使用媒体查询。假设我的笔记本电脑屏幕宽度为 1280 像素。我试图得到3 columns of images into 2 column at 760px breakpoint。问题是当我尝试减小屏幕尺寸时,突然从最后一列生成了 1180px 的尴尬空间。 我明白了,我有 2480px 的刚性容器高度。正因为如此,我的身高保持不变,width is 90% 图像越来越小,第三列图像被添加到其他列中。我已经尽力了,所以欢迎任何建议。有什么可以在 html 或 css 中做的吗?如果您无法理解该场景,我很抱歉。

【问题讨论】:

    标签: html css image flexbox responsiveness


    【解决方案1】:

    对于此类问题,您应该将媒体查询代码添加到您的 css 文件中。如果你想在大屏幕上显示 3 张图片,你应该删除 flex-direction: row 来自 .image-container 以便 image-wrappers 可以并排。当屏幕缩小时,它们应该换行。我在此处添加 codepen 链接,以便您查看它。如果您还有其他问题。我可以回答他们。 https://codepen.io/atesirem/pen/ExmPgdP

    .image-container {
            width: 90%;
            margin: 20px auto 0px auto;
            padding:20px;
            background: rgba(209, 217, 223, 0.705);
            display: flex;
            flex-wrap: wrap;
            flex-direction: row;
            justify-content: center;
            align-content: space-evenly;
            position: relative;            
       }
    
      .image-wrapper {
           width: 30%;
           margin: 0px auto 8px;
           flex-shrink: 0;
       }
        img{
            width: 100%;  
            height: auto;
       }
    
    @media screen and (max-width:991px) {
      .image-container {
            flex-wrap: wrap;
       }
    }
    @media screen and (max-width:768px) {
      .image-container {
                    
       }
      .image-wrapper {
           width: 45%;
       }
    }
    @media screen and (max-width:500px) {
      .image-container {
            flex-direction: column;
                     
       }
      .image-wrapper {
           width: 90%;
       }
    }
    

    【讨论】:

    • 非常感谢您的帮助。我现在可以对场景进行排序,在哪里更好地使用哪些属性。我使用flex-direction: column 的原因是我有不同高度的图像,我不想让它们都具有相同的大小以正确对齐。这是我的代码笔的链接。我在顶部使用了您的 css,下面对我的 css 进行了评论。也检查我的。 codepen.io/Biebk/pen/KKmKpVy
    • 嗨,我检查了你的代码,我想我可以清楚地理解你想要做什么(我希望)。我认为您想要 3 列,并且图像将开始从上到下列出自己,以及何时进入下一列。我将在下面的答案中添加我所做的。
    【解决方案2】:

    我做了什么来使它变得更好。希望能给你一点帮助。

            .image-container {
                width: 90%;
                height: 2800px;
                min-height:fit-content;
              scroll-behavior:smooth;
              overflow:scroll;
              max-width:1280px;
                margin: 20px auto 20px auto;
                padding: 20px 0px;
                background: rgba(209, 217, 223, 0.705);
                display: flex;
                flex-wrap: wrap;
                flex-direction: column;
                justify-content: flex-start;
                align-content: space-evenly;
                position: relative;
    
                
           }
     @media screen and (max-width:500px) {
      .image-container {
            flex-direction: column;
            max-height: inherit;
                     
       }
      .image-wrapper {
           width: 90%;
       }
    }
    @media screen and (max-width:500px) {
      .image-wrapper {
           width: 90%;
       }
    }
    

    【讨论】:

    • 是的,这样更好也更容易理解
    • @ates_irem,重要的是您帮助了解我做错了什么。谢谢!
    【解决方案3】:

    这里我添加一个显示不同高度的图像及其响应的示例

    /*
    * The function calc is working wrong in case calculations in the Firefox
    */
    
    .photobox{
      display: inline-block;
    }
    
    .photobox__previewbox{
      position: relative;
      overflow: hidden;
    }
    
    .photobox__preview{
      display: block;
      max-width: 100%;
    }
    
    .photobox__previewbox:before{
      content: "";
    }
    
    /* type 1 */
    
    .photobox_type1 .photobox__previewbox:before{
      width: 0;
      height: 0;
      padding: 25%;
      border-radius: 50%;
      
      position: absolute;
      top: 0;
      left: 0;
    
      background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
      transition: transform calc(var(--photoboxAnimationDuration, .4s) / 2) ease calc(var(--photoboxAnimationDuration, .2s) / 2);
      will-change: transform;
      transform: scale(0);
    }
    
    .photobox_type1:hover .photobox__previewbox:before{
      transform: scale(2);
      transition-duration: var(--photoboxAnimationDuration, .4s);
      transition-delay: 0s;
    }
    
    .photobox_type1 .photobox__label{
      width: 50%;
      transform: translate(-200%, -50%);
      transition: transform var(--photoboxAnimationDuration, .4s) ease-out;
      will-change: transform;
      
      position: absolute;
      top: 50%;
      left: 15%;
    }
    
    .photobox_type1:hover .photobox__label{
      transition-duration: var(--photoboxAnimationDuration, .4s);
      transform: translate(0, -50%);
    }
    
    /* type 2*/
    
    .photobox_type2 .photobox__previewbox:before{
      width: 0;
      height: 0;
      padding: 25%;
      
      border-radius: 50%;
      background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
      
      position: absolute;
      top: 0;
      right: 0;
    
      transition: transform var(--photoboxAnimationDuration, .2s) ease calc(var(--photoboxAnimationDuration, .2s) / 2);
      will-change: transform;
      transform: scale(0);
    }
    
    .photobox_type2:hover .photobox__previewbox:before{
      transform: scale(2);
      transition-duration: var(--photoboxAnimationDuration, .4s);
      transition-delay: 0s;
    }
    
    .photobox_type2 .photobox__label{
      width: 50%;
      text-align: right;
      
      transform: translate(200%, -50%);
      transition: transform var(--photoboxAnimationDuration, .4s) ease-out;
      will-change: transform;
      
      position: absolute;
      top: 50%;
      right: 15%;
    }
    
    .photobox_type2:hover .photobox__label{
      transition-duration: var(--photoboxAnimationDuration, .4s);
      transform: translate(0, -50%);
    }
    
    /* type 3 */
    
    .photobox_type3 .photobox__previewbox:before{
      width: 0;
      height: 0;
      padding: 25%;
      
      background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
      border-radius: 50%;
      
      position: absolute;
      top: 50%;
      left: 50%;
      z-index: 2;
    
      transition: transform var(--photoboxAnimationDuration, .4s) ease;
      will-change: transform;
      transform: translate(-50%, -50%) scale(0);
    }
    
    .photobox_type3:hover .photobox__previewbox:before{
      transform: translate(-50%, -50%) scale(4);
      transition-duration: calc(var(--photoboxAnimationDuration, .4s) * 2);
    }
    
    .photobox_type3 .photobox__label{
      width: 95%;
      text-align: center;
      
      opacity: 0;
      transform: translate(-50%, -50%);
      transition: opacity var(--photoboxAnimationDuration, .4s) ease-out;
      will-change: opacity;
      
      position: absolute;
      top: 50%;
      left: 50%;
      z-index: 3;
    }
    
    .photobox_type3:hover .photobox__label{
      transition-duration: calc(var(--photoboxAnimationDuration, .4s) / 2);
      transition-delay: calc(var(--photoboxAnimationDuration, .4s) / 2);
      opacity: 1;
    }
    
    .photobox_type3 .photobox__preview{
        transition: transform var(--photoboxAnimationDuration, .4s) cubic-bezier(0.71, 0.05, 0.29, 0.9);
      will-change: transform;
        transform: scale(1);
    }
    
    .photobox_type3:hover .photobox__preview{
      transform: scale(1.2);
    }
    
    /* type 4 */
    
    .photobox_type4 .photobox__previewbox:before{
      width: 0;
      height: 0;
      padding: 25%;
      
      background-color: var(--photoboxOverlay, rgba(0, 0, 0, .8));
      border-radius: 50%;
      
      position: absolute;
      top: 50%;
      left: 50%;
      z-index: 2;
    
      transition: transform var(--photoboxAnimationDuration, .4s) ease;
      will-change: transform;
      transform: translate(-50%, -50%) scale(0);
    }
    
    .photobox_type4:hover .photobox__previewbox:before{
      transform: translate(-50%, -50%) scale(4);
      transition-duration: calc(var(--photoboxAnimationDuration, .4s) * 2);
    }
    
    .photobox_type4 .photobox__label{
      width: 95%;
      text-align: center;
      
      opacity: 0;
      transform: translate(-50%, -50%);
      transition: opacity var(--photoboxAnimationDuration, .4s) ease-out;
      will-change: opacity;
      
      position: absolute;
      top: 50%;
      left: 50%;
      z-index: 3;
    }
    
    .photobox_type4:hover .photobox__label{
      transition-duration: calc(var(--photoboxAnimationDuration, .4s) / 2);
      transition-delay: calc(var(--photoboxAnimationDuration, .4s) / 2);
      opacity: 1;
    }
    
    .photobox_type4 .photobox__preview{
        transition: transform var(--photoboxAnimationDuration, .4s) cubic-bezier(0.71, 0.05, 0.29, 0.9);
      will-change: transform;
        transform: scale(1) translate(0, 0);
    }
    
    .photobox_type4:hover .photobox__preview{
      transform: scale(1.2) translate(4%, 4%);
    }
    /*
    * demo styles for photobox
    */
    
    .photobox{
      color: #fff;
      font-size: 2.5rem;
      font-weight: 700;
      width: 33.33333%;
      --photoboxOverlay: rgba(72, 27, 174, .7);
      /*--photoboxAnimationDuration: .5s;*/
    }
    
    @media screen and (max-width: 480px){
      .photobox{
        width: 100%;
      }
    }
    
    /*
    =====
    DEMO
    =====
    */
    
    @media (min-width: 768px){
    
      html{
        font-size: 62.5%;
      }
    }
    
    @media (max-width: 767px){
    
      html{
        font-size: 50%;
      }
    }
    
    
    
    a{
      text-decoration: none;
      color: inherit;
    }
    
    a:hover, a:focus{
      text-decoration: underline;
    }
    
    .page{
      flex-grow: 1;
      order: 1;
    }
    
    .page__container{
      display: flex;
      flex-wrap: wrap;
      align-items: flex-start;
    }
    <div class="page">
      <div class="page__demo">
        <div class="page__container">
          <div class="photobox photobox_type1">
            <div class="photobox__previewbox">
              <img src="https://profiladv.ro/wp-content/uploads/2017/03/spring-awakening-1197602_960_720.jpg" class="photobox__preview" alt="Preview">
              <span class="photobox__label">Spring and beauty of nature</span>
            </div>
          </div>
          <div class="photobox photobox_type2">
            <div class="photobox__previewbox">
              <img src="https://www.cesarsway.com/wp-content/uploads/2015/06/Spring-health-tips.jpg" class="photobox__preview" alt="Preview">
              <span class="photobox__label">Looks so adorable</span>
            </div>
          </div>
          <div class="photobox photobox_type3">
            <div class="photobox__previewbox">
              <img src="https://headtopics.com/images/2019/8/3/independent/fear-and-persecution-in-pakistan-s-hazara-community-1157503460163690496.webp" class="photobox__preview" alt="Preview">
              <span class="photobox__label">Chashm badami</span>
            </div>
          </div>
          <div class="photobox photobox_type4">
            <div class="photobox__previewbox">
              <img src="https://www.sidetracked.com/wp-content/uploads/2014/09/wakhan-corridor-JanBakker.jpg" class="photobox__preview" alt="Phot otaken by Jan Bakker">
              <span class="photobox__label">A Short Walk in the Other Asia</span>
            </div>
          </div>            
        </div>  
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 2017-06-07
      • 1970-01-01
      相关资源
      最近更新 更多