Back to top

HTML and CSS Essentials: A Comprehensive Guide for Beginners

Published: Dec 23, 2023


Welcome to the world of web development! Mastering HTML and CSS is the key to creating visually appealing, user-friendly websites. This post will provide a solid foundation in HTML and CSS fundamentals.


Introduction


HTML gives web pages structure and meaning, while CSS controls visual styling like colors, fonts, and layout.


Well-structured HTML improves the semantic meaning of content, boosting accessibility, SEO, and user experience. Creative use of CSS brings web designs to life with captivating visuals tailored to your brand.


HTML and CSS are the backbone of robust, high-quality web development. Whether you want to create a simple blog or a complex web app, understanding HTML and CSS is essential.


We’ll cover key concepts like elements, selectors, properties, and best practices. By the end, you will be ready to start building web pages with HTML and CSS. Let’s jump in!


HTML Basics


HTML stands for HyperText Markup Language. It provides the structural framework for web page content using markup elements.


For example, <h1> indicates a top-level heading while <p> indicates a paragraph. HTML elements tell browsers how to display content.


Here is a simple HTML document structure:


<!DOCTYPE html> 
<html>
  <head>
    <title>Page Title</title>
  </head>
  <body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
  </body>
</html>

Some key HTML elements include:


  • <h1> to <h6> - Headings
  • <p> - Paragraphs
  • <a> - Links
  • <ul>, <ol> - List items
  • <img> - Images
  • <table>, <tr>, <td> - Tables
  • <form>, <input> - Forms
  • <nav>, <header>, <footer> - Page sections

Using semantic HTML elements rather than generic <div> or <span> improves accessibility, SEO, and readability. For example, a screen reader can identify a <nav> element as the navigation bar.


Well-structured HTML is the foundation for any quality web project.


CSS Basics


CSS (Cascading Style Sheets) adds visual styling to HTML. With CSS, you can control colors, fonts, layouts, animations, and more.


The key building block of CSS is the selector. This targets an HTML element to style:


/* Element selector */
p {
  color: red; 
  font-size: 20px;
}

/* Class selector */  
.blue-text {
  color: blue;
} 

/* ID selector */
#main-heading {
  font-size: 36px;
}

Common CSS selectors:


  • Elements - p, h1
  • Classes - .blue-text
  • IDs - #main-heading
  • Universal - *

Some popular CSS properties include:


  • color - Text color
  • font-size - Font size
  • background - Background color
  • margin - Outer spacing
  • padding - Inner spacing

With endless properties and selectors, CSS allows for creative visuals and layouts.


CSS Specificity


When styling web pages, developers often bang their heads to figure out why some CSS rules are applied over others. This conflict arises due to CSS specificity - determining which style declarations take precedence. Mastering specificity is key to genuinely advancing your CSS skills.


The Specificity Hierarchy


Each CSS selector carries a specificity weight. When multiple competing selectors target the same element, the browser follows an explicit hierarchy to determine the winner:


  1. Inline styles - Applied directly to elements in HTML using a style attribute. Highest specificity.
  2. IDs - Represented by #. More specific than classes and elements.
  3. Classes, pseudo-classes, attributes - Denoted by ., :, []. Higher specificity than type selectors.
  4. Elements and pseudo-elements - Element names like p, h1, div. Least specific.

So, a class selector will override a basic element selector regardless of location in the stylesheet.


Calculating the Specificity Score


For complex situations, each selector type contributes a numeric value to generate an overall specificity score:


  • 1000 points per inline-style
  • 100 points per ID
  • 10 points per class, pseudo-class, attribute
  • 1 point per element, pseudo-element

The higher cumulative total wins out in applying styles.


Some examples:


p {} /* 1 element -> SPECIFICITY: 0,0,0,1 */
.class {} /* 1 class -> SPECIFICITY: 0,0,1,0 */
#id {} /* 1 ID -> SPECIFICITY: 0,1,0,0 */  

Real World Usage


Understanding specificity enables total style control:


  • Use IDs for strong overrides
  • Utilize classes for reusability without inflating scores
  • Allow cascading styles for maintainability
  • Employ !important as a last resort nuclear option

Controlling specificity avoids chaotic, unwieldy stylesheets.


By mastering specificity hierarchies and scoring, you can precisely craft selector strategies that yield the intended styling behavior in any project.


Combining HTML and CSS


To apply CSS to an HTML document, you need to link the CSS file using <link> or add inline CSS styles with <style>:


<!-- External CSS -->
<head>
    <link rel="stylesheet" href="styles.css"> 
</head>

<!-- Internal CSS -->
<head>
    <style>
        p {
            color: green; 
        }
    </style>
</head> 

Then, use CSS selectors to target HTML elements:


/* styles.css */

h1 {
    color: blue;
}

p {
    font-size: 18px;
} 

Remember that ID and class selectors have higher specificity when styling than element selectors. So .blue-text will override a p selector.


Additionally, some CSS properties are inherited from parent elements. For example, font-family applied to <body> will cascade to all child elements.


Best Practices


Here are some best practices for writing quality HTML and CSS:


HTML


  • Use semantic elements like <header>, <nav>, <main> where possible
  • Write accessible alt text for all images
  • Validate markup with HTML validators
  • Indent nested elements for readability
  • Use meaningful class names like .author-bio

CSS


  • Organize rules by selector type or page section
  • Use consistent naming schemes like .button or .card
  • Break into smaller, reusable components
  • Leverage variables for brand colors
  • Add comments to explain parts of complex code

Following standards and guidelines will improve your HTML and CSS skills over time.


Tools


Having the right tools goes hand in hand with learning HTML and CSS:


  • Text Editor: Visual Studio Code, Sublime Text, Atom
  • Browsers: Chrome, Firefox

Helpful extensions include syntax highlighters, auto-formatters like Prettier, and validations like HTMLHint.


Conclusion


This guide covered the fundamentals of HTML and CSS. With an understanding of elements, selectors, properties, and best practices, you can start coding web pages!


Apply your skills to a simple landing page project for the next steps. Get creative! Then, continue growing as a developer by expanding into advanced techniques like CSS grids or JavaScript interactivity.


The web provides endless possibilities. You can bring your unique ideas to life on the web by combining quality HTML and CSS skills. Happy coding!