Preface
One of the most common solutions on the Internet is to relative in the parent element, and then absolute in the child element. This solution is certainly good, but one disadvantage is that it will change the hierarchical relationship of elements. If used in multiple places, such a cascading and nested relationship will be very confusing. This article will temporarily abandon that solution and share with you several CSS background image positioning solutions.
1. Dependency-free absolute positioning
On the Internet, there is a misunderstanding about absolute. It is believed that the use of absolute positioning absolute must set the relative positioning of the parent element relative. . This understanding cannot be considered wrong, it can only be said that the definition is not fully understood. Absolute is defined in the W3C document like this:
Generate an absolutely positioned element and position it relative to the first parent element other than static positioning. The position of the element is specified through the 'left', 'right', 'bottom' and 'top' attributes.
The understanding of this sentence should be as follows (L: left, R: right, B: bottom, T: top)
a: When position:absolute is set to an element, if If the parent element does not set position: relative, the element is positioned according to the upper left corner of the visual window area through LRBT; if the parent element sets position: relative, the element is positioned according to the upper left corner of the parent element container through LRBT.
b: When generating absolutely positioned elements, regardless of whether the parent element is set to position:relative, use two non-opposite directions of margin-top, margin-left, margin-right, and margin-bottom. Positioning, its effect will be like relative positioning, positioning based on its own position. However, the only difference between using margin positioning and relative is that absolute is separated from the document flow and the original physical space has disappeared, while relative is not separated from the document flow and the original physical space is still occupied.
So, you can use absolute without dependencies for positioning. The positioning method is to use margin, but not LRBT.
The code is as follows:
<p class="keith"> <p class="main"></p> <p> </p> </p>
.keith { margin: 2em; width: 5em; height: 5em; background: lightgreen; }</p> <p>.keith .main { position: absolute; background: url("../images/my-icons/Loginicon.png") scroll no-repeat 0 0; width: 21px; height: 21px; margin-left: calc(5em - 25px); margin-top: calc(5em - 25px); }
In the above code, margin-left, margin-top and position:absolute are used to position the image. Use calc() in CSS3 to calculate the value that needs to be positioned.
The sample picture is as follows:
2.background-position extended syntax
in CSS3 background and border , the background-position property has been extended, which allows us to specify the offset of the background image from any corner, as long as we specify the keyword in front of the offset.
The code is as follows:
<p class="keith"></p>
.keith{ margin:2em; width:5em; height:5em; background:lightgreen url('../images/my-icons/Loginicon.png') scroll no-repeat ; background-position:right 5px bottom 5px; };
In the above code, using the background-position extension syntax can achieve positioning 5px away from the lower right corner.
3.background-origin positioning
background-origin is a new attribute in CSS3. It is mainly used to determine the reference origin of the background-position attribute, that is, to determine the background. The starting point for image positioning. By default, the background-position attribute of the background image always positions the background image based on the upper left corner of the element.
Background-origin has three attribute values: content-box, padding-box (default value), and border-box.
Let’s see how to use this attribute to position the background image at 5px in the lower right corner. The code is as follows.
<p class="keith"></p>
.keith { margin: 2em; width: 5em; height: 5em; padding: .5em; background: lightgreen url("../images/my-icons/Loginicon.png") scroll no-repeat; background-position: right bottom; -moz-background-origin: content; -o-background-origin: content-box; -webkit-background-origin: content; background-origin: content-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
In the above code, the padding value is set for the box. Use the box-sizing attribute to adjust the box model of p under the browser to the IE box model. Border-box means total width = content area + padding + border. The content-box is set for the background-origin attribute here. You may not understand why it is not padding-box. Look at a screenshot under Firefox.
#In the above picture, the yellow area is margin, the purple area is padding, and the light blue area is content-area. If padding-box is set for the element, then the image will be positioned in the lower right corner without any space from the lower right corner. Therefore, content-box should be used for positioning at this time. The final effect is as follows.
4.calc() positioning
If we want to expand the width and height of the container through the content, without When fixing the height and width, you need to use the calc attribute + background-position attribute in combination to position the image. Because the height and width of the container are not known at this time, only 100% can be used for calculation.
<p class="keith"> 这是一段文字,用于撑开容器。 </p>
.keith{ margin:2em; padding:2em; display:inline-block; background:lightgreen url('../images/my-icons/Loginicon.png') scroll no-repeat; background-position:calc(100% - 5px) calc(100% - 5px); }</p> <p>
Sample pictures are as follows:
The above is the detailed content of Summary of common methods for positioning background images using CSS. For more information, please follow other related articles on the PHP Chinese website!

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

Article discusses three methods to add CSS to HTML: inline, internal, and external. Each method's impact on website performance and suitability for beginners is analyzed.(159 characters)


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
