Responsive vector backgrounds
Scalable Vector Graphics (SVG) are a great way to implement simple yet great-looking backgrounds for our responsive web pages.
By manually specifying the vectors’ coordinates we can generate fitting geometrical shapes over which we have full control. And the best is, the SVG code can be embedded right within our stylesheet so now additional HTTP requests are issued.
The markup for the background can be extremely concise:
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'>
<path d='M1 1 L9 1 L9 9 L1 9' fill='yellow' />
Like this yellow rectangle. It can be inserted into CSS using a data URL:
background-image: url("data:image/svg+xml;utf8, <svg>...</svg>");
The UTF-8 encoding means the code will be human-readable and editable.
With a few additional rules the background will scale to fit any container:
background-position: center center;
background-size: 100% 100%, auto;
To use just add the classes to your HTML element:
<div class="bg cool-shape">Element with SVG background</div>
Check out the companion project for this article at https://github.com/coderdiego/svg-bg-demo