2015-11-08 99 views
1

想知道任何人都可以提供幫助,我試圖定位一個div,但它似乎並沒有動。下面是代碼:定位一個div

.gallery-photos clearfix { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    margin-top: -50px; 
 
    margin-left: -50px; 
 
} 
 
.gallery-photos { 
 
    padding: 20px; 
 
} 
 
.gallery-photos .big-photo { 
 
    display: block; 
 
    background-color: #ffffff; 
 
    padding: 3px; 
 
    border: 1px solid #e7e7e7; 
 
    margin-right: 210px; 
 
} 
 
.gallery-photos .big-photo img { 
 
    display: block; 
 
    max-width: 100%; 
 
    height: auto; 
 
    margin: 0 auto; 
 
} 
 
.gallery-photos .photo-thumbnails { 
 
    float: right; 
 
    width: 210px; 
 
} 
 
.gallery-photos .photo-thumbnails .thumbnail { 
 
    float: left; 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    -webkit-box-sizing: border-box; 
 
    width: 30%; 
 
    height: 63px; 
 
    cursor: pointer; 
 
    padding: 3px; 
 
    border: 1px solid #e7e7e7; 
 
    margin-left: 3.33333%; 
 
    margin-bottom: 6px; 
 
    opacity: 0.4; 
 
} 
 
.gallery-photos .photo-thumbnails .thumbnail.current { 
 
    opacity: 1; 
 
    background-color: #ffffff; 
 
} 
 
.gallery-photos .photo-thumbnails .thumbnail .thumbnail-inner { 
 
    height: 100%; 
 
    overflow: hidden; 
 
} 
 
.gallery-photos .photo-thumbnails .thumbnail img { 
 
    display: block; 
 
    width: auto; 
 
    max-height: 100%; 
 
    margin: 0 auto; 
 
}
<div class="gallery-photos clearfix"> 
 
    <div class="photo-thumbnails"> 
 
    <div class="thumbnail current"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/1.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/2.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/3.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/4.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/5.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/6.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/1.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/2.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="images/3.jpg" alt="" /> 
 
     </div> 
 
    </div> 
 
    </div> 
 
    <div id="big-photo" class="big-photo"> 
 
    <img src="images/1.jpg" alt="" /> 
 
    </div> 
 
</div>

+1

你也可以提哪個div和哪裏? –

+0

他們都在屏幕的左上角,我想讓他們到頁面的中心 – eee

回答

2

更改這個選擇:.gallery-photos clearfix{.gallery-photos.clearfix{

爲中心,你可以做到這一點的元素:

.gallery-photos.clearfix{ 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%,-50%); 
} 
+0

仍然沒有動,還有其他建議嗎? – eee

+0

@EricaManning我已經更新了我的回答,所以我不確定你是否嘗試過更新後的版本('.gallery-photos.clearfix')與'.gallery-photos .clearfix') –

+0

這似乎現在工作,但大的照片div不會再出現在頁面上 – eee

1

試試這個https://jsfiddle.net/2Lzo9vfc/31/

HTML

<div class="gallery-photos clearfix"> 
    <div class="photo-thumbnails"> 
    <div class="thumbnail current"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    <div class="thumbnail"> 
     <div class="thumbnail-inner"> <img src="http://placehold.it/350x150" alt="" /> </div> 
    </div> 
    </div> 
    <div id="big-photo" class="big-photo"> <img src="http://placehold.it/900x600" alt="" /> </div> 

CSS

.gallery-photos.clearfix{ 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    margin-top: -50px; 
    margin-left: -50px; 
} 


.gallery-photos { 
    padding: 20px; 
} 

.gallery-photos .big-photo { 
    display: block; 
    background-color: #ffffff; 
    padding: 3px; 
    border: 1px solid #e7e7e7; 
    margin-right: 210px; 
} 

.gallery-photos .big-photo img { 
    display: block; 
    max-width: 100%; 
    height: auto; 
    margin: 0 auto; 
} 



.gallery-photos .photo-thumbnails { 
    float: right; 
    width: 210px; 

} 

