---
title: "如何使能DIV垂直和水平居中"
author: "lsjlym"
date: "2012-01-26T17:33:00Z"
tags: []
slug: "div"
draft: false
lastmod: "2015-09-08T20:25:38Z"
id: 2241857
url: "https://lsjlym.iteye.com/blog/2241857"
---

&lt;style type="text/css"&gt;  
.center-middle1 {  
position:absolute;  
background-color: #e2e2e2;  
width: 100%;  
height: 300px;  
top:50%;  
margin-top: -150px; }  
.center-middle2 {  
position:absolute;  
background-color: #e2e2e2;  
width: 300px;  
height: 200px;  
left:50%;  
top:50%;  
margin-left: -150px;  
margin-top: -100px; }

&lt;/style&gt;

&lt;style type="text/css"&gt; &lt;!-- .center-middle1 {position:absolute; background-color:#e2e2e2; width:100%; height:300px; top:50%; margin-top:-150px} .center-middle2 {position:absolute; background-color:#e2e2e2; width:300px; height:200px; left:50%; top:50%; margin-left:-150px; margin-top:-100px} --&gt; &lt;/style&gt;

用例:

&lt;div class="enter-middle2"&gt;  
如何使能DIV垂直和水平居中  
&lt;/div&gt;