search
HomeWeb Front-endFront-end Q&AWhat is the difference between fixed positioning and absolute positioning in css

The difference between fixed positioning and absolute positioning: 1. Fixed positioning uses the "position: fixed;" style setting, while absolute positioning uses the "position: absolute;" style setting; 2. The offset datum of fixed positioning is The screen (browser viewport), while the base for absolute positioning is the parent element.

What is the difference between fixed positioning and absolute positioning in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

Fixed positioning in css

Fixed positioning uses the "position: fixed;" setting.

A fixed-positioned element is positioned relative to the viewport, which means that it will always be in the same position even if the page is scrolled. That is, the fixed element will not change position as the scroll bar is dragged. The position of fixedly positioned elements does not change within the field of view.

The top, right, bottom and left attributes are used to position this element.

position:fixed;
top:像素值;
bottom;像素值;
left:像素值;
right:像素值;

"position:fixed;" is used in combination with the four attributes top, bottom, left and right. "position:fixed;" makes the element a fixed positioning element, and then uses top, bottom, The four attributes left and right are used to set the position of the element relative to the browser.

Not all of the four attributes top, bottom, left and right are used. Note that the reference objects of these four values ​​are the four edges of the browser.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body{
height: 1500px;
}
header {
width: 100%;
background-color: #FFC0CB;
position: fixed;
top: 0;
}
</style>
</head>
<body>
<header>
<h1 id="网站标题">网站标题</h1>
</header><br><br><br><br><br><br><br>
<div>测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!</div>
</body>
</html>

What is the difference between fixed positioning and absolute positioning in css

Absolute positioning in css

Absolute positioning is set using "position: absolute;".

In CSS, absolute positioning is a positioning method that makes the position of an element independent of the document flow.

An element box set to absolute positioning is completely removed from the document flow and positioned relative to its containing block, which may be another element in the document or the initial containing block. By default, the position of absolute positioning is relative to the browser, and is positioned with top, right, bottom, and left.

The space that the element originally occupied in the normal document flow is closed, as if the element did not exist. The element generates a block-level box after positioning, regardless of what type of box it originally generated in the normal flow.

Absolutely positioned elements are positioned relative to the nearest positioned ancestor element (rather than positioned relative to the viewport, such as fixed). However, if an absolutely positioned element has no ancestors, it will use the document body and move with the page scroll.

Let’s take a closer look at absolute positioning (absolute). In fact, absolute positioning absolute and floating float are partially similar; if you can understand floating float, it will be of great help to understand absolute positioning absolute.

Let’s start with the similarities between absolute and float: Wrapping and Highly deceptive

Wrapping

It is said that a picture is worth a thousand words (the only difference is: p in the picture below adds absolute)

What is the difference between fixed positioning and absolute positioning in css

<p   style="max-width:90%">
  <img  src="/static/imghwm/default1.png"  data-src="img/25/1.jpg"  class="lazy"   / alt="What is the difference between fixed positioning and absolute positioning in css" >
</p>
<p style="border:4px solid red; position: absolute;">
  <img  src="/static/imghwm/default1.png"  data-src="img/25/2.jpg"  class="lazy"   / alt="What is the difference between fixed positioning and absolute positioning in css" >
</p>

Once you add absolute or float to an element, it is equivalent to adding absolute to the element. Added display:block;. What does that mean? For example, the default width of the inline element span is adaptive, and it will not work if you add width to it. To set width, you need to set span to display:block. But if you add absolute or float to span, the display attribute of span will automatically become block, and the width can be specified. Therefore, if you see absolute/float and display:block appearing at the same time in CSS, then display:block is redundant CSS code.

High deception

In the above example, absolute is added to the p in the outer layer of the picture, so the high degree of deception is not reflected well, so absolute is moved to the inner picture. on, the effect comes out:

What is the difference between fixed positioning and absolute positioning in css

<p   style="max-width:90%">
  <img  src="/static/imghwm/default1.png"  data-src="img/25/1.jpg"  class="lazy"   / alt="What is the difference between fixed positioning and absolute positioning in css" >
</p>
<p style="border:4px solid red;">
  <img  src="/static/imghwm/default1.png"  data-src="img/25/2.jpg"  class="lazy"    style="max-width:90%"  / alt="What is the difference between fixed positioning and absolute positioning in css" >
</p>

If you have read the detailed explanation of CSS floating float, you will find that the effect is the same. But the principles behind them are actually different and not exactly the same. You can see it by adding some text:

What is the difference between fixed positioning and absolute positioning in css

<p   style="max-width:90%">
  <img  src="/static/imghwm/default1.png"  data-src="img/25/1.jpg"  class="lazy"   / alt="What is the difference between fixed positioning and absolute positioning in css" >
