如何在HTML / CSS中水平对齐图像(浮动和显示内联块不工作)

时间:2017-10-06 09:56:08

标签: html css

我有一个水平图像的图库,然后我做了它,以便当你悬停时图像有文本覆盖,现在没有什么我曾经用来使项目水平排列的工作。我尝试在所有选择器上使用float(同样使用display inline-block),但没有

deleteUser(user) {
    console.log("user to delete:" + user._id);
    let myParams = new URLSearchParams();
    myParams.append('id', user._id);
    return this.http.delete('/api/user', { search: myParams })
      .map(res => res.json());
}

和CSS

    <div class = "gallery">
      <div>
        <div class = "container">
            <img src="Images/ConorWhisky.jpg" class = "img"><div class = "overlay">
            <div class = "text">Hello</div>
        </div>
      </div>
    </div>
    <div>
        <div class = "container">
            <img src = "Images/Cricket.jpg" class = "img">
            <div class = "overlay">
            <div class = "text">Hello</div>
            </div>    
         </div>
    </div>

3 个答案:

答案 0 :(得分:0)

使用flex;你可以了解它here

&#13;
&#13;
.container {
  position: relative;
  width: 100%;
}

.gallery {
  width: 25%;
  padding: 0vw;
  margin: 3%;
  border-radius: 5vw;
  display: flex;
  flex-direction: row;
  margin-left: 5%
}

.img {
  width: 100%;
  height: 120%;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 95%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: blueviolet;
}

.container:hover .overlay {
  opacity: 1;
}

.text {
  color: aliceblue;
  font-size: 2vw;
  font-family: 'poppins', sans-serif;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}
&#13;
<div class="gallery">
  <div>
    <div class="container">
      <img src="Images/ConorWhisky.jpg" class="img">
      <div class="overlay">
        <div class="text">Hello</div>
      </div>
    </div>
  </div>
  <div>
    <div class="container">
      <img src="Images/Cricket.jpg" class="img">
      <div class="overlay">
        <div class="text">Hello</div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用以下代码段更新了您的css ...

&#13;
&#13;
.container,
.gallery > div {
    position: relative;
    display: inline-block;
}

.gallery {
    width: 100%;
    padding: 0vw;
    margin: 3%;
    border-radius: 5vw;
    float: left;
}

.img {
    width: 100%;
    height: 120%;
    min-width: 200px;
    min-height: 200px;
}

.overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 95%;
    width: 100%;
    opacity: 0;
    transition: .5s ease;
    background-color: blueviolet;
}

.container:hover .overlay {
    opacity: 1;
}

.text {
    color: aliceblue;
    font-size: 2vw;
    font-family: 'poppins', sans-serif;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
}
&#13;
<div class="gallery">
    <div>
        <div class="container"> <img src="Images/ConorWhisky.jpg" class="img">
            <div class="overlay">
                <div class="text">Hello</div>
            </div>
        </div>
    </div>
    <div>
        <div class="container"> <img src="Images/Cricket.jpg" class="img">
            <div class="overlay">
                <div class="text">Hello</div>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我已更改了您的HTML和CSS,并使用flexbox属性来实现您的布局。这样可以避免使用floatinline-block

&#13;
&#13;
.gallery {
  width: 25%;
  margin: 3%;
  border-radius: 5vw;
  margin-left: 5%;
  display: flex;
}

.container {
  position: relative;
  width: 100%;
}

.img {
  width: 100%;
  height: auto;
  display: flex;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: blueviolet;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container:hover .overlay {
  opacity: 1;
}

.text {
  color: aliceblue;
  font-size: 2vw;
  font-family: 'poppins', sans-serif;
}
&#13;
<div class="gallery">
  <div class="container">
    <img src="https://unsplash.it/200/300" class="img">
    <div class="overlay">
      <div class="text">Hello</div>
    </div>
  </div>
  <div class="container">
    <img src="https://unsplash.it/200/300" class="img">
    <div class="overlay">
      <div class="text">Hello</div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;