我正在尝试将 <div>
居中使用 <center>
包含文本和图像的元素标签,但出于某种原因,它只适用于文本而不适用于图像。
HTML:
<body>
<h1 align="center">Search For a Member</h1>
<br>
<br>
<form action="SearchController" method="get">
Enter the member's first name: <input type="text" style="width: 18em;" name="searchBox">
<input style="width:6em;" type="submit" value="Search">
<input style="width:6em;padding-left:7px;" type="submit" value="Back">
</form>
<center>
<div class="profile">
<img style="float:left;" src="default.jpg"height=100 width=100>
First Name
<br>
Last Name
<br>
</div>
</center>
</body>
CSS:
<style>
body {
background-color: #BCD2EE;
margin-left: 20%;
margin-right: 20%;
border: 1px outset #4876FF;
border-width: 5px;
padding: 10px 10px 30px 10px;
}
.profile {
overflow: hidden;
padding-top: 30px;
}
</style>
但是如果您尝试一下,您会发现只有文本“名字”和“姓氏”居中,而图片不会居中。 (我知道你看不到图片 default.jpg
,但它会告诉你图片的位置)。
为什么图片不会居中?是因为 float:left;
属性,从 <center>
中排除图片标签?
如果是这样,如何解决?
最佳答案
<img style="float:left;"
你不应该有 style="float:left;"
删除它,并添加一个换行符 <br>
在 img 标签之后
关于html - <center> 标签只会让文字居中,不会让图片居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25229445/