stick定位一如其名:它随“正常”文档流而动,直到规定位置,尔后“粘”在那里;或者,当它发现自己可以跟随“正常”文档流而脱离sticky位置时,就果断离开从而加入文档流。

代码与效果如下:

<div  style="height: 200px; overflow:scroll;">
<p style="background-color:lightgrey; position:sticky; top: 0px;">This is header A</p>
<p>This is content A</p>
<p>This is content A</p>
<p>This is content A</p>
<p>This is content A</p> <p style="background-color:lightgrey; position:sticky; top: 0px;">This is header B</p>
<p>This is content B</p>
<p>This is content B</p>
<p>This is content B</p>
<p>This is content B</p> <p style="background-color:lightgrey; position:sticky; top: 0px;">This is header C</p>
<p>This is content C</p>
<p>This is content C</p>
<p>This is content C</p>
<p>This is content C</p> <p style="background-color:lightgrey; position:sticky; top: 0px;">This is header D</p>
<p>This is content D</p>
<p>This is content D</p>
<p>This is content D</p>
<p>This is content D</p> </div>

标题行设置了背景色。如果不设置背景色(背景透明),正常文档流的文字就会和标题行文字重叠在一起显示。

sticky定位的元素会遮住滚动而来的“正常”的文档流;后面的sticky元素会覆盖前面的sticky元素,就好像一层层的便利贴。

This is header A

This is content A

This is content A

This is content A

This is content A

This is header B

This is content B

This is content B

This is content B

This is content B

This is header C

This is content C

This is content C

This is content C

This is content C

This is header D

This is content D

This is content D

This is content D

This is content D

CSS布局:sticky定位的更多相关文章

  1. CSS布局与定位——height百分比设置无效/背景色不显示

    CSS布局与定位——height百分比设置无效/背景色不显示 html元素属性width和height的值有两种表达方式,一是固定像素如“100px”,一是百分比如“80%”, 使用百分比的好处是元素 ...

  2. 测开之路三十八:css布局之定位

    常用的布局方式: static:静态定位(默认),什么都不用管,元素会按照默认顺序排列,排不下是会默认换行relative:相对定位(同一层),相对于某一个元素进行定位fixed:绝对定位,指定位置a ...

  3. Head First HTML与CSS — 布局与定位

    1.流(flow)是浏览器在页面上摆放HTML元素所用的方法. 对于块元素,浏览器从上到下沿着元素流逐个显示所遇到的各个元素,会在每个块元素之间加一个换行: 对于内联元素,在水平方向会相互挨着,总体上 ...

  4. css 布局之定位 相对/绝对/成比例缩放

    给body添加 overflow: hidden; 可以将页面所有的 滚动条隐藏,但必须要给body 设置一个高度 overflow: hidden; height:864px; 父元素必须要设置 p ...

  5. 前端学习笔记--CSS布局--float定位

    1.float属性 box1向右移动,box2顶替了box1的位置,box3顶替了box2的位置. 2.clear属性 案例: 一列三行布局: <!DOCTYPE html> <ht ...

  6. 前端学习笔记--CSS布局--层定位

    1.层定位概述: z-index:前后叠加顺序 2.position属性: 3.fixed: 2.relative: 移动后: static没有往上移动占据box1的位置. 3.absolute: 移 ...

  7. 深入css布局篇(2) — 定位与浮动

    深入css布局(2) - 定位与浮动      在css知识体系中,除了css选择器,样式属性等基础知识外,css布局相关的知识才是css比较核心和重要的点.今天我们来深入学习一下css布局相关的知识 ...

  8. 【CSS】Sticky Footer 布局

    什么是 Sticky Footer 布局? Sticky Footer 布局是一种将 footer 吸附在底部的CSS布局. footer 可以是任意的元素,该布局会形成一种当内容不足,footer ...

  9. 奇妙的CSS之布局与定位

    前言 关于布局与定位是Web前端开发里非常基础而又重要的部分.介绍相关知识的文章,很容易就可以找到.虽然,这方面的知识点不是很多,但我们如果不弄清楚,在运用时候往往会出现预料之外的布局,这些“意外”有 ...

随机推荐

  1. 【转】如何在 Linux 中查看可用的网络接口

    原文:https://www.cnblogs.com/qianpangzi/p/10563979.html 查看ubuntu系统当前的可用的网络接口.方法如下 -------------------- ...

  2. easyui datebox 只显示日期,本文为转载,稍加改动

    var DateBoxHandler = {}; DateBoxHandler.onlyShowMonth = function(id) { function padding(v) {if (v &l ...

  3. Ruby Raise rescue

    ruby1.9以上,retry只能支持在rescue里面使用,不支持在block里面用:你要去用ruby1.8 rescue使用代码例子 # -*- coding: UTF-8 -*- n = 0 b ...

  4. self & _cmd

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles ...

  5. isa objc_msgSend

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles ...

  6. [Algorithm] 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. go语言 函数return值的几种情况

    分三种情况 (以下 “指定返回值”这句话, 仅指return后面直接跟着的返回值) 退出执行,不指定返回值 (1) 函数没有返回值 package main import ( "fmt&qu ...

  8. linux学习11 Linux基础命令及命令历史

    一.Linux系统上的文件类型 1.- :常规文件:在其它程序中用f表示.比如我们用ls -l命令查看的第一个内容 [root@localhost ~]# ls -l total -rw------- ...

  9. Fiborial 题解——2019.10.14

    一看到这个题 就感觉...cao,, 什么东西...??! 然后就开始暴力求Fn 然鹅我并不会写高精(我太菜了) 只能求到大概10左右 在吧Fn给质因数分解 求出其因子个数 妄图找到什么有关的规律 但 ...

  10. shell 判断大小

    test.sh #!/bin/bash read -p "" a read -p "" b if [ $a -eq $b ];then echo "= ...