HTML CSS Concepts
Tags: html, css, webdev
Published: 2023-03-02 06:48:34


The following are some of the concepts that are included :

- Box Model
- Inline versus Block Elements
- Positioning: Relative/Absolute
- Common CSS structural classes
- Common CSS syling classes
- CSS Specificity
- CSS Responsive Queries
- Flexbox/Grid
- Common header meta tags
- Open Graph Tags

##1. Box Model

According to the box model concept, every element on a page is a rectangular box and may have width, height, padding, borders, and margins.

It refers to the way in which a box element is rendered on a web page. 

It consists of the content area, padding area, border area, and margin area.

Here is a breakdown of each component of the box model:

###Content area: 

This is where the actual content of the element is displayed. 

The content area is determined by the height and width properties.

###Padding area: 

This is the space between the content and the border. 

Padding is used to add space around the content of an element. 

The padding area is determined by the padding property.

###Border area: 

This is the area around the padding and content. 

The border is used to add a border around an element. 

The border area is determined by the border property.

###Margin area: 

This is the space between the border and the outside edge of the element. 

Margin is used to add space between elements. 

The margin area is determined by the margin property.

**Total width**: 

margin-right + border-right + padding-right + width + 
 padding-left + border-left + margin-left

**Total height**: 

margin-top + border-top + padding-top + height + 
padding-bottom + border-bottom + margin-bottom

Here are some examples of how the box model is used in CSS:

**Example 1**: Adding padding and border to an element


```sh
<div class="box">
  This is some text in a box.
</div>
```

```sh
.box {
  padding: 10px;
  border: 1px solid black;
}
```

In this example, we've added padding and a border to the div element. 

The padding property adds 10 pixels of space around the content of the element, and the border property adds a 1-pixel border around the element.

**Example 2**: Setting the height and width of an element


```sh
<div class="box">
  This is some text in a box.
</div>
```

```sh
.box {
  width: 200px;
  height: 100px;
  border: 1px solid black;
}
```

In this example, we've set the width and height properties of the div element. 

This determines the size of the content area of the element. 

We've also added a 1-pixel border around the element.

**Example 3**: Adding margin to an element

```sh

<div class="box1">
  This is box 1.
</div>
<div class="box2">
  This is box 2.
</div>
```

```sh
.box1 {
  border: 1px solid black;
  margin: 10px;
}

.box2 {
  border: 1px solid black;
  margin: 20px;
}
```

In this example, we've added margin to two div elements. 

The margin property adds space between the border of the element and the next element on the page. 

In this case, box1 has a margin of 10 pixels and box2 has a margin of 20 pixels.

## 2. Inline versus Block Elements

In HTML and CSS, elements can be classified as either inline or block elements, depending on how they are displayed on the webpage.

### Block Elements:

Block-level elements are those which create a rectangular block on the webpage that occupies the entire width of their parent container. 

Examples of block-level elements include div, p, h1, ul, ol, table, and form. 

Block-level elements can have margin, padding, and borders applied to them, and they will start on a new line.

```sh
<div>
  <h1>This is a heading</h1>
  <p>This is a paragraph.</p>
  <ul>
    <li>List item 1</li>
    <li>List item 2</li>
  </ul>
</div>
```

In this example, the div, h1, p, and ul elements are all block-level elements, and will each start on a new line, taking up the full width of their parent container.

### Inline Elements:

Inline elements are those which are displayed inline with the text. 

Examples of inline elements include a, span, img, and button. 

Inline elements do not start on a new line, and their width is determined by their content.

```sh
<p>This is a paragraph with an <a href="#">inline link</a>.</p>
```

In this example, the p and a elements are both inline. 

The a element is displayed inline with the text of the paragraph, and does not start on a new line.


## 3. Positioning: Relative/Absolute


In CSS, positioning refers to the way elements are positioned on the web page. 

There are different types of positioning available in CSS, including relative and absolute positioning.

### Relative Positioning:

Relative positioning is the default positioning of elements on a web page. 

With relative positioning, an element is positioned relative to its normal position in the document flow. 

This means that the element will still take up space in the normal document flow, but it can be moved around using CSS properties such as top, bottom, left, and right.

```sh
div {
  position: relative;
  top: 20px;
  left: 50px;
}
```

In this example, the div element is set to 
position: relative, which means it will still take up space in the normal document flow. 

However, it has also been moved 20 pixels down and 50 pixels to the right using the top and left properties.

### Absolute Positioning:

Absolute positioning allows an element to be positioned relative to its nearest positioned ancestor, or relative to the initial containing block if no positioned ancestor is found. 

With absolute positioning, the element is taken out of the normal document flow, and it will not affect the position of other elements on the page.

```sh
div {
  position: absolute;
  top: 20px;
  left: 50px;
}
```

