Search code examples
csstailwind-css

CCS to position an HTML element to the left edge of window without 'absolute' or JS?


On a static website (possible but not desirable to use JS), I've a blog with a column width (on a large screen) of maybe 50% for easy reading.

But then I have a large picture (actually it's an interactive object) with lots of detail that would be great occupying (nearly) the full width of the screen.

A full-width image

width: 100vw; (or w-screen in Tailwind) is great, but the left edge of the picture is in line with the parent. Specifying relative positioning, I can force the picture to the left using Left = -X px, but the distance X varies, so won't work without JavaScript.

Using Tailwind, or vanilla CSS, I've also tried transform: translateY(-25%);, but I still need to calculate the distance.

Absolute positioning is great, but the remainder of the blog doesn't flow around the picture.

Fixed positioning doesn't work either as the picture should scroll with the rest of the text.

So, I think I'm left with Javascript:

document.getElementById("myElementId").style.Left = 
-document.getElementById("myElementId").getBoundingClientRect().left;

Any other ideas? (I would have thought my issue wasn't that unusual, but haven't seen any clean solutions.)


Solution

  • OK, the real, elegant solution is well described at https://piccalil.li/blog/creating-a-full-bleed-css-utility/

    It's called a "Full-Bleed", and is just:

    .full-bleed {
      width: 100vw;
      margin-left: calc(50% - 50vw);
    }
    

    Or to leave a bit of margin, try calc(50% - 45vw), and setting your image to width:95vw;