Html 文本对齐不适用于img
我有一个网站,我使用Html 文本对齐不适用于img,html,css,Html,Css,我有一个网站,我使用文本对齐将一些图像居中 现在,在我的主要内容div中,我试图将img置于中心位置,但它不起作用 <div id="Content" style="width:100%;position:absolute;overflow:auto;top:66px;bottom:200px;"> <img src="myimg.png" style="text-align: center;" /> </div> 就这些。为什么所有其他图像都居中
文本对齐
将一些图像居中
现在,在我的主要内容div
中,我试图将img
置于中心位置,但它不起作用
<div id="Content" style="width:100%;position:absolute;overflow:auto;top:66px;bottom:200px;">
<img src="myimg.png" style="text-align: center;" />
</div>
就这些。为什么所有其他图像都居中,而不是此图像?在
img
标记上使用文本对齐
,而在div
标记上使用它:
<div id="Content" style="width:100%;position:absolute;overflow:auto;top:66px;bottom:200px;text-align: center;">
<img src="myimg.png" />
</div>
文本对齐中心应该是您的div特征,而不是img
<div id="Content" style=" text-align: center; width:100%;position:absolute;overflow:auto;top:66px;bottom:200px;">
文本对齐:居中
需要应用于父元素,在您的情况下,div
:
<div id="Content" style="width:100%;position:absolute;overflow:auto;top:66px;bottom:200px;text-align: center;">
<img src="myimg.png" />
</div>
另一种方法是自动在图元周围分布边距,但这仅适用于块级图元:
#Content>img{
显示:块;
保证金:0自动;
}
您可以为此使用自动边距:
img {
display: block;
margin-left: auto;
margin-right: auto;
}
更多阅读:
或者,如果您知道图像的宽度:
img {
position: absolute;
left: 50%;
margin-left: -(half ot the image width)px
}
从避免使用内联CSS,在网站上这样做是不好的,因为搜索引擎对此持否定态度,总是尝试使用不同的CSS样式表。您可以使用溜冰场服务员提供的代码
文本对齐只能在块容器中使用设置标记的方式意味着图像标记中的内容将居中对齐。图像没有内容,所以它什么也不做。我将样式应用于
div
元素,而不是img
元素。div
元素的内容包含一个图像。感谢您的回答。我只使用内联,因为它可以快速、轻松地提出问题。