</p>
<p style="border:4px solid red;">
  <img  src="/static/imghwm/default1.png"  data-src="img/25/2.jpg"  class="lazy"    style="max-width:90%"  / alt="What is the difference between fixed positioning and absolute positioning in css" >
  我是一个绝对定位的absolute元素
</p>

It is obvious from the picture that the text is covered by the picture, which is different from float. float deceives the parent element into thinking that its height has collapsed, but the float element itself is still in the document flow, and the text will surround the float element and will not be obscured.

But absolute can no longer be regarded as deceiving the parent element, but has a hierarchical relationship. If the parent element in the normal document flow is considered a mortal, then absolute has become an immortal. In today's terms, it is no longer in the same dimension. From the perspective of the parent element, the image set to absolute has completely disappeared, so the text is displayed from the far left. The absolute level is high, so the picture covers the text.

I remember when I first came into contact with CSS, which was still a scumbag with a combat power of 5, and I knew that absolute could have the concept of hierarchy, I mistakenly thought that I had completely understood it. Now that I think about it, it is really confusing (of course this is not a bad thing) , whenever you feel that your previous self was like a piece of tofu, it means you have made progress. On the other hand, if you always think about how you were back then, it means that you are still resting on your laurels).

After having the above foundation, you also need to understand the following characteristics of absolute

  • How to determine the anchor point
  • Love and kill with relative
  • Relationship with z-index
  • Reduce the overhead of redrawing and reflow

The difference between fixed positioning and absolute positioning

1. Different setting methods

Fixed positioning uses "position: fixed;" setting.

Absolute positioning uses the "position: absolute;" setting.

2. Different offset bases

The offset base for fixed positioning is the screen (browser window), while the base for absolute positioning is the parent element.

And it’s best to note that ie6 is not compatible with fixed positioning but is compatible with absolute positioning

(Learning video sharing: web front-end)

The above is the detailed content of What is the difference between fixed positioning and absolute positioning in css. For more information, please follow other related articles on the PHP Chinese website!

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
CSS IDs vs Classes: which is better for accessibility?CSS IDs vs Classes: which is better for accessibility?May 10, 2025 am 12:02 AM

Classesarebetterforaccessibilityinwebdevelopment.1)Classescanbeappliedtomultipleelements,ensuringconsistentstylesandbehaviors,whichaidsuserswithdisabilities.2)TheyfacilitatetheuseofARIAattributesacrossgroupsofelements,enhancinguserexperience.3)Classe

CSS: Understanding the Difference Between Class and ID SelectorsCSS: Understanding the Difference Between Class and ID SelectorsMay 09, 2025 pm 06:13 PM

Classselectorsarereusableformultipleelements,whileIDselectorsareuniqueandusedonceperpage.1)Classes,denotedbyaperiod(.),areidealforstylingmultipleelementslikebuttons.2)IDs,denotedbyahash(#),areperfectforuniqueelementslikeanavigationmenu.3)IDshavehighe

CSS Styling: Choosing Between Class and ID SelectorsCSS Styling: Choosing Between Class and ID SelectorsMay 09, 2025 pm 06:09 PM

In CSS style, the class selector or ID selector should be selected according to the project requirements: 1) The class selector is suitable for reuse and is suitable for the same style of multiple elements; 2) The ID selector is suitable for unique elements and has higher priority, but should be used with caution to avoid maintenance difficulties.

HTML5: LimitationsHTML5: LimitationsMay 09, 2025 pm 05:57 PM

HTML5hasseverallimitationsincludinglackofsupportforadvancedgraphics,basicformvalidation,cross-browsercompatibilityissues,performanceimpacts,andsecurityconcerns.1)Forcomplexgraphics,HTML5'scanvasisinsufficient,requiringlibrarieslikeWebGLorThree.js.2)I

CSS: Is one style more priority than another?CSS: Is one style more priority than another?May 09, 2025 pm 05:33 PM

Yes,onestylecanhavemoreprioritythananotherinCSSduetospecificityandthecascade.1)Specificityactsasascoringsystemwheremorespecificselectorshavehigherpriority.2)Thecascadedeterminesstyleapplicationorder,withlaterrulesoverridingearlieronesofequalspecifici

What are the significant goals of the HTML5 specification?What are the significant goals of the HTML5 specification?May 09, 2025 pm 05:25 PM

ThesignificantgoalsofHTML5aretoenhancemultimediasupport,ensurehumanreadability,maintainconsistencyacrossdevices,andensurebackwardcompatibility.1)HTML5improvesmultimediawithnativeelementslikeand.2)ItusessemanticelementsforbetterreadabilityandSEO.3)Its

What are the limitations of React?What are the limitations of React?May 02, 2025 am 12:26 AM

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

React's Learning Curve: Challenges for New DevelopersReact's Learning Curve: Challenges for New DevelopersMay 02, 2025 am 12:24 AM

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate

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

Video Face Swap

Video Face Swap

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

Hot Tools

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),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

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.

MantisBT

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software