[英]How can I center an image in HTML while it's inside a div?
我在div內有一個圖像,可以在圖像上放置一個按鈕。 我在網上搜索過,但找不到任何使圖像居中的方法。
我試着使其成為自己的類,並將img標簽本身居中,但它們似乎都不起作用。
<div class="container"> <img src="https://cdn.discordapp.com/avatars/543553627600584735/470015f633d8ae88462c3cf9fa7fd01f.png?size=256" alt="utili"> <a href="utili.html" class="btn">Utili</a> </div>
圖片應居中於頁面中間,因此我可以排成一行3。
在HTML中:
<img src="paris.jpg" alt="Paris" class="center">
要使圖像居中,請將左右邊距設置為auto
並將其設置為block
元素:
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
因此,您可以將任何圖像居中放置在div中。 希望對您有所幫助。
您可以將.btn
絕對值.btn
在相對容器中。 如果您知道圖像的大小,那就更好了。
我將如何實現它:
.container {
position: relative;
height: (the height of your image);
width: (the width of your image);
}
img {
position: relative;
width: 100%;
height: 100%;
z-index: 1;
}
.btn {
position: absolute;
bottom: (however far you want it from the bottom in pxs - so lets say 10px);
left: 50%;
transform: translateX(-50%);
z-index: 2;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.