A little HTML and CSS that everyone with a WordPress site should know

You can have a lovely WordPress site without ever opening a code editor, but sooner or later you hit that one small thing the theme will not let you change: a button that does not stand out, a gap that is too big, an element that should disappear on mobile. Calling someone in over five lines of code is hardly practical, and understanding the basics of HTML and CSS solves almost all of it. You will not become a developer, but you will know enough to make changes safely.

Where to write your CSS without breaking anything

The first rule is never to edit the main theme's files, because an update will wipe your changes. There are four safe places, and the right one depends on your theme (according to Hostinger's guide):

  • Classic theme: Appearance → Customise → Additional CSS. It is the quickest option, but the CSS is stored with the active theme and disappears if you switch.
  • Block theme: Appearance → Editor → Styles → Additional CSSYou give the block a class, under Advanced → Additional CSS class(es), and style that class.
  • Child theme: the CSS goes in the child theme's style.css , which survives updates to the parent theme. It is the most professional route.
  • • Snippets plugin (such as WPCode): the CSS is independent of the theme and stays put if you change it.

Menu names can vary slightly with your WordPress version and language. And if you have changed some CSS and nothing happens, clear the cache first: in your browser, in your caching plugin and on the server.

The most useful tool: the browser inspector

Before you write a single line of CSS, you need to know what you want to change. Right-click the element and choose Inspect (in Chrome, Edge, Firefox or Safari). A panel opens showing the page's HTML, with the element you chose highlighted.

From there you get two things: the element's classe class (for example, wp-block-button__linkand the styles already applied to it. You can edit those values right there and see the result in real time without breaking anything, because nothing is saved. Once you like the result, you copy the rule to the right place in WordPress. This saves more time than any tutorial: instead of guessing, you test.

Five tweaks that solve common problems

1. Hide an element on mobile. Give the block the class hide-on-mobile (em Advanced → Additional CSS class(es)and add:

@media (max-width: 768px) {
  .esconder-telemovel {
    display: none;
  }
}

2. Bring buttons to life with a gentle hover. A button that reacts to the mouse looks more polished:

.wp-block-button__link {
  transition: background-color 0.2s ease, transform 0.2s ease;
}
.wp-block-button__link:hover {
  transform: translateY(-2px);
}

3. Smooth scrolling for anchor links. Handy on long pages with an in-page menu. I always wrap it in a condition so it respects people who have switched off animations in their system settings, which is an accessibility matter:

@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
}

4. Images with the same proportions. In card grids, images of different heights spoil the alignment. With aspect-ratio and object-fit they all match, without distortion:

.cartao img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

5. Fix sideways scrolling on mobile. If the page “escapes” to the side, some element is wider than the screen. Use the inspector to track it down. If you cannot find it, this temporary remedy usually does the job:

body {
  overflow-x: clip;
}

Prefer clip to hidden, because it does not interfere with fixed or stickyBut it is a quick plaster: the proper fix is to correct the element causing the problem.

And a little HTML

HTML is the structure of the page, and WordPress handles most of it for you. But there are four things worth knowing:

  • Heading hierarchy. Each page should have a single <h1>, followed by <h2> for sections and <h3> for subsections, with no skipped levels. It helps SEO and anyone using a screen reader. Do not pick a heading just because it is the size you want: the level is structure, the size is style.
  • Anchors. To create a link that jumps to a section of the same page, give the block an HTML anchor (under Advanced), for example contacts, and link to #contacts.
  • Alt text. Describe each image with useful information, and leave the field empty for purely decorative ones, so screen readers skip them.
  • External links. If you open a link in a new tab (target="_blank"), add rel="noopener". WordPress usually takes care of this when you tick “Open in new tab”, but it is worth checking in the code.

Good practice and pitfalls

When your CSS “does not work”, it is almost always for one of these reasons:

  • Specificity. If another, more specific rule already applies to the element, yours loses. Instead of reaching straight for !important, make your selector more specific (for example, .card .wp-block-button__link). Keep !important as a last resort.
  • Cache. Before you doubt the code, clear the cache in your browser, in your caching plugin and on the server.
  • Changing classes. Avoid basing your CSS on classes generated automatically by plugins or page builders, because they can change in an update. Create your own, with clear names.

And three habits that will save you some nasty surprises: back up your CSS before touching it (just paste it into a text file), comment what each block does, and always test on mobile and in a second browser. With that, and the inspector open, most small tweaks stop being a problem and become five minutes’ work.

Share: