响应式弹性盒问题内的图像

时间:2018-03-28 20:31:31

标签: html css flexbox

当屏幕尺寸发生变化时,我试图将植物图像放入柔性盒内,并具有响应的高度和宽度,但它似乎不起作用。此外,它改变了柔性盒的形状,以适应图像尺寸,这不是我想要的。图像应完全适合柔性盒,而不会改变其形状,仍能保持响应。我究竟做错了什么?什么都有帮助,谢谢。



/*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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

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

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

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

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

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

答案 1 :(得分:0)

我不确定您是否在谈论植物图像或个人资料图片,但我认为您的意思是植物。我所做的可能就是你所追求的,我不确定。我刚拿出所有代码来向您展示植物图像如何响应浏览器大小。希望这很有用。我不确定我是否真的回答了你的问题。

&#13;
&#13;
img {
  height: auto;
  width: 100%;
}
&#13;
<img src="https://www.dropbox.com/s/4c05cegbonfqo5k/Photo%202018-03-03%2C%202%2020%2022%20PM.jpg?raw=1">
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这可能只是一个疯狂的猜测,但您是否尝试过使用background-size: contain;代替background-size: cover;

封面属性倾向于调整大小和拉伸图像。