老师好,background:url( ) 为什么不能撑起高度,不知道图片宽、高情况下如何让其自适应
来源:4-3 编程练习
慕尼黑4041527
2020-01-25 15:07:06
老师好,
1。 请问background:url( ) 设置背景为什么不能撑起高度呢,在我不知道图片宽和高情况下 该怎样做 才能让其宽度平铺满浏览器窗口,高度自适应呢?
2。麻烦老师检查一下我的代码有没有需要修改完善的地方,谢谢。
2回答
同学你好,关于同学的疑问,解答如下:
1、使用背景图片,是需要设置高度的,不然可能会不显示。一般情况下,图片的宽高是已知的,同学不用担心。
2、效果实现的不错,代码简化一下会更好,把相同代码写到一起,如下:
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
慕尼黑4041527
提问者
2020-01-25
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{padding: 0;margin: 0;}
.all{
/*height: 4069px;*/
background:url(http://climg.mukewang.com/59c9f7ce0001839219034033.png) center top no-repeat;
/*background-size:100%;*/
/*position: absolute;*/
width: 100%;
height:4033px;
}
.left{
position: fixed;
top: 50%;
left: 0;
margin-top: -182px;
width: 224px;
height: 364px;}
.right{
position: fixed;
top: 50%;
right: 0;
margin-top: -182px;
width: 224px;
height: 364px;
}
</style>
</head>
<body>
<div class="all">
<div class="left">
<img src="http://climg.mukewang.com/5a3383c70001f1b702240364.png">
</div>
<div class="right">
<img src="http://climg.mukewang.com/5a3383d00001a3dd02240364.png">
</div>
</div>
</body>
</html>
相似问题