仅当页面大小太小时隐藏溢出
我有一个名为 page 的 div,可以说 1000px width
和 position: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 CSS 媒体查询 (http://www.w3.org/TR/css3-mediaqueries/) 根据屏幕尺寸更改布局。 IE:
You can use CSS Media queries (http://www.w3.org/TR/css3-mediaqueries/) to change the layout based on screen size. i.e.:
解决方案非常简单...
只需在页面周围制作一个包装 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 setoverflow: hidden
for 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 defaultoverflow: visible
).And that's it... works in (almost) every 5 year old browser.
您应该使用 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.