Thuộc tính CSS nền-vị trí-x
Ví dụ
Cách định vị hình nền trên trục x:
div
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: center;
}
Hãy tự mình thử »Thêm ví dụ "Hãy tự mình thử" bên dưới.
Định nghĩa và cách sử dụng
Thuộc tính background-position-x
đặt vị trí của hình nền trên trục x.
Mẹo: Theo mặc định, hình nền được đặt ở góc trên cùng bên trái của một phần tử và được lặp lại theo cả chiều dọc và chiều ngang.
Giá trị mặc định: | 0% |
---|---|
Thừa hưởng: | KHÔNG |
Hoạt hình: | Đúng. Đọc về hoạt hình Hãy thử nó |
Phiên bản: | CSS3 |
Cú pháp JavaScript: | object .style.backgroundPositionX="center" Hãy dùng thử |
Hỗ trợ trình duyệt
Các số trong bảng chỉ định phiên bản trình duyệt đầu tiên hỗ trợ đầy đủ thuộc tính.
Property | |||||
---|---|---|---|---|---|
background-position-x | 1.0 | 12.0 | 49.0 | 1.0 | 15.0 |
background-position-x (with two value syntax) |
Not supported | Not supported | 49.0 | 15.4 | Not supported |
Cú pháp CSS
background-position-x: value ;
Giá trị tài sản
Value | Description | Demo |
---|---|---|
left | Positions background left side on x-axis. | Demo ❯ |
right | Positions background right side on x-axis. | Demo ❯ |
center | Positions background center on x-axis. | Demo ❯ |
x% | Left side is 0% on x-axis, and right side is 100%. Percentage value refers to width of background positioning area minus width of background image. | Demo ❯ |
xpos | Horizontal distance from left side. Units can be pixels (0px) or any other CSS units . | Demo ❯ |
xpos offset | Two value syntax, only supported in Firefox and Safari. - xpos is set either to "left" or "right". - offset is horizontal distance from background image and "left" or "right" side set with xpos . Units can be pixels or any other CSS units . |
|
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
Thêm ví dụ
Ví dụ
Cách định vị hình nền ở bên phải:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: right;
}
Hãy tự mình thử »Ví dụ
Cách định vị hình nền bằng phần trăm:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: 50%;
}
Hãy tự mình thử »Ví dụ
Cách định vị hình nền bằng pixel:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-position-x: 150px;
}
Hãy tự mình thử »Ví dụ
Sử dụng các thuộc tính nền khác nhau để tạo hình nền bao phủ vùng chứa của nó:
.hero-image {
background-image: url("photographer.jpg"); /* The
image used */
background-color: #cccccc; /* Used if the image is
unavailable */
height: 300px; /* You must set a specified height */
background-position-x: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
}
Hãy tự mình thử »Trang liên quan
Hướng dẫn CSS: Nền CSS
Tham chiếu CSS: thuộc tính hình nền
Tham chiếu CSS: thuộc tính vị trí nền
Tham chiếu CSS: thuộc tính nền-vị trí-y
Tham chiếu DOM HTML: thuộc tính BackgroundPosition