如何使一个位于 div 内的图像不拉伸?

huangapple go评论99阅读模式
英文:

How to make an image inside a div not strech?

问题

Sure, here's the translated content:

我正在制作一个页眉,其中包含一个图像(目前只关心左侧的图像),该图像设置了最大宽度并且可以响应性地更改大小。一旦我没有为任何元素设置高度,页眉的高度将取决于其中最大的元素。通常情况下,图像是页眉的较大元素,但是当页眉的高度开始受到导航栏的限制时,图像开始拉伸以保持最大宽度,但同时保持与页眉相同的高度。

我希望图像变小,以保持纵横比并适应页眉在变小时的尺寸。

大屏幕上的页眉

小屏幕上的页眉

图像被拉伸

  1. <!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->
  2. <!-- 语言: lang-css -->
  3. header {
  4. background-color: #030052;
  5. padding: 1.5em;
  6. display: flex;
  7. justify-content: space-between;
  8. }
  9. .logo {
  10. max-width: 22%;
  11. }
  12. nav {
  13. width: 23%;
  14. margin: 0;
  15. display: flex;
  16. flex-direction: column;
  17. }
  18. <!-- 语言: lang-html -->
  19. <header class="flex">
  20. <img class="logo" src="./images/movon.png" alt="MovOn Logo">
  21. <nav>
  22. <h2 class="inv">导航</h2>
  23. <ul>
  24. <li><a href="./index.html" title="主页">主页</a></li>
  25. <li><a href="./pages/explore.html" title="探索页面">探索</a></li>
  26. <li><a href="./pages/mylist.html" title="我的列表">我的列表</a></li>
  27. </ul>
  28. </nav>
  29. <div class="profile-section">
  30. <div class="login-container">
  31. <a class="login-link subtext" href="./pages/login.html" title="登录页面">登录</a>
  32. <a class="login-link subtext" href="./pages/login.html" title="登录页面">登陆</a>
  33. </div>
  34. <img class="icon profile-icon" src="./images/accountpicture.png" alt="个人资料图标">
  35. </div>
  36. </header>
  37. <!-- 结束代码段 -->
  38. 我尝试过删除并更改图像的宽度,并尝试为其设置高度和最大高度(但完全没有更改),但都没有起作用。
  39. <details>
  40. <summary>英文:</summary>
  41. I am making a header and there is an image inside it (I&#39;m only worried with the left one for now) that is set with a max-width and changes size responsively. Once I haven&#39;t set height for anything, the header&#39;s height is dependent on the biggest element inside it. The image is normally the bigger element of the header, however, when the header&#39;s height starts to be limited by the nav, the image starts to stretch to keep the max-width, but also keeping it the same height as the header.
  42. I wanted the image to get smaller so it keeps the aspect ratio and fits inside the header when it gets smaller.
  43. [The header on a bigger screen](https://i.stack.imgur.com/7XxGR.png)
  44. [The header on a smaller screen](https://i.stack.imgur.com/5waQF.png)
  45. [The image stretched](https://i.stack.imgur.com/WNXiU.png)
  46. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  47. &lt;!-- language: lang-css --&gt;
  48. header {
  49. background-color: #030052;
  50. padding: 1.5em;
  51. display: flex;
  52. justify-content: space-between;
  53. }
  54. .logo {
  55. max-width: 22%;
  56. }
  57. nav {
  58. width: 23%;
  59. margin: 0;
  60. display: flex;
  61. flex-direction: column;
  62. }
  63. &lt;!-- language: lang-html --&gt;
  64. &lt;header class=&quot;flex&quot;&gt;
  65. &lt;img class=&quot;logo&quot; src=&quot;./images/movon.png&quot; alt=&quot;MovOn Logo&quot;&gt;
  66. &lt;nav&gt;
  67. &lt;h2 class=&quot;inv&quot;&gt;Navigation&lt;/h2&gt;
  68. &lt;ul&gt;
  69. &lt;li&gt;&lt;a href=&quot;./index.html&quot; title=&quot;Home Page&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  70. &lt;li&gt;&lt;a href=&quot;./pages/explore.html&quot; title=&quot;Explore Page&quot;&gt;Explore&lt;/a&gt;&lt;/li&gt;
  71. &lt;li&gt;&lt;a href=&quot;./pages/mylist.html&quot; title=&quot;My list&quot;&gt;My List&lt;/a&gt;&lt;/li&gt;
  72. &lt;/ul&gt;
  73. &lt;/nav&gt;
  74. &lt;div class=&quot;profile-section&quot;&gt;
  75. &lt;div class=&quot;login-container&quot;&gt;
  76. &lt;a class=&quot;login-link subtext&quot; href=&quot;./pages/login.html&quot; title=&quot;Sign In Page&quot;&gt;Sign In&lt;/a&gt;
  77. &lt;a class=&quot;login-link subtext&quot; href=&quot;./pages/login.html&quot; title=&quot;LogIn Page&quot;&gt;Login&lt;/a&gt;
  78. &lt;/div&gt;
  79. &lt;img class=&quot;icon profile-icon&quot; src=&quot;./images/accountpicture.png&quot; alt=&quot;Profile Icon&quot;&gt;
  80. &lt;/div&gt;
  81. &lt;/header&gt;
  82. &lt;!-- end snippet --&gt;
  83. I&#39;ve tried removing and changing the width on the image and tried setting a height and max-height to it (which changed nothing at all), but nothing worked.
  84. </details>
  85. # 答案1
  86. **得分**: 1
  87. Flexbox的`align-items`属性默认设置为`stretch`,因此图像将始终被拉伸。将图像上的`align-self`设置为center(或者如果您想将其对齐到顶部,则设置为start),以强制该元素不拉伸。有关详细信息,请参见[MDN][2]。
  88. ```css
  89. header {
  90. background-color: #030052;
  91. padding: 1.5em;
  92. display: flex;
  93. justify-content: space-between;
  94. }
  95. .logo {
  96. max-width: 22%;
  97. align-self: center; /* 添加了这一行 */
  98. }
  99. nav {
  100. width: 23%;
  101. margin: 0;
  102. display: flex;
  103. flex-direction: column;
  104. }
  1. <header class="flex">
  2. <img class="logo" src="https://placekitten.com/100/100" alt="MovOn Logo">
  3. <nav>
  4. <h2 class="inv">Navigation</h2>
  5. <ul>
  6. <li><a href="./index.html" title="Home Page">Home</a></li>
  7. <li><a href="./pages/explore.html" title="Explore Page">Explore</a></li>
  8. <li><a href="./pages/mylist.html" title="My list">My List</a></li>
  9. </ul>
  10. </nav>
  11. <div class="profile-section">
  12. <div class="login-container">
  13. <a class="login-link subtext" href="./pages/login.html" title="Sign In Page">Sign In</a>
  14. <a class="login-link subtext" href="./pages/login.html" title="LogIn Page">Login</a>
  15. </div>
  16. <img class="icon profile-icon" src="https://picsum.photos/id/237/200/200" alt="Profile Icon">
  17. </div>
  18. </header>
英文:

Flexbox's align-items property is set to stretch as a default so the image will always stretch up. Set align-self to center (or start if you want it alignt to the top) on the image to force that element not to stretch. See MDN for details

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. header {
  2. background-color: #030052;
  3. padding: 1.5em;
  4. display: flex;
  5. justify-content: space-between;
  6. }
  7. .logo {
  8. max-width: 22%;
  9. align-self: center; /* addded this */
  10. }
  11. nav {
  12. width: 23%;
  13. margin: 0;
  14. display: flex;
  15. flex-direction: column;
  16. }

<!-- language: lang-html -->

  1. &lt;header class=&quot;flex&quot;&gt;
  2. &lt;img class=&quot;logo&quot; src=&quot;https://placekitten.com/100/100&quot; alt=&quot;MovOn Logo&quot;&gt;
  3. &lt;nav&gt;
  4. &lt;h2 class=&quot;inv&quot;&gt;Navigation&lt;/h2&gt;
  5. &lt;ul&gt;
  6. &lt;li&gt;&lt;a href=&quot;./index.html&quot; title=&quot;Home Page&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  7. &lt;li&gt;&lt;a href=&quot;./pages/explore.html&quot; title=&quot;Explore Page&quot;&gt;Explore&lt;/a&gt;&lt;/li&gt;
  8. &lt;li&gt;&lt;a href=&quot;./pages/mylist.html&quot; title=&quot;My list&quot;&gt;My List&lt;/a&gt;&lt;/li&gt;
  9. &lt;/ul&gt;
  10. &lt;/nav&gt;
  11. &lt;div class=&quot;profile-section&quot;&gt;
  12. &lt;div class=&quot;login-container&quot;&gt;
  13. &lt;a class=&quot;login-link subtext&quot; href=&quot;./pages/login.html&quot; title=&quot;Sign In Page&quot;&gt;Sign In&lt;/a&gt;
  14. &lt;a class=&quot;login-link subtext&quot; href=&quot;./pages/login.html&quot; title=&quot;LogIn Page&quot;&gt;Login&lt;/a&gt;
  15. &lt;/div&gt;
  16. &lt;img class=&quot;icon profile-icon&quot; src=&quot;https://picsum.photos/id/237/200/200&quot; alt=&quot;Profile Icon&quot;&gt;
  17. &lt;/div&gt;
  18. &lt;/header&gt;

<!-- end snippet -->

答案2

得分: 0

I think the display flex property causes the image to be squashed. I would put a container around the image. Then the image always scales proportionally.

  1. 我认为display flex属性导致图像被压缩。我会在图像周围放置一个容器。然后图像始终按比例缩放。
英文:

I think the display flex property causes the image to be squashed. I would put a container around the image. Then the image always scales proportionally.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

  1. header {
  2. background-color: #030052;
  3. padding: 1.5em;
  4. display: flex;
  5. justify-content: space-between;
  6. }
  7. .logowrap {
  8. max-width: 22%;
  9. }
  10. .logo {
  11. max-width: 100%;
  12. }
  13. nav {
  14. width: 23%;
  15. margin: 0;
  16. display: flex;
  17. flex-direction: column;
  18. }

<!-- language: lang-html -->

  1. &lt;header class=&quot;flex&quot;&gt;
  2. &lt;div class=&quot;logowrap&quot;&gt;
  3. &lt;img class=&quot;logo&quot; src=&quot;https://place-hold.it/200x200&quot; alt=&quot;MovOn Logo&quot;&gt;
  4. &lt;/div&gt;
  5. &lt;nav&gt;
  6. &lt;h2 class=&quot;inv&quot;&gt;Navigation&lt;/h2&gt;
  7. &lt;ul&gt;
  8. &lt;li&gt;&lt;a href=&quot;./index.html&quot; title=&quot;Home Page&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
  9. &lt;li&gt;&lt;a href=&quot;./pages/explore.html&quot; title=&quot;Explore Page&quot;&gt;Explore&lt;/a&gt;&lt;/li&gt;
  10. &lt;li&gt;&lt;a href=&quot;./pages/mylist.html&quot; title=&quot;My list&quot;&gt;My List&lt;/a&gt;&lt;/li&gt;
  11. &lt;/ul&gt;
  12. &lt;/nav&gt;
  13. &lt;div class=&quot;profile-section&quot;&gt;
  14. &lt;div class=&quot;login-container&quot;&gt;
  15. &lt;a class=&quot;login-link subtext&quot; href=&quot;./pages/login.html&quot; title=&quot;Sign In Page&quot;&gt;Sign In&lt;/a&gt;
  16. &lt;a class=&quot;login-link subtext&quot; href=&quot;./pages/login.html&quot; title=&quot;LogIn Page&quot;&gt;Login&lt;/a&gt;
  17. &lt;/div&gt;
  18. &lt;img class=&quot;icon profile-icon&quot; src=&quot;https://place-hold.it/200x200&quot; alt=&quot;Profile Icon&quot;&gt;
  19. &lt;/div&gt;
  20. &lt;/header&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月13日 01:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定