如何将带有图像的浮动div水平居中?

时间:2018-04-03 12:23:23

标签: css html5

我正在尝试将我的图片库与ebay对齐,而我无法拥有有效内容,因此我必须将其与HTML& CSS。我无法让图像与彼此一起浮动。保持水平居中在页面上。他们似乎处于中心位置。我很难过,我尝试了很多不同的方法,但到目前为止还没有运气。我在这个问题上花了3个小时尝试其他人的解决方案但没有用。

div.gallery {
    border: 1px solid #ccc;
    }

    div.gallery:hover {
    border: 1px solid #777;
    }

    div.gallery img {
    width: 100%;
    height: auto;
    }

    div.desc {
    padding: 15px;
    text-align: center;
    }

    * {
    box-sizing: border-box;
    }

    .responsive {
    padding: 0 6px;
    float: left;
    width: 24.99999%;
    }

    @media only screen and (max-width: 700px) {
    .responsive {
        width: 49.99999%;
        margin: 6px 0;
    }
    }

    @media only screen and (max-width: 500px) {
    .responsive {
        width: 100%;
    }
    }

    .clearfix:after {
    content: "";
    display: table;
    clear: both;
    }
<html>
      <div class="responsive">
        <div class="gallery">
          <a target="_blank" href="https://s20.postimg.org/eaz0ihd0d/usb3.png">
            <img src="https://s20.postimg.org/eaz0ihd0d/usb3.png" alt="image of bluetooth 4.0 usb adapter dongle" width="100%" height="auto">
          </a>
        </div>
      </div>

      <div class="clearfix">
      </div>
</html>

2 个答案:

答案 0 :(得分:0)

当你漂浮它们时,你不能使元素居中。在这里,我已将其设为block并将margin置于中心位置。

&#13;
&#13;
div.gallery {
  border: 1px solid #ccc;
}

div.gallery:hover {
  border: 1px solid #777;
}

div.gallery img {
  width: 100%;
  height: auto;
}

div.desc {
  padding: 15px;
  text-align: center;
}

* {
  box-sizing: border-box;
}

.responsive {
  padding: 0 6px;
  display: block;
  width: 24.99999%;
  margin: auto;
}

@media only screen and (max-width: 700px) {
  .responsive {
    width: 49.99999%;
    margin: 6px 0;
  }
}

@media only screen and (max-width: 500px) {
  .responsive {
    width: 100%;
  }
}

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}
&#13;
<div class="responsive">
  <div class="gallery">
    <a target="_blank" href="https://s20.postimg.org/eaz0ihd0d/usb3.png">
      <img src="https://s20.postimg.org/eaz0ihd0d/usb3.png" alt="image of 
bluetooth 4.0 usb adapter dongle" width="100%" height="auto">
    </a>
  </div>
</div>
<div class="clearfix">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是你期待的吗?

&#13;
&#13;
Grp_Avg = 
CALCULATE(EmployeeFeedback[Emp_AVG], 
ALLEXCEPT(EmployeeFeedback,EmployeeFeedback[Group]))
&#13;
div.gallery {
  border: 1px solid #ccc;
}

div.gallery:hover {
  border: 1px solid #777;
}

div.gallery img {
  width: 100%;
  height: auto;
}

div.desc {
  padding: 15px;
  text-align: center;
}

.responsive {
  padding: 0 6px;
  width: 60%;
  margin: auto;
}

@media only screen and (max-width: 700px) {
  .responsive {
    width: 49.99999%;
    margin: auto;
  }
}

@media only screen and (max-width: 500px) {
  .responsive {
    width: 50%;
  }
}
&#13;
&#13;
&#13;