如何居中对齐此html内容

时间:2018-08-01 16:33:42

标签: html css

我希望我的html内容居中对齐,同时保持id game-inst的列结构 请随时编辑我的CSS代码,等待您的帮助,谢谢

enter image description here

#score-container {
  display: block;
  width: 100%;
  height: 100%;
  text-align: center;
  background-color: #1B2631;
  color: #ffffff;

}

.key {
  width: 40%;
  display: inline-block;
}

.instruction {
  display: inline-block;
  width: 40%;
  text-align: left;
}

#my-score,
#high-score,
#game-inst {
  font-size: 1.25em;
  padding: 0px 10px;
  line-height: 1;
}
<div id="score-container">
  <div id="game-inst">
    <div class="key">Key 4:</div>
    <div class="instruction">Move UP</div>
    <div class="key">Key 6:</div>
    <div class="instruction">Move DOWN</div>
    <div class="key">Key 5:</div>
    <div class="instruction">PLAY/PAUSE</div>
  </div>
  <p id="my-score">123</p>
  <p id="high-score">786</p>
</div>

1 个答案:

答案 0 :(得分:-1)

我相信这就是你想要的。

#score-container {
  top: 100px;
  display: block;
  position: relative;
  left: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  z-index: 10;
}

.key {
  width: 30%;
  display: inline-block;
  float: left;
}

.instruction {
  width: 70%;
  display: inline-block;
  float: left;
}

#score-box {
  position: relative;
  width: 300px;
  margin-left: auto;
  margin-right: auto;
  padding: 0;
}

#game-inst:after {
  content: ".";
  visibility: hidden;
  display: block;
  height: 0;
  clear: both;
}

#my-score,
#high-score,
#game-inst {
  font-size: 1.25em;
  padding: 0px 10px;
  line-height: 1;
  text-align: left;
}

#score-box p {
  width: 42%;
  display: inline-block;
  text-align: left;
  border: solid 1px silver;
}
<div id="score-container">
<div id="score-box">
  <div id="game-inst">
    <div class="key">Key 4:</div>
    <div class="instruction">Move UP</div>
    <div class="key">Key 6:</div>
    <div class="instruction">Move DOWN</div>
    <div class="key">Key 5:</div>
    <div class="instruction">PLAY/PAUSE</div>
  </div>
  <p id="my-score">123</p>
  <p id="high-score">786</p>
</div>
</div>

请注意 .key .instruction #game-inst #game-inst的更改:之后

#game-inst:after {
  content: ".";
  visibility: hidden;
  display: block;
  height: 0;
  clear: both;
}

这将清除添加到键和指令类的浮点数。