css - 查找占据所有宽度但仅占页面高度 25% 的图像

标签 css image

<分区>


想改进这个问题吗? 添加细节并通过 editing this post 澄清问题。

关闭 5 年前

我找不到能够占据整个页面宽度且仅占页面高度 25% 的图片。一旦我更改了图像的 css,它看起来很奇怪,因为它的宽度与高度相比太大了。有人知道我在哪里可以找到适用于此的图像吗?或者我应该如何处理?

提前谢谢你,

最佳答案

您有很多方法可以解决这个问题。一个简单的方法是使用 background-image属性而不是 <img>标签。

例如:

div.image {
  background-image: url('your/img/path.jpg');
  background-repeat: no-repeat;
  background-size: cover (will cover all the div surface) || contain (will fully contains your image);
}

fiddle :

div.image {
  display: block;
  width: 100%;
  height: 50vh;
  
  background-image: url('http://musiccitiessummit.com/wp-content/uploads/chicago-1.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  
  background-position: center center;
}
<div class="image"></div>

编辑 如果您必须使用 <img>标签,看看这个object-fit属性(property):https://css-tricks.com/almanac/properties/o/object-fit/

关于css - 查找占据所有宽度但仅占页面高度 25% 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45635548/

上一篇:Css/Pseudo element::before 在他的容器后面不起作用

下一篇:html - 如何将元素锁定到容器(方形)中的 div 底部?

相关文章:

python - matplotlib 中图像网格上的两个颜色条

jquery - 面板组悬停将 Img 更改为另一个

c++ - 从 PNG 数据到 tesseract

HTML5/CSS - 将大图像放入较小的 DIV

image - iTextSharp 水平多页超宽图像

html - 右对齐标签并向左浮动文本,使它们在 bootstrap 中出现在同一行

html - 带有悬停的两列

ios - 如何为 iPhone 使用锐化图像

html - 为什么我的粘性导航仅在标题设置为内联时才有效?

ios - @Font-face 不适用于 IOS