WebDev Beginner
  • Welcome to TechAcademy
  • Project
    • Project Introduction
    • Minimal Requirements
    • More Ideas
    • Curriculum
  • Video Plan
    • Youtube
  • Web Development Fundamentals
    • How to use this section?
    • 2. HTML
    • 3. CSS
    • 4. JavaScript
    • 5. Interaction HTML, CSS and JavaScript
  • Tools
    • IDE - Visual Studio Code
    • API
  • ADDITIONAL RESOURCES
    • Git & GitHub
    • JSON
    • Font Awesome
Powered by GitBook
On this page
  • 1. Here are some basic steps to use CSS in your web pages:
  • 2. How to connect your CSS file with your HTML document?
  • 3. How to style each tag? --> Selectors
  • 4. What are some common attributes to style?
  • 5. Box model:
  • 6. Cascading and Specifity - What happens if multiple styles are applied to an element? Which style "wins"?
  • 7. Layout - How to make your webpage responsive? --> Flexbox
  1. Web Development Fundamentals

3. CSS

CSS (Cascading Style Sheets) is a powerful style sheet language that is used to control the layout and styling of web pages. Here are some of the most important concepts in CSS:

1. Here are some basic steps to use CSS in your web pages:

  1. Create a new CSS file: You can create a new CSS file using a plain text editor like Notepad, or a specialized code editor like Visual Studio Code.

  2. Link the CSS file to your HTML document: To link the CSS file to your HTML document, you can use the <link> tag in the <head> section of your HTML document, as described in the previous answer.

  3. Write CSS code: In your CSS file, you can write CSS code to style your HTML document. CSS uses selectors to target HTML elements, and then applies properties to those elements. For example:

2. How to connect your CSS file with your HTML document?

To connect your CSS file with your HTML document, you can use the <link> tag in the <head> section of your HTML document. Here's an example:

// Some code
<!DOCTYPE html>
<html>
  <head>
    <title>My Webpage</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <!-- Your HTML content here -->
  </body>
</html>

In this example, we've included a link to our styles.css file using the <link> tag. The href attribute specifies the location of the CSS file, relative to the location of the HTML file.

By including the CSS file in this way, all of the styles defined in the CSS file will be applied to the HTML document.

3. How to style each tag? --> Selectors

Selectors are used to target HTML elements and apply styles to those elements. There are many types of selectors, including element selectors, class selectors, ID selectors, and more.

Element selectors target specific HTML elements by their tag name. For example, to target all <p> elements, you would use the p selector.

Class selectors target elements based on their class attribute. For example, to target all elements with the highlight class, you would use the .highlight selector.

ID selectors target elements based on their id attribute. IDs are unique, so there should only be one element with a particular ID on a page. To target an element by its ID, use the # symbol followed by the ID value.

/* Element selector */
p {
  color: red;
}

/* Class selector */
.highlight {
  background-color: yellow;
}

/* ID selector */
#header {
  font-size: 24px;
}

4. What are some common attributes to style?

CSS has a wide variety of attributes, but some of the most common ones include:

  1. color - Sets the text color.

  2. background-color - Sets the background color of an element.

  3. font-size - Sets the size of the font.

  4. font-family - Sets the font family for the text.

  5. padding - Sets the padding around an element.

  6. margin - Sets the margin around an element.

  7. border - Sets the border around an element.

  8. text-align - Aligns text within an element.

  9. display - Controls how an element is displayed (e.g. block, inline, flex).

  10. position - Controls the position of an element relative to its parent.

These attributes are just a few examples of the many different ways CSS can be used to style and layout web pages.

5. Box model:

The CSS box model is a way of representing the layout of an HTML element. Every element on a web page can be thought of as a box with four parts: content, padding, border, and margin.

The content area is where the actual content of the element is displayed. Padding is the space between the content area and the border. Border is a line around the content and padding. Margin is the space between the border and the neighboring elements.

Here's an example of how the box model works:

styles.css

.box {
  width: 200px;
  height: 200px;
  padding: 20px;
  border: 1px solid black;
  margin: 10px;
}

Every HTML tag is automatically treated as a box and can be associated with the box model. Each box has a width, height, and is built from the inside out: content, padding, border, and margin. The box model can be controlled with CSS by defining the appropriate properties such as width, height, padding, border, and margin. For example, we can define a box with a width and height of 200 pixels, 20 pixels of padding, a 1-pixel border, and a 10-pixel margin on all sides using CSS. These properties can be adjusted as needed to control the appearance of the box.

6. Cascading and Specifity - What happens if multiple styles are applied to an element? Which style "wins"?

Cascading is the process by which multiple CSS styles are combined to determine the final style of an element. When multiple styles apply to an element, the rules of specificity come into play.

CSS selectors have different levels of specificity, which determines which styles will take precedence. The most specific selector wins.

Here's an example of how specificity works:

styles.css

/* Specificity */
p {
  color: red;
}

.highlight {
  color: blue;
}

/*
  The color of this <p> element will be blue, because 
  the .highlight class selector is more specific 
  than the element selector 
*/
<p class="highlight">This is some text.</p>

In this example, we've defined a style for all <p> elements to be red. However, we've also defined a style for elements with the highlight class to be blue. Since the .highlight selector is more specific than the p selector, the text will be blue.

Selectors can be classified into four types, ordered by increasing specificity:

  1. Type selectors and pseudo-elements (e.g. div, span, ::before, ::after)

  2. Class selectors, attribute selectors, and pseudo-classes (e.g. .my-class, [type="text"], :hover)

  3. ID selectors (e.g. #my-id)

  4. Inline styles (e.g. style attribute in HTML)

When multiple selectors target the same element and have conflicting styles, the selector with the highest specificity will win. For example, if we have the following styles:

// css file

#my-id {
  color: red;
}

.my-class {
  color: blue;
}

And we apply both of these styles to an element with id="my-id" class="my-class", the color of the text will be red because the ID selector has a higher specificity than the class selector.

7. Layout - How to make your webpage responsive? --> Flexbox

CSS provides a variety of layout techniques to create complex and responsive layouts for web pages. The most common techniques include:

  • Box layout: The default layout of elements is based on the box model, which we discussed earlier. You can use the display property to control how boxes are laid out on the page.

  • Flexbox layout: Flexbox is a powerful layout technique that allows you to create flexible and responsive layouts. You can use the display: flex property on a container element to turn it into a flex container. Flexbox allows you to easily control the alignment and spacing of elements.

// css file

/* Flexbox layout */
.container {
  display: flex;
  justify-content:
}

Here's an example of an easy layout of three boxes next to each other using flexbox:

HTML:

index.html

<div class="container">
  <div class="box red"></div>
  <div class="box green"></div>
  <div class="box blue"></div>
</div>

CSS:

styles.css

.container {
  display: flex; /* Use flexbox to arrange the child elements */
}

.box {
  flex: 1; /* Use flex-grow to make the boxes equal width */
  height: 100px;
}

.red {
  background-color: red;
}

.green {
  background-color: green;
}

.blue {
  background-color: blue;
}

In this example, we have a container element that uses display: flex to enable flexbox layout. We then have three child elements with the class .box, each with a different background color. We use the flex property to make the boxes equal width, and set a fixed height of 100px for each box.

This will result in three boxes next to each other that are equally spaced and fill the available width of the container.

Previous2. HTMLNext4. JavaScript