0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【フロントエンド1000本ノック】0071_レスポンシブイメージを実現するため、img要素のwidth: 100%; height: auto;のスタイルがなぜ重要なのかを説明せよ。

Posted at

この記事は人間が書きました

はじめに

こんにちは、赤神です!
この記事は、「1000本ノック」という取り組みの中のフロントエンドのための課題の1つです。

「1000本ノックとは」
https://qiita.com/sora_akagami/items/c8da4296dea3823376ca

width: 100%; height: auto; の役割

<img> 要素にこのスタイルを適用すると、width: 100%; 画像が持つ固有の幅を上書きし、「常に親要素の幅の100%に合わせなさい」という命令になります。親要素が縮めば、画像も一緒に縮むことになります。

height: auto; は、width100% に指定されたことで、高さも無理やり引き伸ばされると比率が崩れてしまいます。height: auto; は「高さは width に合わせて、元の画像の縦横比を保ったまま自動で調整しなさい」という命令です。

作成した回答

この2つのスタイルを組み合わせることで、画像は親要素いっぱいに広がりつつ、アスペクト比を維持したまま表示されることになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?