仅当页面大小太小时隐藏溢出

发布于 2025-01-07 07:08:19 字数 534 浏览 2 评论 0原文

我有一个名为 page 的 div,可以说 1000px widthposition:relative; 在这个 page div 中,我有一个带有 position:absolute;< 的徽标/code> 和 top:-20px;右:-20px。当页面宽度大于 1000px 时,应显示图像,但当页面宽度等于或小于 1000px 时,应隐藏溢出(overflow: hide;)。

当我将页面 div 中的溢出属性设置为 overflow: hide; 时,徽标会被裁剪,当我选择 visible 时,当页面宽度相等时,我会看到一个水平滚动条或低于 1000 像素。

我解决这个问题的想法是使用 JavaScript 并根据页面宽度设置 overflow 属性。我更喜欢 CSS 解决方案,但找不到。 :-/

有人建议如何使用 CSS 解决这个问题吗?

谢谢!

I've a div named page with lets say 1000px width and position: relative; and in this page div I've a logo with position: absolute; and top:-20px; right: -20px. When the page width is more than 1000px the image should be displayed but when the page width is equal or lower than 1000px the overflow should be hidden (overflow: hidden;).

When I set the overflow attribute in the page div to overflow: hidden; the logo is cropped and when I choose visible I get a horizontal scroll bar when the page width is equal or lower than 1000px.

My idea to solve this issue is to use JavaScript and set the overflow attribute depending on the page width. I would prefer a CSS solution but couldn't find one. :-/

Does anyone have a suggestion how to solve this using CSS?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痞味浪人 2025-01-14 07:08:19

您可以使用 CSS 媒体查询 (http://www.w3.org/TR/css3-mediaqueries/) 根据屏幕尺寸更改布局。 IE:

@media screen and (max-width: 1000px) { 
#page { overflow: hidden; } 
}

#page { overflow: visible; }

You can use CSS Media queries (http://www.w3.org/TR/css3-mediaqueries/) to change the layout based on screen size. i.e.:

@media screen and (max-width: 1000px) { 
#page { overflow: hidden; } 
}

#page { overflow: visible; }
撕心裂肺的伤痛 2025-01-14 07:08:19

解决方案非常简单...

只需在页面周围制作一个包装 div 即可。假设页面 div 的宽度为 1000px,包装器 div 的宽度为 width: 100%。然后,这是为包装器 div 设置 overflow:hidden 的最简单方法,当页面大小缩小到例如 1020px 时,会裁剪一部分图像,当大小进一步缩小到 1000px 时,会出现水平滚动条因为页面 div(具有默认的 overflow:visible)。

就是这样...适用于(几乎)所有 5 年历史的浏览器。

The solution is really simple...

Just make a wrapper div around the page. Let's say the page div is 1000px width and the wrapper div has width: 100%. Then it's the easiest way to set overflow: hiddenfor the wrapper div and when the page size shrinks to for example 1020px a piece of the image is cropped and when the size shrinks further to 1000px the horizontal scrollbar appears because of the page div (which has the default overflow: visible).

And that's it... works in (almost) every 5 year old browser.

何处潇湘 2025-01-14 07:08:19

您应该使用 CSS(针对浏览器布局问题而不是 Javascript)来执行此操作 - 并使用 @media 查询。

像这样,你想要水平滚动出现的地方是1200px及以下。

@媒体屏幕和(最小宽度:1200px){
html {overflow-x: hide;}

将其粘贴在样式表的末尾,或者标头中的某些标记中以进行测试。

You should do this with CSS ( Not Javascript for browser layout issues ) - and use an @media query.

Like this, where the place you want the horizontal scroll to appear is 1200px and below.

@media screen and (min-width: 1200px) {
html {overflow-x: hidden;}

Stick that at the end of your style sheet, or in some tags in your header to test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文