Home Web Front-end HTML Tutorial How much do you know about css (11)--position_html/css_WEB-ITnose

How much do you know about css (11)--position_html/css_WEB-ITnose

Jun 24, 2016 am 11:48 AM

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;
  1. The positions of the other three

    have not changed, which is also very important.

Therefore, relative will cause relative changes in its own position without affecting changes in the position and size of other elements. This is one of the main points of relative. There is a second important point, which is that relative generates a new positioning context. There is a detailed introduction to positioning context below. Here you can use an example to show the difference:

 

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)
  1. The absolute element has "wrapping properties". Previously, the width of

    filled the entire screen, but now, the width of

    is exactly the width of the content.

  2. 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.
  3. The absolute element will float above the page and block the page content below.
Finally, by setting the top and left values ​​​​for the absolute element, you can customize its content, which is commonly used in daily life. What needs to be noted here is that when the top and left values ​​are set, the element is positioned relative to the nearest positioning context, not relative to the browser. Positioning context will be discussed shortly below.

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.

  1. Setting absolute will cause the inline element to be "blocked", as mentioned in the previous section when displaying;
  2. Setting absolute will invalidate the element's existing float. However, float and absolute are rarely used at the same time;
  3. 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"
4. fixed

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 context

5.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

---------------------------------------- -------------------------------------------------- ------------------

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

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.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

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

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

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: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

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.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

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...

See all articles