How to make image flex to it's width?

做~自己de王妃 提交于 2019-12-23 17:31:06

问题


how do I prevent my image from being stretched over it's natural 200px width?

footer {
  display: flex;
}
img {
  flex: 1;
  margin: 1em;
}
<footer>
  <img src=http://www.placebacon.net/200/150>
</footer>

回答1:


Take the flex off of the image and set max-width to 100%. This will allow the image to grow to it's max size and shrink if it's parent shrinks. Then add align-items: flex-start to the container. This will allow the aspect ratio to remain intact if it shrinks.

footer {
  display: flex;
  align-items: flex-start;
}
img {
  margin: 1em;
  max-width: 100%;
}
<footer>
  <img src=http://www.placebacon.net/200/150>
</footer>



回答2:


You could toss it into a simple container, and use that for flexing:

<footer>
  <div>
    <img src=http://www.placebacon.net/200/150>
  </div>
</footer>
footer {
  display: flex;
}
div {
  flex: 1;
  margin: 1em;
}
img {
  max-width: 100%;
}


来源:https://stackoverflow.com/questions/39761602/how-to-make-image-flex-to-its-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!