<div id="a">
<div id="b"></div>
</div>

用样式控制b在a居中显示

1、题目没说上下居中,只是左右居中的话,很简单 #a{width:100px;height:100px;background:#ff0000;}

考虑上下居中#a{width:100px;height:100px;background:#ff0000;position:relative;}

2、面试用了一段时间,竟然有些人不会写,或者写text-align:center;或者align:center的

3、今天面试一个小伙,基本如下的写法

#a{width:100px;height:100px;background:#ff0000;position:relative;}

#b{width:50px;height:50px; background:#0000ff;position:absolute;


  left:0;top:0;bottom:0;right:0;


  margin:auto;



当时自己非常惊讶,这样竟然可以?后来经过自己的测试,确实实现了上下左右居中的效果