圖片
響應(yīng)式 Web 設(shè)計(jì) - 圖片
使用 width 屬性
如果 width 屬性設(shè)置為 100%,圖片會(huì)根據(jù)上下范圍實(shí)現(xiàn)響應(yīng)式功能:
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> <style> img { width: 100%; height: auto; } </style> </head> <body> <img src="https://img.php.cn/upload/article/000/000/024/5c668566136f4479.jpg" width="460" height="345"> <p>調(diào)整瀏覽器窗口查看圖像是如何擴(kuò)展的。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
注意在以上實(shí)例中,圖片會(huì)比它的原始圖片大。我們可以使用 max-width 屬性很好的解決這個(gè)問題。
使用 max-width 屬性
如果 max-width 屬性設(shè)置為 100%, 圖片永遠(yuǎn)不會(huì)大于其原始大?。?/p>
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> <style> img { max-width: 100%; height: auto; } </style> </head> <body> <img src="https://img.php.cn/upload/article/000/000/024/5c668566136f4479.jpg" width="460" height="345"> <p>調(diào)整瀏覽器大小,在寬度小于 460px 時(shí)查看圖片比例變化。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
網(wǎng)頁中添加圖片
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> * { box-sizing: border-box; } img { width: 100%; height: auto; } .row:after { content: ""; clear: both; display: block; } [class*="col-"] { float: left; padding: 15px; width: 100%; } @media only screen and (min-width: 600px) { .col-s-1 {width: 8.33%;} .col-s-2 {width: 16.66%;} .col-s-3 {width: 25%;} .col-s-4 {width: 33.33%;} .col-s-5 {width: 41.66%;} .col-s-6 {width: 50%;} .col-s-7 {width: 58.33%;} .col-s-8 {width: 66.66%;} .col-s-9 {width: 75%;} .col-s-10 {width: 83.33%;} .col-s-11 {width: 91.66%;} .col-s-12 {width: 100%;} } @media only screen and (min-width: 768px) { .col-1 {width: 8.33%;} .col-2 {width: 16.66%;} .col-3 {width: 25%;} .col-4 {width: 33.33%;} .col-5 {width: 41.66%;} .col-6 {width: 50%;} .col-7 {width: 58.33%;} .col-8 {width: 66.66%;} .col-9 {width: 75%;} .col-10 {width: 83.33%;} .col-11 {width: 91.66%;} .col-12 {width: 100%;} } html { font-family: "Lucida Sans", sans-serif; } .header { background-color: #9933cc; color: #ffffff; padding: 15px; } .menu ul { list-style-type: none; margin: 0; padding: 0; } .menu li { padding: 8px; margin-bottom: 7px; background-color :#33b5e5; color: #ffffff; box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); } .menu li:hover { background-color: #0099cc; } .aside { background-color: #33b5e5; padding: 15px; color: #ffffff; text-align: center; font-size: 14px; box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); } .footer { background-color: #0099cc; color: #ffffff; text-align: center; font-size: 12px; padding: 15px; } </style> </head> <body> <div class="header"> <h1>Chania</h1> </div> <div class="row"> <div class="col-3 col-s-3 menu"> <ul> <li>The Flight</li> <li>The City</li> <li>The Island</li> <li>The Food</li> </ul> </div> <div class="col-6 col-s-9"> <h1>The City</h1> <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p> <img src="https://img.php.cn/upload/article/000/000/024/5c668566136f4479.jpg" width="460" height="345"> </div> <div class="col-3 col-s-12"> <div class="aside"> <h2>What?</h2> <p>Chania is a city on the island of Crete.</p> <h2>Where?</h2> <p>Crete is a Greek island in the Mediterranean Sea.</p> <h2>How?</h2> <p>You can reach Chania airport from all over Europe.</p> </div> </div> </div> <div class="footer"> <p>調(diào)整瀏覽器窗口大小查看內(nèi)容變化。</p> </div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
背景圖片
背景圖片可以響應(yīng)調(diào)整大小或縮放。
以下是三個(gè)不同的方法:
1、 如果 background-size 屬性設(shè)置為 "contain", 背景圖片將按比例自適應(yīng)內(nèi)容區(qū)域。圖片保持其比例不變:
這是 CSS 代碼:
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> <style> div { width: 100%; height: 400px; background-image: url('https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg'); background-repeat: no-repeat; background-size: contain; border: 1px solid red; } </style> </head> <body> <p>調(diào)整瀏覽器大小查看效果。</p> <div></div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
2. 如果 background-size 屬性設(shè)置為 "100% 100%" ,背景圖片將延展覆蓋整個(gè)區(qū)域:
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> <style> div { width: 100%; height: 400px; background-image: url('https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg'); background-size: 100% 100%; border: 1px solid red; } </style> </head> <body> <p>調(diào)整瀏覽器大小查看效果。</p> <div></div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
3. 如果 background-size 屬性設(shè)置為 "cover",則會(huì)把背景圖像擴(kuò)展至足夠大,以使背景圖像完全覆蓋背景區(qū)域。注意該屬性保持了圖片的比例因此 背景圖像的某些部分無法顯示在背景定位區(qū)域中。
這是 CSS 代碼:
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> <style> div { width: 100%; height: 400px; background-image: url('https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg'); background-size: cover; border: 1px solid red; } </style> </head> <body> <p>調(diào)整瀏覽器大小查看效果。</p> <div></div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
不同設(shè)備顯示不同圖片
大尺寸圖片可以顯示在大屏幕上,但在小屏幕上確不能很好顯示。我們沒有必要在小屏幕上去加載大圖片,這樣很影響加載速度。所以我們可以使用媒體查詢,根據(jù)不同的設(shè)備顯示不同的圖片。
以下大圖片和小圖片將顯示在不同設(shè)備上:
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> <style> /* For width smaller than 400px: */ body { background-repeat: no-repeat; background-image: url('https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg'); } /* For width 400px and larger: */ @media only screen and (min-width: 400px) { body { background-image: url('https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg'); } } </style> </head> <body> <p style="margin-top:360px;">調(diào)整瀏覽器寬度,背景圖片在小于 400 px 時(shí)將改變。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
你可以使用媒體查詢的 min-device-width 替代 min-width 屬性,它將檢測(cè)的是設(shè)備寬度而不是瀏覽器寬度。瀏覽器大小重置時(shí),圖片大小不會(huì)改變。
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> <style> /* For device width smaller than 400px: */ body { background-repeat: no-repeat; background-image: url('https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg'); } /* For device width 400px and larger: */ @media only screen and (min-device-width: 400px) { body { background-image: url('https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg'); } } </style> </head> <body> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
HTML5 <picture> 元素
HTML5 的 <picture>
元素可以設(shè)置多張圖片。
瀏覽器支持
函數(shù) | ![]() | ![]() | ![]() | ![]() | ![]() |
:visited | 不支持 | 38.0 | 38.0 | 不支持 | 25.0 |
<picture>
元素類似于 <video>
和<audio>
元素??梢栽O(shè)備不同的資源,第一個(gè)設(shè)置的資源為首選使用的:
實(shí)例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文網(wǎng)</title> </head> <body> <picture> <source srcset="https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg" media="(max-width: 400px)"> <source srcset="https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg"> <img src="https://img.php.cn/upload/article/000/000/024/5c66874932cfb699.jpg" alt="Flowers" style="width:auto;"> </picture> <p>調(diào)整瀏覽器寬度和高度,背景在寬度小于 400px 時(shí)將改變。 </p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
srcset
屬性的必須的,定義了圖片資源。
media
屬性是可選的,可以在媒體查詢的CSS @media 規(guī)則 查看詳情。
對(duì)于不支持 <picture>
元素的瀏覽器你也可以定義 <img>
元素來替代。