---
title: "div 元素居中以及元素内容居中"
author: "hello_world_hello"
date: "2015-01-18T23:03:26Z"
tags: []
categories: ["Web前端"]
slug: "div"
draft: false
lastmod: "2015-01-18T23:10:39Z"
id: 2177095
url: "https://hello-world-hello.iteye.com/blog/2177095"
---

1）如何让一个div元素居中，通过设置div 元素的margin属性即可，如下：

```
.wrap {
       width:200px;
       margin: 0 auto;
}
```

 其中width属性必须设定，否则没有效果。

2）div 元素内容水平居中，设置text-align属性

```
.wrap{
        text-align:center;
}
```

 3) div 元素内容垂直居中，可通过设置行高等于width高度

```
.wrap {
        width:60px;
        line-height:60px;
}
```