Home  >  Article  >  Web Front-end  >  Div+Css控制背景图片水平垂直居中显示_html/css_WEB-ITnose

Div+Css控制背景图片水平垂直居中显示_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:29:371187browse

在Web开发中我们经常要碰到这样的问题:在为一个页面设置背景图片之后往往希望图片能够在分辨率比较大的情况下水平垂直都居中显示。通常水平居中显示在Css中是很容易做到的,而垂直居中就需要使用一些Css的技巧:

1. 首先为了能使得网站能够根据浏览器大小自适应,我们需要将页面的body元素height值设为100%,而在这之前,我们需要将xhtml验证从网站头删除。

之后在页面上添加如下css文件设置body的高度为100%,html元素的设置为兼容FF:

}

2. 之后需要设置最外层的div元素为100%高度和宽度:

3. 在外层div中再套用一层div并使得其距页面顶端为50%:

4. 接下来将我们需要设置背景图片的div元素放置在内层div元素中,并设置css属性如下:

.login_background
{
height:600px;
width:1000px;
background-image:url(images/login_background.png);
background-repeat:no-repeat;
background-position:center center;
margin:0px auto;
+position:relative;
top:-50%;
left:-50%;
}

这样页面的大小将能够根据浏览器的不同自适应,同时背景图片也能做到垂直和水平居中显示。

经测试在IE8下F11模式有效。

Statement:
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