{"id":60395,"date":"2024-02-27T12:25:58","date_gmt":"2024-02-27T06:55:58","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=60395"},"modified":"2024-02-27T12:25:58","modified_gmt":"2024-02-27T06:55:58","slug":"sticky-ads-with-html-and-css","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/sticky-ads-with-html-and-css\/","title":{"rendered":"Sticky Ads with HTML and CSS."},"content":{"rendered":"<p class=\"p1\">Today, digital advertising is growing at a faster pace. Therefore, lots of clients want to sell ads on the website.<\/p>\n<h3 class=\"p1\"><b>What are sticky ads?<\/b><\/h3>\n<p class=\"p1\">Sticky ads are ads that stay visible while the user scrolls through the content of the web page. There are two types of sticky ads horizontal and vertical sticky ads.<\/p>\n<p class=\"p1\"><b>Horizontal ads<\/b>:<\/p>\n<p class=\"p1\">These ads have a landscape view, and they are mostly displayed on the top, middle, and bottom of the page.<\/p>\n<p class=\"p1\"><b>Vertical ads:<\/b><\/p>\n<pre class=\"p1\">These are portrait view ads and they're placed vertically on the sidebar of the webpage. When the user scrolls through the content, they stick to the webpage in the given position.<\/pre>\n<p class=\"p1\"><strong>Sticky ads with HTML and CSS:<\/strong><\/p>\n<p class=\"p1\">With the help of CSS position: sticky, we can create sticky elements on a webpage.<span class=\"Apple-converted-space\">\u00a0 <\/span>It is very simple and straightforward CSS.<\/p>\n<p class=\"p1\"><b>Here is the HTML and CSS code for vertical ads:<\/b><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-60390\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.42.43\u202fAM-839x1024.png\" alt=\"HTML for ads\" width=\"625\" height=\"763\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.42.43\u202fAM-839x1024.png 839w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.42.43\u202fAM-246x300.png 246w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.42.43\u202fAM-768x938.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.42.43\u202fAM-1258x1536.png 1258w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.42.43\u202fAM-624x762.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.42.43\u202fAM.png 1610w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><b>CSS code:<\/b><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-60391\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.45.06\u202fAM-833x1024.png\" alt=\"CSS for sticky ads\" width=\"625\" height=\"768\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.45.06\u202fAM-833x1024.png 833w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.45.06\u202fAM-244x300.png 244w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.45.06\u202fAM-768x945.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.45.06\u202fAM-624x768.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.45.06\u202fAM.png 1026w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p class=\"p1\"><b>Here is the jsfiddle link for vertical ads with a working demo:<span class=\"Apple-converted-space\">\u00a0 <\/span><\/b><a href=\"https:\/\/jsfiddle.net\/zkLw8ayd\/8\/\"><b>https:\/\/jsfiddle.net\/zkLw8ayd\/9\/<\/b><\/a><\/p>\n<p class=\"p1\">Sometimes clients want there ads to be in the middle of the contents, so we need to create horizontal ads (Landscape View).<\/p>\n<p class=\"p1\"><strong>Here we have a code for horizontal ads:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-60392\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.53.05\u202fAM-843x1024.png\" alt=\"landscape sticky\" width=\"625\" height=\"759\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.53.05\u202fAM-843x1024.png 843w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.53.05\u202fAM-247x300.png 247w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.53.05\u202fAM-768x933.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.53.05\u202fAM-1264x1536.png 1264w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.53.05\u202fAM-624x758.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.53.05\u202fAM.png 1590w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h3 class=\"p1\"><strong>CSS code for horizontal ads:<\/strong><\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-60394\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.56.33\u202fAM-1-683x1024.png\" alt=\"CSS for Landscape view\" width=\"625\" height=\"937\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.56.33\u202fAM-1-683x1024.png 683w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.56.33\u202fAM-1-200x300.png 200w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.56.33\u202fAM-1-768x1151.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.56.33\u202fAM-1-1025x1536.png 1025w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.56.33\u202fAM-1-624x935.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2024-02-20-at-11.56.33\u202fAM-1.png 1058w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p class=\"p1\"><b>In the upper CSS code, sticky ads in the middle of the content are not working.<\/b> This is because, most of the time, beginners give the same height to both the parent and child elements. Therefore, there is no room left for the sticky position. So whenever we use the sticky position on the child element, make sure the child&#8217;s parent is taller than the child. So that it gives the child element room to be sticky to its parent.<\/p>\n<p class=\"p1\"><strong>CSS for the sticky nested child element:<\/strong><\/p>\n<pre class=\"p1\">.p-static{\r\n<span class=\"Apple-converted-space\">\u00a0 <\/span>position: static;\r\n<span class=\"Apple-converted-space\">\u00a0 <\/span>padding: 100px 0;\r\n<span class=\"Apple-converted-space\">\u00a0 <\/span>height:1000px;\r\n}<\/pre>\n<p class=\"p1\">Here is the CSS jsfiddle link for Nested Child element content sticky ads: https:\/\/jsfiddle.net\/sdkf1qro\/1\/<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Today, digital advertising is growing at a faster pace. Therefore, lots of clients want to sell ads on the website. What are sticky ads? Sticky ads are ads that stay visible while the user scrolls through the content of the web page. There are two types of sticky ads horizontal and vertical sticky ads. Horizontal [&hellip;]<\/p>\n","protected":false},"author":568,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":107},"categories":[3429],"tags":[245,3259,4842],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60395"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/568"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=60395"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60395\/revisions"}],"predecessor-version":[{"id":60478,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60395\/revisions\/60478"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=60395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=60395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=60395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}