tanszek:oktatas:techcomm:cascading_style_sheets
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tanszek:oktatas:techcomm:cascading_style_sheets [2024/10/01 09:37] – [9. Margin, padding and border] kissa | tanszek:oktatas:techcomm:cascading_style_sheets [2026/09/27 17:49] (current) – [Exercise] kissa | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== CSS (Cascading Style Sheets) ===== | ===== CSS (Cascading Style Sheets) ===== | ||
| - | ==== 1. What is CSS? ==== | + | ==== What is CSS? ==== |
| Cascading Style Sheets (CSS) is a fundamental technology used in web development to control the formatting and layout of HTML documents. While HTML provides the structure and content of a web page, CSS allows developers to apply styles, such as colors, fonts, spacing, and positioning, | Cascading Style Sheets (CSS) is a fundamental technology used in web development to control the formatting and layout of HTML documents. While HTML provides the structure and content of a web page, CSS allows developers to apply styles, such as colors, fonts, spacing, and positioning, | ||
| - | ==== 2. Selectors and declarations | + | ==== Development Environment |
| - | CSS is built around selectors and declarations. | + | For setting up the development environment, |
| - | + | ||
| - | + | ||
| - | {{:tanszek: | + | |
| - | + | ||
| - | **Selectors** specify which HTML elements the styles should target. They can be as simple as selecting an element by its name (e.g., '' | + | |
| - | + | ||
| - | **Declarations** define the style properties and their values that should be applied to the selected elements. A declaration consists of a property (e.g., '' | + | |
| - | + | ||
| - | Let's see it in a very simple example: | + | |
| - | + | ||
| - | <code XML> | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | p { | + | |
| - | color: blue; | + | |
| - | } | + | |
| - | </ | + | |
| - | </ | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | </ | + | |
| - | </ | + | |
| - | ==== 3. Defining CSS styles ==== | + | ==== Defining CSS styles ==== |
| CSS offers three main methods for applying styles to HTML documents: **inline**, **embedded**, | CSS offers three main methods for applying styles to HTML documents: **inline**, **embedded**, | ||
| Line 90: | Line 63: | ||
| <code CSS> | <code CSS> | ||
| + | /* styles.css */ | ||
| p { | p { | ||
| color: blue; | color: blue; | ||
| Line 97: | Line 71: | ||
| External CSS is ideal for websites because it allows for a consistent style across multiple pages, simplifies maintenance, | External CSS is ideal for websites because it allows for a consistent style across multiple pages, simplifies maintenance, | ||
| - | ==== 4. CSS comments | + | |
| + | ==== Selectors and declarations ==== | ||
| + | |||
| + | CSS is built around selectors and declarations. | ||
| + | |||
| + | |||
| + | {{: | ||
| + | |||
| + | **Selectors** specify which HTML elements the styles should target. They can be as simple as selecting an element by its name (e.g., '' | ||
| + | |||
| + | **Declarations** define the style properties and their values that should be applied to the selected elements. A declaration consists of a property (e.g., '' | ||
| + | |||
| + | Let's see it in a very simple example: | ||
| + | |||
| + | <code XML> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | p { | ||
| + | color: blue; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | === CSS comments === | ||
| Comments in CSS are used to add notes, explanations, | Comments in CSS are used to add notes, explanations, | ||
| Line 109: | Line 115: | ||
| </ | </ | ||
| - | ==== 5. Selecting elements, classes and IDs ==== | + | ==== Selecting elements, classes and IDs ==== |
| CSS selectors are patterns used to select and style HTML elements. Three common types of selectors are **element**, | CSS selectors are patterns used to select and style HTML elements. Three common types of selectors are **element**, | ||
| Line 183: | Line 189: | ||
| | ID selector | | ID selector | ||
| - | ==== 6. Cascading ==== | + | ==== Role of span and div elements ==== |
| - | + | ||
| - | By default, when multiple rules target the same selector, the later rule will override the previous one: | + | |
| - | + | ||
| - | <code XML> | + | |
| - | < | + | |
| - | < | + | |
| - | p { | + | |
| - | color: red; | + | |
| - | } | + | |
| - | + | ||
| - | p { | + | |
| - | color: green; | + | |
| - | } | + | |
| - | </ | + | |
| - | </ | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 7. Role of span and div elements ==== | + | |
| The **span** and **div** elements are fundamental components in HTML used for grouping and organizing content. They serve as containers that can be styled using CSS. | The **span** and **div** elements are fundamental components in HTML used for grouping and organizing content. They serve as containers that can be styled using CSS. | ||
| Line 223: | Line 208: | ||
| </ | </ | ||
| - | ==== 8. Background styling ==== | + | ==== Text styling and element sizing ==== |
| + | |||
| + | There are several properties available for text formatting. | ||
| + | |||
| + | <code CSS> | ||
| + | p { | ||
| + | font-family: | ||
| + | font-size: 16px; | ||
| + | font-weight: | ||
| + | font-style: italic; | ||
| + | text-decoration: | ||
| + | text-align: justify; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | If you are interested, [[https:// | ||
| + | |||
| + | The '' | ||
| + | |||
| + | <code CSS> | ||
| + | img { | ||
| + | width: 100px; | ||
| + | height: 100px; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Background styling | ||
| Background styling in CSS allows developers to customize the appearance of an element' | Background styling in CSS allows developers to customize the appearance of an element' | ||
| Line 237: | Line 248: | ||
| </ | </ | ||
| - | ==== 9. Margin, padding | + | The '' |
| + | |||
| + | <code CSS> | ||
| + | div { | ||
| + | border: 1px solid red; | ||
| + | border-radius: | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Box model ==== | ||
| The **box model** is a fundamental concept in CSS that describes how elements are structured and spaced on a webpage. Every HTML element is represented as a rectangular box consisting of four distinct areas: **margin**, **border**, **padding**, | The **box model** is a fundamental concept in CSS that describes how elements are structured and spaced on a webpage. Every HTML element is represented as a rectangular box consisting of four distinct areas: **margin**, **border**, **padding**, | ||
| Line 243: | Line 263: | ||
| {{: | {{: | ||
| - | The '' | + | The '' |
| + | |||
| + | ==== Margin and padding ==== | ||
| The example below shows how to format a button. Try what happens when you change the button' | The example below shows how to format a button. Try what happens when you change the button' | ||
| Line 271: | Line 293: | ||
| </ | </ | ||
| - | ==== 10. Setting width and height | + | ==== Cascading |
| - | The '' | + | By default, when multiple rules target |
| - | < | + | < |
| - | img { | + | < |
| - | | + | |
| - | | + | p { |
| - | } | + | color: red; |
| + | } | ||
| + | |||
| + | p { | ||
| + | color: green; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| </ | </ | ||
| - | ==== 11. Text styling | + | ==== Specificity |
| - | There are several properties available | + | When different selectors target the same element and define different values |
| - | <code CSS> | + | For the selector types introduced above, the order of priority is: |
| - | p { | + | |
| - | font-family: Arial; | + | |
| - | font-size: 16px; | + | |
| - | font-weight: | + | |
| - | font-style: italic; | + | |
| - | text-decoration: underline; | + | * **ID selectors**, |
| - | | + | * **Class selectors**, |
| - | } | + | * **Element selectors**, |
| + | |||
| + | Let's see it in a simple example: | ||
| + | |||
| + | <code XML> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | | ||
| + | < | ||
| + | #main-text { | ||
| + | color: red; | ||
| + | } | ||
| + | |||
| + | .highlight { | ||
| + | color: green; | ||
| + | } | ||
| + | |||
| + | p { | ||
| + | color: blue; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | <p id=" | ||
| + | <p class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| </ | </ | ||
| + | All three selectors match the first paragraph, but the ID selector wins, so its text is red. The second paragraph matches the class and element selectors, so the class selector makes it green. Only the element selector matches the third paragraph, making it blue. The element selector appears last, but this does not allow it to override the more specific selectors. | ||
| + | |||
| + | For selectors that combine several parts, specificity is compared by counting **IDs**, then **classes**, | ||
| + | |||
| + | If two competing selectors have the **same specificity**, | ||
| + | |||
| + | **Inline styles** normally take priority over normal declarations in stylesheets. The '' | ||
| - | ==== 12. Exercise ==== | + | ==== Exercise ==== |
| - | Create the following webpage | + | Create the following webpage using HTML and CSS. Use embedded |
| - | Feel free to use online resources (e.g. [[https:// | + | Feel free to use online resources (e.g. [[https:// |
| {{: | {{: | ||
| Line 318: | Line 380: | ||
| John von Neumann | John von Neumann | ||
| - | Image: https:// | + | Image: https:// |
| Born: December 28, 1903 | Born: December 28, 1903 | ||
tanszek/oktatas/techcomm/cascading_style_sheets.1727775450.txt.gz · Last modified: by kissa
