Learn and Explore CSS
Published: 2023-07-05 11:38:36

# Type of CSS

### Three Ways to Insert CSS

There are three ways of inserting a style sheet:

- External CSS
- Internal CSS
- Inline CSS

### Inline CSS

**Use style =”” attribute**

An inline style may be used to apply a unique style for a single element.

To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property.

**Example:**

Inline styles are defined within the "style" attribute of the relevant element:

```html
<!DOCTYPE html>
<html>
<body>

<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>

</body>
</html>
```

### **Internal CSS**

**Use <style> tag to write**

An internal style sheet may be used if one single HTML page has a unique style.

The internal style is defined inside the <style> element, inside the head section.

**Example:**

Internal styles are defined within the <style> element, inside the <head> section of an HTML page:

```html
<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-color: linen;
}

h1 {
  color: maroon;
  margin-left: 40px;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
```

### **External CSS**

Ues .css file to write 

With an external style sheet, you can change the look of an entire website by changing just one file!

Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section.

**Example:**

External styles are defined within the <link> element, inside the <head> section of an HTML page:

```html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
```

## CSS Selectors:

| Selectors Name | Description  |
| --- | --- |
| Class selector | 1). The class selector in CSS is denoted by dot (.) followed by the class name. 2). It is used to select and style multiple HTML element that share the same class. 3). to apply a class to an element, the class attribute is used in the HTML markup. 4) It is recommended to use meaningful class names is improve code readability.  5). Class selector offer a higher level og specificity compared to tag selectors. |
| id selector  | 1). The ID selector in CSS is denoted by hash 2) followed by ID name. 2). IT is used to select and style a single unique HTML element on a webpage. 3). To apply an ID to an element, the id attribute is used in the HTML markup. 4). IDs must be unique within a webpage, as duplicate IDs can load to incorrect styles or scripting behavior.  5). ID selectors have a higher specificily compared to class or tag selectors, making them more powerful for targeting specific elements. |
| Element selector | 1). The element selector in CSS targets HTML elements based on their tag name. 2). To  use an element selector, simply specify the element’s tag name in the CSS. 3). Element selectors have lower specificity compared to class or ID selectors.  |
| Universal selector | 1). The universal selector in CSS is denoted by an astrisk (*). 2). It selects and applies styles to all elements on a webpage. 3). The universal selector has the lowest specificity, meaning it has the least weight in overriding other selectors. 4). It can be used to apply common styles to all elements, such as setting default margins or padding. |
| Grouping selector  |  1). The grouping selector in CSS allows you to select and apply style to multiple selectors at ones. 2). It helps you avoid repeating styles for indicidual selectors by combibing them into a single rule. 3). The grouping selector is denoted by a comma (,) between the selectors. 4). Selectors separated by commas withn yhe grouping selector will share the same style.  |
|  Attributes selector | 1). Attribute selectors in CSS allow you to target elements based on their attributes and attribute values. 2). The basic syntax of an attribute selector is [attribute], where “attribute” is the name of the attribute you want to select. 3). Attribute selectors are useful for selecting elements with specific attribute values, such as input fileds with a specific type, links with a  specific target etc. |

## ID VS ClASS

| ID | CLASS |
| --- | --- |
| Each element can have only one ID | You can use the same class on multiple elements. |
| an ID selector is a name preceded by a hash character (”#”) | a class selector is a name preceded by a full stop (”.”) |
|  |  |

## **CSS Box Model**

In CSS, the term "box model" is used when talking about design and layout.

The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model:

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0efcceef-23cc-4581-902a-da6f7c864bb9/Untitled.png)

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/182ce02c-7fe5-4511-af20-a2e9224723e2/Untitled.png)

Explanation of the different parts:

- **Content** - The content of the box, where text and images appear
- **Padding** - Clears an area around the content. The padding is transparent
- **Border** - A border that goes around the padding and content
- **Margin** - Clears an area outside the border. The margin is transparent

The box model allows us to add a border around elements, and to define space between elements.

**Example:**

Demonstration of the box model:

```html
div {
  width: 300px;
  border: 15px solid green;
  padding: 50px;
  margin: 20px;
}
```

## **CSS Syntax**

A CSS rule consists of a selector and a declaration block.

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/610426f0-fe11-4490-9b82-f4bbf068544c/Untitled.gif)

The selector points to the HTML element you want to style.

The declaration block contains one or more declarations separated by semicolons.

Each declaration includes a CSS property name and a value, separated by a colon.

Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces.

**Example:**

In this example all <p> elements will be center-aligned, with a red text color:

```html
p {
  color: red;
  text-align: center;
}
```

**Example Explained:**

- `p` is a selector in CSS (it points to the HTML element you want to style: <p>).
- `color` is a property, and `red` is the property value
- `text-align` is a property, and `center` is the property value

## CSS Background Position

The background-position property is used to control the position of an image in the background.

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/041da70f-7284-4a08-a6a5-6e551eeea912/Untitled.png)

## CSS Margin and Padding

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6fec3fb0-642a-4df0-8248-5f4ec6015107/Untitled.png)

## CSS max/min-height

### max-height

max-height is a CSS property that allows you to set the maximum height that an element can have.

It restricts the element's height from exceeding the specified value

For example, if you set max-height: 200px; on a <div> element, it will not grow beyond a height of 200 pixels, regardless of its content.

### min-height

min-height is a CSS property that allows you to set the minimum height that an element should have.

It ensures that the element's height is at least the specified value

set max-height: ement, it will not t of 200 pixels, tent.

For example, if you set min-height: 100px; on a <div> element, it will be at least 100 pixels tall, even if its content is smaller.

## CSS box-shadow

**box-shadow is a CSS property that allows you to add a shadow effect to an element’s box ( such as a div, button ot image)**

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8311cc8d-4653-4556-8411-6acb7f309136/Untitled.png)

## CSS border-radius

**border-radius is a CSS property that allows you to create rounded corners on an element’s border**

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/3e4e4a53-f65e-40a6-842b-b874f7481936/Untitled.png)

## Display: inline, block, inline-block

![Untitled](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/208963be-a8af-42a4-9211-1f332560103d/Untitled.png)

# Practice Task

- [x]  **Task 1**

### CSS types Challenge:

- Create an HTML document with three paragraphs.
- Apply CSS styles to each paragraph using inline, internal and external.
- [ ]  **Task 2**

### **Text styling Challenge:**

- Design a webpage with a heading, paragraph and a list.
- Apply CSS styles to change the text color, alignment, font-size, font-family and font style for different elements.

## **Task 3**

### Selectors Challenge:

- Create an HTML document with various elements, including heading, paragraphs and divs.
- Use different CSS selectors (id, class, universal, tag) to apply unique styls to specific elements.

## **Task 4**

### Background Styleing Challenge:

- Design a webpage with multiple sections/divs.
- Apply CSS style to set different background color, image, repeats, position and size for each section.

## **Task 5**

### Borders Challenge:

- Create a webpage with various elements, such as divs, paragraphs, and images.
- Apply CSS styles to add borders with different widths, colors, border-radius, and box-shadow effects to the elements.

## **Task 5**

- Bulid a webpage with different elements (e.g., image, headings, paragraph, buttonss).
- Apply CSS styles to set the display property to none, hidden, inline, block and inline-block for specific elements.