close
close

Formatting HTML and CSS for beginners

Well, I see it’s been a long time since I last posted here. A lot of interesting things have happened in recent years! But let’s get back to the topic at hand: you want to know how to properly format your HTML and CSS, and this is how you’ll do it.

Some benefits of styling with CSS

Many of these benefits also translate to HTML, when you organize your markup using HTML5 elements.

  • By keeping style separate from structure, CSS makes for cleaner, more maintainable code. This separation improves the accessibility of content and makes style changes on a website much more manageable.

  • CSS provides comprehensive control over presentation, from fonts to layout, allowing developers to create responsive designs that adapt across devices.

  • External CSS files can be cached, improving load times for subsequent page views.

Techniques for applying CSS with your HTML

CSS can be applied in three ways:

  1. Inline styling: Using the style attribute directly within HTML elements. Although this method is simple, it is generally discouraged due to its lack of maintainability.
   

style="color: blue;">This text is blue.

Go to full screen mode

Exit full screen mode

  1. Internal styling: Using a tag in the HTML . This method is useful for small projects or unique styling for a single page.
   
   p {
       color: red;
   }
   
Go to full screen mode

Exit full screen mode

  1. External styling: Link to a separate .css file, which is most recommended for extensive projects due to its maintainability and reusability.
    rel="stylesheet" href="styles.css">
Go to full screen mode

Exit full screen mode

In styles.css:

   p {
       color: green;
   }
Go to full screen mode

Exit full screen mode

Best practices

  • Use Semantic HTML, as I wrote above.

  • Avoid inline styles.

  • Prefer classes over IDs when creating selectors.

  • Consider organizing your CSS properties alphabetically.

  • Use class names that are descriptive, but avoid overly specific selectors that can conflict or be difficult to override.

  • Use media queries. They are good for responsive design. I cannot emphasize this enough.

On a different note…

Did you know that I have a free ebook giveaway on X/Twitter? You will learn how Google formats their own HTML and CSS and their own advice. If you like free stuff, check out my tweet.

Copyright © 2023 Summer Blog. All Rights Reserved.