解决网格内容

时间:2019-06-04 21:31:47

标签: html css responsive-design

我正在尝试将grades_dashboard_box的全部内容居中。布局本身很棒,但内容应位于griddiv的中心。

.separation {
	width: 100%;
  display: grid;
  grid-gap: 50px;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-template-rows: repeat(2, auto);}
}

.grades_dashboard_box {
	height: 130px;
	width: auto;
	color: var(--text-color);
	transition: color 1s;
	margin: 0 auto;
	position: relative;
	display: flex;
  align-items: center;
}

.grade_info {
	margin-left: 20px;
}

.grades_dashboard_box>div {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.grades_dashboard_box {
  width: 100px;
}
<div id="grade_shortboard" class="separation">
                    <div class='grades_dashboard_box'>
                      <div>
                        <h1 class="grade_display" id="grade_display_worst">14</h1>
                        <div class="grade_info">
                          <p style="font-size: 15px;">Schlechteste Durchschnittsnote</p>
                          <p id="grade_display_worst_sub" style="font-size: 20px;">Biologie</p>
                        </div>
                      </div>
                    </div>
                    <div class='grades_dashboard_box'>
                      <div>
                        <h1 class="grade_display" id="grade_display_average">7</h1>
                        <div class="grade_info">
                          <p style="font-size: 15px;">Gesamte Durchschnittsnote</p>
                        </div>
                      </div>
                    </div>
                    <div class='grades_dashboard_box'>
                      <div>
                        <h1 class="grade_display" id="grade_display_best">10</h1>
                        <div class="grade_info">
                          <p style="font-size: 15px;">Beste Durchschnittsnote</p>
                          <p id="grade_display_best_sub" style="font-size: 20px;">Swppl</p>
                        </div>
                      </div>
                    </div>
                  </div>

这是一个示例图片:
中心:
centered
未定中心:
not_centered
〜filip

编辑:
enter image description here
如您所见,字体是开箱即用的。据我所知,此框居中。因此,对于用户来说,由于文本即将到来,因此看起来好像框居中。另一种方法是使字体变小,但我不希望这样做。如何解决?

2 个答案:

答案 0 :(得分:1)

我对您的代码做了一些修改以适合您的需求。 我在 strong文本上添加了 width:100px ,而不是 .grades_dashboard_box 我还在 .grades_dashboard_box

中添加了 margin:auto;

.separation {
	width: 100%;
  display: grid;
  grid-gap: 50px;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-template-rows: repeat(2, auto);}
}

.grades_dashboard_box {
	height: 130px;
	width: auto;
	color: var(--text-color);
	transition: color 1s;
	margin: 0 auto;
	position: relative;
	display: flex;
  align-items: center;
}

.grade_info {
	margin-left: 20px;
  width: 100px;
}

.grades_dashboard_box>div {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.grades_dashboard_box {
    margin: auto;
}
<div id="grade_shortboard" class="separation">
                    <div class='grades_dashboard_box'>
                      <div>
                        <h1 class="grade_display" id="grade_display_worst">14</h1>
                        <div class="grade_info">
                          <p style="font-size: 15px;">Schlechteste Durchschnittsnote</p>
                          <p id="grade_display_worst_sub" style="font-size: 20px;">Biologie</p>
                        </div>
                      </div>
                    </div>
                    <div class='grades_dashboard_box'>
                      <div>
                        <h1 class="grade_display" id="grade_display_average">7</h1>
                        <div class="grade_info">
                          <p style="font-size: 15px;">Gesamte Durchschnittsnote</p>
                        </div>
                      </div>
                    </div>
                    <div class='grades_dashboard_box'>
                      <div>
                        <h1 class="grade_display" id="grade_display_best">10</h1>
                        <div class="grade_info">
                          <p style="font-size: 15px;">Beste Durchschnittsnote</p>
                          <p id="grade_display_best_sub" style="font-size: 20px;">Swppl</p>
                        </div>
                      </div>
                    </div>
                  </div>

答案 1 :(得分:0)

添加一个新的父元素并为其赋予text-align:center;

.separation {
	width: 100%;
  display: grid;
  grid-gap: 50px;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-template-rows: repeat(2, auto);}
}

.grades_dashboard_box {
	height: 130px;
	width: auto;
	color: var(--text-color);
	transition: color 1s;
	margin: 0 auto;
	position: relative;
	display: flex;
  align-items: center;
}

.grade_info {
	margin-left: 20px;
}

.grades_dashboard_box>div {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.grades_dashboard_box {
  width: 100px;
}

#newParent{
text-align:center;
}
<div id="grade_shortboard" class="separation">
		<div id="newParent">
                    <div class='grades_dashboard_box'>
                      <div>
                        <h1 class="grade_display" id="grade_display_worst">14</h1>
                        <div class="grade_info">
                          <p style="font-size: 15px;">Schlechteste Durchschnittsnote</p>
                          <p id="grade_display_worst_sub" style="font-size: 20px;">Biologie</p>
                        </div>
                      </div>
                    </div>
		</div>