CSS 圖片
本章節將為大家介紹如何使用CSS 來佈局圖片。
圓角圖片
縮略圖
我們使用border
屬性來創建縮略圖。
實例
img {
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
}
< img src="paris.jpg" alt="Paris" >
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
}
< img src="paris.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="paris.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="paris.jpg" alt="Paris" >
< /a >
嘗試一下»
響應式圖片
響應式圖片會自動適配各種尺寸的屏幕。
實例中,你可以通過重置瀏覽器大小查看效果:
如果你需要自由縮放圖片,且圖片放大的尺寸不大於其原始的最大值,則可使用以下代碼:
提示: Web響應式設計更多內容可以參考CSS響應式設計教程 。
圖片文本
如何定位圖片文本:
卡片式圖片
實例
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;
}
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%);
}
-webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */
filter: grayscale(100%);
}
嘗試一下»
提示:訪問CSS濾鏡參考手冊查看更多內容。
響應式圖片相冊
實例
.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%;
}
}
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(模態)
本實例演示瞭如何結合CSS 和JavaScript 來一起渲染圖片。
首先,我們使用CSS 來創建modal 窗口(對話框), 默認是隱藏的。
然後,我們使用JavaScript 來顯示模態窗口,當我們點擊圖片時,圖片會在彈出的窗口中顯示:
實例
//獲取模態窗口
var modal = document.getElementById( 'myModal' );
//獲取圖片模態框,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" ;
}
var modal = document.getElementById( 'myModal' );
//獲取圖片模態框,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" ;
}
嘗試一下»