【CSS】固定页面-overflow属性

固定页面:

页面设计或是大屏展示时我们时常会遇到需要固定页面(不希望页面滚动)的情况。

/* 给需要的页面设置以下属性 */

height: 100vh; /* 高度撑满屏幕 */
overflow: hidden; /* 当元素内的内容超出了其容器的大小时,超出部分会被隐藏。 */

解释:

  • overflow属性 用于设置一个元素的内容如果溢出其内容区(即元素的宽度和高度)时的处理方式。它可以取以下值:
    • visible:默认值。内容不会被裁剪,可能会溢出容器。
    • hidden:内容会被裁剪,并且不可见。
    • scroll:内容会被裁剪,并且浏览器会显示滚动条以便查看溢出的内容。
    • auto:如果内容溢出,浏览器会根据需要显示滚动条。

适用场景:

  • 防止溢出内容干扰布局:隐藏多余的内容,避免破坏页面布局。
  • 创建裁剪效果:仅显示部分内容,适用于图片裁剪或固定尺寸的内容显示区域。
  • 提高用户体验:在一些情况下,隐藏滚动条和溢出内容可以简化界面,提高用户体验。
background-attachment属性指定背景图像是否固定或随着页面滚动而移动。它有以下几个值: 1. scroll(默认值):背景图像随着页面滚动而移动。 2. fixed:背景图像固定不动,不随着页面滚动而移动。 3. local:背景图像相对于元素内容固定,不随着页面滚动而移动。 以下是演示: ```html <!DOCTYPE html> <html> <head> <title>Background Attachment Demo</title> <style type="text/css"> body { height: 2000px; } .bg-scroll { background-image: url('https://picsum.photos/1200/800'); background-repeat: no-repeat; background-size: cover; background-position: center; background-attachment: scroll; } .bg-fixed { background-image: url('https://picsum.photos/1200/800'); background-repeat: no-repeat; background-size: cover; background-position: center; background-attachment: fixed; } .bg-local { background-image: url('https://picsum.photos/1200/800'); background-repeat: no-repeat; background-size: cover; background-position: center; background-attachment: local; height: 500px; overflow: scroll; } .content { padding: 50px; color: #fff; text-align: center; font-size: 2em; } </style> </head> <body> <div class="bg-scroll"> <div class="content"> Scroll Background </div> </div> <div class="bg-fixed"> <div class="content"> Fixed Background </div> </div> <div class="bg-local"> <div class="content"> Local Background </div> </div> </body> </html> ``` 以上代码将创建三个不同的元素,每个元素都有不同的background-attachment属性。第一个元素的背景图像随着页面滚动而移动,第二个元素的背景图像固定不动,第三个元素的背景图像相对于元素内容固定,只有元素内容区域内可以滚动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值