如何使用 CSS 将 <div> 标签居中?
cssweb developmentfront end technology更新于 2024/3/28 19:56:17
示例
<html> <head> <title>div in center without css</title> </head> <body> <div> THIS IS ONE PARAGRAPH. HERE WE ARE NOT USE ANY ALIGNMENT. </div> <div align="center" style="border: 2px solid blue"> THIS PARAGRAPH IS IN CENTER </div> </body> </html>
示例
<html> <head> <title>div in center with css</title> <link href="../../Content/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div> THIS IS ONE PARAGRAPH. HERE WE ARE NOT USE ANY ALIGNMENT. </div> <div class="mydiv"> THIS PARAGRAPH IS IN CENTER </div> </body> </html>
Style.css
body {
background-color:Gray;
}
.mydiv {
padding:10px;
margin:auto;
width:30%;
border:4px solid black;
}
如果您希望文本也位于框的中心,那么在 CSS 文件的 .mydiv 类中写入 text-align:center。
相关文章
有用资源
css 参考教程 - 该教程包含有关 css 的更多信息:https://www.cainiaomax.com/css/

