How to make styled native IAB portrait ads

Portrait ads are vertical ads that have three expandable modules. The dimensions for a fixed size portrait ad is 300 x 1050. When the viewer clicks on one of the thumbnails, the ad expands to reveal the content, which can be another image, a gallery of images, a video, or other HTML content. You can use native templates to create IAB portrait ad items that are tailored to your needs.

A sample portrait ad

This feature requires the Enhanced Ads add-on. For more information on adding add-ons to your subscription, read How to change your subscription.
This feature requires the Enhanced Ads add-on. For more information on adding add-ons to your subscription, read How to change your subscription.

In this guide, we're going to create a simple portrait ad using a styled native ad template. This type of native template has only the HTML code. You must add the necessary JavaScript code and style element directly to your website to display the ad properly.

If you'd rather have all the necessary code (HTML, JavaScript and CSS) in the template, you should make your portrait ad using a custom native ad template instead. For more information on the differences between custom native ads and styled native ads, read About native ad templates.

Templates for custom native ad items are not compatible with styled native ad items, and vice-versa.

You will learn:

  1. How to create a template for a styled portrait ad. You can customize the sample code to fit your needs.
  2. How to create a styled portrait ad item once you have a styled portrait template.

How to create a styled portrait ad template

This template will require the user to provide the URLs to where the image creatives are hosted when creating the portrait ad item. Our sample will contain only static images, but keep in mind that you can also put videos and other HTML content in each module. Define your variables accordingly based on the content that you want to have on each module.

  1. Click Native Ad Templates in the left navigation menu to go to the Templates section.
  2. Click Add Native Ad Template in the Native Ad Templates table. The New Native Template window will appear.
  3. Click Styled. The New Native Ad Template page will appear.

Selecting a native template type

  1. Name the template then add an image URL variable for each image that will be in the ad: a logo, the ad creatives themselves, and the module backgrounds. In our sample code, we called the logo variable LogoURL. We called the ad creatives variables ImageURL, ImageURL2, and so on, up to ImageURL6. Finally, we called the module variables ModuleImageURL and ModuleImageURL2.
For templates that have multiple variables such as this one, you can drag the icon on the far left of each variable field up or down to reorder your variables.

Creating a styled native portrait ad template in AdButler

You can save variables and use them in other templates. After adding a variable, click on the three dots at the far right of the variable fields then click Save Variable as Template. To use a saved variable, click Add Saved Variable while creating a native template.
  1. Enter the code in the HTML Template (Styled) field, then click Save Changes.

Here's the code we used in our example:

<div class="portrait-container">
    <div class="portrait-logo">
        <img src="[%LogoURL%]" />
    </div>
    <div class="portrait-gallery-container">
        <div class="gallery-current-image-container">
            <a target="_blank" href="[TRACKING_LINK]">
                <div class="gallery-current-image"></div>
            </a>
            <div class="expand-collapse-button expand-button"><i class="fa fa-expand" aria-hidden="true"></i></div>
            <div class="expand-collapse-button collapse-button"><i class="fa fa-compress" aria-hidden="true"></i></div>
        </div>
        <div class="gallery">
            <div class="gallery-control-button gallery-prev-button">
                <
            </div>
            <div class="gallery-groups-container">
                <div class="gallery-group gallery-group-1">
                    <img class="gallery-image" src="[%ImageURL%]" />
                    <img class="gallery-image" src="[%ImageURL2%]" />
                    <img class="gallery-image" src="[%ImageURL3%]" />
                </div>
                <div class="gallery-group gallery-group-2">
                    <img class="gallery-image" src="[%ImageURL4%]" />
                    <img class="gallery-image" src="[%ImageURL5%]" />
                    <img class="gallery-image" src="[%ImageURL6%]" />
                </div>
            </div>
            <div class="gallery-control-button gallery-next-button">
                >
            </div>
        </div>
    </div>
    <div class="module">
        <a target="_blank" href="[TRACKING_LINK]">
            <img class="module-image" src="[%ModuleImageURL%]"/>
        </a>
    </div>
    <div class="module">
        <a target="_blank" href="[TRACKING_LINK]">
            <img class="module-image" src="[%ModuleImageURL2%]"/>
        </a>
    </div>
    <div class="portrait-close-button-container">
        <span class="portrait-close-button">×</span>
    </div>
</div>

You can now use this template to create a styled portrait ad item.

Back to top

How to create a styled portrait ad item

  1. Go to the section of the relevant zone (Your AdButler > Publishers > Your Publisher > Your Zone) or campaign (Your AdButler > Advertisers > Your Advertiser > Your Campaign).
