本章節(jié)將為大家介紹如何使用 CSS 來布局圖片。


圓角圖片

實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
<style>
img {
    border-radius: 8px;
}
</style>
</head>
<body>
<h2>圓角圖片</h2>
<p>使用 border-radius 屬性來創(chuàng)建圓角圖片:</p>
<img src="https://img.php.cn/upload/course/000/000/004/580989ec73194566.jpg" alt="Paris" width="400" height="300">
</body>
</html>

實例

橢圓形圖片:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
<style>
img {
    border-radius: 50%;
}
</style>
</head>
<body>
<h2>橢圓形圖片</h2>
<p>使用 border-radius 屬性來創(chuàng)建橢圓形圖片:</p>
<img src="https://img.php.cn/upload/course/000/000/004/580989ec73194566.jpg" alt="Paris" width="400" height="300">
</body>
</html>



縮略圖

我們使用 border 屬性來創(chuàng)建縮略圖。

實例

img {
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
}
<img src="https://img.php.cn/upload/course/000/000/004/580989ec73194566.jpg" alt="Paris">

實例

a {
    display: inline-block;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
    transition: 0.3s;
}
a:hover {
    box-shadow: 0 0 2px 1px rgba
    (0, 140, 186, 0.5);
}
<a href="paris.jpg">
  <img src="https://img.php.cn/upload/course/000/000/004/580989ec73194566.jpg" alt="Paris">
</a>


響應(yīng)式圖片

響應(yīng)式圖片會自動適配各種尺寸的屏幕。

實例中,你可以通過重置瀏覽器大小查看效果:

Norway

如果你需要自由縮放圖片,且圖片放大的尺寸不大于其原始的最大值,則可使用以下代碼:

實例

img {
    max-width: 100%;
    height: auto;
}

圖片文本

如何定位圖片文本:

實例

Norway
左下角
左上角
右上角
右下角
居中

卡片式圖片

實例

div.polaroid {
    width: 80%;
    background-color: white;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
img {width: 100%}
div.container {
    text-align: center;
    padding: 10px 20px;
}

圖片濾鏡

CSS filter 屬性用為元素添加可視效果 (例如:模糊與飽和度) 。

注意: Internet Explorer或 Safari 5.1 (及更早版本) 不支持該屬性。

實例

修改所有圖片的顏色為黑白 (100% 灰度):

img 
{
    
-webkit-filter:
 grayscale(100%);
 
/* Chrome, Safari, 
Opera */
     
    
filter:
 grayscale(100%);
}


響應(yīng)式圖片相冊

實例

.responsive {
    padding: 0 6px;
    float: left;
    width: 24.99999%;
}
@media only screen and (max-width: 700px){
    .responsive {
        width: 49.99999%;
        margin: 6px 0;
    }
}
@media only screen and (max-width: 500px){
    .responsive {
        width: 100%;
    }
}



圖片 Modal(模態(tài))

本實例演示了如何結(jié)合 CSS 和 JavaScript 來一起渲染圖片。

首先,我們使用 CSS 來創(chuàng)建 modal 窗口 (對話框), 默認是隱藏的。

然后,我們使用 JavaScript 來顯示模態(tài)窗口,當我們點擊圖片時,圖片會在彈出的窗口中顯示:

實例

// 獲取模態(tài)窗口

var modal = document.getElementById('myModal');
// 獲取圖片模態(tài)框,alt 屬性作為圖片彈出中文本描述
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    modalImg.alt = this.alt;
    captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
    modal.style.display = "none";
}