- index.html
- style.css
HTML
<!DOCTYPE html>
<html>
<head>
<title>Flexible Background with preserveAspectRatio</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="background no-viewbox"></div>
<div class="background has-viewbox"></div>
<div class="background has-viewbox-psa-none"></div>
</body>
</html>
CSS
.background {
height: 200px;
background-repeat: no-repeat;
background-position: 50% 0;
background-size: 100% 100%;
border: 2px solid gold;
}
.no-viewbox {
background-image: url("no-viewbox.svg");
}
.has-viewbox {
}
.has-viewbox-psa-none {
}
You’ve gone to a different page
Goalscompleted
0
The background image is assigned to the .no-viewbox
block without a viewbox, and it does not stretch.
- Set a background image address
has-viewbox.svg
for the.has-viewbox
block, and observe how the background with the viewbox behaves: The image fills the available space while maintaining its aspect ratio. - Set a background image address
has-viewbox-psa-none.svg
for the.has-viewbox-psa-none
block. Note how the background image is now stretched without preserving its aspect ratio, and the background is completely flexible.
Comments