Skip to content

NssResponsive

Responsive scaling system that adjusts font-size based on screen width. NUI

Getting Started

NssResponsive sets the font-size of the <html> element proportionally to the screen width. At 1920px, font-size is 100% (= 1rem = 16px). The scaling is linear — wider screens get larger values, narrower screens smaller.

js
import {NssResponsive} from "nui://nss_libs/ui/NssResponsive/NssResponsive.js";

new NssResponsive();
// Done — font-size is now responsive

Constructor

new NssResponsive()

Initializes the responsive system. Sets the font-size immediately and attaches a debounced resize listener.

js
new NssResponsive();

How It Works

The component calculates: font-size = (window.outerWidth / 1920) * 100%

This means:

  • At 1920px screen width: font-size: 100%1rem = 16px
  • At 1280px screen width: font-size: ~66.7%1rem = ~10.7px
  • At 2560px screen width: font-size: ~133.3%1rem = ~21.3px

Using --1rpx for Pixel-Based Scaling

The CSS custom property --1rpx represents a responsive pixel. Use it to convert pixel values from a 1920px design:

css
.my-element {
    width: calc(600 * var(--1rpx));  /* 600px at 1920, scales proportionally */
    padding: var(--20rpx);           /* 20px at 1920, scales proportionally */
    border-radius: var(--5rpx);      /* 5px at 1920 */
}

Using rem for Fonts

Use rem for font sizes — they automatically scale with the responsive system:

css
.my-text {
    font-size: 1rem;                 /* 16px at 1920 */
    font-family: rdr_crock, sans-serif;
}

Available Fonts

Font FamilyFile
rdr_crockcrock.ttf
rdrchineserocks.ttf

Methods

.initialize()

Called automatically by the constructor. Can be called manually to re-initialize after DOM changes. Guards against double-initialization.

Returns undefined

js
// Normally not needed — constructor handles this
responsive.initialize();

.updateScreenWidthFactor()

Recalculates and applies the font-size factor. Called automatically on resize.

Returns undefined

js
// Normally not needed — resize listener handles this
responsive.updateScreenWidthFactor();

Events / Callbacks

None. The component operates automatically via a resize event listener (debounced at 100ms).

CSS

Custom Properties (:root)

PropertyValueDescription
--1rpxcalc(1rem / 16)Base responsive pixel unit
--5rpxcalc(var(--1rpx) * 5)5px equivalent
--7rpxcalc(var(--1rpx) * 7)7px equivalent
--10rpxcalc(var(--1rpx) * 10)10px equivalent
--15rpxcalc(var(--1rpx) * 15)15px equivalent
--20rpxcalc(var(--1rpx) * 20)20px equivalent
--25rpxcalc(var(--1rpx) * 25)25px equivalent
--30rpxcalc(var(--1rpx) * 30)30px equivalent
--35rpxcalc(var(--1rpx) * 35)35px equivalent
--40rpxcalc(var(--1rpx) * 40)40px equivalent
--50rpxcalc(var(--1rpx) * 50)50px equivalent
--75rpxcalc(var(--1rpx) * 75)75px equivalent
--100rpxcalc(var(--1rpx) * 100)100px equivalent
--scrollbar-radiusvar(--5rpx)Default scrollbar border-radius
--scrollbar-widthvar(--10rpx)Default scrollbar width

Dependencies

  • NssUiComponentInterface — base class.
  • NssHelper.debounceClocked — used for the resize listener.

Things to Know

TIP

All nss_libs UI components that extend NssUiComponent automatically initialize NssResponsive. You only need to call new NssResponsive() explicitly in your own app entry point.

Why not percentages?

Percentages are relative to the parent element, making calculations unpredictable in nested layouts. --1rpx is always relative to the screen width, regardless of nesting.

  • Double-initialization is prevented via a data-nss-response-initialized attribute on <html>.
  • The resize listener uses debounceClocked(100ms) for performance — the first resize fires immediately, subsequent ones are throttled.

NIGHTSHIFT STUDIO Documentation