HTML and CSS Fundamentals

Master HTML and CSS fundamentals with this comprehensive guide for students. Learn core concepts, syntax, and best practices to build stunning websites. Start your web development journey today!

Podcast

HTML: Stavební kameny webu0:00 / 14:12
0:001:00 zbývá

The World Wide Web (WWW) and its foundational languages, HTML and CSS, are essential for anyone venturing into web development. Understanding these fundamentals is crucial for creating structured, styled, and accessible websites. This comprehensive guide will break down HTML and CSS basics, their history, key concepts, and practical applications, perfect for students seeking a clear overview.

Unpacking the World Wide Web: History and Core Concepts

Before diving into HTML and CSS, it's vital to grasp the environment they operate within: the World Wide Web. Often confused with the Internet, the Web is a service built upon the Internet's physical network.

Internet vs. Web: A Clear Distinction

  • Internet: This refers to the physical network infrastructure, the global system of interconnected computer networks that use the Internet protocol suite (TCP/IP) to link billions of devices worldwide.
  • World Wide Web (WWW or Web): This is an information space where items of interest, called resources, are identified by Uniform Resource Identifiers (URI). It's a service that utilizes the Internet to access interconnected documents and other web resources via hypertext links.

Formation of the WWW: A Brief History

The concept of the World Wide Web emerged from a proposal by Timothy Berners-Lee in 1989 at CERN (European Organization for Nuclear Research). His document, "Information Management: A Proposal," laid the groundwork for a system to manage and share documents. Key developments included:

  • 1989: Proposal for handling documents in CERN.
  • 1990s: Development of the first browser for viewing HTML files.
  • Transmission: Via HTTP (Hypertext Transfer Protocol).
  • Addressing: Via URL (Uniform Resource Locator), e.g., www.example.com.

Key Organizations: W3C and WHATWG

The evolution and standardization of web technologies are primarily driven by two organizations:

  • W3C (World Wide Web Consortium): Led by Timothy Berners-Lee, the W3C comprises experts from major tech companies. It maintains and develops web standards, referred to as specifications, for various web technologies.
  • WHATWG (Web Hypertext Application Technology Working Group): Founded in 2004 by individuals from Apple, Mozilla Foundation, and Opera Software. Concerns about the W3C's direction with XHTML led to the WHATWG's mission to address the needs of real-world web developers, focusing on the HTML Living Standard.

HTML Fundamentals: Structuring Web Content

HTML, or HyperText Markup Language, is the standard markup language for creating web pages. It works in conjunction with CSS for styling and JavaScript for interactivity. At its core, HTML defines the structure and meaning of web content.

HTML Specification and Syntax Basics

HTML has evolved significantly, with HTML5 (2014) and the HTML Living Standard (2019) being the current predominant versions. Its specification covers syntax, elements, semantics, user interaction, web application APIs, and page rendering.

HTML documents are built using a hierarchical structure of elements, defined by tags and configured by attributes.

  • Tags: Tags typically come in pairs (opening and closing), like <body> and </body>. Some elements are self-closing, like <img>.
  • Elements: An element consists of the opening tag, content, and the closing tag (e.g., <p>Hello World!</p>).
  • Attributes: These provide additional information about an element and are placed in the opening tag (e.g., <img src="image.jpg" alt="A descriptive image">).

HTML Syntax vs. XHTML Syntax

Historically, there were differences in syntax conventions:

FeatureHTML SyntaxXHTML Syntax (XML)
Closed TagsOften optional for some elementsAlways required (e.g., <br/>)
Case SensitivityCase-insensitiveSmall case characters only
AttributesValues optional for some (e.g., checked)Values always required (e.g., checked="checked")

Modern HTML5 is more forgiving, but best practice often aligns with XML-like strictness for cleaner code.

Structure of an HTML Document

Every HTML document follows a basic structure:

<!DOCTYPE html>
<html>
<head>
 <title>Page Title</title>
 <meta charset="UTF-8">
 <link rel="stylesheet" href="style.css">
 <script src="script.js"></script>
</head>
<body>
 <!-- Content visible to the user goes here -->
 <p>Hello World!</p>
</body>
</html>
  • <!DOCTYPE html>: Declares the document type and HTML version (HTML5).
  • <html>: The root element that encloses all other HTML elements.
  • <head> section: Contains meta-information about the page, not directly visible to the user. This includes:
  • <title>: Defines the title of the browser window or tab.
  • <meta>: Provides metadata like character encoding (charset), keywords, description, and author information (<meta name="keywords" content="html, css, web">).
  • <link>: Links to external resources, most commonly CSS stylesheets (<link href="screen.css" rel="stylesheet" type="text/css"/>).
  • <style>: Embeds CSS directly within the HTML document.
  • <script>: Embeds or links to JavaScript files.
  • <body> section: Contains all the content visible to the user, such as text, images, links, and forms.

HTML Semantics: Giving Meaning to Content

HTML semantics are crucial for accessibility, SEO, and maintainability. Semantic elements clearly describe their purpose to both browsers and developers. The Semantic Web Vision by W3C emphasizes the meaning of content for computers, supported by element names and metadata.