In this example, the div element is set to 
position: absolute, which means it will be taken out of the normal document flow. 

It has also been positioned 20 pixels down and 50 pixels to the right of its nearest positioned ancestor element.

It's important to note that if an element is absolutely positioned, its position will be fixed relative to the browser window, and will not change if the user scrolls the page. 

This can be useful for creating fixed position elements such as navigation bars or headers.


##4. Common CSS structural classes

There are several common CSS structural classes that are used to style web page elements. 

Here are some examples:

### Container: 

This class is used to define the outermost element of a web page. 

It is typically used to set the maximum width of the page and center its content.
 

```sh
.container {
  max-width: 1200px;
  margin: 0 auto;
}
```

### Grid: 

This class is used to create a grid layout for a web page. 

It is typically used to divide the page into rows and columns to organize content.
 

```sh
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 20px;
}

```

### Flex: 

This class is used to create a flexible layout for a web page. 

It is typically used to align and distribute elements within a container.
 

```sh
.flex {
  display: flex;
  justify-content: center;
  align-items: center;
}

```

### Header: 

This class is used to style the header section of a web page. 

It is typically used to set the background color, font size, and font style of the heading.
 

```sh
.header {
  background-color: #333;
  color: #fff;
  font-size: 24px;
  font-family: Arial, sans-serif;
}
```

### Navigation: 

This class is used to style the navigation menu of a web page. 

It is typically used to set the background color, font size, and font style of the menu items.
 

```sh
.navigation {
  background-color: #f1f1f1;
  font-size: 16px;
  font-family: Arial, sans-serif;
}
```

### Footer: 

This class is used to style the footer section of a web page.

It is typically used to set the background color, font size, and font style of the footer.
 

```sh
.footer {
  background-color: #333;
  color: #fff;
  font-size: 16px;
  font-family: Arial, sans-serif;
  text-align: center;
  padding: 20px;
}
```

### Section: 

This class is used to create a section of a web page. 

It is typically used to group related content together and apply styling to the section as a whole.
 

```sh
.section {
  background-color: #f1f1f1;
  padding: 20px;
}
```

### Button: 

This class is used to style a button element on a web page. 

It is typically used to set the background color, font size, and font style of the button.
 

```sh
.button {
  background-color: #333;
  color: #fff;
  font-size: 16px;
  font-family: Arial, sans-serif;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
}
```

### Form: 

This class is used to style a form element on a web page. 

It is typically used to set the width, background color, and padding of the form.
 

```sh
.form {
  width: 100%;
  background-color: #f1f1f1;
  padding: 20px;
}
```

### Card: 

This class is used to create a card element on a web page. 

It is typically used to group related content together and apply styling to the card as a whole.
 

```sh
.card {
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin: 20px;
}
```

In this example, we've set the background color of the card to white, added a 1-pixel solid border with a light gray color, and set the border radius to 5 pixels to give the card rounded corners. 

We've also added a box shadow to give the card a slight drop shadow effect. 

Finally, we've added padding and margin to create space between the content of the card and other elements on the page.

To use the "card" class in HTML, you would simply add the "card" class to a div element that contains the content you want to display in the card. 

Here's an example:


```sh
<div class="card">
  <h2>Card Title</h2>
  <p>This is some example content for the card.</p>
  <button>Click me</button>
</div>
```

In this example, we've created a div element with the "card" class and added a heading, paragraph, and button element inside it. 

When we view the page in a web browser, the content will be displayed inside a card with the styling we defined in the CSS.


## 5. Common CSS syling classes

CSS styling classes are used to apply a particular style or effect to an HTML element. 

Here are some commonly used CSS styling classes:

###Text styles: 

These classes are used to define the font size, font weight, font family, color, and text alignment of text content.
 

```sh
.text-center {
  text-align: center;
}

.text-bold {
  font-weight: bold;
}

.text-large {
  font-size: 24px;
}

.text-serif {
  font-family: serif;
}

.text-muted {
  color: #999;
}
```

###Background and color: 

These classes are used to define the background color and text color of an HTML element.
 

```sh
.bg-primary {
  background-color: #007bff;
  color: #fff;
}

.bg-secondary {
  background-color: #6c757d;
  color: #fff;
}

.bg-light {
  background-color: #f8f9fa;
}

.bg-dark {
  background-color: #343a40;
  color: #fff;
}
```

###Borders: 

These classes are used to define the border style, width, color, and radius of an HTML element.
 


```sh
.border {
  border: 1px solid #ccc;
}

.border-top {
  border-top: 1px solid #ccc;
}

.border-radius {
  border-radius: 5px;
}
```

### Padding and margin: 

These classes are used to define the padding and margin of an HTML element.
 

```sh
.p-4 {
  padding: 16px;
}

.m-4 {
  margin: 16px;
}

```

### Display and positioning:

