CSS Images
In this chapter we introduce how to use CSS to lay out images.
Rounded Images
Example
Rounded-corner image:
img
{
border-radius:
8px;
}
Try it »
Example
Elliptical image:
img
{
border-radius:
50%;
}
Try it »
Thumbnails
We use border property to create thumbnails.
Example
img
{
border:
1px solid #ddd;
border-radius:
4px;
padding:
5px;
}
Try it »
Example
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);
}
Try it »
Responsive Images
Responsive images automatically adapt to screens of various sizes.
In the example, you can see the effect by resizing the browser:

If you need to scale an image freely, and the enlarged size is no larger than its original maximum size, you can use the following code:
Example
img
{
max-width:
100%;
height:
auto;
}
Try it »
Tip: For more on Web responsive design, you can refer to the CSS Responsive Design tutorial。
Image Text
How to position image text:
Example
Bottom left
Top left
Top right
Bottom right
Centering
Try it yourself:
Top left »
Top right »
Bottom left »
Bottom right »
Center »
Card-style images
Example
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;
}
Try it »
Image filters
CSS filter The property is used to add visual effects (such as blur and saturation) to elements.
Note: Internet Explorer or Safari 5.1 (and earlier) does not support this property.
Example
Change the color of all images to black and white (100% grayscale):
img
{
-webkit-filter:
grayscale(100%);
/* Chrome, Safari, Opera */
filter:
grayscale(100%);
}
Try it »
Tip: Access the CSS Filter Reference Manual See more content.
Responsive image gallery
Example
.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%;
}
}
Try it »
Image Modal
This example demonstrates how to combine CSS and JavaScript to render images together.
First, we use CSS to create the modal window (dialog), which is hidden by default.
Then we use JavaScript to show the modal window; when we click an image, it is displayed in the popup window:
Example
// Get the modal window
var
modal = document.getElementById(
'myModal'
);
// Get the image modal; the alt attribute serves as the text description in the image popup
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 element that closes the modal
var
span = document.getElementsByClassName(
"close"
)[
0
];
// When the user clicks on (x), close the modal
span.onclick =
function
() {
modal.style.display =
"none"
;
}
Try it »