演習(2)レスポンシブWebデザイン
例題2
- 画像をデバイスに最適化する
フルードイメージ
- Fluid Image
- ブラウザのウィンドウ幅に合わせて画像のサイズをフィットさせる方法
- ウィンドウサイズより大きい画像でも、ウィンドウサイズ(または親要素の幅)に応じて、縦横比を保持したまま自動的に画像が拡大・縮小するようにする手法
img { max-width:100%; height:auto; }
《style.css》
@charset "UTF-8"; /* reset */ html, body, div, h1, h2, h3, p, ul, li { margin: 0; padding: 0; line-height: 1.0; font-family: "Hiragino Kaku Gothic ProN", Meiryo, sans-serif; } ul { list-style: none; } a { text-decoration: none; } img { border: none; vertical-align: bottom; } /* font-size */ html { font-size: 62.5%; } /* =10px */ body { font-size: 16px; font-size: 1.6rem; } /* =16px */ h1 { font-size: 24px; font-size: 2.4rem; } h2 { font-size: 20px; font-size: 2.0rem; } h3 { font-size: 16px; font-size: 1.6rem; } p { font-size: 14px; font-size: 1.4rem; } ul { font-size: 14px; font-size: 1.4rem; } /* body */ body { color: #333; line-height: 1.5; background: #FFF url(img/bg.gif) repeat-y center top; } /* layout */ #container { width: 600px; margin: 0 auto; } #header { margin-bottom: 30px; text-align: center; } #content { margin-bottom: 20px; padding: 0 20px; } /* #header */ #header h1 { margin-bottom: 20px; } #header h2 { margin-bottom: 10px; } #header p { text-align: left; margin-left: 48px; margin-left: 4.8rem; line-height: 1.5; } /* #content */ #content h3 { margin-bottom: 10px; } #content p { line-height: 1.5; text-indent: 14px; text-indent: 1.4rem; margin-bottom: 25px; margin-left: 30px; margin-left: 3.0rem; } #content ul { margin-bottom: 25px; padding-left: 16px; padding-left: 1.6rem; } #content li { margin-bottom: 6px; margin-left: 14px; margin-left: 1.4rem; text-indent: -1.4rem; line-height: 1.5; } #content h2 { text-align: center; margin-bottom: 10px; } #content p.center { margin-left: 0; text-align: center; } @media screen and (max-width:640px) { /* font-size */ html { font-size: 62.5%; } body { font-size: 1.4rem; } h2 { font-size: 1.8rem; } h3 { font-size: 1.5rem; } h4 { font-size: 1.4rem; } p { font-size: 1.2rem; } ul { font-size: 1.2rem; } /* images */ img { max-width:100%; height:auto; } /* layout */ #container { width: 90%; } #header { margin-bottom: 1.0rem; } #content { margin-bottom: 1.0rem; padding: 0; } /* #header */ #header h1, #header h2 { margin-bottom: 1.0rem; } #header p { text-align: left; margin-bottom: 1.6rem; margin-left: 1.4rem; line-height: 1.4; } /* #content */ #content h3 { margin-bottom: 1.0rem; } #content p { line-height: 1.4; margin-bottom: 2.0rem; margin-left: 1.4rem; } #content ul { margin-bottom: 2.0rem; padding-left: 1.2rem; } #content li { margin-bottom: 0.6rem; margin-left: 1.2rem; text-indent: -1.2rem; line-height: 1.4; } #content h2 { text-align: center; margin-bottom: 1.0rem; } }
幅100%で表示
@charset "UTF-8"; /* reset */ html, body, div, h1, h2, h3, p, ul, li { margin: 0; padding: 0; line-height: 1.0; font-family: "Hiragino Kaku Gothic ProN", Meiryo, sans-serif; } ul { list-style: none; } a { text-decoration: none; } img { border: none; vertical-align: bottom; } /* font-size */ html { font-size: 62.5%; } /* =10px */ body { font-size: 16px; font-size: 1.6rem; } /* =16px */ h1 { font-size: 24px; font-size: 2.4rem; } h2 { font-size: 20px; font-size: 2.0rem; } h3 { font-size: 16px; font-size: 1.6rem; } p { font-size: 14px; font-size: 1.4rem; } ul { font-size: 14px; font-size: 1.4rem; } /* body */ body { color: #333; line-height: 1.5; background: #FFF url(img/bg.gif) repeat-y center top; } /* layout */ #container { width: 600px; margin: 0 auto; } #header { margin-bottom: 30px; text-align: center; } #content { margin-bottom: 20px; padding: 0 20px; } /* #header */ #header h1 { margin-bottom: 20px; } #header h2 { margin-bottom: 10px; } #header p { text-align: left; margin-left: 48px; margin-left: 4.8rem; line-height: 1.5; } /* #content */ #content h3 { margin-bottom: 10px; } #content p { line-height: 1.5; text-indent: 14px; text-indent: 1.4rem; margin-bottom: 25px; margin-left: 30px; margin-left: 3.0rem; } #content ul { margin-bottom: 25px; padding-left: 16px; padding-left: 1.6rem; } #content li { margin-bottom: 6px; margin-left: 14px; margin-left: 1.4rem; text-indent: -1.4rem; line-height: 1.5; } #content h2 { text-align: center; margin-bottom: 10px; } #content p.center { margin-left: 0; text-align: center; } @media screen and (max-width:640px) { /* font-size */ html { font-size: 62.5%; } body { font-size: 1.4rem; } h2 { font-size: 1.8rem; } h3 { font-size: 1.5rem; } h4 { font-size: 1.4rem; } p { font-size: 1.2rem; } ul { font-size: 1.2rem; } /* images */ img { max-width:100%; height:auto; } /* layout */ #container { width: 100%; } #header { margin-bottom: 1.0rem; } #content { margin-bottom: 1.0rem; padding: 0; } /* #header */ #header h1, #header h2 { margin-bottom: 1.0rem; } #header p { text-align: left; margin: 0 1.4rem 2.0rem 1.4rem; line-height: 1.4; } /* #content */ #content h3 { margin-bottom: 1.0rem; } #content p { line-height: 1.5; margin: 0 1.4rem 2.0rem 1.4rem; } #content ul { margin-bottom: 2.0rem; padding-left: 1.2rem; } #content li { margin: 0 1.2rem 0.6rem 1.2rem; text-indent: -1.2rem; line-height: 1.4; } #content h2 { text-align: center; margin-bottom: 1.0rem; } }