{"id":186,"date":"2020-05-03T19:06:12","date_gmt":"2020-05-03T19:06:12","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.oxyset.com\/?p=186"},"modified":"2026-10-10T19:54:51","modified_gmt":"2026-10-10T19:54:51","slug":"resize-image","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/resize-image\/","title":{"rendered":"Stretch an Image to the Edge of the Page in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A common layout puts text in one column and an image in the other, with the image running past the content container all the way to the edge of the browser window. With CSS alone you can only do that when you know the exact container width and column position. This snippet resizes the image from inside its container to the left or right edge of the WordPress page, whatever the container width.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Two parts: CSS for the head and a small script for the footer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">\/* Image that bleeds from its column to the left or right edge of the page. *\/\n.dpc-bleed-left,\n.dpc-bleed-right {\n\tposition: relative;\n\tmax-width: none !important;\n}\n\n.dpc-bleed-left img,\n.dpc-bleed-right img,\nimg.dpc-bleed-left,\nimg.dpc-bleed-right {\n\tdisplay: block;\n\tmax-width: none !important;\n\theight: auto;\n}\n\n.dpc-bleed-left img,\n.dpc-bleed-right img {\n\twidth: 100%;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">\/* Stretch .dpc-bleed-left \/ .dpc-bleed-right from their column to the page edge. *\/\n(function () {\n\tvar minWidth = 992; \/\/ Below this screen width the image stays inside its column.\n\n\tfunction update() {\n\t\tvar items = document.querySelectorAll('.dpc-bleed-left, .dpc-bleed-right');\n\t\tvar wide = window.matchMedia('(min-width: ' + minWidth + 'px)').matches;\n\t\tvar page = document.documentElement.clientWidth; \/\/ Page width without the scrollbar.\n\n\t\titems.forEach(function (el) {\n\t\t\tel.style.marginLeft = '';\n\t\t\tel.style.marginRight = '';\n\t\t\tel.style.width = '';\n\n\t\t\tif (!wide || !el.parentElement) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t\/\/ The column's content box: its border box minus its padding.\n\t\t\tvar box = el.parentElement.getBoundingClientRect();\n\t\t\tvar css = window.getComputedStyle(el.parentElement);\n\t\t\tvar left = box.left + parseFloat(css.paddingLeft);\n\t\t\tvar right = box.right - parseFloat(css.paddingRight);\n\n\t\t\tif (el.classList.contains('dpc-bleed-left')) {\n\t\t\t\tel.style.marginLeft = -left + 'px';\n\t\t\t\tel.style.width = right + 'px';\n\t\t\t} else {\n\t\t\t\tel.style.width = page - left + 'px';\n\t\t\t}\n\t\t});\n\t}\n\n\tvar frame = 0;\n\tfunction schedule() {\n\t\twindow.cancelAnimationFrame(frame);\n\t\tframe = window.requestAnimationFrame(update);\n\t}\n\n\tif (document.readyState === 'loading') {\n\t\tdocument.addEventListener('DOMContentLoaded', update);\n\t} else {\n\t\tupdate();\n\t}\n\twindow.addEventListener('load', update);\n\twindow.addEventListener('resize', schedule);\n})();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Use it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the class <code>dpc-bleed-left<\/code> or <code>dpc-bleed-right<\/code> to the image, or to the block that holds it. In the block editor, select the Image block inside a Columns block and enter the class under <strong>Advanced \u2192 Additional CSS class(es)<\/strong>. In a theme or page builder, put it on the image element:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"html\" class=\"language-html\">&lt;div class=\"container\"&gt;\n\t&lt;div class=\"column\"&gt;\n\t\t&lt;img class=\"dpc-bleed-left\" src=\"hero.jpg\" alt=\"\"&gt;\n\t&lt;\/div&gt;\n\t&lt;div class=\"column\"&gt;Text&lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The script measures the column the image sits in (its parent) with <code>getBoundingClientRect()<\/code> and subtracts the column&#8217;s padding. That replaces the hard-coded <code>+ 40<\/code> for column padding in the old version.<\/li>\n\n\n<li><code>dpc-bleed-left<\/code> gets a negative left margin as large as the gap between the column and the page edge, and a width up to the column&#8217;s right edge. <code>dpc-bleed-right<\/code> keeps its left edge and grows to the right edge of the page.<\/li>\n\n\n<li>The page width comes from <code>document.documentElement.clientWidth<\/code>, which leaves out the scrollbar, so there is no horizontal scrollbar.<\/li>\n\n\n<li>Below 992 px nothing happens and the image stays in its column, which is usually stacked by then. On resize the script runs again, at most once per frame.<\/li>\n\n\n<li>The CSS removes the theme&#8217;s <code>max-width: 100%<\/code> from these images only, which the old post asked you to do by hand.<\/li>\n\n\n<li>The old version needed jQuery, which block themes do not load, and its small-screen branch reset <code>.var<\/code> instead of the image. This one is plain JavaScript.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Conditions<\/strong> and add the condition <strong>Page\/Post: All<\/strong>, or only the pages that use the layout. Under <strong>Script location<\/strong> tick <strong>Header<\/strong> and <strong>Footer<\/strong>. Paste the CSS into the Header editor with the mode set to <strong>CSS<\/strong>, and the JavaScript into the Footer editor with the mode set to <strong>JavaScript<\/strong>, then publish.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Scripts Organizer prints Header and Footer code only with the <strong>Conditions<\/strong> trigger on the front end, not with <strong>Everywhere<\/strong>. The footer is the right place for the script: the images it measures are already on the page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Other breakpoint: change <code>minWidth<\/code> in the script to match where your columns stack.<\/li>\n\n\n<li>Crop the image to a fixed height instead of scaling it: add <code>height: 520px; object-fit: cover;<\/code> to <code>img.dpc-bleed-left, img.dpc-bleed-right<\/code>.<\/li>\n\n\n<li>A full-width section in the block editor does not need this: use the <strong>Full width<\/strong> alignment of the block.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Put the class on the image or its direct wrapper. The script measures the parent element, so an extra wrapper with its own width changes the result.<\/li>\n\n\n<li>If a parent has <code>overflow: hidden<\/code>, the part outside it is cut off. Remove the overflow on the column or its row.<\/li>\n\n\n<li>Until the script runs, the image shows inside its column for a moment. Keep the code in the footer so the run happens right after the content loads.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Stretch an image from inside its WordPress content column to the left or right edge of the page, with a small CSS and JavaScript snippet.<\/p>\n","protected":false},"author":1,"featured_media":46717,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Stretch an Image to the Edge of the Page in WordPress","description":"Stretch an image from inside its WordPress content column to the left or right edge of the page, with a small CSS and JavaScript snippet."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[73],"tags":[],"dpc_library":[204],"class_list":["post-186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":46716,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/186\/revisions\/46716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46717"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=186"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=186"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}