【问题标题】:Two div elements needs to be side by side两个 div 元素需要并排
【发布时间】:2020-08-11 22:15:33
【问题描述】:

我有一个 div 模板,并且我通过 PHP 生成了多个具有动态内容的 div。 现在的问题是我的 div 在不规则的位置创建(检查下面的图片),我想要的是。 “我希望每行有四个大小相等的 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 并在行 div 中粘贴 4 次,我认为您是在循环中创建多行。
  • 4 不固定。我想创建“N”个 div。 “N”是通过PHP检索到的动态数

标签: html css user-interface bootstrap-4 flexbox


【解决方案1】:

你需要改变你的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%;
}

【讨论】:

    【解决方案2】:

    简单的修复,只要去掉行 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>
    
    

    【讨论】:

    • 我做了同样的事情,但没有改变。可以分享一下代码吗
    • 完成,我还删除了#except,它们也可能是个问题
    • 如果我的回答是正确的,请您这样标记。
    • 通过实现这个我有两个问题请你解决(关于悬停效果)
    • 当然,悬停效果除了变绿还能做什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多