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 08:55] – [2. Selectors and declarations] kissa | tanszek:oktatas:techcomm:cascading_style_sheets [2024/10/01 10:32] (current) – [3. Defining CSS styles] kissa | ||
---|---|---|---|
Line 37: | Line 37: | ||
==== 3. Defining CSS styles ==== | ==== 3. Defining CSS styles ==== | ||
+ | |||
+ | CSS offers three main methods for applying styles to HTML documents: **inline**, **embedded**, | ||
+ | |||
+ | Choosing the right method depends on the project requirements. Inline CSS is useful for quick fixes or unique styling, embedded CSS works well for single-page designs, and external CSS is best for larger projects with multiple pages, as it promotes code reusability and maintainability. | ||
=== Inline === | === Inline === | ||
- | Inline styles | + | Inline |
<code XML> | <code XML> | ||
- | <p style=" | + | <p style=" |
+ | < | ||
</ | </ | ||
+ | |||
+ | Inline CSS is useful for making quick style changes to a specific element without affecting other parts of the page. However, it is not ideal often, as these styles are not easily reusable in other parts of the page. | ||
=== Embedded === | === Embedded === | ||
- | Styles can also be embedded | + | Embedded CSS, also known as internal CSS, is used within the ''< |
<code XML> | <code XML> | ||
Line 55: | Line 62: | ||
h1 { | h1 { | ||
color: blue; | color: blue; | ||
+ | } | ||
+ | | ||
+ | .bold { | ||
+ | font-weight: | ||
} | } | ||
</ | </ | ||
Line 60: | Line 71: | ||
< | < | ||
< | < | ||
+ | < | ||
</ | </ | ||
</ | </ | ||
- | === External File === | + | Embedded CSS is beneficial for styling a single page because it keeps all the CSS rules in one place within the document. However, it is less efficient when working with multiple pages, as styles have to be repeated in each document. |
- | Styles can be imported from an external | + | === External === |
+ | |||
+ | External CSS is defined in a separate '' | ||
<code XML> | <code XML> | ||
Line 73: | Line 87: | ||
</ | </ | ||
- | ==== 4. CSS comments ==== | + | An external stylesheet might include rules like: |
- | + | ||
- | CSS comments are placed between ''/ | + | |
<code CSS> | <code CSS> | ||
- | /* This is a CSS comment, | + | /* styles.css */ |
- | which can span multiple lines. */ | + | p { |
+ | color: blue; | ||
+ | line-height: | ||
+ | } | ||
</ | </ | ||
- | ==== 5. Element, ID, and class Selectors | + | External CSS is ideal for websites because it allows for a consistent style across multiple pages, simplifies maintenance, |
+ | ==== 4. CSS comments | ||
- | * **Element selector**: Applies | + | Comments in CSS are used to add notes, explanations, |
- | * **ID selector**: Targets | + | |
- | * **Class selector**: Targets one or more elements with a specific class. | + | |
<code CSS> | <code CSS> | ||
- | /* Element selector, applies to all paragraphs */ | + | /* Formatting rules for paragraphs */ |
p { | p { | ||
- | color: | + | color: |
+ | font-size: 16pt; /* Set font size to 16 pts */ | ||
} | } | ||
+ | </ | ||
- | /* ID selector, applies to the element with the specific ID " | + | ==== 5. Selecting elements, classes and IDs ==== |
- | #header { | + | |
- | font-size: 24px; | + | |
- | } | + | |
- | /* Class selector, | + | CSS selectors are patterns used to select and style HTML elements. Three common types of selectors are **element**, |
- | .button { | + | |
- | | + | An **element |
- | } | + | |
+ | <code XML> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | | ||
+ | p { | ||
+ | font-size: 16px; | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
</ | </ | ||
+ | |||
+ | A **class selector** targets one or more elements that have the same '' | ||
+ | |||
+ | <code XML> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | .highlight { | ||
+ | background-color: | ||
+ | font-weight: | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | <p class=" | ||
+ | < | ||
+ | <div class=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | An **ID selector** targets a single, unique HTML element by referencing its '' | ||
+ | |||
+ | <code XML> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | # | ||
+ | font-size: 24px; | ||
+ | text-align: center; | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | <h1 id=" | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | 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 | ||
==== 6. Cascading ==== | ==== 6. Cascading ==== | ||
- | When multiple rules target the same selector, the later rule will override the previous one. | + | By default, when multiple rules target the same selector, the later rule will override the previous one: |
<code XML> | <code XML> | ||
Line 126: | Line 205: | ||
</ | </ | ||
- | ==== 7. Role of '' | + | ==== 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 '' | ||
+ | |||
+ | <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.</ | + | |
- | < | + | |
</ | </ | ||
</ | </ | ||
Line 142: | Line 226: | ||
==== 8. Background styling ==== | ==== 8. Background styling ==== | ||
- | The '' | + | Background styling in CSS allows developers to customize |
<code CSS> | <code CSS> | ||
Line 156: | Line 240: | ||
==== 9. Margin, padding and border ==== | ==== 9. Margin, padding and border ==== | ||
- | The '' | + | The **box model** |
{{: | {{: | ||
- | < | + | The '' |
- | div { | + | |
- | margin: 10px; | + | The example below shows how to format a button. Try what happens when you change the button' |
- | padding: 20px; | + | |
- | border: 1px solid black; | + | < |
- | border-radius: | + | < |
- | } | + | < |
+ | < | ||
+ | | ||
+ | < | ||
+ | .styled-button { | ||
+ | color: green; | ||
+ | background-color: | ||
+ | | ||
+ | padding: 20px; | ||
+ | border: 1px solid red; | ||
+ | border-radius: | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | <button class=" | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
</ | </ | ||
Line 190: | Line 293: | ||
font-weight: | font-weight: | ||
font-style: italic; | font-style: italic; | ||
- | |||
text-decoration: | text-decoration: | ||
text-align: justify; | text-align: justify; | ||
} | } | ||
</ | </ | ||
+ | |||
+ | If you are interested, [[https:// | ||
==== 12. Exercise ==== | ==== 12. 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.1727772907.txt.gz · Last modified: 2024/10/01 08:55 by kissa