---
title: "[转]在css+div里面实现div的底对齐"
author: "uule"
date: "2011-08-01T17:43:14Z"
tags: []
categories: ["Web前端"]
slug: "css-div-div"
draft: false
lastmod: "2011-08-01T17:59:04Z"
id: 1137521
url: "https://uule.iteye.com/blog/1137521"
---

1、之前代码：

```
<style>
#parent{
width:300px;
height:300px;
background:gray;
}
#i_want_to_be_bottom{
width:100px;
height:30px;
background:red;
}
</style>
<div id="parent">
    <div id="i_want_to_be_bottom"></div>
</div>
```

   
![](http://dl.iteye.com/upload/attachment/528281/f97b8899-d05a-37d0-b86d-8028093e77b6.gif)

修改后代码：

```
<style>
#parent{
width:300px;
height:300px;
background:gray;
position:relative;
}
#i_want_to_be_bottom{
width:100px;
height:30px;
background:red;
position:absolute;
bottom:0px;
}
</style>
<div id="parent">
    <div id="i_want_to_be_bottom"></div>
</div>
```

   
![](http://dl.iteye.com/upload/attachment/528284/1d849884-f3ff-312e-9117-2fe20e053292.gif)  
 **变动点提示**

#parent{

....

**postion:relative;**

....

}

#i\_want\_to\_be\_bottom{

....

**position:absolute;**

**bottom:0px;**

....

} 

当然，你也可以设置子div的margin选项达到底对齐的目的，但是如果父div的高度是可变的时候，这样做就不行了。所以，达到子div底对齐的万能办法是使用如上所述办法，当然，有的时候，你可以使用子div的right:0px来达到右对齐的目的。

本例来源： [http://blog.sina.com.cn/s/blog\_4c4a58ca01000bed.html](http://blog.sina.com.cn/s/blog_4c4a58ca01000bed.html)