网页背景图片自适应浏览器大小

背景图片自适应浏览器大小

 
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>背景图片自适应浏览器大小</title>
<script type=”text/javascript”>
window.onresize = window.onload = function(){
var w,h
if(!!(window.attachEvent && !window.opera))
{
h = document.documentElement.clientHeight;
w = document.documentElement.clientWidth;
}else{
h =    window.innerHeight;
w = window.innerWidth;
}
document.getElementById(‘container’).value  =’窗口大小:’ + ‘width:’ + w + ‘; height:’+h;
var bgImg = document.getElementById(‘bg’).getElementsByTagName(‘img’)[0];
bgImg.width = (w);
bgImg.height= (h) ;

}
</script>
<style type=”text/css”>
*html#container{background:url(../images/bg2.gif) no-repeat; width:100%;  min-height:1000px; height:auto !important; height:1000px;}
#bg{position:fixed;top:0px;left:0px;z-index:-999;}
.login{margin:0 auto; width:100%; height:420px; position:absolute; top:15%;}
.logo{margin:0 auto; width:450px; height:36px; background:url(../images/logo.png) no-repeat;}
.login_ct{background:#666; height:323px; margin-top:50px;}
.login_form{width:681px; height:323px; background:#CCC; margin:0 auto;}
</style>
</head>

<body>
<div id=”container”>

<div id=”bg”><img src=”images/bg2.gif” width=”1920″ height=”1080″ />
</div>
<div>
<div>
<div>

</div>
</div>
</div>
</div>
</body>
</html>

 

转载于:https://www.cnblogs.com/siri/p/3410837.html

### 回答1: 想要实现网页背景图片自适应全屏,可以使用CSS中的background-size属性。该属性可以控制背景图片大小,使其适配网页大小。 具体实现方法如下: 1. 在CSS样式表中设置背景图片,可以使用background-image属性。 2. 设置背景图片的repeat属性为no-repeat,防止背景图片重复显示。 3. 设置背景图片的位置,可以使用background-position属性。 4. 将背景图片的宽度和高度设置为100%。 5. 使用background-size属性将背景图片拉伸或缩放,使其适应浏览器大小。可以使用cover属性来保持纵横比并覆盖整个区域,或使用contain属性来适应区域并保持纵横比。 举个例子,比如你想要设置一个背景图片,宽度为100%、高度为100%,并且保持纵横比,可以使用以下CSS代码: body { background-image: url('yourimage.jpg'); background-repeat: no-repeat; background-position: center center; background-attachment: fixed; background-size: cover; } 其中,background-attachment属性可以使背景图片固定在页面中,在滚动页面时可以保持背景不变,同时也可以使用background-size: cover属性实现自适应全屏的效果。 以上就是CSS实现网页背景图片自适应全屏的方法。通过简单的CSS样式设置,可实现网站背景图片在不同设备和分辨率下的适应,达到更好的用户体验。 ### 回答2: 在开发网页时,网页背景图片作为网页设计中的重要一环,其在网站视觉效果和用户体验方面都起到了重要作用。然而,网页背景图片大小与设备屏幕分辨率之间的不匹配可能会导致在某些设备上显示不佳。因此,在此我们介绍一下如何使用CSS实现网页背景图片自适应全屏的方法。 1. 使用CSS3的background-size属性 background-size属性能够自动调整图片的大小,以适应元素的大小。我们可以在CSS文件中将background-size属性设置为"cover"。这意味着图片将被拉伸并调整大小,以适应整个元素的大小。使用此方法,在任何分辨率下,图片始终保持其纵横比。 ``` css body { background-image: url('bg.jpg'); background-size: cover; background-repeat: no-repeat; } ``` 其中,background-repeat属性设置为"no-repeat",以确保图片不会在屏幕上重复出现。 2. 将背景图片设置为页面的fixed定位 通过将背景图片设置为fixed定位,可以使图片不随页面内容滚动,而是固定在网页视口中。这个方法可以通过以下代码实现: ``` css body { background-image: url('bg.jpg'); background-attachment: fixed; background-position: center; background-repeat: no-repeat; background-size: cover; } ``` 其中,background-position属性将图片设置为居中位置,以便任何大小的屏幕都可以将图片居中。 总之,以上两种方法都可以很好的实现网页背景图片自适应全屏,让网站在任何设备上都能够有良好的视觉效果和用户体验。同时,在开发网站时,我们还需要注意图片的质量和大小,以避免影响网站的加载速度和性能。 ### 回答3: CSS实现网页背景图片自适应全屏的方法是通过设置背景图片的属性和样式,使其适应不同大小的屏幕。 首先,我们需要设置背景图片的属性background-size为cover,这样图片会自动缩放以填满整个屏幕,并保持比例。 其次,我们需要设置背景图片的位置属性background-position为center center,这样图片可以始终保持在屏幕中央,并不会被拉伸或压缩。 然后,我们还可以设置其他样式,如背景颜色、重复方式等。 最后,在HTML文档中引入这个CSS文件,即可实现网页背景图片自适应全屏。 以下是CSS的代码示例: body { background-image: url('bg.jpg'); background-size: cover; background-position: center center; background-repeat: no-repeat; background-color: #f5f5f5; /*可选背景颜色*/ } 总之,通过使用CSS让网页背景图片自适应全屏,可以增加网站的视觉效果和用户体验,使网页更加吸引人。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值