Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="pageWrapper">
  <h1>Scaling an image by 10% </h1>
  <h2>With content re-flowing properly around it</h2>
  <p>The goal is to scale an image by a given percentage regardless of its proportions. Just using transform:scale() by itself does not adjust the rest of a page's content to flow around the new size. The same thing happens with increasing an image's dimensions
    using calc() - and a container is required for calc to even be useful in resizing an image. </p>
  <p>These are the steps I came up with to achieve the scaling of the image <em>and</em> the re-flowing of the surrounding content. It involves both a container and some in-line styles. It's assumed that these styles would be added through javascript:</p>
  <ul>
    <li>Wrap each image in a containing span (.container), and set the display to inline-flex</li>
    <li>Take the image's width and set that as the font-size on the container. If an image is 200px wide, set it's container to a font-size of 200px.</li>
    <li>Take the image's height and set that as the font-size for the image (strange, I know). If an image is is 100px tall, the image's font-size would be 100px.</li>
    <li>Set the percentage you want to scale - 10% in this example - with the transform:scale(1.1) on .scaleMe. At this point, the image will be scaled to the new dimensions, but will still occupy its original dimension in the page flow.</li>
    <li>To make room for the newly scaled image vertically, add a margin to the top and bottom that equals 5% of the original image's height (5x2=10). Because we set the font-size on the image earlier equal to it's height, a margin of .05em works out to 5%.
      </li>
    <li>To fully occupy the newly scaled image's horizontal space, we set a transparent border on the left and right of the container also equal to 5% of the original image's width. Normally, we wouldn't be able to use percentages in borders, but because
      of the font-size we set earlier, we can again use the em unit and have it match up. </li>
  </ul>

  <h3>Some observations and comments</h3>
  <p>This is a pretty convoluted way to resize an image. Ideally, you would just change the values of the dimensions directly on the image itself. For this sample, I've kept the css as simple as possible, but if you were going to build this into a system,
    it would be pretty easy to use CSS variables to define your percentage, and then CSS calc() to convert the percentage you want scaled into the borders, margins and transform. <a href="https://codepen.io/spicedham/pen/MPgxxo">This pen shows how you could do that.</a></p>

  <p>Lorem ipsum dolor sit amet,
    <span class="container" style="font-size: 300px;"><img class="scaleMe" src="svg.svg" width="300" height="100" alt="" style="font-size: 100px"/></span>Aliquam bibendum malesuada ex, eu feugiat erat efficitur sit amet. Sed lacus felis, viverra euismod
    condimentum id, faucibus sit amet tellus. Quisque maximus justo et ultricies placerat. Etiam dapibus, augue eu tristique feugiat, lacus turpis faucibus massa, ut sagittis risus neque vitae neque. </p>
  <p>Lorem ipsum dolor sit amet,
    <span class="container" style="font-size: 100px"><img class="scaleMe" src="svg.svg" width="100" height="200" alt="" style="font-size: 200px"/></span>Aliquam bibendum malesuada ex, eu feugiat erat efficitur sit amet. Sed lacus felis, viverra euismod
    condimentum id, faucibus sit amet tellus. Quisque maximus justo et ultricies placerat. Etiam dapibus, augue eu tristique feugiat, lacus turpis faucibus massa, ut sagittis risus neque vitae neque. </p>
  <p>Lorem ipsum dolor sit amet,
    <span class="container" style="font-size: 20px"><img class="scaleMe" src="svg.svg" width="20" height="20" alt="" style="font-size: 20px"/></span>Aliquam bibendum malesuada ex, eu feugiat erat efficitur sit amet. Sed lacus felis, viverra euismod condimentum
    id, faucibus sit amet tellus. Quisque maximus justo et ultricies placerat. Etiam dapibus, augue eu tristique feugiat, lacus turpis faucibus massa, ut sagittis risus neque vitae neque. </p>
</div>
            
          
!
            
              .container {
			display: inline-flex;
			vertical-align: middle;
			border-left: solid .05em transparent;
			border-right: solid .05em transparent;
		}
		.scaleMe {
			background-color: orange;
			transform: scale(1.1);
			margin: .05em 0;
		}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console