2017-06-14 104 views
0

我试图将图像组(图像为3x3的图像)居中放置到网页中心,我在添加图像叠加图像之前设法做到这一点。但是,由于我添加了图像叠加,图像出现在网页的左上角。我如何将它们分组并将它们居中,以及我应该如何获取图像位置,以便在设置图像叠加层时,它会转到特定图片,因为每张图片都会具有不同的图像叠加文本。将包含图像叠加图像的图像分组到图像中心

CSS

.container { 
    position: relative; 
    width: 100px; 
    height: 100px; 
} 

.image { 
    display: block; 
    width: 100px; 
    height: 100px; 
} 

.overlay { 
    position: absolute; 
    top: 0; 
    bottom: 0; 
    left: 0; 
    right: 0; 
    height: 100%; 
    width: 100%; 
    opacity: 0; 
    transition: .5s ease; 

} 

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

.text { 
    color: red; 
    font-size: 20px; 
    font-weight: bold; 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%, -50%); 
    -ms-transform: translate(-50%, -50%); 
} 

HTML

<div style="text-align:center"> 
<div class="container"> 
<img src="wheel1.jpg" class="image"> 
    <div class="overlay"> 
     <div class="text">Hello World</div> 
    </div> 
</div> 
<div class="container"> 
<img src="wheels2.jpg" class="image"> 
    <div class="overlay"> 
     <div class="text">Hello World</div> 
    </div> 
</div> 
<div class="container"> 
<img src="wheel3.jpg" class="image""> 
    <div class="overlay"> 
     <div class="text">Hello World</div> 
    </div> 
</div> 

`

回答

0

你可以使用flexbox居中。你的主要DIV

<div style="text-align-center;"> 
    ...... 
</div> 

更改为

<div style="display: flex; flex-direction: column;align-items: center;"> 
    ..... 
</div> 

,它应该工作。

.wrapper{ 
 
    display: flex; 
 
    flex-direction: row; 
 
    justify-content: center; 
 
} 
 

 
.container { 
 
    position: relative; 
 
    width: 100px; 
 
    height: 100px; 
 
} 
 

 
.image { 
 
    display: block; 
 
    width: 100px; 
 
    height: 100px; 
 
    border: 1px solid red; 
 
} 
 

 
.overlay { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    right: 0; 
 
    height: 100%; 
 
    width: 100%; 
 
    opacity: 0; 
 
    transition: .5s ease; 
 

 
} 
 

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

 
.text { 
 
    color: red; 
 
    font-size: 20px; 
 
    font-weight: bold; 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    -ms-transform: translate(-50%, -50%); 
 
}
<div class="wrapper"> 
 
    <div class="container"> 
 
    <div class="image"></div> 
 
     <div class="overlay"> 
 
      <div class="text">Hello World</div> 
 
     </div> 
 
    </div> 
 
    <div class="container"> 
 
    <div class="image"></div> 
 
     <div class="overlay"> 
 
      <div class="text">Hello World</div> 
 
     </div> 
 
    </div> 
 
    <div class="container"> 
 
    <div class="image"></div> 
 
     <div class="overlay"> 
 
      <div class="text">Hello World</div> 
 
     </div> 
 
    </div> 
 
    </div>

Here is the fiddle.

+0

没错它的工作,映像文件,现在是中心,但是我想要一个3x3的表格,所以3图像每一行,此刻IM下另一个获得1个图像。添加图片叠加之前,他们工作得很好,现在找不到错误。我尝试将flex-direction改为“row”,现在3张图像连续显示,但是他们又回到了左上角。 – Luke

+0

@Luke添加'justify-content:center'和'flex-direction:row'。查看更新后的答案 – Boky

+0

是的,我想到了flex-direction:row是正确的。感谢帮助它现在的工作。 – Luke