对于div来说,默认的宽度就是撑到父级元素的宽度,如果定义的max-width一般宽度都是扩展到设定的最大宽度,当确定了宽度后并且定义了margin-left、margin-right为auto就会让这个div块级元素居中,但此时div中的文字还是左对齐,要让文字在块中居中还需要设置text-align为center。
jquery的居中:
| ? | $(window).resize(function(){ |
| ? | ? |
| ? | ????$('.className').css({ |
04 |
????????position:'absolute', |
05 |
????????left: ($(window).width() - $('.className').outerWidth())/2, |
06 |
????????top: ($(window).height() - $('.className').outerHeight())/2 |
07 |
????}); |
08 |
?? |
09 |
}); |
10 |
?? |
11 |
// To initially run the function: |
12 |
$(window).resize(); |
其实模仿的上一种办法,只是在浏览器窗口改变时,动态计算div的高度和宽度。偷巧的完成了div的