Basic Semantic Elements:

  • Structural elements:

  • <main>: Main content of the document.

  • <section>: A thematic grouping of content.

  • <article>: Independent, self-contained content (e.g., a blog post).

  • <header>: Introductory content for a section or document.

  • <footer>: Concluding content for a section or document.

  • <aside>: Content indirectly related to the main content.

  • <nav>: Navigation links.

  • Typography:

  • <p>: Paragraph text.

  • <h1> to <h6>: Headings (determines heading level, <h1> being the most important).

  • <ul>: Unordered list.

  • <ol>: Ordered list.

  • <li>: List item (used within <ul> or ol).

  • <dl>: Definition list.

  • <dt>: Definition term (within dl).

  • <dd>: Definition description (within dl).

  • Highlighting text: <b> (bold), <strong> (strong importance).

  • Tables:

  • <table>: Base for a table.

  • <tr>: Table row.

  • <td>: Table cell.

  • <th>: Table header cell.

  • <caption>: Table caption.

  • <thead>: Table header group.

  • <tbody>: Table body group.

  • <tfoot>: Table footer group.

Other Important HTML Elements

  • Images:

  • <img src="path/to/image.jpg" alt="Alternative text" width="100">: Embeds an image. alt text is crucial for accessibility and SEO.

  • Responsive Images: srcset and sizes attributes for <img> or the <picture> element provide optimized images based on screen size or resolution. <picture> offers more art direction control and allows for different image formats.

  • Forms: Used for user input.

  • <form>: Defines an HTML form.

  • <fieldset>: Groups related elements in a form.

  • <input>: Input field (e.g., text, submit, checkbox, radio).

  • <button>: Clickable button.

  • <label>: Label for an input element.

  • <legend>: Caption for a <fieldset> element.

  • <select>: Drop-down list.

  • <option>: Option in a drop-down list.

  • <textarea>: Multi-line text input control.

  • HTML5 introduced new input types (e.g., date, email, url), validation, and functions like placeholder.

  • Hypertext Links:

  • <a href="...">Text</a>: Creates a hyperlink.

  • External links: Link to another website, often using target="_blank" to open in a new tab.

  • Internal links: Link to another document within the same website (relative or absolute addressing).

  • Anchor links: Link to a specific section within the same document using id attributes (e.g., <a href="#someid">Jump</a> to <h2 id="someid">Section</h2>).

  • Multimedia Support:

  • <audio>: Embeds audio content.

  • <video>: Embeds video content. HTML5 significantly improved browser compatibility for multimedia, reducing the need for third-party players.

  • Generic Elements for Styling:

  • <div>: A block-level container with no semantic meaning, primarily used for CSS styling.

  • <span>: An inline-level container with no semantic meaning, used for styling parts of text.

  • <hr>: A thematic break, rendered as a horizontal line.

HTML Living Standard and Browser Support

The WHATWG's HTML Living Standard is continuously evolving, emphasizing "what works everywhere" rather than fixed versions. Browser support is crucial, as the practical reality of web development means adhering to what browsers implement. The W3C often formally adopts the status quo defined by WHATWG.

Flashcards

1 / 39

Co je HTML (v kontextu vývoje webu)?

Značkovací jazyk používaný pro strukturování webových dokumentů; součást HTML5 (2014) a HTML Living Standard (2019).

Tap to flip · Swipe to navigate

CSS Fundamentals: Styling Web Pages

CSS, or Cascading StyleSheets, is a stylesheet language used to describe the presentation of an HTML document. It dictates how HTML elements should be displayed, covering aspects like colors, fonts, layout, and responsiveness. CSS is maintained by the W3C and follows a modular architecture.

CSS Syntax and Where to Place CSS

A CSS rule consists of a selector and one or more declarations. Each declaration includes a property and a value.

selector {
 property: value;
 property: value;
}

/* Example */
p {
 color: blue;
 font-size: 12px;
}

There are three ways to apply CSS:

  1. Inline styles: Directly within an HTML element's style attribute (e.g., <p style="color:red;">). Not recommended for maintainability.
  2. Internal stylesheet: Within <style> tags in the HTML <head> section.
  3. External stylesheet (preferred): Linked via a <link> element in the <head> section (e.g., <link href="style.css" rel="stylesheet" type="text/css"/>). This allows multiple HTML documents to share one stylesheet, improving reusability and maintainability.

CSS Selectors: Targeting Elements for Styling

Selectors are used to "focus our styles on certain element/s."

  • Tag name: p (selects all paragraph elements).
  • Class selector: .test (selects all elements with class="test"). A class can be used multiple times on a page.
  • ID selector: #identifier (selects the unique element with id="identifier"). An ID must be unique on a page.

