Introduction to Images
To make your website look great, you may want to add a few images. Although there are CSS-tricks all around that allow you to insert icons, and create your own pure-CSS clipart, you may want to import some graphics created from an external program.
Here, we'll look at how to properly adjust images through CSS.
Image Size width, height
To specify the image size, we specify the width
andheight
properties. This way our page will know exactly how much space to leave for the image, resulting in a page that is very quickly loaded.
Here is an example HTML markup, with the corresponding CSS.
#sample {
height: 250px;
}
When using these properties, be sure to scale properly to avoid any distorted images. To allow CSS to automatically select the width or height, simply specify just one of the properties.
Image classes
A common technique web designers can use is giving their image names a class of small, medium and large. This allows users to keep a consistent size scale so that images on a website are conformed.
Changing Image Opacity
It's easy to change the opacity of an image in CSS.
Simply use the opacity
property, and specify a decimal number from 0 to 1.0
We haven't learned pseudo-selectors yet, but just know that the :hover
attribute attached to a selector gives the style when the mouse hovers over the element.
#sample {
opacity: 1;
height: 250px
}
#sample:hover {
opacity: 0.4;
}
Try hovering over the logo to see the transparency!