0

I've been searching around both here and at other places for answers, but I have only found part of the answer.

I'm putting a newsarticle in an iframe on my website. I can either get it to look awesome on PC/laptop or perfect on my phone. Never both. When it's good on the phone, the iframe is like 1 cm wide on my laptop. When it looks good on my laptop the iframe and article go outside the screen on my phone.

When searching around some, this bit of code makes it look good at my laptop.

It feels like I need to specify two different width, but I have no idea how. Also, if I use width: 100%; the actual width on both laptop and phone is about 1 cm. I've tried to combine width:100% with min-width:300 and max-width:1024 but I cant get that to work either.

                                  <div
                                   style="
                                      overflow: hidden;
                                      display:inline-block;">
                                   <iframe
                                      src='https://nyheter24.se/nyheter/ekonomi/1037042-kryptosparare-kan-bli-blast-20-ganger-om-dagen#overlay-wrapper-outer-0'
                                      scrolling="auto"
                                      frameBorder="0"
                                      style="
                                          width: 1024px;
                                          height: 800px;
                                          margin-top: -180px;
                                          margin-left: -200px;
                                        -webkit-transform:scale(0.7);
                                        -moz-transform:scale(0.7);
                                        -o-transform:scale(0.7);
                                        -ms-transform:scale(0.7);"
                                   ></iframe>
                                 </div>
Creepybits
  • 53
  • 4
  • Is there a reason your wrapper div is `inline-block`? If it was `block`, 100% width would work. – Geat Jun 20 '22 at 23:33
  • There's too little code to work with, please post a [reprex]. Generic formula for `margin` on a scaled element would be: `margin: calc(-1 * (height - scale * height) / 2) calc(-1 * (width - scale * width) / 2)`. This will shrink the original size occupied by the unscaled element. Your solution seems ambiguous, hence the error... – Rene van der Lende Jun 21 '22 at 01:38
  • Have a look at this: [25 scaled ` – Rene van der Lende Jun 21 '22 at 01:42
  • Sorry, I'm still learning. @Geat4 I Changed inline-block to block and width:100%. iframe take up about 50% of the width on my laptop, and can barely be seen on my phone unless I turn my phone then a small part can be seen. Like this https://imgur.com/a/NVBl40z – Creepybits Jun 21 '22 at 04:10
  • Sorry @RenevanderLende As I said, I'm still learning. Tested the code in fiddle, and it works there. https://jsfiddle.net/Creepybits/zy9p67ea/2/ – Creepybits Jun 21 '22 at 04:14
  • I give up....the best I can get is somewhat okay on laptop, and for some reason as if there's som extra margin-left on the phone. Maybe someone knows what I'm doing wrong. This is how it looks now https://imgur.com/a/3tCBGO8 – Creepybits Jun 21 '22 at 05:12

1 Answers1

0

Your problem is not really reproducible, but it is related to your using fixed units (px) for both the iframe itself and its margin and faulty calculations.

As many mobiles have output pixel sizes varying around 360x720, regardless of their hardware pixel sizes (often much larger), you will get in trouble using W/H values of 1024x800px and negative margins of -180px and -200px.

As you did not post the formula for the calculations you made, I can only guess what you are aiming for to achieve. As I mentioned in the comments, the scaled element will still occupy the original unscaled size. When regular element alignment is needed, that space needs to be resized relative to the used scale factor using negative margins.

General equation: y = -1 * (size - scale * size) / 2

This equation has to be used for all four margin properties, substituting the elements width and height for size (see snippet).

I created a responsively sizing and scaling demo to use as a framework for you to build upon. As the code is heavily commented, here only a summary of what it does:

  • Use the full browser viewport for the demo
  • Use a Flexbox layout wrapper to easily center the <iframe> in the viewport
  • Introduced CSS custom properties to easily fine-tune sizing and scaling
  • Create negative margins using the equation mentioned above to shrink the total size occupied by the <iframe>

I leave it up to you how and where you place the scaled <iframe> in your document...

snippet, go 'Full page' and resize your browser to see it all happen. Mobile size only tested in DevTools device emulation!

/********************************/
/* some convenient global rules */
/********************************/
/* Check at MDN 'box-sizing' for 'why'. There are many other explanations online. */
 *, ::before, ::after { -webkit-box-sizing: border-box; box-sizing: border-box }

html        { scroll-behavior: smooth }
body        { min-height: 100vh; margin: 0 } /* forcing body to go full screen */
html, body  { width: 100%; max-width: 100% }

/* use in <body> for debugging */
[outlines^="1"] * { outline: 1px dashed }

/*******************/
/* SO72693780 DEMO */
/*******************/
.wrapper {
    /* Flexbox Layout for easy positioning and sizing child elements */
    display: flex;

    justify-content: center; align-content: center; align-items: center;
    /* centers all child elements in parent */

    width: 100%;   /* stretch to full screen */
    height: 100vh;

    overflow: hidden; /* clips content and hides scrollbars */
}

iframe {
    border: none; /* as per OP original */

    /*
        W/H will have to be made variable for mobile/desktop use:
        - either using vw/vh relative to browser viewport
        - or a percentage relative to parent container

        Using viewport units for the demo 
        and custom properties for easy manipulation/testing. 
    */
    --width : 100vw; /* only change these values */
    --height: 100vh;
    --scale : 0.7;

    width : var(--width);  /* was 1024px */
    height: var(--height); /* was  800px */
    /* NOTE to me: original aspect ratio is 1024:800 => 1.28:1 */

    /* No vendor prefixes required */
    transform: scale(var(--scale)); /* scaled to % of original frame size */
    /*
        While scaled down, the element still occupies the original
        unscaled space, which can be corrected by using a negative margin
        relative to the elements current W/H.

        equations: yh = -1 * (height - scale * height) / 2
                   yw = -1 * (width  - scale * width ) / 2

        yh and yw because of width/height dependency 
        /2 because T/B and L/R margins
        -1 to create negative margin values
    */
    margin: calc(-1 * (var(--height) - var(--scale) * var(--height)) / 2)
            calc(-1 * (var(--width)  - var(--scale) * var(--width))  / 2);
    /*
        REMEMBER: other size properties like padding and border will be scaled too.
                  If you don't want that, compensate the scaling down 
                  by multiplying the size values of those properties with 1/scale
    */
}
<body outlines="0">
<div class="wrapper">
    <!-- inline 'scrolling' attribute is obsolete, removed from code -->
    <iframe
        src="https://nyheter24.se/nyheter/ekonomi/1037042-kryptosparare-kan-bli-blast-20-ganger-om-dagen#overlay-wrapper-outer-0">
    </iframe>
</div>
</body>
Rene van der Lende
  • 4,992
  • 2
  • 14
  • 25