gpt4 book ai didi

html - 响应式 flexbox 问题中的图像

转载 作者:太空宇宙 更新时间:2023-11-04 07:29:42 24 4
gpt4 key购买 nike

当屏幕尺寸发生变化时,我试图让植物图像适应具有响应高度和宽度的 flexbox ,但它似乎不起作用。它还改变了 flex box 的形状以适应图像大小,这不是我想要的。图像应该完全适合 flex box 而不改变它的形状并且仍然是响应式的。我究竟做错了什么?任何帮助,谢谢。

/*Fonts*/
@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,600);
@import url(https://use.fontawesome.com/releases/v5.0.8/css/all.css);

/*Body*/
body {
font-family: "Open Sans", Arial, sans-serif;
background-color: #fefefe;
padding-bottom: 3rem;}

/*Profile Picture*/
.profile-picture {
display: flex;
margin-top: 55px;
margin-bottom: 35px;
display: inline-block;
width: 125px;
height: 125px;
border-radius: 50%;
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-color: #f1f1f1;}
.image {
background-image: url('https://www.dropbox.com/s/8dtg60sbha99x2o/Photo%202018-02-05%2C%209%2026%2043%20AM.jpg?raw=1');}

/*Profile Name*/
.profile-name {
text-align:center;
margin-top:-20px;
margin-bottom:35px;
font-weight:bold;}

/*User Name*/
.user-name {
text-align:center;
margin-top:-30px;
margin-bottom:35px;
color:rgba(1,1,1,0.35);}

/*Follow Button*/
.follow-button-position {
text-align:center;
margin-bottom:55px;
margin-top:-10px;}
.follow-button {
text-align:center;
padding-top:7.5px;
padding-bottom:7.5px;
padding-left:25px;
padding-right:25px;
border-radius:2px;
background-color:rgba(1,1,1,0);
border-style:solid;
border-color:#af985a;
border-width:1px;
color:#af985a;}
.follow-button:hover {
cursor:pointer;}

/*Posts*/
.flex-container {
display: flex;
justify-content: center;}
.flex-post {
background-color: #f1f1f1;
text-align: center;
line-height: 75px;
font-size: 30px;
flex: 1 0 auto;
height: auto;
max-width: 275px;
align-items: center;
justify-content: center;
display: flex;}
.flex-post:before {
content: '';
float: left;
padding-top: 100%;}
.flex-post:hover {
background-color: rgba(1, 1, 1, 0.5);}


.heart{
position: relative;
width: 55px;
height: 45px;
float: left;
}
.heart:before,
.heart:after{
position: absolute;
content: "";
left: 24px;
top: 0;
width: 25px;
height: 40px;
background: white;
-moz-border-radius: 25px 25px 0 0;
border-radius: 25px 25px 0 0;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transform-origin: 0 100%;
-moz-transform-origin: 0 100%;
-ms-transform-origin: 0 100%;
-o-transform-origin: 0 100%;
transform-origin: 0 100%;
}
.heart:after{
left: 0;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transform-origin: 100% 100%;
-moz-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
-o-transform-origin: 100% 100%;
transform-origin :100% 100%;
}

img {max-width:100%;}
<div class="flex-container">
<div class="profile-picture image">
</div>
</div>

<div class="profile-name">
<p>Cole Gwoz</p>
</div>

<div class="user-name">
<p>colegwoz</p>
</div>

<div class="follow-button-position">
<button class="follow-button">Follow</button>
</div>

<div class="flex-container">
<div class="flex-post">
<img src="https://www.dropbox.com/s/4c05cegbonfqo5k/Photo%202018-03-03%2C%202%2020%2022%20PM.jpg?raw=1">
</div>
<div class="flex-post"></div>
<div class="flex-post"></div>
</div>

<div class="flex-container">
<div class="flex-post"></div>
<div class="flex-post"></div>
<div class="flex-post"></div>
</div>

<div class="flex-container">
<div class="flex-post"></div>
<div class="flex-post"></div>
<div class="flex-post"></div>
</div>

最佳答案

您可以将容器设置为相对位置,将元素设置为绝对位置。因为它是伪元素,所以在您的示例中保存每个框中的尺寸。

.flex-post {
...
position: relative;
overflow: hidden;
}

img {
position: absolute;
max-width: 100%;
}

此外,如果您需要图像始终覆盖每个框,无论图像大小如何,您可以使用object-fit。属性(property)。或者,使用背景而不是内联图像以获得更好的浏览器支持 background-size: cover;

img {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
}

关于html - 响应式 flexbox 问题中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49543656/

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