Show DevBest How to achieve diagonal containers in CSS

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Hiiiii

There's a nice looking trend that's been going on for a while now on websites, and that's the effect of diagonal containers where the container looks slanted, but the content inside looks normal.

It's actually really easy to achieve. Most people use really long SVGs to apply as a background or :before,:after pseudo elements to the containers to get the effect, but I'm going to show you a much easier way, and when put to real usage can look even better than this example :D

Basically, all you have to do is skew/rotate your content container, then have another div inside that container which holds the content and skew/rotate it to the negative values of your content container.

The only thing is that you'll need to apply quite a high padding-bottom to your main container to make up for the loss made when skewing the container up.

Code:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Diagonal Containers</title>

        <style>
            body {
                background-color: #efefef;
                font: 400 14px/1.6 'Helvetica Neue', Helvetica, Arial, sans-serif;
                color: #212121;
                margin: 0;
                padding: 0;
            }
            
            .container {
                max-width: 960px;
                margin: 0 auto;
            }
            
            header {
                text-align: center;
                padding: 30px 0;
                border-bottom: 1px dashed #090909;
            }
            
                header h1 {
                    margin: 10px 0;
                }
            
                header h4 {
                    margin: 0;
                }
            
            section {
                margin: 100px 0;
                background-color: #fff;
                padding: 15px 30px;
                padding-bottom: 60px;
                transform: skew(5deg) rotateZ(2deg); /* skew the main container */
            }
            
                .section-content {
                    transform: skew(-5deg) rotateZ(-2deg); /* now skew it back, negative of your main container */
                }
        </style>
    </head>
    
    <body>
        <div class="container">
            <header>
                <h1>Hello World</h1>
                <h4>Here's just a small tagline.</h4>
            </header>
            
            <main>
                <section class="features">
                    <div class="section-content">
                        <h2>Features</h2>
                        <p>Here are some perks of using this service...</p>
                        <ul>
                            <li>Perk #1</li>
                            <li>Perk #2</li>
                            <li>Perk #3</li>
                            <li>Perk #4</li>
                        </ul>
                    </div>
                </section>
                
                <section class="pricing">
                    <div class="section-content">
                        <h2>Pricing</h2>
                        <p>Here are our pricing options</p>
                        <ul>
                            <li>Free: £0 p/m</li>
                            <li>Basic: £5.49 p/m</li>
                            <li>Advanced: £15.49 p/m</li>
                        </ul>
                    </div>
                </section>
            </main>
        </div>
    </body>
</html>

I've made a pen so you can see an example:


Hope it helps if you choose to use it :D
 

Menkz

Member
Jul 9, 2010
374
167
Personally not a fan, but could change many websites up a bit if they want to be unique
Thanks for the release/tutorial.
 

Users who are viewing this thread

Top