Introduction to CSS

What is CSS?

CSS, which stands for Cascading Style Sheets, is a language that helps us make web pages look pretty and organized. It’s like giving our web pages a special outfit to wear!

Just like how we have rules for grammar when we write sentences, CSS also has its own set of rules called syntax. These rules tell the computer how to understand and apply the styles we want to add to our web pages.

CSS syntax is made up of three important parts: selectors, properties, and values.

  • Selectors: Selectors are like names or labels we give to HTML elements.
  • Properties: Properties are like the things we want to change about our web page.
  • Values: Values are like the choices we make for each property.

To put it all together, here’s an example of CSS syntax:


selector {
   property: value;
}

Let’s say we want to make all the headings in our web page red. We would write CSS like this:


h1 {
   color: red;
}

This tells the computer to select all the “h1” elements and change their color property to red.

Remember, CSS syntax is like following a set of rules to tell the computer how to style our web pages. By using selectors, properties, and values, we can create beautiful and personalized designs for our websites!

style tag

The <style> tag is used to add CSS code to an HTML file. It is placed inside the <head> section of an HTML document, and any CSS code written inside it will apply to the HTML elements specified in the code.

Here’s an example:

<!DOCTYPE html>
<html>
<head>
  <title>My CSS Example</title>
  <style>
    h1 {
      color: blue;
    }
  </style>
</head>
<body>
  <h1>Welcome to my webpage!</h1>
  <p>This is a paragraph of text.</p>
</body>
</html>

In this example, the CSS code inside the <style> tags specifies that the font size of all <h1> tags should be 36px and their color should be blue. The code also specifies that the font family of all <p> tags should be Arial and their font size should be 16px.

By using the <style> tag to add CSS code to an HTML document, we can change the appearance of elements on the webpage, such as their font size, font family, color, and more.

Selectors

In CSS, selectors are like special names or labels we use to pick and choose which parts of a web page we want to style. Imagine you have a big group of friends, and you want to invite only a few of them to your party. Selectors help you do just that!

Element selectors are like calling out a specific type of HTML element. They target a whole group of elements with the same name. For example, This CSS rule targets all the <h1> elements on the web page and changes their color to blue.

h1 {
  color: blue;
}

Activity: Using An Element Selector

Use an element selector to target all the paragraphs and set their color property to red.

Class selectors are like giving a special tag or nickname to a specific group of HTML elements. You can choose any name you want! Let’s say you have a group of paragraphs that you want to style differently from the others. You can add a class attribute to those paragraphs, like “class=‘special-paragraphs’“. Then, in your CSS, you can use “.special-paragraphs” as a selector to target only those paragraphs. For example, The paragraph with the class “highlight” is selected, and its background color is set to yellow. The regular paragraph is not affected.

<p class="highlight">This paragraph is special.</p>
<p>Regular paragraph.</p>
.highlight {
  background-color: yellow;
}

Activity: Using An Class Selector

Use a class selector to target the paragraphs with the class “special” and set their “background-color” property to red

ID selectors are like giving a unique name to one specific HTML element. It’s like saying, “Hey, you! I’m talking to you, and only you!” IDs are special because they should only be used once on a page. Let’s say you have a special heading at the top of your web page that you want to style differently. You can give it an ID, like “id=‘special-heading’”, and then in your CSS, you can use “#special-heading” as a selector. For example, The heading with the ID “main-heading” is targeted, and its font size is set to 24 pixels. The other paragraph remains unaffected.

<h2 id="main-heading">Welcome to My Website</h2>
<p>This is the content of my web page.</p>
#main-heading {
  font-size: 24px;
}

Activity: Using An ID Selector

Use an ID selector to target the heading with the ID “main-heading” and set its color property to blue

By using these different types of selectors, we can be really specific about which elements we want to style. It’s like having a set of magical glasses that help us see and pick out exactly what we want on our web pages.

Properties

CSS properties are like special instructions that tell the computer how to change the appearance or behavior of elements on a web page. It’s like giving specific commands to make things look cool and fun!

Just like we have different tools in an art kit to create different effects, CSS properties help us modify different aspects of elements in web design.

Common Colors

Color NameCSS Value
Redred
Blueblue
Greengreen
Yellowyellow
Blackblack
Whitewhite
Orangeorange
Pinkpink
Purplepurple
Brownbrown
Graygray
Cyancyan
Magentamagenta
Silversilver
Goldgold

Common Border Styles

Border StyleCSS Value
Nonenone
Solidsolid
Dasheddashed
Dotteddotted
Doubledouble
Groovegroove
Ridgeridge
Insetinset
Outsetoutset
Hiddenhidden

Common Font Sizes

Font SizeCSS Value
12px12px
14px14px
16px16px
18px18px
20px20px
24px24px
28px28px
32px32px
36px36px
40px40px
48px48px
56px56px
64px64px
72px72px
96px96px

Here are some common CSS properties and their explanations:

The color property determines the color of text or other elements. It’s like choosing the color of a marker or a paintbrush. For example, this sets the text color of all <h1> headings to red.

h1 {
  color: red;
}

Activity: Using the color property

Set the text color of the element to blue:

<p>This is a <span>highlighted</span> word.</p>

The font-size property controls the size of the text. It’s like adjusting the size of the letters on a sign or in a book. We can make the text bigger or smaller by using values like “12px” or “20px”. This example sets the font size of all paragraphs (<p>) to 16 pixels.

