HTML背景图片的相对位置设置
链接:https://jingyan.baidu.com/article/ff42efa9c22b11c19e220200.html
重点:background-attachment, background-position
方法/步骤
首先我们需要插入一张图片,并且图片只出现一次,并设计图片出现的位置在左上角,可以按照如下代码来完成:
<html>
<head>
<title>图片位置设置</title>
<style type="text/css">
body{
background-image:url("2.jpg");
background-attachment:scorll;
background-repeat:no-repeat;
background-position:top left;
}
</style>
</head>
<body>
</body>
</html>
可以看到背景图片出现的位置在浏览器的左上角,这个和默认的设置是一样的。

我们需要让图片出现在上方的正中间该怎么做呢?这里我就用代码来说明问题,具体详细代码如下:
<html>
<head>
<title>图片位置设置</title>
<style type="text/css">
body{
background-image:url("2.jpg");
background-attachment:scorll;
background-repeat:no-repeat;
background-position:top center;
}
</style>
</head>
<body>
</body>
</html>
从下图的执行结果可以看到,我们的背景图片出现在了上方的正中间这个位置上了。

在上方的中间和左边都出现了,接下来设置图片出现在上方的右边,具体代码如下所示:
<html>
<head>
<title>图片位置设置</title>
<style type="text/css">
body{
background-image:url("2.jpg");
background-attachment:scorll;
background-repeat:no-repeat;
background-position:top right;
}
</style>
</head>
<body>
</body>
</html>
可以看到如下图所示的执行效果图,图片出现的位置在上方的右边了。

如果需要图片出现的位置在正中间该怎么做呢?我们知道设计网页的时候背景图片等等元素一般都是需要放在正中间这个位置上的,这里我就来分析下如何将背景图片放置在正中间这个位置上,具体代码如下:
<html>
<head>
<title>图片位置设置</title>
<style type="text/css">
body{
background-image:url("2.jpg");
background-attachment:scorll;
background-repeat:no-repeat;
background-position:center center;
}
</style>
</head>
<body>
</body>
</html>
可以看到如下的执行结果,背景图片出现在了下方的正中间这个位置上了。

我们该如何设置图片出现在背景图片的下方左边这个位置呢?下边用到了bottom这个属性值来设置的,具体代码如下:
<html>
<head>
<title>图片位置设置</title>
<style type="text/css">
body{
background-image:url("2.jpg");
background-attachment:scorll;
background-repeat:no-repeat;
background-position:bottom left;
}
</style>
</head>
<body>
</body>
</html>
可以看到如下图所示的结果,

还可以设置背景图片出现的位置在最下方,可以看到如下代码:
<html>
<head>
<title>图片位置设置</title>
<style type="text/css">
body{
background-image:url("2.jpg");
background-attachment:scorll;
background-repeat:no-repeat;
background-position:bottom;
}
</style>
</head>
<body>
</body>
</html>
通过如下图可以看到具体的执行效果,只用一个bottom就能设置图片出现在最下方这个位置上了。

用background-position设置图片的位置除了使用相对位置还能使用绝对位置来设置,可以设置像素值来确定背景图片左上角的坐标点来确定,具体代码如下:
<html>
<head>
<title>图片位置设置</title>
<style type="text/css">
body{
background-image:url("2.jpg");
background-attachment:scorll;
background-repeat:no-repeat;
background-position:150px 150px;
}
</style>
</head>
<body>
</body>
</html>
从下图可以看到我们的执行结果,背景图片出现的位置是150px,150px这个点作为左上角的起始点。
END
HTML背景图片的相对位置设置的更多相关文章
- css-sprite 雪碧图的使用,合并多张小图,背景图片当按钮的设置
背景图片基础: 使用background-image来设置背景图片 语法: background-image:url(相对与css的路径) 如果背景图片大于元素,默认会显示图片的左上角 如果背景图片和 ...
- Swift自定义UINavigationController(背景颜色、背景图片、返回按钮设置、字体大小等)
1.0 自定义UINavigationController时,背景图片.颜色等只需要设置一次,所以我们可以重写 initializa 这个方法来实现我们想要的效果 override class ...
- background-origin 设置背景图片原始起始位置
- CSS那个背景图片的坐标怎么设置?怎么计算的?
background:url(images/hh.gif) no-repeat -10px 0;},作用是移动背景的位置. 背影图片的左上角相对当前元素左上角的坐标. 右为X轴正半轴, 下为Y轴正半轴 ...
- android 自定义Dialog背景透明及显示位置设置
先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...
- (转)background-position—CSS设置背景图片的位置
background-position :在 CSS 中通过 background-position 属性可以调整背景图片的位置.因为在默认情况下背景图片都是从设置了 background-posit ...
- background-size (设置背景图片的大小)
设置背景图片的大小,以长度值或百分比显示(数值包括 长度length和百分比percentage),还可以通过cover和contain来对图片进行伸缩. 语法:background-size: au ...
- CSS3背景图片(多重背景、起始位置、裁剪、尺寸)
一.多重背景图片 ①CSS3允许我们在一个元素上添加多个图片 ②多重背景可以把多个图片资源添加到background属性上,用逗号隔开,然后用background-position把他们定位到你想要的 ...
- background-size 设置背景图片的大小
background-size 设置背景图片的大小,以长度值或百分比显示,还可以通过cover和contain来对图片进行伸缩. 语法: background-size: auto | <长度值 ...
随机推荐
- javascript柯里化及组合函数~
大家是不是看我上篇博文有点蒙.用的的curry和compose是什么鬼,怎么那么神奇.上篇博文也是主要用到了这两个函数.那今天我们来聊一下curry和compose,这两个东西是函数式编程很重要的东西 ...
- spring boot 连接mysql mongodb with jpa
https://github.com/bigben0123/gs-accessing-data-mysql-mongo-jpa
- 1062.Talent and Virtue
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...
- fopen() 返回 NULL, 奇葩原因:当前进程打开多个句柄,忘记关闭。(bug)
今天在测试一个程序的时候,突然第一次弹出错误对话框,提示: 创建新文件失败. fopen() 返回 NULL 我又重启程序测试,还是提示同样的错误. 经过几分钟的检查,发现一个规律:当处理到500多个 ...
- Redis和memcached区别须知
1.支持的数据类型不同(memcached只支持简单的key-value的数据类型,Redis支持5种数据类型(1.string,2.list,3.set,4.zset,5.hash)) 2.redi ...
- 解决samtools报错:[main_samview] region "chr2:20,100,000-20,200,000" specifies an unknown reference name. Continue anyway.
根据Samtool 的manual文档介绍,如果想搜索bam文件的某段区域,需要用到以下命令: samtools view aln.sorted.bam chr2:20,100,000-20,200, ...
- Hibernate 基础解析(Configuration,SessionFactory,Session,Transaction,Query,Criteria)
1 框架体系结构 对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实 现面向对象编程语言里 ...
- pytest 3.fixture介绍一 conftest.py
前言: 前面一篇pytest2 讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录, ...
- vue中显示静态图片怎么引用
有多种方法解决这个问题: 首先,如果使用的是img标签那么可以这样 data () { return { img: require('path/to/your/source') } } 然后在temp ...
- springboot1.5升级2.0后遇到的问题
https://blog.csdn.net/zhiquanzhou/article/details/80566630