2017-04-27 33 views
0

我希望「category_images」具有其子元素的寬度,因此「category_images」可以從「category_images_wrapper」的「margin:0 auto」居中。
當前「category_images」具有其父「category_images_wrapper」的寬度,並且它不居中。如何獲得子項的寬度,所以我可以居中塊

HTML:

<center><h2>Featured Categories</h2></center> 

<div class="category_images_wrapper"> 
<div class="category_images"> 


<!--First image--> 

<div class="category_images_item"> 
<a href=""> 
<span>Watches</span> 

<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 

</div> 
</a> 
</div> 



<!--second image--> 

<div class="category_images_item"> 
<a href=""> 
<span>Gadgets</span> 

<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 

</div> 
</a> 
</div> 


<!--third image--> 

<div class="category_images_item"> 
<a href=""> 
<span>Headshop</span> 

<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 

</div> 
</a> 
</div> 


<!--fourth image--> 

<div class="category_images_item"> 
<a href=""> 
<span>test</span> 

<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 

</div> 
</a> 
</div> 


<!--fifth image--> 

<div class="category_images_item"> 
<a href=""> 
<span>test</span> 

<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 

</div> 
</a> 
</div> 



<!--sixth image--> 

<div class="category_images_item"> 
<a href=""> 
<span>teeest</span> 

<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 

</div> 
</a> 
</div> 







<div style="clear: both"></div> 

</div> 
</div> 

CSS:

.category_images_wrapper{ 
    max-width:1050px; 
    margin:0 auto; 
} 
.category_images{ 

} 
.category_images_item{ 
    width: 310px; 
    height: 280px; 
    float:left; 
    position:relative; 
    background-color: black; 
    overflow:hidden; 
    transition: all 0.8s ease; 
    margin-left:25px; 
    margin-top:25px; 
} 
.category_images_item span{ 
    color: white; 
    font-weight: bold; 
    font-size: 32px; 
    font-style: italic; 
    z-index: 100; 
    position: absolute; 
    width:100%; 
    text-align:center; 
    top:120px; 
} 
.category_images_img{ 
    width:310px; 
    height:280px; 
    transition: all 0.8s ease; 
    opacity:0.7; 
} 
.category_images_item:hover .category_images_img{ 
    transform: scale(1.06) rotate(-2deg); 
    opacity: 0.5; 
    } 

小提琴:https://jsfiddle.net/kn05uv4k/

非常感謝!

回答

1

您可以使用彈性盒將它們居中。另外我建議你不要因爲響應性問題而使用固定寬度的元素。如果你想設置的一排2項補充width:calc(50% - 30px)

,30PX是左右頁邊距的總和

讓我知道如果這是你要找的人

.category_images_wrapper{ 
 
    max-width:1050px; 
 
    margin:0 auto; 
 
} 
 
.category_images{ 
 
\t display: flex; 
 
    flex-wrap: wrap; 
 
\t 
 
} 
 
.category_images_item{ 
 
    height: 280px; 
 
    position: relative; 
 
    background-color: black; 
 
    overflow: hidden; 
 
    transition: all 0.8s ease; 
 
    margin: 15px 15px 0; 
 
    width: calc(50% - 30px); 
 
} 
 
.category_images_item span{ 
 
\t color: white; 
 
    font-weight: bold; 
 
    font-size: 32px; 
 
    font-style: italic; 
 
\t z-index: 100; 
 
\t position: absolute; 
 
\t width:100%; 
 
\t text-align:center; 
 
\t top:120px; 
 
} 
 
.category_images_img{ 
 
\t width:100%; 
 
\t height:100%; 
 
\t transition: all 0.8s ease; 
 
\t opacity:0.7; 
 
} 
 
.category_images_item:hover .category_images_img{ 
 
    transform: scale(1.06) rotate(-2deg); 
 
    opacity: 0.5; 
 
    }
<center><h2>Featured Categories</h2></center> 
 

 
<div class="category_images_wrapper"> 
 
<div class="category_images"> 
 

 

 
<!--First image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>Watches</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 

 

 

 
<!--second image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>Gadgets</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 

 

 
<!--third image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>Headshop</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 

 

 
<!--fourth image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>test</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 
    
 

 
<!--fifth image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>test</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 
    
 
    
 
    
 
<!--sixth image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>teeest</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 
    
 
    
 

 
    
 
    
 
    
 

 
<div style="clear: both"></div> 
 

 
</div> 
 
</div>

+0

完美!正是我在找什麼。非常感謝:)) – N1njaWTF

+0

很高興我能幫上忙。歡呼! :d –

0

沒有必要設置它使用保證金,則可以使用flexboxinline-block設置...

.category_images_wrapper{ 
 
    max-width:1050px; 
 
    margin:0 auto; 
 
} 
 
.category_images{ 
 
\t text-align: center; 
 
} 
 
.category_images_item{ 
 
\t width: 310px; 
 
\t height: 280px; 
 
    display: inline-block; 
 
\t position:relative; 
 
    background-color: black; 
 
\t overflow:hidden; 
 
\t transition: all 0.8s ease; 
 
\t margin-left:25px; 
 
    margin-top:25px; 
 
} 
 
.category_images_item span{ 
 
\t color: white; 
 
    font-weight: bold; 
 
    font-size: 32px; 
 
    font-style: italic; 
 
\t z-index: 100; 
 
\t position: absolute; 
 
\t width:100%; 
 
\t text-align:center; 
 
\t top:120px; 
 
    left: 0px; 
 
} 
 
.category_images_img{ 
 
\t width:310px; 
 
\t height:280px; 
 
\t transition: all 0.8s ease; 
 
\t opacity:0.7; 
 
} 
 
.category_images_item:hover .category_images_img{ 
 
    transform: scale(1.06) rotate(-2deg); 
 
    opacity: 0.5; 
 
    }
<center><h2>Featured Categories</h2></center> 
 

 
<div class="category_images_wrapper"> 
 
<div class="category_images"> 
 

 

 
<!--First image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>Watches</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 

 

 

 
<!--second image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>Gadgets</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 

 

 
<!--third image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>Headshop</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 

 

 
<!--fourth image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>test</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 
    
 

 
<!--fifth image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>test</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 
    
 
    
 
    
 
<!--sixth image--> 
 

 
<div class="category_images_item"> 
 
<a href=""> 
 
<span>teeest</span> 
 

 
<div class="category_images_img" style="background: url(https://static.pexels.com/photos/1279/fashion-wristwatch-time-watch.jpg)no-repeat center;background-size:cover;"> 
 

 
</div> 
 
</a> 
 
</div> 
 
    
 
    
 

 
    
 
    
 
    
 

 
<div style="clear: both"></div> 
 

 
</div> 
 
</div>

相關問題