Lifestyle

CSS Basics: Selectors, the Box Model, and Layout

CSS beginners often find styles fail to apply, cards are wider than expected, or longer text overlaps. Using an original workshop event card, this article starts with reading selectors and declarations, explains how content, padding, border, margin, and box-sizing relate, and outlines uses for normal flow, Flexbox, and Grid. Width calculations, steps, a comparison table, and original diagrams help you build layout exercises to check in a browser.

About 8 min read

Share on LINE
An original Mokaair illustration of a rules document, a layout view, and a structure document, showing how CSS declarations control card box models and arrangement.
Image: Mokaair (© Mokaair)
Contents
  1. 1. Understand a CSS rule, then decide what to select
  2. 2. When styles do not apply, check loading and competing rules first
  3. 3. Distinguish padding, borders, and margins in the box model
  4. 4. Start with normal flow, then choose a layout method
  5. 5. Find layout problems by making content longer
  6. 6. Verify layouts using actual computed values

CSS controls how web pages look, including text, colors, spacing, and arrangement. You do not need to memorize every property before you start learning. Begin with an event card: make the title clear, keep content away from the edges, and leave space between cards. Then understand what each rule changes.

The workshop event cards and values below are original practice examples. The syntax and box model explanations are based on MDN documentation consulted on 2026-9-14. This article provides checks readers can perform; it does not claim that compatibility testing has been completed in every browser.

Understand a CSS rule, then decide what to select

For example, in .workshop-card { padding: 16px; }, the leading .workshop-card is the selector, which finds elements with the workshop-card class. Inside the braces is a declaration: padding is the property, and 16px is its value. A colon separates the property from its value, and semicolons separate declarations. Spelling and punctuation must be correct.

An element selector such as p affects paragraphs matching that element name. .workshop-card is a class selector that can be reused across multiple cards, while #workshop-list selects the specified ID. IDs should remain unique within an HTML document. For reusable visual styles, classes are usually more convenient than creating an ID for each card.

First give the HTML a sensible content order, such as the event name, time, description, and registration link, then add styles. Enlarging a paragraph with CSS does not automatically make it a semantic heading. Styling an ordinary container to look like a button does not automatically give it a real button's keyboard functionality. Plan appearance and element purpose together.

When styles do not apply, check loading and competing rules first

External stylesheets are usually loaded through an HTML link element, with rel set to stylesheet and href pointing to the correct file. If no styles appear, first check the path, response, and whether the stylesheet actually loaded. If only one property remains unchanged, select the target element and inspect the rules being applied before adding more CSS.

An element may match multiple selectors at once. The browser uses cascade rules to resolve factors such as origin, importance, layers, and specificity. Only when the other relevant conditions are equal does the later declaration determine the result. Do not treat the last rule always wins as a universal principle, or rely solely on repeatedly adding !important to resolve conflicts.

Styles and Computed in browser developer tools help you see which declarations have been overridden and what the actual computed values are. Properties such as text color are often inherited from the parent element, while borders and padding do not simply copy the parent's values. Find where a value comes from before changing it, so fixing one card does not affect the entire website.

Distinguish padding, borders, and margins in the box model

For an ordinary block box, content is the area that holds the content; padding is the space between the content and the border; border surrounds the content and padding; and margin is the space outside the border between the box and other boxes. To keep text away from the edge of its background, usually adjust padding. To separate a card from nearby objects, check margin or container spacing.

With content-box, width specifies the content width. In this original example, width is 280px, left and right padding are 16px each, and left and right border widths are 2px each. The width between the outer border edges is therefore 280+16+16+2+2=316px. Padding and borders on both sides total 36px. margin is excluded from this border dimension but affects the surrounding space.

After switching to box-sizing: border-box, the same width of 280px includes the left and right padding and borders, leaving a content width of 244px in this example. This setting helps you estimate card dimensions, but it does not automatically resolve overflow caused by long URLs, images, or child elements. It also does not include margin in width.

Start with normal flow, then choose a layout method

In a typical document with horizontal writing, blocks stack downward in content order, while inline text elements flow with the text. First make a single card readable in normal flow, then arrange multiple cards side by side. Do not use absolute positioning for everything just to align each position: when text grows, other content may fail to make room.

Flexbox primarily handles arrangement in one direction, making it suitable for button rows inside cards, tags, or toolbars. Grid plans columns and rows together, making it suitable for event card grids that need alignment. Flexbox can also wrap, but the distribution of items across wrapped rows is not equivalent to Grid's shared column tracks. Choose according to your actual alignment needs.

For example, set display: flex, flex-wrap: wrap, and gap: 12px on an action row to arrange its direct children as flex items and allow wrapping. gap controls spacing between items, while padding can still provide space along the container's inner edges. This example is a starting point for practice; you still need to inspect button widths and text lengths in the actual layout.

Find layout problems by making content longer

