【发布时间】:2018-04-18 13:42:11
【问题描述】:
我尝试在容器 DIV 中居中(水平和垂直)文本和图像的串联。这是代码:
<!DOCTYPE html>
<html>
<head>
<style>
#myBox {display:block; width:300px; height:100px;
text-align:center; font-size:20px; line-height:500%;
background-color:yellow;}
#myImg {display:block; width:33.33%; height:100%;}
</style>
</head>
<body>
<div id="myBox">
My text
<img id="myImg" src="red100x100.jpg" />
</div>
</body>
</html>
文本确实居中,但图像出现在新行中。
编辑:
这类似于center image next to two lines of text responsively,但后者也处理margin:auto的效果,而本题没有。
【问题讨论】:
-
设置图片为
div的背景图片。要使 div 居中,请参阅 flexbox。 demo.agektmr.com/flexbox -
center image next to two lines of text responsively 的可能重复项以及通过搜索 SO 找到的许多其他项。此外,
<img>标签不使用或不需要结束斜线。