Friday, June 26, 2020

How to Enhance Your Website Layouts With SVG Shapes

In this new tutorial, we’ll get rid of the usual boring rectangular boxes and learn to build a landing page full of complex SVG shapes. We’ll even enhance it with Bootstrap styles as well as a full-screen background video. 

The best way to understand what we’re going to build is by checking out the demo, so it’s all yours:

Shall we start?

1. Begin With the Necessary Assets

To speed up the development process, I’ve included Bootstrap 4 in the pen. 

Pen dependencies

If you check the markup, you’ll notice that I heavily use Bootstrap’s helper classes. Although not necessary, in case you aren’t familiar with Bootstrap, take a minute to look at its documentation or my other Bootstrap tutorials.


2. Continue With the Page Markup

Our page will consist of an SVG and three sections:

Note: In the demo, there’s also a simple footer layout which I won’t discuss in the tutorial.

3. Create the SVG Sprite

For our layout, we’ll need a bunch of SVG shapes (waves, curves, whatever you’d like to call them). To create them, we won’t use an editing program like Adobe Illustrator or Sketch as you might expect; we’ll generate them by taking advantage of an awesome online tool made by z creative labs

Tool for generating SVG waves

Pretty cool, right? No need to spend time manually creating them in a program (though obviously you can if you prefer). All we need is to experiment a bit with the options of this tool until we end up with the desired SVGs.

After grabbing the generated code for each SVG, we’ll create a sprite by wrapping them within an SVG container. This is a technique that I’ve discussed many times in the past.

The generated SVG code
The generated SVG shape code

Note: Depending on the layout, we might have to manually change the SVG’s path color. 

We’ll place each icon inside a symbol element with a unique ID and a viewbox attribute which will specify its dimensions. Also, we’ll let it scale without keeping an eye on its aspect ratio thanks to the preserveAspectRatio="none" attribute value. Finally, we’ll render the target icon on the screen via the use element.

Here’s the markup needed for the SVG sprite:


4. Define Some Basic Styles

Beyond Bootstrap’s default styles, let’s define some additional ones. We’ll also override some of Bootstrap’s button styles to cover our needs:


5. Build the First Section

The first section will include: 

  • A full screen background video.
  • An overlay with a blockquote which will sit on top of the video.
  • An absolutely positioned SVG shape at the bottom of this section which will give it a diagonal look and feel.

Here’s its appearance:

The layout of the first section

Section #1 HTML

Tip: Instead of using an SVG shape, we can create diagonal layouts with other methods like CSS transforms (mainly with the skew() function) and the clip-path property.

Section #1 CSS

As we’ve already discussed this section will be fullscreen. With that in mind, let’s give it height: 100vh. Unfortunately, this unit isn’t still stable across all devices. So, as a best practice, we’ll keep it as a fallback and let JavaScript calculate the section height based on the window height.

Here are all styles that we’ll need:

Section #3 JavaScript


6. Build the Second Section

The second section will include four equal-width columns. Inside each column, we’ll put a card which will contain:

  • An image.
  • Its associated caption.
  • An absolutely positioned SVG shape placed at the bottom part of the image. That way, each image will have a unique form.

To create the columns, we’ll take advantage of Bootstrap’s card component.

The number of columns will change depending on the screen size. With that said, on extra-large screens, the section should look like this:

The layout of the second section on extra large screens

On medium and large screens though, it should appear as follows:

The layout of the second section on medium and large screens

Finally, on small screens all columns will be stacked:

The layout of the second section on small screens

Section #2 HTML

Section #2 CSS


7. Build the Third Section

The third section will include:

  • A fixed background image.
  • The image caption.
  • An absolutely positioned SVG shape (a curve) that will serve as a small overlay.

Here’s its appearance:

The layout of the third section

Section #3 HTML

Section #3 CSS


8. Browser Support

I’ve tested the demo in various browsers and devices. It works great, apart from one small issue. In some browsers (like on Firefox) you might see that an SVG leaves a trace like having a bottom border. I’ve encountered this inconsistency as well on other projects for clients where the layout requires absolutely positioned SVGs like here:

Firefox issue frustrating
Firefox issue; frustrating!

One fix that I’ve tried and seems to work is to decrease the SVG’s viewport height by one. For example, instead of viewBox="0 0 1440 320", set it as viewBox="0 0 1440 319". Feel free to do this modification, if you like.

If you know what causes this problem or you have a more elegant fix, share it with us!

Your Web Layout With SVG Shapes is Complete!

That’s all, folks! Today, we learned how to give life to our pages by building non-rectangular layouts with SVG shapes. Let’s recap the steps:

  • Firstly, we went through a handy online tool that allows us to create different kinds of SVG shapes (curves and waves) in next to no time. 
  • Next, we created an SVG sprite with all the icons wrapped within it.
  • Then, we rendered all those icons on the screen by setting them as absolutely positioned elements and give them some height.

As discussed earlier, using an inline SVG shape isn’t the only way for creating non-rectangular layouts. Depending on your needs, CSS transforms, the clip-path property, the border-radius property, and even a simple image can also do the job. For most cases though, inline SVGs seem the best way for constructing these complex layouts. 

Let’s look at our creation once again:

Layout Challenge

Before closing, I have another challenge for you! This time your job is to extend the demo by adding functionality that stops the video if it’s out of the viewport. Hopefully, one of you will provide a solution in the comments below! Good luck!

As always, thanks a lot for reading!

More Landing Page Tutorials

If you want to get some inspiration about how to build attractive Bootstrap powered landing pages, check out the following tutorials:

No comments:

Post a Comment