Detailed explanation of the sticky position attribute in CSS

Detailed explanation of the sticky position attribute in CSS

When developing mobile apps, you often encounter a situation where you need to make part of the content appear as a navbar when the website scrolls to a certain height. We usually use js to listen to the scroll event to achieve this, but the newly added CSS attribute position:sticky can be easily achieved.

I'm not the last one to know: position: sticky

The meaning of position refers to the positioning type. The possible value types are: static, relative, absolute, fixed, inherit and sticky. Here sticky is a newly released attribute of CSS3. What I want to focus on today is the sticky attribute

Usage of position:sticky

  • position:sticky is called a sticky positioned element, which is an element whose calculated position attribute is sticky.
  • The simple understanding is: within the target area, it behaves like position:relative; during the sliding process, when the distance between an element and its parent element reaches the requirement of sticky positioning (for example, top: 100px); the effect of position:sticky at this time is equivalent to fixed positioning, fixed to the appropriate position.
  • It can be said to be a combination of relative positioning and fixed positioning.
  • The fixed relative offset of an element is relative to its nearest ancestor element with a scroll box. If none of the ancestor elements can be scrolled, the offset of the element is calculated relative to the viewport.

Conditions for using position:sticky

1. The parent element cannot have overflow:hidden or overflow:auto attributes. 2. One of the top, bottom, left, and right values ​​must be specified, otherwise it will only be relatively positioned 3. The height of the parent element cannot be lower than the height of the sticky element 4. The sticky element is only effective within its parent element

example

When the mouse slides down to a certain height, the position:sticky positioning requirement is triggered, so that "Popular, New, Featured" is fixed at 44px from the top.

CSS Code

.tab-control{
  position: sticky;
  top: 44px;
}

HTML area

<tab-control class="tab-control" :titles="['Popular','New','Featured']"></tab-control>

Pay attention to compatibility in web development:

Sticky is still an experimental attribute and is not a W3C recommended standard. It occurs because listening to scroll events to implement sticky layout puts the browser into slow scrolling mode, which is contrary to the browser's desire to improve the scrolling experience through hardware acceleration. For details, see the figure below. Basically, the only browsers that use this property are Firefox and iOS Safari.

Summarize

This is the end of this article about the detailed explanation of the sticky position attribute in CSS. For more relevant content about the sticky position attribute in CSS, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope that everyone will support 123WORDPRESS.COM in the future!

<<:  How to build ssh service based on golang image in docker

>>:  Unity connects to MySQL and reads table data implementation code

Recommend

Web development tutorial cross-domain solution detailed explanation

Preface This article mainly introduces the cross-...

SQL statements in Mysql do not use indexes

MySQL query not using index aggregation As we all...

Detailed tutorial on installing Docker and nvidia-docker on Ubuntu 16.04

Table of contents Docker Installation Nvidia-dock...

Install MySQL (including utf8) using Docker on Windows/Mac

Table of contents 1. Docker installation on Mac 2...

How to upload projects to Code Cloud in Linux system

Create a new project test1 on Code Cloud Enter th...

CSS Viewport Units for Fast Layout

CSS Viewport units have been around for the past ...

Example of how to quickly build a Redis cluster with Docker

What is Redis Cluster Redis cluster is a distribu...

How to change the terminal to a beautiful command line prompt in Ubuntu 18

I reinstalled VMware and Ubuntu, but the command ...

A brief discussion on VUE uni-app template syntax

1.v-bind (abbreviation:) To use data variables de...

Solve the problem of black screen when starting VMware virtual machine

# Adjust VMware hard disk boot priority Step 1: E...

JavaScript implements asynchronous submission of form data

This article example shares the specific code of ...

Solution to "No input file specified" in nginx+php

Today, the error "No input file specified&qu...

Automatically log out inactive users after login timeout in Linux

Method 1: Modify the .bashrc or .bash_profile fil...

calc() to achieve full screen background fixed width content

Over the past few years, there has been a trend i...

Details of various font formats in HTML web pages

This section starts with the details of text modi...