How much do you know about css (11)--position_html/css_WEB-ITnose
1. Introduction
This article will use an article to introduce position. Before learning position, we should think about a question: under what circumstances do we need positioning? If there is no positioning, what needs will not be met? We must know that every piece of knowledge created by humans has its use and must solve some problems encountered before.
If there is no positioning, the web page we make will be tiled on the browser step by step from top to bottom, left to right, plus adjusting the spacing through margin and padding, and through float to float certain elements. It is enough to make some simple web pages, such as Yahoo N years ago, although it seems very low now.
But in some cases, this step-by-step web page layout cannot meet our requirements. We need certain elements to come out and float on the web page, and we need to specify a position for it. At this time we need to use position, and it is absolutely necessary. As shown below:
2. relative
From the information, we can see that position has four optional attributes: static/relative/absolute/fixed. Among them, static (static positioning) is the default value, that is, if no other position value is set for all elements, its position value is static, and it is the same as without it. No more introduction.
Relative positioning relative can be easily demonstrated with an example. For example, if we write 4
, everyone can know what it looks like without looking at it.
Then we are on the third & lt; p & gt; add posity: related and set the left and top values to see this & lt; p & gt; what changes.
In the picture above, you should identify two pieces of information (I believe most people will ignore the second piece of information)
- The third one< The position of p> has changed, moving 10px to the right and down respectively;
- The positions of the other three
have not changed, which is also very important.
Pay attention The difference between these two pictures will be explained below.
3. absolute
Speaking of absolute, I recommend everyone to watch a video tutorial. The instructor explained absolute thoroughly. Some of the content of this article is also based on this tutorial. Please share the good stuff with us!Let’s get back to the subject, let’s take the previous example and write a basic page first?? 4
Then, we Change the third
to absolute and see what happens.
From the above results, we can see several pieces of information:
- The absolute element is out of the document structure. Unlike relative, the positions of the other three elements are rearranged. Whenever an element breaks away from the document structure, it becomes destructive, causing the parent element to collapse. (At this point you should be able to remember immediately that the float element will also be separated from the document structure)
- The absolute element has "wrapping properties". Previously, the width of
- Absolute elements have "following properties". Although the absolute element is separated from the document structure, its position has not changed, and it still stays in its original position, because we have not set the values of top and left at this time.
- The absolute element will float above the page and block the page content below.
filled the entire screen, but now, the width of
is exactly the width of the content.
But sometimes setting the top and left values is not the best solution for positioning. For example, if you want element A to be immediately above element B, you can set element B to absolute and then adjust the margin value of B. This is more efficient. As shown below:
The information mentioned above, if expanded, contains a lot of content, and it is difficult to describe in text. I still recommend everyone to watch the video on MOOC.com The tutorial is very interesting.
Finally, a few more tips.
- Setting absolute will cause the inline element to be "blocked", as mentioned in the previous section when displaying;
- Setting absolute will invalidate the element's existing float. However, float and absolute are rarely used at the same time;
- As mentioned above, absolute will make elements float on the page. If there are multiple suspended elements, how to determine the level? The answer is "the last ones come first"
In fact, fixed and absolute are the same. The only difference is that the absolute element determines its position based on the nearest positioning context, while fixed is always based on the browser Determine the location.
"Positioning context" has been mentioned many times above, so what exactly is it? The answer will be revealed soon.
5. Positioning context5.1 Relative positioning
The positioning of a relative element is always relative to the element's own position. It has nothing to do with other elements and will not affect other elements. .
5.2 fixed positioning
Fixed element positioning is always relative to the browser boundary and has nothing to do with other elements. But it is destructive and causes changes in the position of other elements.
5.3 Absolute positioning
The positioning of absolute is much more complicated than the first two. If top and left are set for absolute, how will the browser determine its vertical and horizontal offsets? The answer is that the browser will recursively search all parent elements of the element. If an element with position: relative/absolute/fixed is found, it will be positioned based on that element. If not found, it will be positioned based on the browser boundary. As shown in the following two figures:
The "ancestor element of a certain layer" in the figure above is the positioning context of the absolute element. Having said that, can everyone understand the picture we mentioned above? If you still don’t understand, you can leave a message to me.
6. Summary
I feel that this article is the most difficult to write in this series. It has many knowledge points and is very difficult to understand. Therefore, position is also a key knowledge of CSS. Front-end developers who do not understand position should hurry up and make up for it.
After writing so much, I still feel that the writing is not very comprehensive or not very systematic. I hope everyone can give me your feedback. This blog series is the first version of this tutorial. I plan to take the time to record a video version later. I hope it will be more comprehensive and systematic.
----------------------------------------------- -------------------------------------------------- ---------------
Welcome to follow my tutorial: "From Design to Pattern" " In-depth understanding of javascript prototypes and closures Series 》《Microsoft petshop4.0 source code interpretation video》《json2.js source code interpretation video》
You are also welcome to follow my open source project??wangEditor, a simple and easy-to-use web rich text editor Device
---------------------------------------- -------------------------------------------------- ------------------

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...
