如何居中垂直浮动元素,元素缩放问题,img元素大小问题

时间:2017-01-16 15:05:01

标签: html css responsive-design

我试图制作一款简单的游戏,而且我遇到的问题似乎无法解决:

  1. 我无法将垂直浮动的元素(.stat.clickable)居中。

  2. 所有元素的总高度应完全符合屏幕高度,但超出了它。

  3. 根据我给出的值,图像的宽度略有不同(在我的屏幕上,它们在32%或29%时看起来相同,但在30%上面的图像稍微宽一点(并且有点模糊)境)。

  4. img元素的高度属性无效。

  5. 这是我的代码(图片宽450像素):

    
    
    * {
      margin: 0;
      padding: 0;
    }
    html,
    body {
      height: 100%;
      background-color: #e6e6e6;
    }
    .statsBar,
    .buttons {
      color: #333333;
      font-family: Impact, Charcoal, sans-serif;
      text-transform: uppercase;
      background-color: #bfbfbf;
      height: 13%;
    }
    #score {
      float: left;
      margin-left: 5%;
    }
    #hp {
      float: right;
      margin-right: 5%;
    }
    .stats:after {
      content: "";
      display: block;
      clear: both;
    }
    .clickable {
      float: left;
      width: 29.33%;
      margin: 1%;
      padding: 1%;
      background: #f2f2f2;
    }
    .game {
      width: 50%;
      height: 100%;
      text-align: center;
      margin: auto;
      background-color: #999999;
    }
    img {
      width: 32%;
      display: block;
      margin: auto;
    }
    #enemyHand {
      transform: rotate(180deg);
      margin-top: 5%;
    }
    #playerHand {
      margin-bottom: 5%;
    }
    
    <html>
    
    <head>
      <link rel="stylesheet" type="text/css" href="style.css">
      <script type="text/javascript" src='game.js'></script>
    </head>
    
    <body>
      <div class="game">
        <div class="statsBar">
          <p id="score" class="stat">score: 0</p>
          <p id="hp" class="stat">hp: 3</p>
        </div>
    
        <div class="hands">
          <img id="enemyHand" src="paper.png">
          <img id="playerHand" src="scissors.png">
        </div>
    
        <div class="buttons">
          <a id="paper" class="clickable" onclick="document.getElementById('playerHand').src='paper.png'">Paper</a>
          <a id="rock" class="clickable" onclick="document.getElementById('playerHand').src='rock.png'">Rock</a>
          <a id="scissors" class="clickable" onclick="document.getElementById('playerHand').src='scissors.png'">Scissors</a>
        </div>
      </div>
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;

3 个答案:

答案 0 :(得分:0)

我不知道你的第一个问题是什么意思。但是我可以用第二个帮助你。我对你的代码做了一些小改动,但我没有这个图像。看下面的代码。游戏容器现在设置为max-height:100%;和身高:100vh;应该有所帮助。 (100vh意味着孔页。我也使身体溢出:隐藏;,因为我认为不需要滚动。我将按钮容器放在页面底部。

* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
  background-color: #e6e6e6;
  overflow:hidden;
}
.statsBar,
.buttons {
  color: #333333;
  font-family: Impact, Charcoal, sans-serif;
  text-transform: uppercase;
  background-color: #bfbfbf;
  height: 13%;
}
.buttons {
  bottom: 0;
  clear:both;
}
#score {
  float: left;
  margin-left: 5%;
}
#hp {
  float: right;
  margin-right: 5%;
}
.stats:after {
  content: "";
  display: block;
  clear: both;
}
.clickable {
  float: left;
  width: 29.33%;
  margin: 1%;
  padding: 1%;
  background: #f2f2f2;
}
.game {
  width: 50%;
  max-height: 100%;
  height: 100vh;
  text-align: center;
  margin: auto;
  background-color: #999999;
}
img {
  height: 50%;
  display: block;
  margin: auto;
}
#enemyHand {
  transform: rotate(180deg);
  margin-top: 5%;
}
#playerHand {
  margin-bottom: 5%;
}