.gallery-photos .photo-thumbnails .thumbnail { 
    float: left; 
    box-sizing: border-box; 
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    width: 30%; 
    height: 63px; 
    cursor: pointer; 
    padding: 3px; 
    border: 1px solid #e7e7e7; 
    margin-left: 3.33333%; 
    margin-bottom: 6px; 
    opacity: 0.4; 
} 

.gallery-photos .photo-thumbnails .thumbnail.current { 
    opacity: 1; 
    background-color: #ffffff; 
} 

.gallery-photos .photo-thumbnails .thumbnail .thumbnail-inner { 
    height: 100%; 
    overflow: hidden; 
} 

.gallery-photos .photo-thumbnails .thumbnail img { 
    display: block; 
    width: auto; 
    max-height: 100%; 
    margin: 0 auto; 
} 
+0

第一個選擇器是錯誤的,它應該是'.gallery-photos.clearfix'(不含空格) –

+0

您是對的。 –

0

如果使用position: absolute,使用父(position: relative你的情況body { position: relative }。如果設置在車身高度和寬度,以及(在body你的情況100vw X 100vh這是全視(整個可視區域)更容易。

html, 
 

 
body { 
 

 
    position: relative; 
 

 
    height: 100vh; 
 

 
    width: 100vw; 
 

 
} 
 

 
.gallery-photos.clearfix { 
 

 
    position: absolute; 
 

 
    top: 50%; 
 

 
    left: 50%; 
 

 
    margin-top: -50px; 
 

 
    margin-left: -50px; 
 

 
} 
 

 
.gallery-photos { 
 

 
    padding: 20px; 
 

 
} 
 

 
.gallery-photos .big-photo { 
 

 
    display: block; 
 

 
    background-color: #ffffff; 
 

 
    padding: 3px; 
 

 
    border: 1px solid #e7e7e7; 
 

 
    margin-right: 210px; 
 

 
} 
 

 
.gallery-photos .big-photo img { 
 

 
    display: block; 
 

 
    max-width: 100%; 
 

 
    height: auto; 
 

 
    margin: 0 auto; 
 

 
} 
 

 
.gallery-photos .photo-thumbnails { 
 

 
    float: right; 
 

 
    width: 210px; 
 

 
} 
 

 
.gallery-photos .photo-thumbnails .thumbnail { 
 

 
    float: left; 
 

 
    box-sizing: border-box; 
 

 
    -moz-box-sizing: border-box; 
 

 
    -webkit-box-sizing: border-box; 
 

 
    width: 30%; 
 

 
    height: 63px; 
 

 
    cursor: pointer; 
 

 
    padding: 3px; 
 

 
    border: 1px solid #e7e7e7; 
 

 
    margin-left: 3.33333%; 
 

 
    margin-bottom: 6px; 
 

 
    opacity: 0.4; 
 

 
} 
 

 
.gallery-photos .photo-thumbnails .thumbnail.current { 
 

 
    opacity: 1; 
 

 
    background-color: #ffffff; 
 

 
} 
 

 
.gallery-photos .photo-thumbnails .thumbnail .thumbnail-inner { 
 

 
    height: 100%; 
 

 
    overflow: hidden; 
 

 
} 
 

 
.gallery-photos .photo-thumbnails .thumbnail img { 
 

 
    display: block; 
 

 
    width: auto; 
 

 
    max-height: 100%; 
 

 
    margin: 0 auto; 
 

 
}
<div class="gallery-photos clearfix"> 
 
    <div class="photo-thumbnails"> 
 
    <div class="thumbnail current"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/000/0f0.png&text=IMG" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/0e0/aa0.png&text=IMG" /> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/ff0/07c.png&text=IMG"/> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/4cd/b40.png&text=IMG"/> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/9e0/160.png&text=IMG"/> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/e56/111.png&text=IMG"/> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/05f/7be.png&text=IMG"/> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/6d0/5ae.png&text=IMG"/> 
 
     </div> 
 
    </div> 
 
    <div class="thumbnail"> 
 
     <div class="thumbnail-inner"> 
 
     <img src="http://placehold.it/210/fa8/9f0.png&text=IMG"/> 
 
     </div> 
 
    </div> 
 
    </div> 
 
    <div id="big-photo" class="big-photo"> 
 
    <img src="http://placehold.it/480x320/000/0f0.png&text=IMG" /> 
 
    </div> 
 
</div>