2017-06-14 103 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