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.

published: reading time: 8 min read author: Geek Workbench
Quick Summary

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

1. What is the relationship between HTML and the DOM?
HTML describes structure; the browser parses it into a DOM tree that scripts and CSS can use.
2. How does React update the browser DOM?
A state or prop change makes React render a new interface description. React compares it with the previous result and updates the DOM. Components describe the result instead of editing rendered content by hand.
3. Why use a semantic button instead of a clickable div?
A native <button> already supports keyboard activation, focus, and button semantics for assistive technology. A clickable <div> needs those behaviors added and maintained by hand.
4. What does CSS specificity do?
Specificity helps the browser choose between competing selectors.
  • Highly specific selectors are harder to override.
  • Prefer simple, scoped classes for component styling.
5. When is a ref useful for DOM access?
Use a ref when an interaction needs a browser API such as focus.
  • Keep normal content updates in JSX and state.
  • Avoid using refs to create a second source of rendered content.
6. How do Flexbox and Grid differ?
Flexbox lays out items along one main direction; Grid organizes rows and columns.
  • Use Flexbox for a row or column of controls.
  • Use Grid when a two-dimensional layout fits the design.
7. Why should actions use native controls?
Native buttons and inputs include expected focus and keyboard behavior.
  • They expose useful semantics to assistive technology.
  • Custom controls require you to implement and maintain those behaviors.
8. What does a React render describe?
JSX describes the interface for the current props and state.
  • React applies changes to its managed DOM.
  • Direct content mutations can be overwritten by a later render.
9. How can you investigate an unexpected CSS layout?
Inspect the element's computed styles in browser developer tools.
  • Find which selector supplies the winning property.
  • Check widths and wrapping at a narrow viewport.
10. How should untrusted text enter the page?
Render it as text instead of treating it as HTML markup.
  • Sanitize supported markup before using an HTML injection API.
  • HTML semantics and TypeScript do not validate untrusted content.

Further Reading

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.

#react #typescript #accessibility

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.

#react #typescript #testing

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.

#npm #react #javascript