Start with a short title, then replace it with a long title containing the event name, intended audience, and location. A fixed card height may cause content to be clipped or overlap buttons. Most text areas can grow in height with their content, with a reasonable minimum height set as needed. Do not rely solely on shrinking the font to fit the original dimensions.

Also check images' original proportions and available width. A common exercise uses max-width: 100% with height: auto to keep images within their containers while preserving their proportions. If you use a fixed cropping frame, decide where to crop separately and confirm that people, text, or important information are not cut off.

Adjacent vertical margins of ordinary block boxes collapse under certain conditions, so the space may not equal the sum of the two values. Flexbox and Grid items do not use this form of adjacent margin collapsing. When spacing differs from expectations, first inspect the box model and the container's display rather than using negative margin everywhere to pull elements back into place.

  1. Create semantic event content in a sensible order, then load the practice stylesheet.
  2. Use classes to set text color, padding, and borders, and inspect the rules that actually match.
  3. Compare content-box and border-box dimensions, confirming that margins are counted separately.
  4. Add multiple cards and long text, then check narrow screens, zooming, and keyboard operation.

Verify layouts using actual computed values

Open developer tools and select the card. Check its content, padding, borders, and margins in the box model, then compare them with your manual calculations. If the displayed width differs, check whether max-width, the parent container, flex shrinking, or other rules affect the final dimensions. Declaring a value does not mean that value alone controls the layout result.

Gradually narrow the window, then enlarge the text and check for unnecessary horizontal scrolling, overlaps, or hidden controls. Use the keyboard to move through actual links and buttons, keeping focus visible. Checking only hover effects may miss situations where keyboard users cannot tell their current position.

After completing the exercise, keep the HTML, CSS, and a short explanation. Record what each selector controls and which dimensions are only examples. When you later encounter frameworks or preprocessors, you can still return to these rules to understand the results. Identifying which box or declaration causes a problem is more useful than memorizing many layout techniques at once.

Three cards compare content-box's 316px width to the outer border edges, border-box's 244px content width, and margin, excluded from width in both.
This example has no other size constraints. In an actual layout, also inspect the parent container and final computed values. · Image: Mokaair (© Mokaair)
Read the full description

Three cards compare content-box's 316px width to the outer border edges, border-box's 244px content width, and margin, excluded from width in both.

All numerical examples use left and right padding of 16px each and borders of 2px each, excluding margins and other layout constraints.
SettingArea or function controlledCommon misconceptions
paddingSpace between content and borderNegative values are not allowed
borderSurrounds content and paddingWhether its width is added depends on the box model
marginSpace outside the boxExcluded from border-box width
content-boxwidth specifies the content widthIn this example, 280px produces an outer width of 316px
border-boxwidth includes padding and bordersIn this example, 280px includes 244px of content
Flexbox/GridArranges direct child itemsChoose according to alignment needs

Found this guide helpful? Share it with someone who needs it

Share on LINE
  • Lifestyle

    www, Subdomains, and Subdirectories: Choosing a Website URL Structure

    Whether to use www or put a blog at blog.example.com or example.com/blog/ depends on platform support and maintenance. This article explains hostnames and paths, then compares content roles, DNS settings, HTTPS certificates, and URL consistency for subdomains and subdirectories. An original decision table and launch checklist help small websites in Taiwan build URL structures that are easy to use, manage, and hand over.

  • Lifestyle

    Before Hiring a Web Design Agency: The Brief, Quotes and Deliverables Checklist

    Before hiring a web design agency, define your site's audience, key actions and deliverables so you can compare quotes. Using a small studio as an example, this guide covers page and template counts, content duties, feature specs, mobile and accessibility checks, account and source-file handover, and post-launch maintenance terms. An editable requirements checklist helps reduce extra costs and communication gaps; no agency recommendations are included.

  • Lifestyle

    How to Start a Web Design Project: Requirements, Content, and Acceptance Testing in Order

    Building your first website often stalls over unclear requirements, content, and acceptance criteria, rather than colors. Using an original neighborhood repair studio example, this guide covers user interviews, content preparation, prototyping, development, and handover, with deliverables for each stage. Steps, a comparison table, learning resources, and original diagrams help readers building or commissioning a site turn “Is it finished?” into tasks they can verify.

  • Lifestyle

    Website Caching and CDNs: Which Layer to Clear When Updates Do Not Appear

    A website may be updated while phones still show old content because caches remain in different places. Using a Taiwan studio's business-hours update, this article explains browser, WordPress/hosting and CDN roles, cache responses, expiry times and targeted purging. An ordered procedure, comparison table and original diagram help site managers trace old versions while keeping account and shopping workflows correct.

Sources

Compiled from the references and verification dates listed above; fares, schedules, product features, and terms can change, so please confirm the latest details on the relevant official website before travelling, booking, or purchasing.

Lifestyle