These classes are used to control the display and positioning of an HTML element.
 

```sh
.d-flex {
  display: flex;
}

.d-inline-block {
  display: inline-block;
}

.position-relative {
  position: relative;
}

.position-absolute {
  position: absolute;
  top: 0;
  right: 0;
}
```

### Hover and active states: 

These classes are used to apply styles to an HTML element when the user hovers over or clicks on it.
 

```sh
.hover-bg-primary:hover {
  background-color: #007bff;
  color: #fff;
}

.active-bg-primary:active {
  background-color: #0062cc;
  color: #fff;
}
```

These are just a few examples of the many CSS styling classes available. 

By combining these classes and customizing their properties, you can create a wide range of styles and effects for your web pages.


## 6. CSS Specificity

CSS specificity is a measure of how specific a selector is in targeting a particular HTML element. 

It determines which styles are applied to an element when there are conflicting styles defined in different stylesheets or within the same stylesheet.

CSS selectors have different levels of specificity based on the elements, classes, and IDs that they target. 

Here's a breakdown of how specificity is calculated:

- Element selectors have the lowest specificity and are worth 1 point.
- Class selectors are worth 10 points.
- ID selectors are worth 100 points.
- Inline styles have the highest specificity and are worth 1000 points.

When multiple selectors target the same element, the selector with the highest specificity is applied. 

For example, if you have the following CSS:


```sh
p {
  color: red;
}

#paragraph {
  color: blue;
}

```
And this HTML:


```sh
<p id="paragraph">This is a paragraph.</p>

```

The color of the text will be blue because the ID selector for "#paragraph" has a higher specificity than the element selector for "p".

If multiple selectors have the same specificity, the one that appears last in the CSS document takes precedence. 

For example:


```sh
p {
  color: red;
}

#paragraph {
  color: blue;
}

p {
  font-size: 24px;
}

```

In this case, the color of the text will be blue and the font size will be 24px because the second "p" selector overrides the font size defined by the first "p" selector.

It's important to be aware of CSS specificity when writing stylesheets to ensure that the correct styles are applied to your HTML elements. 

In general, it's best to use the least specific selectors possible to target elements, and to avoid using inline styles whenever possible.


## 7. CSS Responsive Queries

CSS responsive queries are used to create styles that adapt to different screen sizes and devices, allowing your web pages to look good on a variety of devices such as desktops, laptops, tablets, and smartphones.

There are several ways to create responsive designs using CSS, but the most common technique is using media queries.
 
Media queries allow you to define different styles for different screen sizes and resolutions. 

Here's an example:


```sh
/* Styles for screens smaller than 768px */
@media (max-width: 767px) {
  .container {
    width: 100%;
    padding: 20px;
  }
  .heading {
    font-size: 24px;
  }
}

/* Styles for screens larger than 768px */
@media (min-width: 768px) {
  .container {
    width: 700px;
    margin: 0 auto;
    padding: 40px;
  }
  .heading {
    font-size: 36px;
  }
}
```

In this example, we have defined two media queries. 

The first query targets screens smaller than 768px and applies styles to the "container" and "heading" elements to make them more readable on small screens. 

The second query targets screens larger than 768px and applies different styles to the same elements to make them look better on larger screens.

Media queries can be based on a variety of factors such as screen size, resolution, device orientation, and more. 

Here are some common media query options:

- **max-width**: 

Sets the maximum width of the screen.

- **min-width**: 

Sets the minimum width of the screen.

- **orientation**: 

Sets the device orientation to portrait or landscape.

- **max-device-width**: 

Sets the maximum width of the device screen.

- **min-device-width**: 

Sets the minimum width of the device screen.

By using media queries in your CSS, you can create responsive designs that look good on any device. 

However, it's important to test your styles on a variety of devices to ensure that they work as expected.


## 8. Flexbox/Grid

Flexbox and CSS Grid are two popular CSS layout systems used to create responsive and flexible designs. 

Here's a brief overview of each:

### Flexbox:

Flexbox is a one-dimensional layout system that is designed to make it easier to create flexible and responsive layouts. 

It allows you to align and distribute content within a container in a flexible way. 

Flexbox works by creating a flexible container that can hold a set of flexible items. 

You can define the size, order, and alignment of these items using various properties such as display, flex-direction, justify-content, align-items, and flex-wrap.

### CSS Grid:

CSS Grid is a two-dimensional layout system that is designed to create complex grid layouts that are responsive and flexible. 
It allows you to define rows and columns in a grid and place content in specific areas within the grid. 

CSS Grid works by creating a container that is divided into a set of rows and columns. 

You can define the size and position of these rows and columns using various properties such as grid-template-rows, grid-template-columns, grid-row-start, grid-column-end, and grid-gap.

Flexbox is best suited for creating one-dimensional layouts such as navigation menus, card layouts, and form layouts.

