Элемент Div не выравнивается сразу после добавления background-image - PullRequest
0 голосов
/ 11 апреля 2019

После добавления background-image к моему элементу div он не выравнивается в сетке правильно.

Первоначально я пытался вставить тег <img> в <div>, но затем изменилэто к background-image.Теперь он действует как элемент, отображаемый в блоке, так как он занимает всю строку и сдвигает остальные три, которые должны отображаться в той же строке, к одной под ним, но у него нет поля внизу, поэтомуЯ действительно понятия не имею, что происходит.

HTML

<!-- Container for body content -->
    <div id="bodyContent">
        <!-- If we can find a way to make this enable us to simply put a game in a list and it automatically 
        place it accodingly, change this to that system. Maybe make just one column and have it wrap content? -->
        <div id="adBarLeft" class="adBar"> AD BAR </div>
        <div id="gameColumn"> 
            <div class="gamePreview" id="game1"> </div>
            <div class="gamePreview"> 2 </div>
            <div class="gamePreview"> 3 </div>
            <div class="gamePreview"> 4 </div>
            <div class="gamePreview"> 5 </div>
      <div class="gamePreview"> 6 </div>
      <div class="gamePreview"> 7 </div>
      <div class="gamePreview"> 6 </div>
      <div class="gamePreview"> 9 </div>
      <div class="gamePreview"> 10 </div>
        </div>
        <div id="adBarRight" class="adBar"> AD BAR </div>
    </div>

CSS

.gamePreview {
    display: inline-block;
    width: 20%;
    height: 0;
    padding-bottom:20%;
    border:3px solid orange;
    margin:1.5%; /* Figure out why this value let 4 on compared to 2.5% which didnt, but should've */
}

#game1 { /* figure out why its setting it off from the others */
  background-image: url("https://is1-ssl.mzstatic.com/image/thumb/Purple71/v4/47/cf/cf/47cfcf79-9e1d-b21f-8e10-2658b7650c15/mzl.oiljceng.png/246x0w.jpg");
  background-size: contain;
}

Я хочу, чтобы изображение загружалось внутри <div> и имело четырев том же ряду, а затем переходите к следующей строке, по существу создавая строки с 4 <div> s

Ответы [ 2 ]

1 голос
/ 11 апреля 2019

Добавьте vertical-align: top; к вашему классу .gamePreview, чтобы выровнять встроенный блок с верхом.

.gamePreview {
  display: inline-block;
  width: 20%;
  height: 0;
  padding-bottom: 20%;
  border: 3px solid orange;
  margin: 1.5%;
  vertical-align: top; /* Add this */
  /* Figure out why this value let 4 on compared to 2.5% which didnt, but should've */
}

#game1 {
  /* figure out why its setting it off from the others */
  background-image: url("https://is1-ssl.mzstatic.com/image/thumb/Purple71/v4/47/cf/cf/47cfcf79-9e1d-b21f-8e10-2658b7650c15/mzl.oiljceng.png/246x0w.jpg");
  background-size: contain;
}
<!-- Container for body content -->
<div id="bodyContent">
  <!-- If we can find a way to make this enable us to simply put a game in a list and it automatically 
        place it accodingly, change this to that system. Maybe make just one column and have it wrap content? -->
  <div id="adBarLeft" class="adBar"> AD BAR </div>
  <div id="gameColumn">
    <div class="gamePreview" id="game1"> </div>
    <div class="gamePreview"> 2 </div>
    <div class="gamePreview"> 3 </div>
    <div class="gamePreview"> 4 </div>
    <div class="gamePreview"> 5 </div>
    <div class="gamePreview"> 6 </div>
    <div class="gamePreview"> 7 </div>
    <div class="gamePreview"> 6 </div>
    <div class="gamePreview"> 9 </div>
    <div class="gamePreview"> 10 </div>
  </div>
  <div id="adBarRight" class="adBar"> AD BAR </div>
</div>
0 голосов
/ 11 апреля 2019

Я думаю, что лучше с display:flex в родительском контейнере #gameColumn потому что DIV's разместит пространство

#gameColumn{
    display: flex;
  	flex-wrap: wrap;
}

.gamePreview {
    flex-basis: 100px;
    height: 100px;
    border:3px solid orange;
    margin:2px; /* Figure out why this value let 4 on compared to 2.5% which didnt, but should've */
}

#game1 { /* figure out why its setting it off from the others */
  background-image: url("https://is1-ssl.mzstatic.com/image/thumb/Purple71/v4/47/cf/cf/47cfcf79-9e1d-b21f-8e10-2658b7650c15/mzl.oiljceng.png/246x0w.jpg");
  background-size: contain;
  background-repeat: no-repeat;
}
<!-- Container for body content -->
    <div id="bodyContent">
        <!-- If we can find a way to make this enable us to simply put a game in a list and it automatically 
        place it accodingly, change this to that system. Maybe make just one column and have it wrap content? -->
        <div id="adBarLeft" class="adBar"> AD BAR </div>
        <div id="gameColumn"> 
            <div class="gamePreview" id="game1"> </div>
            <div class="gamePreview"> 2 </div>
            <div class="gamePreview"> 3 </div>
            <div class="gamePreview"> 4 </div>
            <div class="gamePreview"> 5 </div>
      <div class="gamePreview"> 6 </div>
      <div class="gamePreview"> 7 </div>
      <div class="gamePreview"> 6 </div>
      <div class="gamePreview"> 9 </div>
      <div class="gamePreview"> 10 </div>
        </div>
        <div id="adBarRight" class="adBar"> AD BAR </div>
    </div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...