CSS background-position 属性 | CSS 背景 | CSS教程

返回到:CSS 背景 | CSS教程

标签定义及使用说明

background-position属性设置背景图像的起始位置。

注意对于这个工作在Firefox和Opera,background-attachment必须设置为 “fixed(固定)”.

默认值:0% 0%
继承:no
版本:CSS1
JavaScript 语法:object object.style.backgroundPosition=”center”

浏览器支持

表格中的数字表示支持该属性的第一个浏览器版本号。

属性谷歌IE火狐苹果opera
background-position1.04.01.01.03.5

注意: IE8 及更早的浏览器版本不支持一个元素有多个背景图片。

属性值

描述
left top
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom
如果仅指定一个关键字,其他值将会是”center”
x% y%第一个值是水平位置,第二个值是垂直。左上角是0%0%。右下角是100%100%。如果仅指定了一个值,其他值将是50%。 。默认值为:0%0%
xpos ypos第一个值是水平位置,第二个值是垂直。左上角是0。单位可以是像素(0px0px)或任何其他 CSS单位。如果仅指定了一个值,其他值将是50%。你可以混合使用%和positions
inherit指定background-position属性设置应该从父元素继承

实例

1、如何定位background-image。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
body
{ 
	background-image:url('/wp-content/uploads/2022/02/CSS.jpg');
	background-repeat:no-repeat;
	background-attachment:fixed;
	background-position:center; 
}
</style>
</head>

<body>
<p><b>注意:</b>该属性工作在 Firefox 和 Opera, background-attachment 属性会被设置为 "fixed"。</p>
</body>
</html>

2、在页面上设置background-image。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
body
{ 
	background-image:url('/wp-content/uploads/2022/02/CSS.jpg');
	background-repeat:no-repeat;
	background-attachment:fixed;
	background-position:center; 
}
</style>
</head>

<body>
<p><b>注意:</b>该属性工作在 Firefox 和 Opera, background-attachment 属性会被设置为 "fixed"。</p>
</body>
</html>

3、使用%设置页面上的图像位置。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
body
{ 
background-image: url('/wp-content/uploads/2022/02/CSS.jpg');
background-repeat: no-repeat;
background-attachment:fixed;
background-position: 30% 20%; 
}
</style>
</head>

<body>
<p><b>注意:</b>该属性工作在 Firefox 和 Opera, background-attachment 属性会被设置为 "fixed"。</p>
</body>
</html>

4、使用像素设置页面上的图像位置。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>Web176教程(web176.com)</title> 
<style>
body
{ 
background-image: url('/wp-content/uploads/2022/02/CSS.jpg');
background-repeat: no-repeat;
background-attachment:fixed;
background-position: 50px 100px;
}
</style>
</head>

<body>
<p><b>注意:</b>该属性工作在 Firefox 和 Opera, background-attachment 属性会被设置为 "fixed"。</p>
</body>
</html>

作者:terry,如若转载,请注明出处:https://www.web176.com/cssprop/5785.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2022年4月22日 下午5:16
下一篇 2022年4月22日 下午5:28

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注