探花小李 發表於 2026-5-3 22:17:01

html+css实现div居中的8种方法

<h3>水平居中</h3>
<p>1.行级元素:为该行级元素的父元素设置text-align:center配合line-height样式</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;width: 500px;height: 100px;line-height: 100px;border: 1px solid green;text-align:center;&quot;&gt;
    &lt;span&gt;行级元素&lt;/span&gt;
&lt;/div&gt;</pre>
</div>
<p>2.块级元素:为其自身设置margin:auto样式</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;width: 500px;height: 100px;border: 1px solid red;&quot;&gt;
    &lt;div style=&quot;line-height: 100px;text-align: center;margin:auto;width: 100px;height: 100px;border: 1px solid gold&quot;&gt;
      块级元素
    &lt;/div&gt;
&lt;/div&gt;</pre>
</div>
<h3>垂直居中</h3>
<p>方法一:</p>
<p>display:table;此元素会作为块级表格来显示(类似 &lt;table&gt;),表格前后带有换行符.</p>
<p>display:table-cell;此元素会作为一个表格单元格显示(类似 &lt;td&gt; 和 &lt;th&gt;)</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;display: table;width: 500px;height: 200px;border: 1px solid red;&quot;&gt;
    &lt;div style=&quot;display: table-cell;vertical-align: middle;text-align:center;&quot;&gt;块级元素&lt;/div&gt;
&lt;/div&gt;</pre>
</div>
<p>方法二:</p>
<p>利用flex布局使内部块级元素水平,垂直居中(display:flex;justify-content: center; align-items:center;)</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;display:flex;justify-content: center; align-items:center; width: 500px;height: 200px;border: 1px solid green;text-align:center;line-height:100px &quot;&gt;
    &lt;div style=&quot;width: 100px;height: 100px;border: 1px solid gold&quot;&gt;块级元素&lt;/div&gt;
&lt;/div&gt;</pre>
</div>
<p>方法三:</p>
<p>利用定位实现,父元素:position:relative; ,子元素:position: absolute;top:50%;left:50%;transform:translate(-50%,-50%);</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;position:relative; width: 500px;height: 200px;border: 1px solid red;&quot;&gt;
    &lt;div style=&quot;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px&quot;&gt;
      块级元素
    &lt;/div&gt;
&lt;/div&gt;</pre>
</div>
<p>方法四:</p>
<p>绝对定位, left:50%,top: 50% + margin-left:-(自身宽度的一半),margin-top:-(自身高度的一半)<br />
缺点:要自己计算容器的宽高,万一容器的宽高改变还要修改css样式</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;position:relative; width: 500px;height: 200px;border: 1px solid red;&quot;&gt;
    &lt;div style=&quot;position:absolute;top:50%;left:50%;margin-left: -50px;margin-top: -50px;
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px&quot;&gt;
      块级元素
    &lt;/div&gt;
&lt;/div&gt;</pre>
</div>
<p>方法五:</p>
<p>绝对定位,left: 0,right: 0, top: 0, bottom: 0 + margin:auto</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;position:relative; width: 500px;height: 200px;border: 1px solid red;&quot;&gt;
    &lt;div style=&quot;position:absolute;top: 0;left: 0;right: 0;bottom: 0;margin: auto;
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px&quot;&gt;
      块级元素
    &lt;/div&gt;
&lt;/div&gt;</pre>
</div>
<p>方法六:<br />
固定定位position:fixed;并设置一个较大的z-index层叠属性值</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;position:fixed;top: 50%;left: 50%;margin-left: -50px;margin-top: -50px;z-index: 999;
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px&quot;&gt;
    块级元素
&lt;/div&gt;</pre>
</div>
<p>方法七:<br />
要把元素相对于视口进行居中,那么相当于父元素的高度就是视口的高度,视口的高度可以用vh来获取:</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;margin: 50vh auto 0;transform: translateY(-50%);line-height:100px
    width: 100px;height: 100px;border: 1px solid gold;text-align:center;&quot;&gt;
    块级元素
&lt;/div&gt;</pre>
</div>
<p>方法八:</p>
<p>Flex加margin:auto</p>
<div class="jb51code">
<pre class="brush:xhtml;">
&lt;div style=&quot;display: flex;width: 500px;height: 200px;border: 1px solid red;&quot;&gt;
    &lt;div style=&quot;margin: auto;width: 100px;height: 100px;border: 1px solid gold;text-align:center;line-height:100px&quot;&gt;
      块级元素
    &lt;/div&gt;
&lt;/div&gt;</pre>
</div>
頁: [1]
查看完整版本: html+css实现div居中的8种方法