p {
  font-size: 16px;
}

Activity: Using the font size property

Increase the font size of the

heading to 24 pixels:

<h2>Welcome to our website</h2>

The background-color property changes the color of the background behind an element. It’s like painting the background of a picture. We can use colors like “yellow” or “green” to create different backgrounds. This example sets the background color of the entire web page to yellow.

body {
  background-color: yellow;
}

Activity: Using the background color property

Apply a red background color and white text color to the following

element:

<div>This is a colored div</div>

The width and height properties control the size of an element. It’s like stretching or shrinking a box. We can set the width and height using values like “200px” or “50%“. This example sets the width of elements with class “box” to 200 pixels and the height to 150 pixels.

.box {
  width: 200px;
  height: 150px;
}

Activity: Using the width and height properties

Set the width of the following image to 300 pixels and the height to 200 pixels:

<img src="image.jpg" alt="A beautiful image">

The border property adds a border around an element. It’s like drawing a frame around a picture. We can set the thickness, style, and color of the border to create different effects. This example adds a 1-pixel black solid border to elements with class “container”.

.container {
  border: 1px solid black;
}

Activity: Using the border property

Add a border of 1 pixel, dotted style, and gray color to the following

element:

<table>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
  </tr>
</table>

The padding property adds space inside the border of an element. It’s like adding extra cushioning around an object. We can adjust the amount of padding to create spacing between elements. This example adds 10 pixels of padding to all elements with class “content”.

.content {
  padding: 10px;
}

Activity: Using the padding property

Apply a padding of 20 pixels to the following

element:

<div>This is a padded div</div>

The margin property adds space outside the border of an element. It’s like creating a gap between objects. We can adjust the margin to control the spacing between elements on a web page. This example adds a 20-pixel margin around all elements with class “element”.

.element {
  margin: 20px;
}

Activity: Using the margin property

Set the left margin of the following paragraph to 40 pixels:

<p>This is a paragraph with some text</p>

These are just a few examples of CSS properties, but there are many more to explore! By using different properties and values, we can customize and design our web pages in creative ways.

\newpage

Putting It All Together

Using the

<!DOCTYPE html>
<html>
<head>
  <title>Styling Assessment</title>
</head>
<body>
  <h1>Welcome to CSS Styling Assessment</h1>
  <p>This is a paragraph.</p>
  <div class="box">
    <p>This is a box.</p>
  </div>
  <p class="highlight">This paragraph should be highlighted.</p>
</body>
</html>
  1. Set the background color of the element to light blue.
  2. Set the text color of the

    element to dark green.

  3. Set the font size of the

    elements to 18 pixels.

  4. Add a 2-pixel solid black border to the .box element.
  5. Set the background color of the .highlight class to yellow.
\newpage

Vocabulary Review

TermsDefinitions
cssa language used to add style and formatting to websites
selectorhelp us pick which parts of the web page we want to style
propertiestell the computer what aspect we want to style
<style> tagelement used to add CSS code to an HTML file
valuesdetermine how we want the property to look
Element selectorstarget a whole group of elements with the same name
Class selectorsgiving a special tag or nickname to a specific group of HTML elements
ID selectorsgiving a unique name to one specific HTML element
color propertydetermines the color of text or other elements
font-size propertycontrols the size of the text
background-color propertychanges the color of the background behind an element
width and height propertiescontrol the size of an element
border propertyadds a border around an element
padding propertyadds space inside the border of an element
margin propertyadds space outside the border of an element

Review Questions

\begin{enumerate}[1.]

\item Which of the following is the correct way to include CSS styles in an HTML document? \begin{enumerate}[(a)] \item \texttt{

\item Which HTML tag is used to define internal CSS styles? \begin{enumerate}[(a)] \item \texttt{

\item Which CSS property is used to change the text color? \begin{enumerate}[(a)] \item \texttt{background-color} \item \texttt{font-size} \item \texttt{color} \item \texttt{text-decoration} \end{enumerate}

\item Which CSS property is used to set the background color of an element? \begin{enumerate}[(a)] \item \texttt{background-color} \item \texttt{font-color} \item \texttt{color} \item \texttt{text-color} \end{enumerate}

\item Which CSS property is used to set the font size? \begin{enumerate}[(a)] \item \texttt{font-size} \item \texttt{text-size} \item \texttt{size} \item \texttt{font} \end{enumerate}

\item Which CSS property is used to set the width of an element? \begin{enumerate}[(a)] \item \texttt{width} \item \texttt{size} \item \texttt{length} \item \texttt{dimension} \end{enumerate}

\item Which CSS property is used to set the height of an element? \begin{enumerate}[(a)] \item \texttt{height} \item \texttt{size} \item \texttt{length} \item \texttt{dimension} \end{enumerate}

\item Which CSS property is used to add space inside an element? \begin{enumerate}[(a)] \item \texttt{margin} \item \texttt{padding} \item \texttt{spacing} \item \texttt{border} \end{enumerate}

\item Which CSS property is used to add space outside an element? \begin{enumerate}[(a)] \item \texttt{margin} \item \texttt{padding} \item \texttt{spacing} \item \texttt{border} \end{enumerate}

\item Which CSS property is used to add a border around an element? \begin{enumerate}[(a)] \item \texttt{border-style} \item \texttt{border-color} \item \texttt{border-width} \item All of the above \end{enumerate}

\end{enumerate}