Layout and style inside the webapp
On screens from a small phone to a wide desktop, your view gets a rectangle under the top bar of the webapp instead of a browser window of its own. A view that fills the rectangle, scrolls in one area and follows the theme of the webapp looks like part of the webapp. Where the webapp places your view decides which layout fits and which few style rules matter.
The place of your view
The webapp shows your view in an iframe in place of the page of the world:
- The top bar of the webapp is always above the view. It holds the name of the world, the switch between your view and the UNaIVERSE view and the button that leaves the world. On a phone the switch is in the menu of the bar.
- Your view gets the full width and all the height below the bar.
Inside the iframe the rectangle is the window of your page. Every measure of the viewport refers to the rectangle instead of the screen:
100vh,100dvhandheight: 100%onhtmlandbodyare the height of the iframe- media queries (
@media (min-width: 700px)) andwindow.innerWidthmeasure the width of the iframe position: fixedis fixed to the iframe.
No top bar of your own
The top bar of the webapp already shows the name of the world. A second bar under it with your logo and a menu takes a large part of a phone screen and gives the person two bars with unclear roles. Three rules follow for your view:
- no header bar with a logo or a site menu (colours, pictures and text already carry your brand)
- the sections of your view (tabs, pages) go in a bar at the bottom on a phone or in a side column on a desktop.
setPathwrites the open section into the address - a heading for your content ("Round 3", "Your cart") belongs to the page and is not a second app bar.
Fill the view
Most views are a column of three parts: (1) a fixed part at the top (a heading, a score), (2) a middle part that scrolls (the board, the messages, the cards) and (3) a fixed part at the bottom (the message box, the actions). In the example the cards of the middle part fill as many columns as the width allows:
import { useWorld } from './useWorld.js';
// A fake world to build with: no account, no network.
const hotel = {
name: 'Hotel Aurora',
now: { text: 'Choose a room' },
data: { rooms: ['Garden', 'Sea view', 'Tower', 'Attic', 'Library', 'Studio', 'Loft', 'Suite'] },
};
// The page fills the view: a heading, the part that scrolls, a bar at the bottom.
const PAGE = { position: 'fixed', inset: 0, display: 'flex', flexDirection: 'column' };
// Cards as wide as the view allows: one column on a phone, more on a desktop. No media query.
const MAIN = {
flex: 1, minHeight: 0, overflow: 'auto', overscrollBehavior: 'contain', padding: '0 16px',
display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(160px, 1fr))', gap: 12, alignContent: 'start',
};
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: hotel } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
return (
<div style={PAGE}>
<h2 style={{ margin: 0, padding: '12px 16px' }}>{world.name}</h2>
<main style={MAIN}>
{world.data.rooms.map((room) => (
<button key={room} style={{ padding: '24px 8px' }}>{room}</button>
))}
</main>
{/* Keep the text of the bar above the home bar of the phone. */}
<div style={{ padding: `12px 16px ${12 + world.webapp.insets.bottom}px`, borderTop: '1px solid #8886' }}>
{world.now?.text}
</div>
</div>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// A fake world to build with: no account, no network.
const hotel = {
name: 'Hotel Aurora',
now: { text: 'Choose a room' },
data: { rooms: ['Garden', 'Sea view', 'Tower', 'Attic', 'Library', 'Studio', 'Loft', 'Suite'] },
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: hotel } });
// The page fills the view: a heading, the part that scrolls, a bar at the bottom.
const page = document.createElement('div');
page.style.cssText = 'position: fixed; inset: 0; display: flex; flex-direction: column;';
const heading = document.createElement('h2');
heading.style.cssText = 'margin: 0; padding: 12px 16px;';
const main = document.createElement('main');
// Cards as wide as the view allows: one column on a phone, more on a desktop. No media query.
main.style.cssText = 'flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; padding: 0 16px;'
+ 'display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; align-content: start;';
const bar = document.createElement('div');
bar.style.cssText = 'padding: 12px 16px; border-top: 1px solid #8886;';
page.append(heading, main, bar);
document.body.append(page);
world.subscribe((node) => {
heading.textContent = node.name;
main.replaceChildren(...node.data.rooms.map((room) => {
const card = document.createElement('button');
card.textContent = room;
card.style.cssText = 'padding: 24px 8px;';
return card;
}));
bar.textContent = node.now?.text ?? '';
// Keep the text of the bar above the home bar of the phone.
bar.style.paddingBottom = `${12 + node.webapp.insets.bottom}px`;
});The layout rests on three declarations:
The page fills the iframe. The example uses
position: fixed; inset: 0. In a project with a stylesheet the same result takes two rules:csshtml, body { height: 100%; margin: 0; } #app { height: 100%; display: flex; flex-direction: column; }Only the middle scrolls.
flex: 1gives it the remaining height andoverflow: automakes it scroll. Withoutmin-height: 0a flex child never shrinks below the height of its content and nothing scrolls.The bottom bar keeps clear of the home bar through a padding of
webapp.insets.bottom(the phone screen).
Phone and desktop
The width of your view ranges from about 320 pixels (a small phone held upright) to more than 1500 (a desktop):
- Prefer layouts that adapt without media queries. Like the grid of the example, a grid with
repeat(auto-fill, minmax(160px, 1fr))gives one column on a phone and five on a desktop.flex-wrap: wrapdoes the same for a row of buttons. - Use a media query for structural changes such as a side column for the members on a desktop and a bottom sheet on a phone. The media query measures the width of the iframe and therefore the space your view actually has.
- Give every touch target at least 44 by 44 pixels with some space between targets. Success criterion 2.5.5 of WCAG 2.2 asks for this size.
- Do not depend on hover. Tooltips and menus that open on hover must also open on a touch because a phone has no hover.
- Test the view 360 pixels wide in the device mode of your browser with simulated safe areas and keyboard.
The recipe A layout for phone and desktop puts these rules together.
Scrolling
- Use one scrolling area. Two nested scrolling areas are hard to use with a finger.
overscroll-behavior: containon the scrolling area stops the scroll from passing to the page around it when the area reaches its end. On a phone it also removes the bounce of the whole page.- In a conversation, scroll to a new message only when the person has not scrolled up to older ones (Scroll to the newest message).
- Keep the scrollbars of the browser. Without a scrollbar a mouse user cannot tell that the area has more content.
Theme and colours
Render your view in the theme of the webapp (world.webapp.theme) on a background of your own. The look of the webapp shows how to do it with one set of CSS variables per theme and with color-scheme for the controls the browser draws. Check the contrast of every colour in both themes (Accessibility).
Fonts
- System fonts need no download and match the device of the person.
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serifcovers the common systems. - A web font of your own should come as
woff2files and load only the weights you use. Withfont-display: swapthe browser renders the text at once in a system font and swaps it when the web font arrives. Because the webapp gives your view 20 seconds to connect, never wait for a font before callingconnectToHost(). - Font sizes in
remwith at least 16 pixels for body text on a phone keep the text readable without zooming.
Your view needs the 'local-fonts' browser feature only to call the browser API that lists the installed fonts. A CSS rule that names a font installed on the device of the person needs nothing from the webapp.
Your styles are your own
Your page is a separate document. The styles of the webapp never apply to it and your styles never apply to the webapp. You can style the view with plain CSS, CSS modules, Tailwind or a component library. The examples on this site use a few inline styles only to stay short and fit in one file.
Next steps
- Accessibility: a view everyone can use.
- The look of the webapp and the phone screen: the theme, the language, the safe areas and the keyboard.
- Components:
SafeArea,FollowTheme.