创建可变高度“固定"带有可滚动内容的 CSS 标头

2023-02-15前端开发问题
24

本文介绍了创建可变高度“固定"带有可滚动内容的 CSS 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在页面上创建一个在您滚动页面时不会移动的页眉.

I'd like to create a header on the page that doesn't move when you scroll the page.

这看起来很简单,只需在样式中添加位置:固定",但随后内容会出现在标题下方,因为标题已从流程中移除".所以我认为的解决方案是在内容中添加margin-top:height".

This seems simple, just add "position: fixed" to the style, but then the content appears underneath the header, because the header is "removed from the flow". So the solution I think is to add "margin-top: height" to the content.

当标题的高度可变时,解决此问题的最佳方法是什么?

Whats the best way to solve this when the height of the header is variable?

我有一个演示问题的小提琴:

I've got a fiddle that demonstrates the problem:

http://jsfiddle.net/waterlooalex/j4Z8F/2/

如果你的浏览器窗口不是太大,内容文本会在'hello world header'下方滚动,问题是第一行文本Lorem ipsum..."被隐藏了.我有一些评论解决这个问题的 javascript.

Providing your browser window is not too large, the content text will scroll below the 'hello world header', the problem is that the first line of text "Lorem ipsum...' is hidden. I've got some comment out javascript that works around the issue.

推荐答案

您可以添加 not 修复的标题 div 的第二个副本,并使用类似 visibility:hidden 就可以了.这样,它将内容推送到您需要的确切位置,但不使用 JavaScript.有点骇人听闻,但我在你的小提琴中试过了,它可以工作.

You could add a second copy of the header div that's not fixed and use something like visibility:hidden on it. That way it pushes the content down to the exact spot you need, but without using JavaScript. Kinda hackish but I tried it in your fiddle and it works.

这篇关于创建可变高度“固定"带有可滚动内容的 CSS 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13

getFullYear 在一年的第一天返回前一年
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20 前端开发问题
6

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6

如何在开发网站时使用coffeescript?
How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)...
2024-04-20 前端开发问题
10