我不理解这段代码中有关媒体(css)的行。

huangapple go评论100阅读模式
英文:

I don't understand this code for rows in media (css)

问题

我不明白这段用于卡片的代码。

卡片容器的样式如下,容器的高度初始设置为100%,宽度自动适应:

  1. .container-cards {
  2. display: grid;
  3. grid-template-columns: repeat(3, auto);
  4. grid-template-rows: repeat(2, 600px);
  5. gap: 1em;
  6. }

但是,当我尝试添加媒体查询代码时,它对所有卡片都不起作用,高度不再相同。如果我将行的定义更改为以下内容,高度将固定为600px:

  1. @media (max-width: 767px) {
  2. /* 小屏幕:每行只有1个卡片 */
  3. .container-cards {
  4. grid-template-rows: auto;
  5. grid-template-columns: 1fr;
  6. }
  7. }

我不明白为什么这段代码对自动行高度不起作用。

英文:

Hi I don't understand this code for cards

my container for cards is this before heigh of container is 100% and width is auto

  1. .container-cards {
  2. display: grid;
  3. grid-template-columns: repeat(3, auto);
  4. grid-template-rows: repeat(2, 600px);
  5. gap: 1em;
  6. }

but when I tried to type media code it deosnt work for all cards the hegiht isnt the same like from the start. If I write instead for rows this grid-template-rows: repeat(5, 600px) the height will be fixed. I don't understand why this code doesn't work for rows set to auto.

  1. @media (max-width: 767px) {
  2. /* Small screens: 1 card in a row */
  3. .container-cards {
  4. grid-template-rows: auto;
  5. grid-template-columns: 1fr;
  6. }
  7. }

答案1

得分: 1

这是因为你引用了错误的类。

  1. @media (max-width: 767px) {
  2. /* 小屏幕:一行显示一个卡片 */
  3. .container-cards {
  4. grid-template-rows: auto;
  5. grid-template-columns: 1fr;
  6. }
  7. }

你需要在媒体查询规则中针对相同的类来改变内容在不同屏幕尺寸下的大小和位置。

英文:

It's because you are referencing to wrong class

  1. @media (max-width: 767px) {
  2. /* Small screens: 1 card in a row */
  3. .container-cards {
  4. grid-template-rows: auto;
  5. grid-template-columns: 1fr;
  6. }
  7. }

You need to target same class in media rule for it to change size and position of content on different screen sizes

huangapple
  • 本文由 发表于 2023年6月30日 02:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定