From c8a6024ba81771641852ac657bf5ca7bf142ac7d Mon Sep 17 00:00:00 2001 From: Alastair Reid Date: Wed, 21 Feb 2024 13:46:20 +0000 Subject: [PATCH] CSS fun 1: iframe scaling --- _posts/2024-02-21-css-iframe-scaling.md | 172 ++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 _posts/2024-02-21-css-iframe-scaling.md diff --git a/_posts/2024-02-21-css-iframe-scaling.md b/_posts/2024-02-21-css-iframe-scaling.md new file mode 100644 index 0000000000000..4d6e24d43de97 --- /dev/null +++ b/_posts/2024-02-21-css-iframe-scaling.md @@ -0,0 +1,172 @@ +--- +layout: post +title: "CSS fun: scaling iframes" +--- + +It is a while since I refreshed my website so I thought I would experiment a +bit with CSS to explore my options +a bit. + +I sometimes want to embed a web page within another so that I can give a complementary +view of the content. The ingredients of this are + +- Use an iframe to embed the web page. + + + +- Add some buttons to change the contents of the iframe. + + + + + And a little Javascript + + + +- Create a ["flexbox"](https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox) + with two children to hold the iframe and the commentary. + + The following is adapted from [randompast/resize.html](https://gist.github.com/randompast/e3d2fc4319a35858918f). + +
+
+
    +
  • Thing1
  • +
  • Thing2
  • +
  • Thing3
  • +
+
+
+ +
+
+ + + + Some of the key parts of this are + + - The height of the compare class is "90vh" which means "90% of the height of the viewport". + - The compare class has the property "display: flex;" to make it a flexbox and the right-hand box + has the property "flex: 3" to make it 3 times wider than the other at the start. + - The left-hand box has the property "resize: horizontal;" to add a resizing handle in the bottom right. + This makes it possible to move the boundary between the left and right boxes. + - There are some "min-width" properties to prevent either box getting too small. + +- I was initially quite frustrated because the iframe never seemed to be the size it ought to be. + It was too short and the size of the content didn't match the size of the box assigned to it. + + The solution was a little more Javascript to apply a scaling transformation to the iframe + so that it always filled the box perfectly no matter what size you set the right-hand box to. + + This works by dynamically adding "transform: scale(X);" to the iframe's CSS (where "X" is + the appropriate scaling size) whenever the iframe's container is resized. + Because of the way that transforms work, we also need to set the origin of the transformation to (0,0). + + + + + +It took a bit of experimentation to come up with that but, I think it works reasonably well. +You can try resizing the box below here and you can select different web pages to view. + + + + + + +
+
+
    +
  • Thing1
  • +
  • Thing2
  • +
  • Thing3
  • +
+
+
+ +
+
+ + + + +