


How to solve the height problem by adjusting the rem unit in adaptive design?
Adaptive design: Cleverly solve the problem of height adjustment
In responsive design, precise control of page elements height is often a headache. Especially when the toolbar on the top of the browser takes up space, resulting in the actual visible height being less than the design draft height, how to ensure that the height of the page elements is consistent with the design draft has become a major challenge for developers.
Assume that the design draft size is 1920x1080 pixels, but the existence of the browser toolbar makes the actual visual height less than 1080 pixels. If you use rem units directly and set body
's font-size
according to the width of the design draft, the page layout is likely to be biased.
You may have tried the rem scheme based on width adaptation:
export const selfadapter = () => { let designWidth = 1920; // Design draft width let html = document.documentElement; let ww = html.clientWidth; // window width let rem = (ww * 100) / designWidth; html.style.fontSize = rem "px"; const resize = () => { let rem = (ww * 100) / designWidth; html.style.fontSize = rem "px"; }; window.addEventListener("resize", resize, false); };
This scheme calculates the rem value based on the window width, ignoring the height change. To address height differences caused by browser toolbar, a more effective strategy is to calculate the rem value based on window visual height.
The modified code is as follows:
export const selfadapter = () => { let designHeight = 1080; // Design draft height let html = document.documentElement; let wh = html.clientHeight; // window height let rem = (wh * 100) / designHeight; html.style.fontSize = rem "px"; const resize = () => { let rem = (wh * 100) / designHeight; html.style.fontSize = rem "px"; }; window.addEventListener("resize", resize, false); };
By using clientHeight
to obtain the visual height of the window and calculate the rem value based on the design draft height, the page height deviation caused by the browser toolbar can be effectively solved, ensuring that the height of the page element is consistent with the design draft, and avoiding UI misalignment. This enables 100px in the design draft to be converted to 1rem accurately in the browser.
The above is the detailed content of How to solve the height problem by adjusting the rem unit in adaptive design?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Recommended apps on top ten digital currency exchanges: 1. OKX, 2. Binance, 3. gate.io, 4. Huobi, 5. Coinbase, 6. KuCoin, 7. Kraken, 8. Bitfinex, 9. Bybit, 10. Bitstamp, these apps provide real-time market trends, technical analysis and price reminders to help users monitor market dynamics in real time and make informed investment decisions.

The reliable and easy-to-use virtual currency exchange apps are: 1. Binance, 2. OKX, 3. Gate.io, 4. Coinbase, 5. Kraken, 6. Huobi Global, 7. Bitfinex, 8. KuCoin, 9. Bittrex, 10. Poloniex. These platforms were selected as the best for their transaction volume, user experience and security, and all offer registration, verification, deposit, withdrawal and transaction operations.

The top ten digital virtual currency apps are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

Recommended apps for the top ten virtual currency viewing platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi, 5. Coinbase, 6. Kraken, 7. Bitfinex, 8. KuCoin, 9. Bybit, 10. Bitstamp, these platforms provide real-time market trends, technical analysis tools and user-friendly interfaces to help investors make effective market analysis and trading decisions.

Safe and easy-to-use virtual digital currency trading software include: 1. Binance, 2. OKX, 3. Gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. Bittrex, 8. Poloniex, 9. Bitfinex, 10. KuCoin. These exchanges have their own characteristics, provide a variety of cryptocurrency trading and advanced functions, have a friendly user interface, strong security measures, and are suitable for traders of different levels.

The top ten safe and easy-to-use virtual currency trading platforms are: Binance, OKX, gate.io, Coinbase, Kraken, Huobi, Bybit, KuCoin, Bitfinex, and Bittrex. These platforms are highly praised for their high liquidity, low transaction fees, diversified trading products, global layout, strong technical support, innovative trading systems, high security, rich currency and user-friendly interface.

Top 10 virtual currency app exchanges: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi, 5. Coinbase, 6. Kraken, 7. Bitfinex, 8. KuCoin, 9. Bybit, 10. Bitstamp, these platforms provide real-time market trends, technical analysis tools and user-friendly interfaces to help investors conduct effective market analysis and trading decisions.

The top ten digital currency exchanges are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.
