码迷,mamicode.com
首页 > 其他好文 > 详细

实现子元素相对于父元素左右居中

时间:2015-04-08 23:22:38      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

1. 显示的设定子元素的宽度,然后将资源的margin值设定为:margin:0 auto;

html:

<div class="parent">
  <div class="child"></div>
</div>

css:

.parent {
	width:100px;
	height:100px;
	background:red;
}
.child {
	/*如果设定为绝对定位,会不起效
	* 原因是设定为觉得定位后,元素的位置会相对于
	 *向上回溯第一个定位为:absolute/relative
	 *的父级元素定位
	*/
	/*position: absolute;*/
	margin:0 auto;
	width:50px;
	height:100px;
	background:green;
}

2.

实现子元素相对于父元素左右居中

标签:

原文地址:http://www.cnblogs.com/alicePanZ/p/4404345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!