gpt4 book ai didi

CSS:获取占位符图像占用DIV的整个大小而不会失真

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:07 26 4
gpt4 key购买 nike

我想将 png 作为背景图像放置在充当占位符的 div 中: https://jsbin.com/hohegiyazu/edit?html,css,output

代码:

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css" rel="stylesheet">

.container {
display: flex;
flex-direction: column;
}

.row {
display: flex;
flex-direction: row;
}

.placeholder {
background-image:url(/images/placeholder.png);
background-size:100% 100%;
height:50px;
flex:1;
}

</style>
</head>
<body>
<div class="container">

<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>

<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
</div>
</div>
</body>
</html>

我得到这样的效果: svg example

图片根据元素的宽度扭曲,左右边距不相等。

我想实现这样的效果: enter image description here

这可能吗?

最佳答案

一种方法是将 background-size 属性更改为 cover

.item {
background-size: cover;
}

它将确保图像覆盖整个背景,没有拉伸(stretch),尽管这仍然在很大程度上取决于图像的实际外观 以及使用它的元素的设置方式


问题编辑后更新

通过定位,即 center center,您可以控制图像的哪一部分更有值(value),并将其居中或左对齐或...

.container {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.placeholder {
background: url(http://placehold.it/300) center center;
background-size: cover;
height:50px;
flex:1;
margin: 1px;
}
<div class="container">

<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>

<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
</div>
</div>


在第 2 个问题编辑后更新

您可以使用伪元素,没有额外的标记,带有背景色和圆 Angular 。这将为您节省额外的 http 请求,更快地加载页面(强烈推荐),并且您可以轻松更改颜色和边框。

如果你真的需要一个图像,你可以使用图像

.container {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.placeholder {
position: relative;
height:50px;
flex:1;
margin: 1px;
padding: 5px;
color: red;
}
.placeholder::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
border-radius: 14px;
z-index: -1;
background: lightgray;
/* with image
background: url(http://placehold.it/300) center center / cover;
*/
}
<div class="container">

<div class="row">
<div class="placeholder">Hey, there</div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>

<div class="row">
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
<div class="row">
<div class="placeholder"></div>
</div>
</div>

关于CSS:获取占位符图像占用DIV的整个大小而不会失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38371521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com