如何将一个 <div> 水平居中放置在另一个 <div> 中?

htmlweb developmentfront end technology更新于 2024/2/1 16:12:17

这里我有一个 HTML 表单和一个 CSS 文件 (style.css)。"o-div"是外层 div,"i-div"是内层 div 类。

示例

<html >
   <head>
      <title>center a div</title>
      <link href="../../Content/style.css" rel="stylesheet" type="text/css" />
   </head>
   <body>
      <div class="o-div">
         I am OUTER DIV

         <div class="i-div">
            I am INNER DIV,
            I am in Center

         </div>
      </div>
   </body>
</html>

输出

I am OUTER DIV
I am INNER DIV, I am in Center

Style.css

body {
   background-color:Gray;
   padding:30px;
}

.o-div {
   padding:50px;
   background-color:Lime;
}

.i-div {
   background-color:Fuchsia;
   max-width:400px;
   height:200px;
   margin: 0 auto;
   text-align:center;
}

相关文章


有用资源