Home Web Front-end CSS Tutorial Distinguish between sticky positioning and fixed positioning

Distinguish between sticky positioning and fixed positioning

Feb 18, 2024 pm 10:42 PM
position attribute Fixed positioning sticky positioning

Distinguish between sticky positioning and fixed positioning

Sticky positioning and fixed positioning are two positioning methods commonly used in web design and development. They both allow an element to be fixed at a certain location on the page, but in different ways. This article will introduce in detail the difference between sticky positioning and fixed positioning, and provide specific code examples to help readers better understand.

  1. Sticky Positioning:
    Sticky positioning means that the element can be fixed at a certain position on the page when scrolling. When the scroll position reaches the specified position, the element will stop scrolling. and pinned to the page. Sticky positioning is relative to the document flow. Under normal document flow layout, the position of the element will change with scrolling. In sticky positioning, the position of an element is determined by attributes such as top, bottom, left, and right.

The following is a simple sample code that implements the effect of a navigation bar fixed at the top of the page when scrolling to the top of the page:

<!DOCTYPE html>
<html>
<head>
<style>
.navbar {
  position: sticky;
  top: 0;
  background-color: #f1f1f1;
  padding: 10px 0;
  text-align: center;
}
</style>
</head>
<body>

<div class="navbar">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</div>

<div style="height:500px">
  <p>Scroll down to see the effect</p>
</div>

</body>
</html>
Copy after login

In the above code, by setting The position attribute of navbar is sticky, and top is set to 0, which achieves the effect of the navigation bar being fixed at the top of the page as it scrolls.

  1. Fixed Positioning:
    Fixed positioning means that the element is fixed at a certain position relative to the browser window. The position of the element will not change whether it is scrolled or not. In fixed positioning, the position of the element is determined by attributes such as top, bottom, left, and right.

The following is a simple sample code that implements the effect of a floating button at a fixed position in the lower right corner of the page:

<!DOCTYPE html>
<html>
<head>
<style>
.float-button {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background-color: #f44336;
  color: white;
  padding: 16px;
  border-radius: 50%;
  font-size: 24px;
  text-align: center;
  cursor: pointer;
}
</style>
</head>
<body>

<div class="float-button">+</div>

</body>
</html>
Copy after login

In the above code, by setting the float-button The position attribute is fixed, and the bottom is set to 20px and the right is 20px to achieve the effect of the floating button being fixed in the lower right corner of the page.

Summary:
Both sticky positioning and fixed positioning can achieve the fixed effect of elements, but the methods and effects are different. Sticky positioning is a positioning method relative to the document flow. When scrolling to a specified position, the element is fixed on the page; fixed positioning is a positioning method relative to the browser window. The element remains in a fixed position regardless of whether it is scrolled or not. According to specific needs, you can choose a suitable positioning method.

The above is the detailed content of Distinguish between sticky positioning and fixed positioning. For more information, please follow other related articles on the PHP Chinese website!

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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Does sticky positioning break away from the document flow? Does sticky positioning break away from the document flow? Feb 20, 2024 pm 05:24 PM

Does sticky positioning break away from the document flow? Specific code examples are needed. In web development, layout is a very important topic. Among them, positioning is one of the commonly used layout techniques. In CSS, there are three common positioning methods: static positioning, relative positioning and absolute positioning. In addition to these three positioning methods, there is also a more special positioning method, namely sticky positioning. So, does sticky positioning break away from the document flow? Let’s discuss it in detail below and provide some code examples to help understand. First, we need to understand what document flow is

How to adjust a WordPress theme to avoid misaligned display How to adjust a WordPress theme to avoid misaligned display Mar 05, 2024 pm 02:03 PM

How to adjust WordPress themes to avoid misaligned display requires specific code examples. As a powerful CMS system, WordPress is loved by many website developers and webmasters. However, when using WordPress to create a website, you often encounter the problem of theme misalignment, which affects the user experience and page beauty. Therefore, it is very important to properly adjust your WordPress theme to avoid misaligned display. This article will introduce how to adjust the theme through specific code examples.

How to position elements in css How to position elements in css Apr 26, 2024 am 10:24 AM

There are four methods of CSS element positioning: static, relative, absolute, and fixed positioning. Static positioning is the default and the element is not affected by positioning rules. Relative positioning moves an element relative to itself without affecting document flow. Absolute positioning removes an element from the document flow and positions it relative to its ancestor elements. Fixed positioning positions an element relative to the viewport, always keeping it in the same position on the screen.

What is css fixed positioning What is css fixed positioning Oct 25, 2023 pm 05:06 PM

Fixed positioning in CSS is a layout technique that is achieved by setting the "position" attribute of an element to "fixed". Fixed-positioned elements are positioned relative to the viewport, not relative to their parent elements or other elements, which means This means that fixedly positioned elements will remain in a fixed position in the viewport no matter how the user scrolls the page. Fixed positioning requires attention to compatibility, mobile devices, performance impact, etc. Fixed positioning is widely used in scenarios such as navigation bars, advertising banners, return to top buttons, and floating toolbars.

What is layout layout? What is layout layout? Feb 24, 2024 pm 03:03 PM

Layout refers to a typesetting method adopted in web design to arrange and display web page elements according to certain rules and structures. Through reasonable layout, the webpage can be made more beautiful and neat, and achieve a good user experience. In front-end development, there are many layout methods to choose from, such as traditional table layout, floating layout, positioning layout, etc. However, with the promotion of HTML5 and CSS3, modern responsive layout technologies, such as Flexbox layout and Grid layout, have become

Sticky Positioning Revealed: What Features Can It Capture Users' Attention? Sticky Positioning Revealed: What Features Can It Capture Users' Attention? Feb 02, 2024 pm 01:17 PM

Explore the characteristics of sticky positioning: Why does it attract users’ attention? Introduction: Today, the popularity of mobile devices has made people have higher requirements for web design and user experience. In web design, an important element is how to attract users' attention and provide a friendly user experience. Sticky positioning, or StickyPositioning, came into being. It provides users with more convenient navigation and interaction by fixing the position of elements on the page. This article will explore the characteristics of sticky positioning and give specific code implementations.

Detailed explanation of the use of background-position property in CSS Detailed explanation of the use of background-position property in CSS Feb 19, 2024 pm 10:13 PM

The usage of background-position in CSS is detailed. In CSS, the background-position property is used to set the position of the background image within the element. This property is very useful because it allows us to precisely control where the background image appears. The following will introduce the usage of background-position in detail and provide some specific code examples. Syntax: The syntax of the background-position attribute is as follows: back

How to align text boxes in html How to align text boxes in html Mar 27, 2024 pm 04:33 PM

Methods for aligning text boxes in html: 1. Text alignment; 2. Use Flexbox layout alignment; 3. Use Grid layout alignment; 4. Use margin or position for fine-tuning.

See all articles