HTML:将DIV内容并排放置而不重叠

时间:2019-04-01 19:06:10

标签: html css user-interface

如下所示,我有三个方框。您可以看到在方框1和3中一切正常,但是在方框2中,文本内容重叠。

这是因为类<div>的{​​{1}}仅对.vertical_center.grade_info具有margin-left选项。我不要这些路口。

如何分别更改所有框的100px以避免此问题?

到目前为止,这是我的代码:

margin-left
.three_separation {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 60px;
}

.grades_dashboard_box {
  height: 130px;
  width: 320px;
  background-color: #0d0d0d;
  color: #ffffff;
  margin: 0 auto;
  position: relative;
}

.grade_display {
  float: left;
  font-size: 60px;
}

.vertical_center {
  margin: 0;
  position: absolute;
  top: 50%;
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

2 个答案:

答案 0 :(得分:2)

也许您可以使用flex-box通过更新CSS来实现所需的一致布局:

.grade_display {
  font-size: 60px;
}

/* Add this */
.grades_dashboard_box>div {
  /* Use flex box on the first div of .grades_dashboard_box */
  display: flex;
  /* Cause flex layout axis on this div to be horizontal */
  flex-direction: row;
  /* Cause children to center vertically */
  align-items: center;
}

/* Add this (replaces inline margin-left style) */
.grades_dashboard_box .grade_info {
  /* Add space to left of .grade_info */
  margin-left: 40px;
  /* Limit width to break small text onto two lines */
  width: 100px;
}


/* Add this */
.grades_dashboard_box h1 {
  /* Replace h1's default margin to enable vertical centering */
  margin: 0;
}

.three_separation {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 60px;
}

.grades_dashboard_box {
  height: 130px;
  width: 320px;
  background-color: #0d0d0d;
  color: #ffffff;
  margin: 0 auto;
  position: relative;
  /* Add this */
  display: flex;
  align-items: center;
}
<!-- remove margin-left:100px throughout -->
<div class="three_separation">
  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_best">2.3</h1>
      <div class="vertical_center grade_info">
        <p style="font-size: 15px;">Beste Durchschnittsnote</p>
        <p id="grade_display_best_sub" style="font-size: 20px;">Biologie</p>
      </div>
    </div>
  </div>

  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_average">13.70</h1>
      <div class="vertical_center grade_info">
        <p style="font-size: 15px;">Durchschnittsnote</p>
      </div>
    </div>
  </div>

  <div class='grades_dashboard_box'>
    <div class="vertical_center">
      <h1 class="grade_display" id="grade_display_worst">3.4</h1>
      <div class="vertical_center grade_info">
        <p style="font-size: 15px;">Schlechteste Durchschnittsnote</p>
        <p id="grade_display_worst_sub" style="font-size: 20px;">Deutsch</p>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:0)

类似的代码,具有降低的css依赖关系以及可动态缩放的网格

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body{
     display: grid;
     grid-template-columns: repeat(3,1fr);
     }
     div{
      height:100px;
      width:200px;
      background-color:black;
      color:white;
      display: flex;
      align-items: center;
     }
     h1{
         margin-right: 10px;
    }
    </style>
    </head>
    <body>

    <div>
    <h1>12.23</h1>
    <span>
    <p>lorem ipsum</p>
    <h3>ssc</h3>
    </span>
    </div>

    <div>
    <h1>12.23</h1>
    <span>
    <p>lorem ipsum test</p>
    </span>
    </div>

    <div>
    <h1>12.23</h1>
    <span>
    <h3>ssc</h3>
    </span>
    </div>

    </body>
    </html>