属性text-align: center;一个好方法来中心使用CSS图像?
img {
text-align: center;
}
属性text-align: center;一个好方法来中心使用CSS图像?
img {
text-align: center;
}
当前回答
如果你的img元素在一个div中,它本身在另一个div中,它的显示被设置为flexbox,就像我这里的例子: (HTML)
<nav class="header">
<div class="image">
<img
src=troll
alt="trollface"
></img>
</div>
<div class="title">
Meme Generator
</div>
<div class="subtitle">
React Course - Project 3
</div>
</nav>
(CSS)
.header{
display: flex;
}
.image{
width: 5%;
height: 100%;
}
.image > img{
width: 100%;
}
你可以这样设置你的。image div垂直对齐:
.image{
width: 5%;
height: 100%;
align-self: center;
}
其他回答
你可以:
<中心> < img src = "…"/ > < /中心>
如果你正在使用一个带有图像的类,那么下面的步骤就可以了
class {
display: block;
margin: 0 auto;
}
如果它只是一个图像在一个特定的类,你想居中对齐,那么下面将做:
class img {
display: block;
margin: 0 auto;
}
简单地改变父对齐:)
在父属性上试试这个:
text-align:center
我发现,如果我在一个div中有一个图像和一些文本,那么我可以使用text-align:center一次性对齐文本和图像。
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen: https://codepen.io/artforlife/pen/MoBzrL?editors=1100
我找到的最简单的解决方案是添加到我的img-element:
style="display:block;margin:auto;"
看来我不需要像别人建议的那样在auto前面加0。也许这是正确的方法,但对于我的目的来说,没有“0”也足够了。至少在最新的Firefox、Chrome和Edge上是这样。