关于CSS 控制DIV水平居中问题_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:29:57
Original
901 people have browsed it

  关于CSS 控制DIV水平居中问题,我看到很多新人搞不明月。记得第一次看CSS是一个老外写的书,那个里面谈到居中使用。

  margin-left:auto;
  margin-right:auto;

  其实等同于:

  margin:0 auto;

  于是可以使用这种方式,但是有人用IE时发现没有居中。这里建议你看看是否遗漏了DTD声明。

  <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

  非常多的人犯过类似的错误!这种方法也可以说屡试不爽,但是在某些情况下还是行不通的。于是有了第二种方法。

  margin-left:50%;
  left: -width/2;

  这里的width不是CSS中的Width,而是你的DIV的宽度例如你的div是768px宽,那么你就应该设置left:-384px。很好,已经有两种方法了,可以说已经能够解决你可能遇到的问题了。

  有时候你会发现,这两个还不行。不能兼容一些浏览器。于是发现有了第三种方法,这中方法主要是考虑IE,它是建立在第一种方法的基础之上。它需要设置body。

  body {text-align: center;}

  这样IE下也居中了,但是它带来一个新的问题,你发现你的页面中所有文字都是居中的,这样很不好看。这个就很容易解决了,只需要在你的DIV定义中加上 text-align: left; 之类调整的设置就行了。

  以上文字仅是一个饭后参考与小品。大家在实际工作中可以引起注意! 

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!