Fit Webpage to Screen Size Without Scrolling
In this article
Yes, you can fit a webpage exactly to the screen size without scrolling by using CSS to control layout and viewport behavior. This is achieved primarily through the viewport meta tag, setting proper CSS dimensions, and avoiding content that forces overflow. It requires no special tools—just access to the webpage’s HTML and CSS files.
What You'll Need
- A text editor or code editor (such as VS Code, Sublime Text, or Notepad++)
- Access to the webpage's HTML and CSS files
- A modern web browser (Chrome, Firefox, Safari, or Edge) for testing
- Basic knowledge of HTML and CSS
Step-by-Step
- Open the HTML file of your webpage in a code editor. Locate the
<head>section. - Add or verify the presence of the viewport meta tag inside the
<head>. It should read:<meta name="viewport" content="width=device-width, initial-scale=1.0">. This ensures the page scales correctly on all devices. - Open or create the associated CSS file. Link it to your HTML if not already done using
<link rel="stylesheet" href="styles.css">in the<head>. - Set the
htmlandbodyelements to take up exactly 100% of the viewport height. Add this CSS:html, body { height: 100%; margin: 0; padding: 0; overflow: hidden; }This removes default spacing and prevents scrollbars from appearing due to margin or padding. - Apply
height: 100vhandwidth: 100vwto the main container or<body>if using a wrapper. Usevh(viewport height) andvw(viewport width) units to ensure elements scale with screen size. - Ensure all child elements inside the body are sized relative to their container. Avoid fixed heights or widths that exceed 100vh or 100vw. Use percentages,
vh,vw, orflexlayouts instead. - If using a container div, set it to:
.container { width: 100vw; height: 100vh; box-sizing: border-box; }Thebox-sizing: border-boxrule ensures padding and borders don’t push the element beyond its defined size. - Test the page in a browser. Resize the window and check for scrollbars. If any appear, inspect elements using browser developer tools (right-click > Inspect) to identify overflow sources.
- Use CSS
overflow: hiddenon thebodyorhtmlonly as a last resort. This hides content that overflows but does not fix underlying layout issues. - For responsive design, use media queries to adjust layout on different screen sizes:
@media (max-width: 768px) { .container { font-size: 14px; } }This ensures content remains readable and fits smaller screens.
Common Mistakes
- Forgetting to reset default margins and padding on
htmlandbody, which can cause unintended scrollbars. - Using fixed pixel values (e.g.,
height: 600px) instead of relative units likevhor%, leading to overflow on smaller screens. - Adding borders or padding without
box-sizing: border-box, causing elements to exceed their container size. - Over-nesting divs or using absolute positioning incorrectly, which can create hidden overflow.
- Ignoring mobile devices by omitting the viewport meta tag, resulting in zoomed-out layouts.
Tips
- Use
100vhand100vwfor full coverage, but be aware that some mobile browsers include address bars in100vh, making content appear cut off. Test on actual devices. - Use CSS Flexbox or Grid for complex layouts. They naturally adapt to container size and prevent overflow when configured properly.
- Set
min-height: 100vhinstead ofheight: 100vhif content might grow, but ensure overflow is managed. - Use
overflow: autoinstead ofhiddenduring development to catch overflow issues early. - Test on multiple devices and orientations (portrait and landscape) to ensure consistent fit.
Troubleshooting
If the page still scrolls despite correct settings, first check for hidden overflow. Use browser developer tools to inspect elements that may extend beyond the viewport. Look for images, iframes, or text blocks with no maximum width. Set max-width: 100% on images to prevent them from overflowing containers.
If scrollbars appear only horizontally, check for elements with wide content like long words, code blocks, or tables. Apply word-break: break-word or overflow-x: hidden cautiously.
On mobile, if the page appears zoomed out or clipped, verify the viewport meta tag is present and correctly formatted. Some older devices require initial-scale=1 to prevent auto-zooming.
If layout breaks on specific devices, use responsive breakpoints and test with device emulation in browser tools. Avoid hardcoding sizes; use relative units consistently.
If you’ve followed all steps and still have issues, isolate the problem by temporarily removing sections of HTML until scrolling stops. This helps identify the problematic element.
Frequently Asked Questions
Why does my page still scroll vertically on mobile?
Mobile browsers include the address bar in the reported viewport height. Use 100dvh (dynamic viewport height) instead of 100vh if supported, or test with real devices to adjust sizing.
Can I hide scrollbars without hiding overflow content?
No. Hiding scrollbars via CSS (e.g., overflow: hidden) also prevents scrolling. Use overflow: auto and ensure content fits within viewport dimensions.
Does this method work for all screen sizes?
Yes, if you use relative units and test across devices. Fixed sizes will fail on extreme screen dimensions.
Is it safe to use overflow: hidden on the body?
Only as a last resort. It hides content that overflows, which may make parts of the page inaccessible. Fix the layout instead.
Why is my content cut off on iPhone?
iOS Safari calculates 100vh including the viewport overlay. Use JavaScript to dynamically set height or test with min-height: 100% and flexible layouts.
Can I fit a webpage without modifying CSS?
No. HTML alone cannot control layout dimensions or overflow. CSS is required to set sizes, manage box model, and control scrolling behavior.