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:06] – [4. CSS comments] kissa | tanszek:oktatas:techcomm:cascading_style_sheets [2026/09/23 13:46] (current) – [Background styling and borders] 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 ==== | + | ==== Defining CSS styles ==== |
| - | + | ||
| - | 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; | + | |
| - | } | + | |
| - | </ | + | |
| - | </ | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 3. 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 59: | ||
| <code CSS> | <code CSS> | ||
| + | /* styles.css */ | ||
| p { | p { | ||
| color: blue; | color: blue; | ||
| Line 97: | Line 67: | ||
| 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 111: | ||
| </ | </ | ||
| - | ==== 5. Element, ID, and class Selectors | + | ==== Selecting elements, classes |
| - | * **Element selector**: Applies to all specified elements. | + | CSS selectors are patterns used to select and style HTML elements. Three common types of selectors are **element**, **class**, and **ID** |
| - | * **ID selector**: Targets a specific element based on its unique | + | |
| - | * **Class selector**: Targets one or more elements with a specific class. | + | |
| - | <code CSS> | + | An **element |
| - | /* Element | + | |
| - | p { | + | |
| - | | + | |
| - | } | + | |
| - | /* ID selector, applies to the element with the specific ID " | + | <code XML> |
| - | #header { | + | < |
| - | font-size: | + | < |
| - | } | + | < |
| - | + | | |
| - | /* Class selector, applies to all elements | + | p { |
| - | .button { | + | |
| - | | + | |
| - | } | + | |
| + | </ | ||
| + | </head> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| </ | </ | ||
| - | ==== 6. Cascading ==== | + | A **class selector** targets one or more elements that have the same '' |
| - | + | ||
| - | When multiple rules target | + | |
| <code XML> | <code XML> | ||
| + | < | ||
| + | < | ||
| < | < | ||
| < | < | ||
| - | | + | |
| - | color: | + | |
| + | font-weight: | ||
| } | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | <p class=" | ||
| + | < | ||
| + | <div class=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| - | p { | + | An **ID selector** targets a single, unique HTML element by referencing its '' |
| - | | + | |
| + | <code XML> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | # | ||
| + | | ||
| + | text-align: center; | ||
| } | } | ||
| </ | </ | ||
| </ | </ | ||
| < | < | ||
| - | < | + | |
| + | | ||
| </ | </ | ||
| + | </ | ||
| </ | </ | ||
| - | ==== 7. Role of '' | + | In summary, element selectors are used for broad styling, ID selectors offer targeted styling for unique elements, and class selectors provide versatility by enabling the styling of multiple elements at once. Understanding these selectors and how to use them together is key to writing efficient and maintainable CSS. |
| + | |||
| + | Element selectors are marked with the name of the member, class selectors are marked with a '' | ||
| + | |||
| + | ^ Selector type ^ HTML example | ||
| + | | Element selector | ||
| + | | Class selector | ||
| + | | ID selector | ||
| + | |||
| + | ==== 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 '' | ||
| + | |||
| + | <code XML> | ||
| + | < | ||
| + | </ | ||
| - | * '' | + | The '' |
| - | * '' | + | |
| <code XML> | <code XML> | ||
| - | < | + | <div style=" |
| - | <div style=" | + | <h1>Section Title</h1> |
| - | <p>This is bold text.</p> | + | <p>This paragraph |
| - | <p>So is this.</ | + | |
| - | < | + | |
| </ | </ | ||
| </ | </ | ||
| - | ==== 8. Background | + | ==== Text styling |
| - | The '' | + | 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 and borders ==== | ||
| + | |||
| + | Background styling in CSS allows developers to customize the appearance of an element' | ||
| <code CSS> | <code CSS> | ||
| Line 181: | Line 244: | ||
| </ | </ | ||
| - | ==== 9. Margin, padding and border ==== | + | The '' |
| - | + | ||
| - | The '' | + | |
| - | + | ||
| - | {{:tanszek: | + | |
| <code CSS> | <code CSS> | ||
| div { | div { | ||
| - | | + | border: 1px solid red; |
| - | padding: 20px; | + | |
| - | | + | |
| border-radius: | border-radius: | ||
| } | } | ||
| </ | </ | ||
| - | ==== 10. Setting width and height | + | ==== Box model ==== |
| - | The '' | + | The **box model** is a fundamental concept in CSS that describes how elements are structured |
| - | < | + | {{: |
| - | img { | + | |
| - | | + | The '' |
| - | | + | |
| - | } | + | ==== Margin and padding ==== |
| + | |||
| + | The example below shows how to format a button. Try what happens when you change the button' | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | | ||
| + | < | ||
| + | .styled-button { | ||
| + | color: green; | ||
| + | | ||
| + | | ||
| + | padding: 20px; | ||
| + | border: 1px solid red; | ||
| + | border-radius: | ||
| + | | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| </ | </ | ||
| - | ==== 11. Text styling | + | ==== Cascading |
| - | There are several properties available for text formatting. | + | By default, when multiple rules target the same selector, the later rule will override the previous one: |
| - | < | + | < |
| - | p { | + | < |
| - | | + | |
| - | | + | p { |
| - | | + | color: red; |
| - | | + | } |
| - | text-decoration: | + | p { |
| - | | + | color: green; |
| - | } | + | } |
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| </ | </ | ||
| + | ==== Specificity ==== | ||
| + | |||
| + | When different selectors target the same element and define different values for the same property, **specificity** helps the browser decide which declaration to apply. For ordinary CSS rules in the same stylesheet, outside cascade layers and without '' | ||
| + | |||
| + | For the selector types introduced above, the order of priority is: | ||
| + | |||
| + | * **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:// |
| {{: | {{: | ||
tanszek/oktatas/techcomm/cascading_style_sheets.1727773591.txt.gz · Last modified: by kissa