Styled native ad items can be assigned only to Native zones, with or without custom rendering enabled.
  1. Click Add Ad Item in the Ad Items table. The Add Ad Item window will appear.
  2. Click Native (Styled). The New Ad Item page will appear.

Creating a native (styled) ad item

  1. Select your portrait styled native template in the Template dropdown menu.
  2. Fill in the rest of the fields as needed, including the image URLs. If you entered a destination URL, you can test it by clicking Open Destination URL to the right of the field.
  3. Click Save Ad Item.

Creating a styled native portrait ad item in AdButler

At this point, you have finished setting up the ad item on the AdButler interface. You must now add the JavaScript and style element needed to display this ad properly directly to your website. Here's the sample code that we used in our example. You must replace the zone tags and zone class in the code with your own zone tags and zone class.

<!DOCTYPE html>
<html lang="en">
    <head>
        <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
        <style>
            .portrait-container {
                box-sizing: border-box;
                font-family: Arial;
                position: relative;
                width: 300px;
                height: 1050px;
                border: 1px solid #E5E5E5;
                padding: 0 15px;
            }

            .portrait-container > * {
                margin: 15px 0;
            }

            .portrait-logo {
                height: 40px;
            }

            .portrait-gallery-container {
                height: fit-content;
            }

            .gallery-current-image-container {
                position: relative;
            }

            .gallery-current-image {
                height: 380px;
                width: 270px;
                margin: 15px 0;
                background-repeat: no-repeat;
                background-size: cover;
            }

            .expand-collapse-button {
                position: absolute;
                top: 5px;
                left: 5px;
                width: 28px;
                height: 28px;
                box-sizing: border-box;
                cursor: pointer;
                font-size: 16px;
                color: black;
                background: white;
                border-radius: 28px;
                display: flex;
                align-items: center;
                justify-content: center;
                z-index: 2;
            }

            .collapse-button {
                display: none;
            }

            .gallery {
                height: 85px;
                width: 270px;
                display: grid;
                grid-template-columns: repeat(3, auto);
                align-items: center;
                justify-content: space-between;
            }

            .gallery-groups-container {
                height: 85px;
                width: 220px;
                position: relative;
                overflow: hidden;
            }

            .gallery-group {
                position: absolute;
                top: 0;
                width: 220px;
                display: flex;
                justify-content: space-around;
                box-sizing: border-box;
                transition: left 0.75s;
            }

            .gallery-group {
                left: 0;
            }

            .gallery-group-2 {
                left: 100%;
            }

            .gallery-image {
                max-width: 60px;
                min-height: 60px;
                cursor: pointer;
            }

            .gallery-control-button {
                font-size: 25px;
                font-weight: 900;
                color: black;
                cursor: pointer;
                transition: color 0.75s, background-color 0.75s;
                border-radius: 5px;
            }

            .gallery-prev-button {
                cursor: default;
                color: grey;
                padding: 0 5px;
            }

            .gallery-next-button {
                padding: 0 5px;
            }

            .module {
                width: 270px;
            }

            .module-image {
                height: 225px;
                width: 270px;
            }

            .portrait-close-button-container {
                height: 16px;
                position: absolute;
                top: 0;
                right: 0;
                cursor: pointer;
                margin: 0;
                display: flex;
                align-items: center;
            }

            .portrait-close-button {
                width: 16px;
                text-align: center;
                display: inline-block;
                color: #ffffff;
                font-size: 14px;
                background: #000000;
                border-radius: 14px;
                user-select: none;
            }
        </style>
    </head>
    <body>
    <!-- To Edit 1: Replace Zone Tags below here with your own -->
    <script type="text/javascript">if (!window.AdButler){(function(){var s = document.createElement("script"); s.async = true; s.type = "text/javascript"; s.src = 'https://servedbyadbutler.com/app.js'; var n = document.getElementsByTagName("script")[0]; n.parentNode.insertBefore(s, n);}());}</script>
    <div class="plc104344"></div>
    <script type="text/javascript">
        var AdButler = AdButler || {}; AdButler.ads = AdButler.ads || [];
        var abkw = window.abkw || '';
        var plc104344 = window.plc104344 || 0;
        (function(){
            var divs = document.querySelectorAll(".plc104344:not([id])");
            var div = divs[divs.length-1];
            div.id = "placement_104344_"+plc104344;
            AdButler.ads.push({handler: function(opt){ AdButler.register(######, ######, [0,0], 'placement_104344_'+opt.place, opt); }, opt: { place: plc104344++, keywords: abkw, domain: 'servedbyadbutler.com', click: 'CLICK_MACRO_PLACEHOLDER' }});
        })();</script>
    <!-- Replace Zone Tags above here with your own -->

    <script>
        // To Edit 2: Update this class with your zone's class
        var zoneClass = 'plc104344';

        function loadGalleryObject(tag) {
            collapseImage();
            var imageUrl = tag.getAttribute('src');
            document.querySelector('.' + zoneClass + ' .gallery-current-image').style['background-image'] = 'url(' + imageUrl + ')';
        }

        function expandImage() {
            document.querySelector('.' + zoneClass + ' .collapse-button').style.display = 'flex';
            document.querySelector('.' + zoneClass + ' .expand-button').style.display = 'none';
            document.querySelector('.' + zoneClass + ' .gallery-current-image').style.width = '460px';
        }

        function collapseImage() {
            document.querySelector('.' + zoneClass + ' .collapse-button').style.display = 'none';
            document.querySelector('.' + zoneClass + ' .expand-button').style.display = 'flex';
            document.querySelector('.' + zoneClass + ' .gallery-current-image').style.width = '270px';
        }

        function showFirstGroup() {
            document.querySelector('.' + zoneClass + ' .gallery-group-1').style.left = '0';
            document.querySelector('.' + zoneClass + ' .gallery-group-2').style.left = '100%';

            document.querySelector('.' + zoneClass + ' .gallery-prev-button').style.color = 'grey';
            document.querySelector('.' + zoneClass + ' .gallery-prev-button').style.cursor = 'default';

            document.querySelector('.' + zoneClass + ' .gallery-next-button').style.color = 'black';
            document.querySelector('.' + zoneClass + ' .gallery-next-button').style.cursor = 'pointer';
        }

        function showSecondGroup() {
            document.querySelector('.' + zoneClass + ' .gallery-group-1').style.left = '-100%';
            document.querySelector('.' + zoneClass + ' .gallery-group-2').style.left = '0';

            document.querySelector('.' + zoneClass + ' .gallery-prev-button').style.color = 'black';
            document.querySelector('.' + zoneClass + ' .gallery-prev-button').style.cursor = 'pointer';

            document.querySelector('.' + zoneClass + ' .gallery-next-button').style.color = 'grey';
            document.querySelector('.' + zoneClass + ' .gallery-next-button').style.cursor = 'default';
        }

        AdButler.ads.push(function () {
            var wrapper = document.querySelector('.' + zoneClass);
            wrapper.addEventListener(AdButler.EVENTS.LOAD, function (e) {
                <!-- Below code makes the close button functional -->
                var closeButton = document.querySelector('.' + zoneClass + ' .portrait-close-button');
                closeButton.addEventListener('click', function () {
                    document.querySelector('.portrait-container').style.display = 'none';
                });

                <!-- Below code makes the current image expand/collapse buttons functional -->
                var expandButton = document.querySelector('.' + zoneClass + ' .expand-button');
                expandButton.addEventListener('click', function() {
                    expandImage();
                });
                var collapseButton = document.querySelector('.' + zoneClass + ' .collapse-button');
                collapseButton.addEventListener('click', function() {
                    collapseImage();
                });

                <!-- Below code allows clicking on image in gallery to display it as the current gallery image -->
                var galleryImages = document.querySelectorAll('.' + zoneClass + ' .gallery-image');

                // if you cannot use ES6, use the below code
                for (var i = 0; i < galleryImages.length; i++) {
                    galleryImages[i].addEventListener('click', (function (j) {
                       return function () { loadGalleryObject(galleryImages[j]) };
                    })(i));
                }

                // if you can use ES6, uncomment the below code and remove the above code block
                // for (let i = 0; i < galleryImages.length; i++) {
                //     galleryImages[i].addEventListener('click', function () {
                //         loadGalleryObject(galleryImages[i]);
                //     });
                // }

                loadGalleryObject(galleryImages[0]);

                <!-- Below code makes the gallery prev/next buttons functional -->
                var prevButton = document.querySelector('.' + zoneClass + ' .gallery-prev-button');
                prevButton.addEventListener('click', function() {
                   showFirstGroup();
                });
                var nextButton = document.querySelector('.' + zoneClass + ' .gallery-next-button');
                nextButton.addEventListener('click', function() {
                    showSecondGroup();
                });
            });
        });
    </script>
    </body>
</html>

Back to top


Can't find what you're looking for?

Send us an email

hello@adbutler.com

Visit the blog

For more ad serving tips, industry news and AdButler insights.