search
HomeWeb Front-endHTML TutorialCSS background-position随笔_html/css_WEB-ITnose

1. Definition and usage

The background-position property sets the starting position of the background image.

This attribute sets the position of the original background image (defined by background-image ). If the background image is to be repeated, it will start from this point.

Tip: You need to set the background-attachment property to "fixed" to ensure that this property works properly in Firefox and Opera.

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

2. Possible values

Value description
  • top left
  • top center
  • top right
  • center left
  • center center
  • center right
  • bottom left
  • bottom center
  • bottom right
  • 值 描述
  • top left
  • top center
  • top right
  • center left
  • center center
  • center right
  • bottom left
  • bottom center
  • bottom right
  • 如果您仅规定了一个关键词,那么第二个值将是"center"。

    默认值:0% 0%。

    x% y%

    第一个值是水平位置,第二个值是垂直位置。

    左上角是 0% 0%。右下角是 100% 100%。

    如果您仅规定了一个值,另一个值将是 50%。

    xpos ypos

    第一个值是水平位置,第二个值是垂直位置。

    左上角是 0 0。单位是像素 (0px 0px) 或任何其他的 CSS 单位。

    如果您仅规定了一个值,另一个值将是50%。

    您可以混合使用 % 和 position 值。

    If you specify only one keyword, the second value will be "center".

    Default: 0% 0%.

    x% y% The first value is the horizontal position and the second value is the vertical position.

    The upper left corner is 0% 0%. The lower right corner is 100% 100%.

    If you specify only one value, the other value will be 50%.

    xpos ypos The first value is the horizontal position and the second value is the vertical position.

    The upper left corner is 0 0. Units are pixels (0px 0px) or any other CSS unit.

    If you specify only one value, the other value will be 50%.

    You can mix % and position values.

    3. Example operation:

    html css code:

    Rendering effect:

    1. When background-position:-110px -70px;

    Rendering:

    That is, the image is shifted 70px up and 110px to the left;


    2. When background-position: 10px 10px;

    Effect picture:

    That is, move 10px to the right and 10px down;

    3.background-position:100% 100%;

    Rendering:

    The image is in the lower right corner of the container element, which has the same effect as background-position:right bottom;.

    Equivalent to x: {width of container (container)?width of background image}*x percentage, the excess part is hidden. That is, 50px to the right

    is equivalent to y: {height of container (container)? height of background image}*y percentage, and the excess part is hidden. That is, 185px downward;

    4.background-position:50% 50%;

    Rendering:

    The picture is centered horizontally and vertically. It has the same effect as background-position:center center;.

    Equivalent to x: {width of container (container)?width of background image}*x percentage, the excess part is hidden.

    Equivalent to y: {height of container (container)? height of background image}*y percentage, the excess part is hidden.

    5.background-position:-50% -50%;

    Rendering:

    Equivalent to x: -{container (container) The width? The width of the background image}*x percentage, and the excess part is hidden. That is, moved 25px to the left;

    is equivalent to y: - {height of container (container)? height of background image}*y percentage, and the excess part is hidden. That is, moved up 92.5px;

    6.background-position:-100% -100%;

    Rendering:

    Equivalent to x: -{width of container (container)?width of background image}*x percentage, the excess part is hidden. That is, moved 50px to the left;

    is equivalent to y: - {height of container (container)? height of background image}*y percentage, and the excess part is hidden. That is, it has moved up 185px;

    Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

    What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

    What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

    The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools