tanszek:oktatas:techcomm:cascading_style_sheets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:techcomm:cascading_style_sheets [2024/10/01 09:15] – [5. Element, ID, and class Selectors] kissatanszek:oktatas:techcomm:cascading_style_sheets [2024/10/01 10:32] (current) – [3. Defining CSS styles] kissa
Line 90: Line 90:
  
 <code CSS> <code CSS>
 +/* styles.css */
 p { p {
     color: blue;     color: blue;
Line 133: Line 134:
 </code> </code>
  
-A **class selector** targets one or more elements that have the same ''class'' attribute. Class selectors are defined using a period (''.'') followed by the class name. In this example, both the ''<p>'' and ''<div>'' elements with the class highlight will have a yellow background and bold font weight. Since classes can be reused across multiple elements, class selectors offer greater flexibility in styling.+A **class selector** targets one or more elements that have the same ''class'' attribute. Class selectors are defined using a period (''.'') followed by the class name. In this example, both the ''<p>'' and ''div'' elements with the class highlight will have a yellow background and bold font weight. Since classes can be reused across multiple elements, class selectors offer greater flexibility in styling.
  
 <code XML> <code XML>
Line 175: Line 176:
  
 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. 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 ''.'' and the name of the class, and ID selectors are marked with a ''#'' and the identifier:
 +
 +^ Selector type     ^ HTML example                   ^ CSS selector  |
 +| Element selector  | <p>Test</p>                    | p             |
 +| Class selector    | <p class="highlight">Test</p>  | .highlight    |
 +| ID selector       | <p id="main-text">Test</p>     | #main-text    |
 +
 ==== 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 196: Line 205:
 </code> </code>
  
-==== 7. Role of ''span'' and ''div'' elements ====+==== 7. Role of span and div elements ====
  
-* ''span'': An inline element, which is used to mark a portion of text. +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. 
-''div'': A block element, which is used to group multiple contents into a block.+ 
 +The ''span'' element is an **inline container**meaning it only takes up as much width as its content. It is typically used to style or manipulate small portions of text. In this example, the ''span'' is used to color only the word "blue" without affecting the rest of the text. It is perfect for applying specific styles or behaviors to parts of a sentence, such as changing text color or font weight: 
 + 
 +<code XML> 
 +<p>This is a <span style="color: blue;">blue</span> word in a paragraph.</p> 
 +</code> 
 + 
 +The ''div'' element is a **block-level container**meaning it takes up the full width of its parent element by default and creates a line break before and after it. It is commonly used to group larger sections of content, such as paragraphs, images, or other block-level elementsFor example:
  
 <code XML> <code XML>
-<p>This is a <span style="color: red">red</span> text.</p> +<div style="background-colorlightgrey;"> 
-<div style="font-weightbold"> +    <h1>Section Title</h1
-    <p>This is bold text.</p+    <p>This paragraph is inside a div container.</p>
-    <p>So is this.</p> +
-    <p>And this too.</p>+
 </div> </div>
 </code> </code>
Line 212: Line 226:
 ==== 8. Background styling ==== ==== 8. Background styling ====
  
-The ''background'' property defines the background style of an element.+Background styling in CSS allows developers to customize the appearance of an element's background, enhancing the overall visual appeal of a webpage. There are several properties associated with background styling that can be used to control color, images, gradients, and positioningFor example:
  
 <code CSS> <code CSS>
Line 226: Line 240:
 ==== 9. Margin, padding and border ==== ==== 9. Margin, padding and border ====
  
-The ''margin'' is the outer space around an element, while the ''padding'' is the inner space between the content and the borderThe ''border'' defines the outline of the element. This is called the "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**, and the **content** itselfUnderstanding the box model is crucial for effective layout and design.
  
 {{:tanszek:oktatas:techcomm:pasted:20240905-153005.png}} {{:tanszek:oktatas:techcomm:pasted:20240905-153005.png}}
  
-<code CSS+The ''margin'' is the outer space around an element, while the ''padding'' is the inner space between the content and the border. The ''border'' defines the outline of the element. 
-div { + 
-    margin: 10px; +The example below shows how to format a button. Try what happens when you change the button's border, margin and padding properties! 
-    padding: 20px; + 
-    border: 1px solid black+<code XML
-    border-radius: 5px; +<!DOCTYPE html> 
-}+<html> 
 +<head> 
 +    <title>Styled Button Example</title> 
 +    <style> 
 +        .styled-button { 
 +            color: green; 
 +            background-color: lightgreen; 
 +            margin: 10px; 
 +            padding: 20px; 
 +            border: 1px solid red
 +            border-radius: 5px; 
 +        } 
 +    </style> 
 +</head> 
 +<body> 
 +    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p> 
 +    <button class="styled-button">Click me!</button> 
 +    <p>Maecenas sapien arcu, finibus eget dignissim quis, placerat vel ante.</p> 
 +</body> 
 +</html>
 </code> </code>
  
Line 260: Line 293:
     font-weight: bold;     font-weight: bold;
     font-style: italic;     font-style: italic;
- 
     text-decoration: underline;     text-decoration: underline;
     text-align: justify;     text-align: justify;
 } }
 </code> </code>
 +
 +If you are interested, [[https://www.w3schools.com/css/tryit.asp?filename=trycss_text|try more here]].
  
  
 ==== 12. Exercise ==== ==== 12. Exercise ====
  
-Create the following webpage by using HTML and CSS. Use inline CSS for formatting the text, and also apply class-based and id-based selectors!+Create the following webpage using HTML and CSS. Use embedded CSS, and format the content with inline, class-based and id-based selectors!
  
-Feel free to use online resources (e.g. [[https://www.w3schools.com/css/|W3Schools CSS tutorial]]) as help.+Feel free to use online resources (e.g. [[https://www.w3schools.com/css/|W3Schools CSS tutorial]]) as help. The raw text of the webpage can be found below the screenshot.
  
 {{:tanszek:oktatas:techcomm:pasted:20240905-153937.png?600}} {{:tanszek:oktatas:techcomm:pasted:20240905-153937.png?600}}
tanszek/oktatas/techcomm/cascading_style_sheets.1727774118.txt.gz · Last modified: 2024/10/01 09:15 by kissa