使网格div响应迅速

发布于 2025-01-29 10:35:39 字数 3108 浏览 1 评论 0原文

.subcategory-main-wrapper {
  display: grid;
  grid-template-columns: repeat(4, max-content);
  position: absolute;
  width: 100%;
  column-gap: 4%;
  justify-content: center;
  height: 360px;
  @media @tabletScreens {
    height: 280px;
  }
  @media @mobileScreens {
    height: 450px;
    row-gap: 30px;
    column-gap: 7%;
    grid-template-columns: repeat(2, max-content);
  }
}

.subcategory-image-container {
  width: 278px;
  height: 278px;
  border-radius: 50%;
  background-color: black;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}
<div class="category-container">
  <div class="subcategory-main-wrapper">
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
  </div>

我正在尝试使Circle Div按比例地调整大小,以保持其长宽比。但是,这没有发生。以下是图像

当屏幕大小尺寸

它的外观!

该网格的宽度扩展了浏览器的宽度。我希望根据浏览器屏幕的大小调整列和其中的内容。以下是我的代码。

.subcategory-main-wrapper {
    display: grid;
    grid-template-columns: repeat(4, max-content);
    position: absolute;
    bottom: 25px;
    width: 100%;
    column-gap: 4%;
    justify-content: center;
    height: 360px;
    @media @tabletScreens{
        height: 280px;
        bottom: 12px;
    }
    @media @mobileScreens {
        height: 450px;
        bottom: 35px;
        row-gap: 30px;
        column-gap: 7%;
        grid-template-columns: repeat(2, max-content);
    }
}
.subcategory-image-container{
    width: 278px;
    height: 278px;
    border-radius: 50%;
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}

HTML:

<div class="category-container">
<div class="subcategory-main-wrapper">
        <div class="subcategory-container">
        {% if model.config.titleSubCategoryImageOne %}
        <div id="image-1-{{model.id}}" class="subcategory-image-container">
            
        </div>
            <div class="subcategory-title">
                <a href="{{model.config.subCategoryLinkOne}}">
                    {{model.config.titleSubCategoryImageOne}}
                </a>    
         </div>
    </div>
</div>

请帮助解决此问题。我尝试使用Flex显示屏,但列也被固定了。但是网格最适合所有屏幕尺寸,因为圆圈不会被弄脏。

.subcategory-main-wrapper {
  display: grid;
  grid-template-columns: repeat(4, max-content);
  position: absolute;
  width: 100%;
  column-gap: 4%;
  justify-content: center;
  height: 360px;
  @media @tabletScreens {
    height: 280px;
  }
  @media @mobileScreens {
    height: 450px;
    row-gap: 30px;
    column-gap: 7%;
    grid-template-columns: repeat(2, max-content);
  }
}

.subcategory-image-container {
  width: 278px;
  height: 278px;
  border-radius: 50%;
  background-color: black;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}
<div class="category-container">
  <div class="subcategory-main-wrapper">
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
    <div class="subcategory-container">
      <div class="subcategory-image-container">
      </div>
    </div>
  </div>

I'm trying to make the circle Div resize proportionally so that it maintains it aspect ratio. However this not happening. Below are the images

How it should look like when the screen is resized

How it looks like!

The width of this grid extends the width of the browser. I want the columns and the contents in them to be resized based on the size of the browser screen. Below is my code.

.subcategory-main-wrapper {
    display: grid;
    grid-template-columns: repeat(4, max-content);
    position: absolute;
    bottom: 25px;
    width: 100%;
    column-gap: 4%;
    justify-content: center;
    height: 360px;
    @media @tabletScreens{
        height: 280px;
        bottom: 12px;
    }
    @media @mobileScreens {
        height: 450px;
        bottom: 35px;
        row-gap: 30px;
        column-gap: 7%;
        grid-template-columns: repeat(2, max-content);
    }
}
.subcategory-image-container{
    width: 278px;
    height: 278px;
    border-radius: 50%;
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}

HTML:

<div class="category-container">
<div class="subcategory-main-wrapper">
        <div class="subcategory-container">
        {% if model.config.titleSubCategoryImageOne %}
        <div id="image-1-{{model.id}}" class="subcategory-image-container">
            
        </div>
            <div class="subcategory-title">
                <a href="{{model.config.subCategoryLinkOne}}">
                    {{model.config.titleSubCategoryImageOne}}
                </a>    
         </div>
    </div>
</div>

Please help resolve this. I have tried using flex display but the columns get squezed also. However grid works best for all screen sizes as the circle does not get squezzed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2025-02-05 10:35:39

我找到了我的代码解决方案。
我更改了下行
来自

网格 - 板块柱:重复(4,max-content);

网格 - 板块柱:20%20%20%20%; >

.subcategory-image-container{
    width: 100%;
    height: auto;
    aspect-ratio: 1/1;
    border-radius: 50%;
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}

I found solution for my code.
I changed the below line
from

grid-template-columns: repeat(4, max-content);

to

grid-template-columns: 20% 20% 20% 20%;
and

.subcategory-image-container{
    width: 100%;
    height: auto;
    aspect-ratio: 1/1;
    border-radius: 50%;
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文