DEV Community

Cover image for Mobile Web Woes: Navigating Common CSS and Font Issues 📱🔧
Aniket Singh
Aniket Singh

Posted on

Mobile Web Woes: Navigating Common CSS and Font Issues 📱🔧

Introduction 🚀

Ah, the joys of web development! Everything looks perfect on your desktop browser, but then you switch to mobile, and suddenly it's like you've entered a parallel universe. Weird vertical scrolls, fonts that refuse to load on the first render, and those pesky extra scroll glitches. 🤦‍♂️

But fear not, fellow developer! We've got the solutions to these common mobile web development issues. Let's dive in!

1. The Mysterious Case of the Extra Vertical Scroll 📜

Problem: You've set your div to take up 100vh, but on mobile, you're still getting some unexpected vertical scrolls. Why? Mobile browsers like Chrome and Safari introduce extra UI elements that can mess with your perfect layouts.

Solution: Dynamically set the body height based on the window's inner height.

function setBodyHeight() {
    document.body.style.height = `${window.innerHeight}px`;
}

useEffect(() => {
    window.addEventListener('resize', setBodyHeight);
});

useEffect(() => {
    setBodyHeight();
}, []);

Enter fullscreen mode Exit fullscreen mode

Explanation: By setting the body's height to the window's inner height, we ensure that our page takes up the exact height of the visible viewport, regardless of any browser UI elements.

2. The Font Loading Fiasco 🅰️

Problem: You're using custom fonts, but during the initial render, the browser's default fonts load instead. This can lead to a jarring experience for users.

Solution: Preload your fonts!

<link rel="preload" as="font" href="YOUR_FONT_PATH_HERE" crossorigin>
<link rel="preload" as="font" href="YOUR_SECOND_FONT_PATH_HERE" crossorigin>

Enter fullscreen mode Exit fullscreen mode

Explanation: By preloading fonts, you're telling the browser to fetch them early in the loading process. This ensures that by the time your content renders, your custom fonts are ready to be displayed. Storing assets in a CDN like Cloudinary can further speed up this process.

Image description

3. The Horizontal Scroll Sneak Attack 🚫

Problem: You've set your website to be 100vw width with no extra horizontal scrolls. It works on desktop, but some mobile browsers ignore the overflow property on the body, leading to unwanted horizontal scrolling.

Solution: Use a wrapper for your body content.

<body>
    <div class="WholeBodyWrapper">
        <noscript>You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
    </div>
</body>
Enter fullscreen mode Exit fullscreen mode
.WholeBodyWrapper {
    overflow-x: hidden;
    position: relative;
    height: 100%;
}
Enter fullscreen mode Exit fullscreen mode

Explanation: By wrapping the entire content of the body and setting the overflow property on the wrapper, we ensure that the overflow rule is respected, even on mobile browsers that might ignore it on the body element itself.

Conclusion 🎉

Mobile web development can be tricky, but with the right tools and knowledge, you can navigate these common pitfalls with ease. Remember, the key is to test, adjust, and test again. Happy coding! 🚀

Image description

I hope this blog post serves as a helpful guide! Remember to replace the placeholders in the code snippets with your actual paths or values.

Top comments (1)

Collapse
 
cezarytomczyk profile image
Cezary Tomczyk

Additionally you may apply a solution to avoid layout shifting for custom fonts.