Other common selectors:

  • Grouping: elementA, elementB (selects both A and B).
  • Descendant: elementA elementB (selects all B elements anywhere inside A).
  • Child: elementA > elementB (selects all B elements that are direct children of A).
  • Adjacent Sibling: elementA + elementB (selects element B directly following element A).
  • Attribute selector: elementA[attribute=value] (selects elements based on attribute value).
  • Pseudo-class selectors: Target elements based on state or position (e.g., a:hover for hover effect, p:first-child for the first paragraph child).

CSS Values and Units

CSS properties use various types of values:

  • Units for sizes:

  • px: Pixels (1px = 1 pixel on screen).

  • em: Relative to the font-size of the parent element (e.g., 1em = 16px if parent font-size is 16px).

  • rem: Relative to the font-size of the root (HTML) element.

  • %: Percents, relative to the parent element.

  • vw, vh: Viewport width and height, relative to the browser viewport size.

  • Colors:

  • Named colors: red, blue, green.

  • RGB: rgb(0-255, 0-255, 0-255).

  • Hexadecimal: #RRGGBB or shorthand #RGB (e.g., #00FF00 for green).

  • RGBA: rgba(0-255, 0-255, 0-255, 0-1) (adds an alpha channel for transparency).

  • Fonts:

  • font-family: Specifies font order (e.g., Arial, Verdana, sans-serif). Always include a generic family (e.g., sans-serif) as a fallback.

  • @font-face: Imports custom fonts (e.g., from Google Fonts).

  • Keywords: Pre-defined values for properties (e.g., text-decoration: underline, text-align: center, border-style: solid).

The CSS Box Model

The CSS Box Model describes how elements are rendered on a page as rectangular boxes. Every HTML element can be thought of as a box with four layers:

  • Content: The actual content (text, image, etc.).
  • Padding: Space between the content and the border (transparent).
  • Border: A line around the padding and content.
  • Margin: Space outside the border, separating the element from other elements (transparent).

The box-sizing property can alter this behavior, with border-box being a common choice for more intuitive sizing.

Types of Elements: display Property

The display property determines an element's type and how it behaves in the layout:

  • Block-level elements: Occupy the full width available and start on a new line. Examples: <h1>, <p>, <div>, <article>, <section>, <nav>.
  • Inline-level elements: Only take up as much width as necessary and do not start on a new line. Examples: <a>, <span>, <strong>, <b>.
  • Inline-block elements: A combination. They can be aligned in a line like inline elements, but allow setting width, height, margin, and padding like block elements.

Layout Systems and Responsive Design

CSS offers powerful layout systems for arranging elements on a page:

  • Flexbox (Flexible Box Layout): A one-dimensional layout system for distributing space and aligning items in a container, either horizontally or vertically.
  • Grid (CSS Grid Layout): A two-dimensional layout system for arranging items into columns and rows, providing powerful control over complex layouts.
  • Positioning: (static, relative, absolute, fixed, sticky) allows for precise placement of elements.

Responsive Design is crucial for ensuring websites look good and function well across various devices and screen sizes. This involves using flexible layouts, responsive images, and media queries (@media screen and (max-width: 600px) {... }) to apply different styles based on screen characteristics.

CSS Custom Properties (Variables)

CSS Custom Properties, often called variables, allow you to define reusable values, making stylesheets more maintainable and dynamic.

  • Declaration: --variable: value; (e.g., :root { --primary-color: #3498db; } for global variables).
  • Usage: property: var(--variable); (e.g., color: var(--primary-color);).

Frequently Asked Questions (FAQ) about HTML and CSS

Students often have specific questions when learning HTML and CSS. Here are some common ones:

What is the main difference between HTML and CSS?

HTML (HyperText Markup Language) is used to define the structure and content of a webpage. It's like the skeleton and organs of a body. CSS (Cascading StyleSheets) is used to control the presentation and style of that content, such as colors, fonts, and layout. It's like the clothing and appearance.

Why is semantic HTML important for web development?

Semantic HTML uses elements that carry inherent meaning (e.g., <header>, <article>, <footer>). This is important because it improves accessibility for screen readers, enhances SEO by providing context to search engines, makes the code easier for developers to understand and maintain, and allows for better styling and scripting.

What is the CSS Box Model and why should I understand it?

The CSS Box Model is a conceptual box that wraps around every HTML element. It consists of the content, padding, border, and margin. Understanding it is fundamental for controlling the spacing, sizing, and positioning of elements on your webpage, helping you predict how changes in CSS properties will affect your layout.

How do HTML and CSS interact with JavaScript?

HTML provides the structure, CSS provides the styling, and JavaScript adds interactivity and dynamic behavior to a webpage. JavaScript can manipulate both HTML elements (e.g., adding or removing content) and CSS styles (e.g., changing colors or animations) in response to user actions or other events, creating a rich user experience.

What are responsive images in HTML and why are they necessary?

Responsive images are images that adapt their size, resolution, or content based on the user's device, screen size, and network conditions. They are necessary to optimize performance (loading smaller images on smaller screens) and provide a better user experience across the wide range of devices available today, ensuring images look good without wasting bandwidth.

Sign up to access full content

Create a free account to unlock all study materials, take interactive tests, listen to podcasts and more.

Create free account

Related topics