对于第三个问题,我们没有图像...... 对于height属性,请尝试display:block;然后没有宽度。通常检查你的分类应该有效。

我希望我能帮助你!!!

答案 1 :(得分:0)

在这种情况下,你可以利用flexbox(注意滚动是由片段的视口height生成的,理想情况下它甚至不会溢出,但如果确实如此,{{1设置只是为了处理它,你可以根据你的好处评论它:)

&#13;
&#13;
overflow: auto
&#13;
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,
body {
  height: 100%;
  background-color: #e6e6e6;
}
.statsBar,
.buttons {
  color: #333333;
  font-family: Impact, Charcoal, sans-serif;
  text-transform: uppercase;
  background-color: #bfbfbf;
  height: 13%;
  display: flex;
  flex-flow: row wrap;
  justify-content: space-around;
  align-items: center;
}
.hands {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  /* comment if content will never overflow */
  overflow-x: auto;
}
.buttons {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  align-items: center;
}
.clickable {
  float: left;
  width: 29.33%;
  margin: 1%;
  padding: 1%;
  background: #f2f2f2;
}
.game {
  width: 50%;
  height: 100%;
  /*text-align: center;*/
  margin: auto;
  background-color: #999999;
  display: flex;
  flex-flow: column wrap;
  justify-content: center;
}
img {
  width: 32%;
  display: block;
  margin: auto;
}
/*#score {
  float: left;
  margin-left: 5%;
}*/

/*#hp {
  float: right;
  margin-right: 5%;
}*/

/*.stats:after {
  content: "";
  display: block;
  clear: both;
}*/

/*#enemyHand {
  transform: rotate(180deg);
  margin-top: 5%;
}*/

/*#playerHand {
  margin-bottom: 5%;
}*/
&#13;
&#13;
&#13;

答案 2 :(得分:0)

请检查此代码。我解决了你的问题1和2.我不了解你的形象问题。

&#13;
&#13;
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
  background-color: #e6e6e6;
}
.statsBar,
.buttons {
  display: table;
  color: #333333;
  font-family: Impact, Charcoal, sans-serif;
  text-transform: uppercase;
  background-color: #bfbfbf;
  height: 13%;
  width: 100%;
}
#score,
#hp{
  display: table-cell;
  vertical-align: middle;
  padding: 10px;
}
#score {
  /*float: left;
  margin-left: 5%;*/
  text-align: left;
}
#hp {
  /*float: right;
  margin-right: 5%;*/
  text-align: right;
}
.stats:after {
  content: "";
  display: block;
  clear: both;
}
.clickable {
  /*float: left;*/ /*Float sould not use here */
  display: table-cell;
  width: 29.33%;
  /*margin: 1%;
  padding: 1%;*/
  border: 5px solid #bfbfbf;
  vertical-align: middle;
  background: #f2f2f2;
}
.game {
  width: 50%;
  height: 100%;
  text-align: center;
  margin: auto;
  background-color: #999999;
}
img {
  width: 32%;
  display: block;
  margin: auto;
}
#enemyHand {
  transform: rotate(180deg);
  margin-top: 5%;
}
#playerHand {
  margin-bottom: 5%;
}
&#13;
<html>

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <script type="text/javascript" src='game.js'></script>
</head>

<body>
  <div class="game">
    <div class="statsBar">
      <p id="score" class="stat">score: 0</p>
      <p id="hp" class="stat">hp: 3</p>
    </div>

    <div class="hands">
      <img id="enemyHand" src="http://placehold.it/450x450">
      <img id="playerHand" src="http://placehold.it/450x450">
    </div>

    <div class="buttons">
      <a id="paper" class="clickable" onclick="document.getElementById('playerHand').src='paper.png'">Paper</a>
      <a id="rock" class="clickable" onclick="document.getElementById('playerHand').src='rock.png'">Rock</a>
      <a id="scissors" class="clickable" onclick="document.getElementById('playerHand').src='scissors.png'">Scissors</a>
    </div>
  </div>
</body>

</html>
&#13;
&#13;
&#13;