CSS Grid is best suited for creating complex two-dimensional layouts such as magazine-style layouts, image galleries, and dashboard layouts. 

However, both Flexbox and CSS Grid can be used together to create more complex and flexible layouts.

Overall, Flexbox and CSS Grid are both powerful layout systems that can help you create responsive and flexible designs. 

It's important to choose the right layout system based on your specific needs and to use them appropriately to achieve the desired layout.


## 9. Common header meta tags

Header meta tags are an important part of a web page's HTML code that provide information about the page's content to search engines, social media platforms, and other web services. 

Here are some of the most common header meta tags and their purposes:

```sh
<meta charset="UTF-8">
```

Specifies the character encoding used by the page. UTF-8 is the most widely used character encoding and supports all Unicode characters.

```sh
<meta name="viewport" content="width=device-width, initial-scale=1.0">
```

Specifies the viewport settings for mobile devices, which affects the layout and scaling of the page on different devices. 

This tag tells the browser to use the width of the device as the width of the viewport, and to set the initial zoom level to 1.

```sh
<title>Page Title</title>
```

Specifies the title of the page, which appears in the browser's title bar and is used by search engines as the title of the search result.

```sh
<meta name="description" content="Page description">
```

Specifies a brief description of the page's content, which appears in search results and is used by search engines to understand the page's content.

```sh
<meta name="keywords" content="keyword1, keyword2, keyword3">
```

Specifies a comma-separated list of keywords that describe the page's content, which can help search engines understand the page's topic and improve its ranking.

```sh
<meta name="robots" content="index, follow">
```

Specifies whether search engines should index the page and follow its links. 

By default, search engines will index and follow all pages.

```sh
<link rel="canonical" href="https://www.example.com/">
```

Specifies the canonical URL of the page, which is the preferred URL that search engines should use to index the page. 

This is useful when there are multiple URLs that can access the same content.

## 10. Open Graph Tags

Open Graph tags are meta tags that are used to provide social media platforms and search engines with information about a web page. 

They are used to specify the title, description, and image to be used when a page is shared on social media or displayed in search results. 

Here are some examples of Open Graph tags :

### Title Tag:

The title tag specifies the title of the page, which is displayed in search results and on social media platforms.
 
Here's an example:

```sh
<head>
  <title>My Website Title</title>
  <meta property="og:title" content="My Website Title"/>
</head>
```

### Description Tag:

The description tag provides a summary of the page's content and is displayed in search results and on social media platforms. 

Here's an example:

```sh
<head>
  <meta name="description" content="A description of my website."/>
  <meta property="og:description" content="A description of my website."/>
</head>
```

### Image Tag:

The image tag specifies the image to be displayed when the page is shared on social media platforms. 

Here's an example:

```sh
<head>
  <meta property="og:image" content="https://example.com/image.jpg"/>
</head>
```

### URL Tag:

The URL tag specifies the URL of the page, which is displayed in search results and on social media platforms. 

Here's an example:

```sh
<head>
  <link rel="canonical" href="https://example.com"/>
  <meta property="og:url" content="https://example.com"/>
</head>
```

Note that these Open Graph tags are typically placed in the head section of the HTML document, but they can also be included in CSS files using the @import rule. 

However, it's not recommended to include them in CSS files as it can cause problems with some social media platforms.


## Conclusion

In conclusion, understanding the Box Model is essential to creating a well-structured webpage. 

Inline and Block elements offer different styling options and should be chosen accordingly. 

Positioning, both Relative and Absolute, are useful tools to create dynamic layouts. 

CSS Specificity determines how styles are applied and should be used with caution. 

CSS Responsive Queries allow websites to adapt to different screen sizes and devices. 

Flexbox and Grid are powerful tools for creating complex layouts. 

Lastly, using common header meta tags and Open Graph tags can enhance a website's social media presence and search engine visibility. 

By utilizing these CSS techniques and best practices, you can create a visually appealing and engaging website that works across multiple platforms and devices.


##References 

Box Model:


https://www.w3schools.com/css/css_boxmodel.asp

Inline versus Block Elements:

https://css-tricks.com/when-do-you-use-inline-block/

Positioning: Relative/Absolute:

https://developer.mozilla.org/en-US/docs/Web/CSS/position

Common CSS Structural Classes:

https://developer.mozilla.org/en-US/docs/Web/CSS/container

Common CSS Styling Classes:

https://css-tricks.com/almanac/

CSS Specificity:

https://specificity.keegan.st/

CSS Responsive Queries:

https://www.w3schools.com/css/css_rwd_mediaqueries.asp

Flexbox/Grid:

https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://css-tricks.com/snippets/css/complete-guide-grid/

Common Header Meta Tags:

https://www.w3schools.com/tags/tag_head.asp

Open Graph Tags:

https://ogp.me/