gpt4 book ai didi

html - 如何在不使用 !important 且仅使用 float 的情况下制作特定的网格

转载 作者:行者123 更新时间:2023-11-28 02:08:26 25 4
gpt4 key购买 nike

我想制作一个特定的网格。但它不应该用 flexbox、grid 或 tables 来实现。只能使用 float 。我已经有了完成的代码。它应该只被更改,以便在代码中没有!important 并且以相同的方式运行。如果您删除 !important 并调整浏览器窗口的大小,您将看到不同的行为。

这是我的代码:

/* === General === */

.clearfix::after {
content: "";
display: block;
clear: both;
}

.container {
max-width: 1328px;
margin: 3rem auto;
}

/* === Grid === */

.row {
padding-bottom: 2rem;
}

.row .square:nth-of-type(3) {
margin-left: 30px;
}

.row .square:nth-of-type(1) {
margin-right: 30px;
}

.square {
width: calc((100% - 60px) / 3);
float: left;
max-width: 428px;
}

.square::before {
display: block;
content: "";
width: 100%;
padding-top: 100%;
background-color: grey;
}

.rectangle {
width: calc((100% - 60px) / 3 * 2 + 30px);
float: left;
margin-right: 30px;
}

.rectangle::before {
display: block;
content: "";
width: 100%;
padding-top: 48.28375%;
background-color: #e95d0e;
}

@media (max-width: 991px) {
.row {
padding-bottom: 0;
}
.square {
float: none;
margin-right: auto !important;
margin-left: auto !important;
margin-bottom: 2rem;
width: 100%;
max-width: 370px;
}
.rectangle {
margin-right: auto;
margin-left: auto;
margin-bottom: 2rem;
float: none;
width: 100%;
max-width: 370px;
}
.rectangle::before {
padding-top: 100%;
}
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="src/css/styles.css">
</head>

<body>
<div class="container">
<section id="topics">
<div class="row clearfix">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
<div class="row clearfix">
<div class="rectangle"></div>
<div class="square"></div>
</div>
</section>
</div>
</body>

</html>

最佳答案

因此,您可以指定更高的 css 后代级别,例如:

#topics .square

意思是#topics .square > .row .square:nth-of-type(*),表示#topics .square 将覆盖 .row .square:nth-of-type(*) 参数...所以,不需要 !important

参见:https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_selectors

/* === General === */

.clearfix::after {
content: "";
display: block;
clear: both;
}

.container {
max-width: 1328px;
margin: 3rem auto;
}


/* === Grid === */
.row {
padding-bottom: 2rem;
}

.row .square:nth-of-type(3) {
margin-left: 30px;
}

.row .square:nth-of-type(1) {
margin-right: 30px;
}

.square {
width: calc((100% - 60px) / 3);
float: left;
max-width: 428px;
}

.square::before {
display: block;
content: "";
width: 100%;
padding-top: 100%;
background-color: grey;
}

.rectangle {
width: calc((100% - 60px) / 3 * 2 + 30px);
float: left;
margin-right: 30px;
}

.rectangle::before {
display: block;
content: "";
width: 100%;
padding-top: 48.28375%;
background-color: #e95d0e;
}

@media (max-width: 991px) {

.row {
padding-bottom: 0;
}

#topics .square {
float: none;
margin-right: auto;
margin-left: auto;
margin-bottom: 2rem;
width: 100%;
max-width: 370px;
}

.rectangle {
margin-right: auto;
margin-left: auto;
margin-bottom: 2rem;
float: none;
width: 100%;
max-width: 370px;
}

.rectangle::before {
padding-top: 100%;
}

}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="src/css/styles.css">
</head>

<body>
<div class="container">

<section id="topics">


<div class="row clearfix">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>

<div class="row clearfix">
<div class="rectangle"></div>
<div class="square"></div>
</div>

</section>

</div>
</body>

</html>

关于html - 如何在不使用 !important 且仅使用 float 的情况下制作特定的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48896435/

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