我有一个单一的div模板,我已经通过PHP生成了多个带有动态内容的divs.现在的问题是我的divs创建在不规则的位置(检查下面的图片),我想要的是。"我希望每行有四个大小相同的div,每个div必须是并排的,并且每个div之间需要保持一个小的间隙"。
这是我的div。
<div class="row" id="except">
<div class="column">
<div class="card">
<h1><?php echo"$value"; ?></h1>
<h3><?php echo"$description";?></h3>
<h3><?php echo"$index";?></h3>
</div>
</div>
</div>
这是我为每个div设计的CSS。
<style>
#except{
width:1200px;
height:500px;
margin-left:160px;
}
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Float four columns side by side */
.column {
float: left;
width: 25%;
padding: 0 10px;
}
/* Remove extra left and right margins, due to padding */
.row {margin: 0 -5px;}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive columns */
@media screen and (max-width: 600px) {
.column {
width: 100%;
display: block;
margin-bottom: 20px;
}
}
/* Style the counter cards */
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 1);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
transition: width 2s,height 4s;
transition-timing-function: ease-in-out;
}
.card:hover{
background-color: lightgreen;
transition-timing-function: ease-in-out;
width: 300px;
height: 300px;
}
</style>
简单的解决方法,只要把行的div去掉就可以了。我使用这个完全相同的代码从w3学校为我的商店和所有我不得不做的是删除行div和它的工作。)
编辑,添加了下面的代码。我不得不删除php部分的测试目的。编辑2,你也需要摆脱#except其他明智的,将打破它。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Float four columns side by side */
.column {
float: left;
width: 25%;
padding: 0 10px;
}
/* Remove extra left and right margins, due to padding */
.row {margin: 0 -5px;}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive columns */
@media screen and (max-width: 600px) {
.column {
width: 100%;
display: block;
margin-bottom: 20px;
}
}
/* Style the counter cards */
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 1);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
transition: width 2s,height 4s;
transition-timing-function: ease-in-out;
}
.card:hover{
background-color: lightgreen;
transition-timing-function: ease-in-out;
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<div class="column">
<div class="card">
<h1>Rev</h1>
<h3>Df</h3>
<h3>Rev</h3>
</div>
</div>
<div class="column">
<div class="card">
<h1>Rev</h1>
<h3>Df</h3>
<h3>Rev</h3>
</div>
</div>
</body>
</html>
你需要修改你的css代码,如下图所示。
#except {
width: 100%;
height: 100%;
display: flex;
flex-wrap: wrap;
}
.column {
float: left;
width: 25%;
padding: 0 10px;
margin-bottom: 20px;
flex-basis: 25%;
}