CSS Advanced
Discover how to use CSS to style photos.
Create rounded images by using the border-radius property:
Rounded Image:
img {
border-radius: 8px;
}
Circled Image:
img {
border-radius: 50%;
}
Make thumbnail images by utilizing the border property.
img {
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
width: 150px;
}

Images that are responsive will change to fit the screen size on their own.
To observe the effect, resize the browser window:
Add the following if you want an image to scale down when necessary but never up to be greater than its original size:
img {
max-width: 100%;
height: auto;
}
Tip: Read more about Responsive Web Design in our CSS RWD Tutorial.
To center an image, make it a block element and set the left and right margins to auto:
img {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
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;
}
The range of values for the opacity property is 0.0 to 1.0. The more transparent, the lower the value:
img {
opacity: 0.5;
}
How to arrange text within a picture:
An element can have visual effects (such saturation and blur) added to it using the CSS filter property.
Note: Neither Edge 12 nor Internet Explorer implement the filter property.
Make all of the images 100% grayscale and black and white:
img {
filter: grayscale(100%);
}
An element can have visual effects (such saturation and blur) added to it using the CSS filter property.
Note: Neither Edge 12 nor Internet Explorer implement the filter property.
Construct a hovering overlay effect:
Drag the mouse pointer over the image:
img:hover {
transform: scaleX(-1);
}
Image galleries can be made with CSS. In this example, the photos are rearranged on various screen sizes using media queries. To observe the effect, resize the browser window:
.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%;
}
}
This is an illustration of how JavaScript and CSS may cooperate.
First, make a dialog box (modal window) with CSS and set its default hiding behavior.
Next, when a user clicks on the image, use JavaScript to show the modal window and display the image inside of it:
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
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;
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";
}
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.