CSS images
CSS images
This chapter will introduce how to use CSS to layout images.
Rounded corner picture
Thumbnails
We use theborder
attribute to create thumbnails.
Instance
img { border: 1px solid #ddd; border-radius: 4px; padding: 5px; }
Instance
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); }
Responsive images
Responsive images will automatically adapt to screens of various sizes.
In the example, you can check the effect by resetting the browser size:
If you need to freely scale the image, and the enlarged size of the image is not larger than its size For the original maximum value, you can use the following code:
Instance
img { max-width: 100%; height: auto; }
Picture text
How to position Image text:
Example
Card style picture
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; }
Image Filter
CSSfilter
Attributes are used to add visual effects (such as blur and saturation) to elements.
Note:This attribute is not supported by Internet Exploreror Safari 5.1 (and earlier).
Example
Change the color of all pictures to black and white (100% grayscale):
img { -webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */ filter: grayscale(100%); }
Responsive picture album
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%; } }
##Picture Modal (modal) This example demonstrates how to combine CSS and JavaScript to render images together. First, we use CSS to create a modal window (dialog box), which is hidden by default. Then, we use JavaScript to display the modal window. When we click on the image, the image will be displayed in the pop-up window: