我想制作一个特定的网格。但它不应该用flexbox,网格或表格来实现。只应使用浮动。 我已经完成了代码。它应该只是更改,以便代码中没有!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>
&#13;
答案 0 :(得分:0)
因此,您可以指定css的更高后代级别,例如:
#topics .square
这意味着#topics .square
&gt; .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%;
}
}
&#13;
<!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>
&#13;