How I Organize CSS on Small PHP Sites
Back to Blogs

How I Organize CSS on Small PHP Sites

CSSPHPWeb DevelopmentCode Organization

Short answer: On the small PHP sites I build I keep CSS in a handful of plain files with a clear order: base styles, layout, then components. I use CSS variables for colors, fonts and spacing, and I avoid both one giant messy stylesheet and a folder structure that is bigger than the site itself.

CSS gets out of hand quietly. A project starts with a tidy file, then a homepage exception is added, then a quick fix for a tablet view, then a second button style because the first one "could not be touched". Six months later nobody is sure which class is the real one, and a color change means searching the whole project. I do not need a complicated system to prevent that. I need a small, boring structure and the discipline to stick to it.

Match the structure to the size of the site

Most of the sites I build are marketing or service websites: a home page, a handful of inner pages, a blog, a contact form and a simple admin panel. That does not need a design-system monorepo or a file for every button state. It also should not live in a single 3,000-line stylesheet with no headings.

The test I use is simple. Can I find the style for the main header, the primary button and the footer in a few seconds, and can someone else do the same after I have handed the site over? If the answer is no, the CSS is not organized enough, no matter how modern the tooling looks.

The files I usually start with

For a typical small PHP site I use a short list of plain CSS files:

  • base.css. A small reset or normalize, typography, default links, images, forms and a few shared elements such as html and body.
  • layout.css. The header, footer, navigation, main page width, grid or flex layout, and the spacing between those regions.
  • components.css. Reusable blocks: buttons, cards, forms, alerts, pagination, the blog article styles, and similar pieces that appear on more than one page.
  • utilities.css, if I need it. A short set of helper classes for things like visually hidden text, a container, or a spacing nudge I truly reuse. I keep this small on purpose.

If one page is genuinely unusual, a thin page-contact.css or similar is fine. I do not create a file per page by default, because most "unique" pages are just the same components in a different order.

I load these in that order, either as a few <link> tags in the shared header or combined into one file at build time. The order matters: later files can override earlier ones without a fight.

A clear order inside each file

Inside a file I follow the page, top to bottom, and I mark each section with a short comment. Layout starts with the header and ends with the footer. Components are grouped by type, not by the day I added them. Media queries sit with the component they belong to, or in a clearly labeled block at the end of the file if several components share the same breakpoint.

I also try to keep "one idea, one place". The primary button's color, padding and hover state should live together. If I find myself changing the same button in three files, the split is wrong and I put it back in one component rule.

Variables for the things that change

Colors, font stacks, font sizes and the main spacing steps go into CSS variables on :root. Then a button uses var(--color-primary) instead of a one-off hex code. When the client later asks for a slightly darker green, I change it once.

I keep the list short. A primary color, a darker hover, a text color, a muted text color, a background, a border, a danger color, two or three font sizes and a small spacing scale are enough for most small sites. A variable for every shade I might ever need becomes its own mess.

This sits alongside the font and color decisions I wrote about in Choosing Fonts and Colors for a Business Website. If you are still settling the brand itself, my brand style guide generator is a useful place to pin down those colors and type choices before they go into the CSS.

Keep specificity low

Most of the pain in old CSS comes from rules that are too specific to override cleanly. I prefer a single class, such as .btn or .site-header, over long chains and over IDs used only for styling. I avoid !important except for a rare case, such as hiding a third-party widget that cannot be changed any other way, and I leave a comment explaining why.

When a component needs a variation, I add a second class, like .btn.btn--primary, rather than a parent selector that reaches in from the page. That way the button still works if I move it.

Names the next person can follow

Class names should describe the thing, not the look of the day. .card ages better than .blue-box-2. I keep names short and consistent, and I do not invent a private language. If I use a pattern such as a block name plus a modifier, I use it everywhere, not only on the components I wrote on a Tuesday.

A few section comments in the file are worth more than a long README nobody opens. The handover document can point to the CSS folder and say, in one paragraph, which file to edit for layout, buttons and colors. That is usually enough.

What I skip on a small site

I usually write plain CSS. A preprocessor can help on a larger project, but I do not start with one by default, because it adds a build step the next person has to understand. I also do not drop in a large utility framework for a ten-page site that already has its own layout. The cost is a lot of unused CSS and a second way of writing the same spacing.

Vendor prefixes are another place I stay conservative. Modern browsers need far fewer of them than they did several years ago. I add a prefix when I know a property still needs one, not by copying an old snippet in full.

Tidy as you go

Organization is not only the folders. I delete a rule when I remove the HTML it belonged to. I search for a class before I create a new one that does the same job. And before launch I skim the CSS for colors and font sizes that never became variables, and for comments that refer to a layout I have already replaced.

That habit is what keeps the next change small. A well organized stylesheet will not make a site look better on its own, but it will make the visual work, and the later edits, much safer.

If you have a PHP site whose CSS has grown into a single file that nobody wants to touch, send me a link and I will tell you how I would split it, and what I would leave alone.

Start a project

If you want help with this on your own site, send me a message here or see what I've built.