gpt4 book ai didi

How to reduce the number of @media CSS codes? [duplicate](如何减少@media的css代码数量?[复制])

转载 作者:bug小助手 更新时间:2023-10-24 23:34:56 24 4
gpt4 key购买 nike




My CSS is working but how do I reduce the number of @media CSS codes (I feel some are unnecessary)?

我的css工作正常,但我如何减少@media css代码的数量(我觉得有些是不必要的)?




.spandemo {
display: inline-block;
padding-left: 42%;
}


}
@media screen and (max-width: 600px) {
.spandemo {
display: inline-block;
padding-left: 1%;
}
}
.column {
width: 98%;
float: right;
}

}
.column img {
max-height: 129px;
height: 100%;
width: auto;
}

}
@media screen and (max-width: 600px) {
.column {
width: 90%;
float: right;
}
}
@media screen and (max-width: 600px) {
.column img {
max-height: 129px;
height: 70%;
width: 90%;
padding-right: 10px;
}
}
@media screen and (min-width: 600px) {
.column img {
max-height: 372px;
height: 70%;
width: 90%;
padding-right: 10px;
}
}

<div class="row">
<div class="column">
<img class="responsive" src="https://picsum.photos/seed/left/200/300" style="width:50%" border="1" />
<img class="responsive" src="https://picsum.photos/seed/right/200/300" style="float:right; width:50%" border="1" />
<span style="float:left;">&nbsp;&nbsp;Before</span>
<span class="spandemo" style="float:left;">&nbsp;&nbsp;After</span>
</div>
</div>




This is a follow up question to what I asked here: CSS working for mobile view but not wide screens

这是我在这里提出的后续问题:适用于移动视图但不适用于宽屏的CSS


I added all the @media CSS codes to get the desired effects but I think some are unnecessary if used in the correct order. Please let me know what can be removed or edited.
At How to use media queries in an effective way? it tells us to put all the @media queries together but that doesn't seem to work.

我添加了所有@media的css代码以获得想要的效果,但我认为如果按正确的顺序使用,有些代码是不必要的。请让我知道哪些内容可以删除或编辑。如何有效地使用媒体查询?它告诉我们将所有@media查询放在一起,但似乎不起作用。


更多回答

You have the same media query target value (i.e. max-width: 600px) repeated multiple times, which can all be consolidated to one block. That said, there are a few different ways to organize your media queries (e.g. by target query sizes vs by each CSS selector, etc), and it generally isn't a big performance concern to have multiple media queries if it's your concern.

相同的媒体查询目标值(即max-Width:600px)重复多次,可合并为一个块。这就是说,有几种不同的方法来组织你的媒体查询(例如,根据目标查询大小和每个CSS选择器,等等),如果你关心的是多个媒体查询,通常它不是一个大的性能问题。

优秀答案推荐

A few tips:

以下是一些小贴士:


1.) You don't need to repeat the exact same @media query multiple times, you can put multiple CSS rules inside a single query.

1)你不需要多次重复相同的@media查询,你可以在一个查询中放入多个css规则。


2.) When the @media query is triggered, it just adds those styles to the rules that are already in place. So, for example, if an element has float: right; you don't need to add float: right; to the same element inside the @media query.

2.)当@media查询被触发时,它只是将这些样式添加到已经存在的规则中。因此,例如,如果一个元素有Float:Right;,则不需要在@media查询内的同一元素中添加Float:Right;。


3.) In general it's a good idea to write your CSS in a way so that no @media queries are needed at all for the smallest screen size. As the screen size gets larger add more @media queries to override the previous styles. This means you should probably only use @media screen and (min-width: ____) values and not ones that target max-width. Especially important: don't use **both min-width and max-width queries as this will get very confusing for you since they might overlap

3.)一般来说,以一种不需要@media查询的方式编写CSS是一个好主意,因为屏幕尺寸最小。随着屏幕尺寸变大,添加更多的@media查询来覆盖以前的样式。这意味着你应该只使用@media screen和(min-width:_)值,而不是那些针对max-width的值。特别重要的是:不要同时使用 ** min-width和max-width查询,因为这会让你非常困惑,因为它们可能会重叠


Below is just a simple cleanup that removes the duplication I mentioned in those first 2 tips. I have not gone through and figured out how to implement #3 since you would probably know better than me for what you want here.

下面只是一个简单的清理,它删除了我在前两个技巧中提到的重复项。我还没有完成并弄清楚如何实现#3,因为您可能比我更清楚您在这里想要什么。




.spandemo {
display: inline-block;
padding-left: 42%;
}

.column {
width: 98%;
float: right;
}

.column img {
max-height: 129px;
height: 100%;
width: auto;
}

@media screen and (max-width: 600px) {
.spandemo {
padding-left: 1%;
}

.column {
width: 90%;
}

.column img {
height: 70%;
width: 90%;
padding-right: 10px;
}
}

@media screen and (min-width: 600px) {
.column img {
max-height: 372px;
height: 70%;
width: 90%;
padding-right: 10px;
}
}

<div class="row">
<div class="column">
<img class="responsive" src="https://picsum.photos/seed/left/200/300" style="width:50%" border="1" />
<img class="responsive" src="https://picsum.photos/seed/right/200/300" style="float:right; width:50%" border="1" />
<span style="float:left;">&nbsp;&nbsp;Before</span>
<span class="spandemo" style="float:left;">&nbsp;&nbsp;After</span>
</div>
</div>




更多回答

I am able to use one ````@media screen and (min-width: 600px) { which makes all the widescreens obey it but above it, I have to use the @media screen and (max-width: 600px) { for each "spandemo" div, "column" div as well as the "column img"

我可以使用一个`@媒体屏幕和(最小宽度:600px){这使得所有的宽屏都遵守它,但在它上面,我必须使用@媒体屏幕和(最大宽度:600px){对于每个“spandemo”div、“Column”div以及“Column img”

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com