html - 无法使用 overflow-x : hidden on image

标签 html css

我的页面顶部有这张图片,它随着窗口变小,直到某个点需要保持其大小并在这样做时保持在中间。正是在这一点上,图像的右侧开始向右“溢出”。我觉得我已经尝试了一切,但没有任何效果。这是我的代码的一部分:

html

<div class="imageThing"></div>

CSS

.imageThing {
  position: absolute;
  overflow-x: hidden;
}

@media (min-width: 1071px) {
  .imageThing {
    opacity: 0;
    -webkit-animation: fadein 1s; /* Safari and Chrome */
    -moz-animation: fadein 1s; /* Firefox */
    -ms-animation: fadein 1s; /* Internet Explorer */
    -o-animation: fadein 1s; /* Opera */
    animation: fadein 1s;
    animation-delay: .25s;
    animation-fill-mode: both;
    background: #fff url('https://cdn.glitch.com/57cbb378-055c-4d36-a029-5a44e3138ccd%2Fhuekast.jpg?1549906076009') 0px 0px no-repeat ;
    background-size: 100vw 30.15vw;
    height: 30.15vw;
    width: 100vw;
    top: 0px;
    left: 0px;

  }
}

@media (max-width: 1070px) {
 .imageThing {
    background: #fff url('https://cdn.glitch.com/57cbb378-055c-4d36-a029-5a44e3138ccd%2Fhuekast.jpg?1549906076009') 0px 0px no-repeat ;
    background-size: 1070px 322px;
    height: 322px;
    width: 1070px;
    -webkit-animation: fadein 1s; /* Safari and Chrome */
    -moz-animation: fadein 1s; /* Firefox */
    -ms-animation: fadein 1s; /* Internet Explorer */
    -o-animation: fadein 1s; /* Opera */
    animation: fadein 1s;
    animation-delay: .25s;
    animation-fill-mode: both;
    left: 50%;
    margin-left: -535px;
    overflow-x: hidden;
  }
}

我真的希望你能帮助我。提前谢谢你

最佳答案

overflow仅适用于元素内的子元素,不适用于背景图像。 您可以使用 background-position: center; 使背景图像居中。也许适合您的情况,您可以尝试使用 background-size: cover; 以获得“完美契合”。

cover

Scales the image as large as possible without stretching the image. If the proportions of the image differ from the element, it is cropped either vertically or horizontally so that no empty space remains.


阅读您的评论我想这是期望的结果:

CodePen


that gives the right effect, but the image needs to be position: absolute;, because it needs to overlap with something else

这个怎么样?

CodePen

关于html - 无法使用 overflow-x : hidden on image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54716794/

相关文章:

jquery - 折叠DIV底部到顶部

javascript - jquery mobile 底部图标导航栏如何只重新加载页面内容

javascript - Fullcalendar:我应该定位哪个 css 选择器以在日历不可用时显示 'loading' 图像?

html - 什么是 HTML5 语义布局?

java - 如何使用OSGI远程查看html文件

javascript - 我的容器 Div 超出了 Particle.js 背景

html - 使用 PureCSS 使对象在网格宽度内居中

css - bootstrap 2.3.2 中带有响应式主 div 的固定宽度侧边栏

html - 如何使用CSS水平对齐图标?

javascript - 模拟键盘输入/将字符串插入文本区域(adwords)