作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个 html:
<div class="postcard-container">
<div id="postcard-classic" class="postcard-classic">
<div class="postcard-classic-img">
<img ng-src="{{postcard.image.cropped}}" />
</div>
</div>
</div>
还有这个 CSS:
.postcard-container {
position: absolute;
display: table;
width: 100%;
height: 100%;
padding: 20px;
}
.postcard-classic {
display: table-cell;
vertical-align: middle;
background-position: center center;
background-image: url("../img/frames/postcard_00.png");
background-size: contain;
background-repeat: no-repeat;
padding: 10px;
}
.postcard-classic-img {
max-height: 100%;
max-width: 100%;
width: 100%;
}
我希望我的图像从左上角开始。如果它是一个非常小的图片,它应该被夹在左上角,如果它太大我希望不显示溢出。如果图像太大,让我们举个例子。我的目标是:
其中蓝色是图像,红色是 div。如果图片太大,我从上面发布的代码中得到的结果如下:
我该如何解决这个问题?
编辑
这就是我现在有多远:jsfiddle .如您所见,图像不是从左上角开始的,溢出也没有隐藏。
最佳答案
您想要的行为应该是 div 中背景图像的默认行为。所以你应该只需要 backround-image 和 background-repeat 属性。
.postcard-classic {
background-image: url("../img/frames/postcard_00.png");
background-repeat: no-repeat;
}
关于css - 如何在不拉伸(stretch)或固定图像的情况下将图像保留在 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32208341/
我是一名优秀的程序员,十分优秀!