用CSS使DIV水平居中_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:32:51
Original
1098 people have browsed it

一,用CSS使DIV层水平居中

对需要水平居中的DIV层添加以下属性:
   margin-left: auto;
   margin-right: auto;

这样在FF中已经居中了,可是在IE中看还是没有居中!

问题并不在CSS而在XHTML网页本身.

需要加上这样的代码才能使得上述设置有效果:

如何使DIV居中

主要的样式定义如下:

body { TEXT-ALIGN: center; }

#center { MARGIN-RIGHT: auto; MARGIN-LEFT: auto; }

说明:

首先在父级元素定义TEXT-ALIGN: center;这个的意思就是在父级元素内的内容居中;对于IE这样设定就已经可以了。但在mozilla中不能居中。解决办法就是在子元素定义时候设定时再加上“MARGIN-RIGHT: auto;MARGIN-LEFT: auto; ”

需要说明的是,如果你想用这个方法使整个页面要居中,建议不要套在一个DIV里,你可以依次拆出多个div,只

要在每个拆出的div里定义MARGIN-RIGHT: auto;MARGIN-LEFT: auto; 就可以了。

如何使图片在DIV 中垂直居中

用背景的方法。举例:

body{ BACKGROUND: url(http://www.w3cn.org/style/001/logo_w ... )#FFFno-repeatcenter; }

关键就是最后的center,这个参数定义图片的位置。还可以写成“top left”(左上角)或者"bottom right"等,也可以直接写数值"50 30"

如何使文本在DIV中垂直居中

果是文字,便不能用背景方法,可以用增高行距的办法变通实现垂直居中,完整代码如下:











test content





说明:

vertical-align:middle;表示行内垂直居中,我们将行距增加到和整个DIV一样高line-height:200px;然后插入文字,就垂直居中了。

CSS+DIV控制页面中元素垂直居中代码 全局和区域垂直居中









另一方法:

***************************************

二,用JS使层居中

把如下代码加入

区域中










style="CURSOR: hand" width=21>х




height=91>欢迎光临本站!

本站域名:
http://webform.cn

□::BEST4U论坛::


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!