DOM, HTML, and CSS for React Interface Basics
Understand how semantic HTML, the browser DOM, CSS layout, and React updates fit together when building accessible, predictable interfaces.
HTML gives elements meaning, the browser turns markup into a DOM, and CSS determines layout and appearance. React describes changing UI with JSX and updates its managed DOM from component state; refs handle specific browser interactions such as focusing an input. The examples show how to inspect computed styles, test keyboard access, and catch layout problems at narrow widths.
DOM, HTML, and CSS for React Interface Basics
Introduction
React builds interfaces from components, but the browser still parses HTML, creates a Document Object Model (DOM), and applies CSS. Understanding those jobs helps when a page does not look or behave as expected. Describe the interface with HTML and styles, then let React update it as data changes.
This refresher follows semantic markup through the DOM and layout, then shows where React updates fit and when direct DOM access is appropriate.
For example, JSX describes a native button with one element. The browser supplies its semantics and keyboard behavior when React renders it:
<button id="save" type="button">Save</button>
The button is interactive without extra ARIA roles. A small browser script can read its computed style and move keyboard focus to it:
const saveButton = document.querySelector("#save");
const styles = getComputedStyle(saveButton);
console.log({ color: styles.color, display: styles.display });
saveButton.focus();
console.log(document.activeElement === saveButton); // true
Tab to the button, then press Enter or Space. getComputedStyle() reports the browser’s resolved CSS values; focus() moves keyboard focus, which should remain visible through the page’s focus styling.
The DOM is the browser’s page tree
The browser turns HTML into a tree of objects called the DOM. CSS selectors match its elements. In React, JSX describes what to render; when state changes, React applies the needed DOM updates.
The diagrams separate the HTML and CSS paths while showing how React updates affect the final layout:
HTML and JSX to the DOM
graph TD
A[HTML and JSX] --> B[Browser DOM]
CSS to computed layout
graph TD
C[CSS rules] --> D[Computed styles and layout]
B[Browser DOM] --> D
E[React state changes] --> F[React updates the DOM]
F --> D
D --> G[Pixels on screen]
Avoid changing React-managed elements with innerHTML or querySelector; React may overwrite edits and leave the screen out of sync with state. Use a ref for specific browser interactions such as focusing an input.
CSS controls appearance and layout
CSS selects elements and sets properties such as color, spacing, and display. The cascade resolves conflicts using origin, importance, specificity, and source order. Highly specific selectors are hard to override, so start with simple component classes.
Flexbox arranges items in one direction; Grid handles rows and columns. A card list can use responsive minmax() columns. Test narrow widths, where fixed widths or long strings can cause horizontal scrolling.
Trade-Off Table
| Approach | Works well for | Trade-off |
|---|---|---|
| Semantic HTML | Meaning, keyboard support, and browser defaults | Requires choosing the right element |
| CSS classes | Reusable visual rules and responsive layout | Conflicting selectors can be hard to trace |
| Inline styles | A small value computed from data | Poor fit for pseudo states, media queries, and shared rules |
When to Use / When NOT to Use
Use React state for changing content, such as an open menu or filtered results, and CSS for hover and responsive layout. Browser APIs can handle focused interactions such as focusing an input after a dialog opens.
Avoid imperative DOM updates for routine rendering; they create two sources of truth. Use a ref only when a specific browser API, such as focus management, needs direct access.
If a control cannot be reached or activated by keyboard, use a native button or input, add visible focus styles, and test by tabbing through the page. Give meaningful images alt text and decorative images empty alt text.
Production Failure Scenarios
- A control works only with a mouse: a styled
<div>may lack keyboard behavior and button semantics. Replace it with a<button>or suitable input, then test Tab, Enter, and Space. - A page scrolls horizontally on a phone: a fixed-width element or long unbroken string may exceed the viewport. Inspect the computed width, allow content to wrap, and check the layout at narrow sizes.
Common Pitfalls / Anti-Patterns
- Using a generic element for an action when a semantic button or link already provides the needed behavior.
- Reaching for highly specific selectors to override a styling conflict; simplify the selector or scope the class instead.
- Mutating React-managed DOM nodes directly for content that should follow state; use JSX and state, reserving refs for focused browser APIs.
- Relying on hover alone to expose an action that touch and keyboard users also need.
Observability Checklist
- Inspect the DOM to confirm the expected semantic element was rendered.
- Check computed styles to find the winning rule when layout or contrast is wrong.
- Watch the console for React warnings and the network panel for missing assets.
- Tab through the page and test narrow widths after CSS changes.
Security and Compliance Notes
Render untrusted strings as text. Do not pass them to dangerouslySetInnerHTML unless the markup has been sanitized with a suitable allowlist. Semantic HTML and CSS do not validate data or make injected content safe.
Quick Recap Checklist
- Use semantic HTML elements for what they do, not just how they look.
- React state and JSX describe changing interface content; React applies DOM updates.
- Use CSS for cascade, styling, and layout, and test at different widths.
- Use refs for focused browser interactions; avoid manual content mutations.
Interview Questions
<button> already supports keyboard activation, focus, and button semantics for assistive technology. A clickable <div> needs those behaviors added and maintained by hand.
- Highly specific selectors are harder to override.
- Prefer simple, scoped classes for component styling.
- Keep normal content updates in JSX and state.
- Avoid using refs to create a second source of rendered content.
- Use Flexbox for a row or column of controls.
- Use Grid when a two-dimensional layout fits the design.
- They expose useful semantics to assistive technology.
- Custom controls require you to implement and maintain those behaviors.
- React applies changes to its managed DOM.
- Direct content mutations can be overwritten by a later render.
- Find which selector supplies the winning property.
- Check widths and wrapping at a narrow viewport.
- Sanitize supported markup before using an HTML injection API.
- HTML semantics and TypeScript do not validate untrusted content.
Further Reading
- For JSX and React’s element descriptions, read JSX and the Render Tree.
- The guide to accessible React interfaces covers labels, keyboard use, and focus.
- See Styling and Asset Imports for ways to organize component styles.
- Use the MDN HTML element reference to check native elements and their semantics.
- For imperative focus and other browser interactions, see the React guide to DOM refs.
Conclusion
HTML, the DOM, CSS, and React each have a clear job: HTML describes meaning, the browser builds and lays out the DOM, CSS controls presentation, and React keeps rendered UI in step with state. Use those boundaries and interface bugs become easier to find. When something is off, check the element’s semantics, the winning CSS rule, and the state that produced the JSX.
Category
Related Posts
Accessible React UI: Semantics, Labels, and Keyboard Use
Build React interfaces with semantic controls, connected labels, visible keyboard focus, and status announcements through practical TypeScript examples.
Testing React Components and User Interactions
Write behavior-first React tests with accessible roles, realistic user events, and async assertions while avoiding brittle checks of implementation details.
npm and Package Scripts for React Projects
Learn how npm installs React dependencies, records exact versions in a lockfile, and runs repeatable project scripts with a safer everyday workflow.