Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Changing page size

Rafalw

New Coder
Hi
I would like to reduce the size of my page by 10%. I use the command:
body {
transform: scale(0.9);
transform-origin: center;
}
But then there is empty space at the top and bottom of the page.
So what command should I use?
 
Hi
I would like to reduce the size of my page by 10%. I use the command:
body {
transform: scale(0.9);
transform-origin: center;
}
But then there is empty space at the top and bottom of the page.
So what command should I use?
Hi there,
You could just try
CSS:
body { 
    padding: 0 5%;
}
which is the same as
CSS:
body { 
    padding: 0 5% 0 5%;
}
0 padding on top and bottom, and 5% on left and right.
 
Hi there,
You could just try
CSS:
body {
    padding: 0 5%;
}
which is the same as
CSS:
body {
    padding: 0 5% 0 5%;
}
0 padding on top and bottom, and 5% on left and right.
That just reduces the amount of available space, but doesn't actually scale down the page.


We can use a negative margin at the top and bottom of the body which should be 5% of the body's height, and for the side, 5% of the body's width.
Because CSS can't get the width/height of the body, a little bit of JavaScript is needed:
JavaScript:
function scalePage(scale) {
  const body = document.querySelector("body");

  body.style.transform = `scale(${scale})`;               // scale body

  const vMargin = body.clientHeight * ((1 - scale) / 2);  // calculate the margins we need to remove from the top and bottom
  const hMargin = body.clientWidth * ((1 - scale) / 2);   // calculate horizontal margins

  body.style.margin = `-${vMargin}px -${hMargin}px`;      // apply negative margins

}
scalePage(0.9);

To make sure the margin changes when the page size changes, we can add the window resize event listener:
JavaScript:
window.addEventListener("resize", () => {
  scalePage(0.9);
});
 
Last edited:

New Threads

Buy us a coffee!

Back
Top Bottom