CSS (Cascading Style Sheets) UNIT 4

Q1. (a) What is CSS? Explain Internal, Inline and External CSS with examples.

What is CSS?

CSS stands for Cascading Style Sheets. It is used to make HTML web pages attractive and beautiful.

CSS controls the design, colour, font, size, spacing, background and layout of a webpage.

Uses of CSS:

  1. To change text colour and font size.
  2. To add background colours and images.
  3. To create borders.
  4. To control spacing and layout.
  5. To make websites attractive.

There are three types of CSS:

1. Inline CSS

Inline CSS is written directly inside an HTML tag using the style attribute.

Example:

<html>
<body>
    <h1 style="color: red;">Welcome to CSS</h1>
    <p style="font-size: 20px;">
        This is a paragraph.
    </p>
</body>
</html>

Explanation: Here, the heading colour is red and paragraph text size is 20px.

2. Internal CSS

Internal CSS is written inside the <style> tag in the <head> section of an HTML document.

Example:

<html>
<head>
<style>
h1 {
    color: blue;
}

p {
    font-size: 18px;
    color: green;
}
</style>
</head>

<body>
    <h1>Welcome to CSS</h1>
    <p>This is a paragraph.</p>
</body>
</html>

Explanation: Here, the heading colour is blue and paragraph colour is green.

3. External CSS

External CSS is written in a separate .css file and connected to an HTML file using the <link> tag.

HTML File (index.html):

<html>
<head>
<link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>Welcome to CSS</h1>
    <p>This is my website.</p>
</body>
</html>

CSS File (style.css):

h1 {
    color: red;
}

p {
    color: blue;
    font-size: 20px;
}

Explanation: The design is written in a separate CSS file called style.css.

Difference between three types of CSS:

Inline CSSInternal CSSExternal CSS
Written inside HTML tag.Written inside <style> tag.Written in separate CSS file.
Used for one element.Used for one webpage.Used for multiple webpages.
Uses style attribute.Uses <style> tag.Uses <link> tag.

Q1. (b) Explain the purpose of CSS Selectors: Element Selector, Class Selector and ID Selector.

What are CSS Selectors?

CSS selectors are used to select HTML elements and apply styles to them.

There are three common types of selectors:

1. Element Selector

An element selector selects HTML elements by their tag name.

Example:

p {
    color: red;
    font-size: 18px;
}
<p>This is first paragraph.</p>
<p>This is second paragraph.</p>

Explanation: Both paragraphs will have red colour and 18px font size.

2. Class Selector

A class selector is used to apply the same style to multiple HTML elements.

It starts with a dot (.).

Example:

<html>
<head>
<style>
.heading {
    color: blue;
    font-size: 25px;
}
</style>
</head>

<body>
    <h1 class="heading">Welcome</h1>
    <p class="heading">Hello Students</p>
</body>
</html>

Explanation: Both elements with the class name heading will have blue colour and 25px font size.

3. ID Selector

An ID selector is used to apply style to a particular HTML element.

It starts with a hash (#).

Example:

<html>
<head>
<style>
#box {
    color: white;
    background-color: green;
}
</style>
</head>

<body>
    <h1 id="box">Welcome to CSS</h1>
</body>
</html>

Explanation: The heading with ID box will have white text and a green background.

Difference between Element, Class and ID Selector:

SelectorSymbolPurpose
ElementNo symbolSelects all elements of a particular tag.
Class.Selects elements with the same class.
ID#Selects an element with a particular ID.

Q2. (a) What are CSS Properties? Explain Margin, Padding, Border and Color Properties.

What are CSS Properties?

CSS properties are used to define the appearance and design of HTML elements.

For example, colour, font size, background, margin, padding and border are CSS properties.

1. Margin

Margin is the space outside an HTML element. It creates distance between two elements.

Example:

div {
    margin: 20px;
}

Explanation: It creates 20px space outside the DIV element.

2. Padding

Padding is the space between the content and the border of an element.

Example:

div {
    padding: 15px;
}

Explanation: It creates 15px space inside the element around its content.

3. Border

Border is the line around an HTML element. We can change its colour, width and style.

Example:

div {
    border: 2px solid red;
}

Explanation: It creates a 2px solid red border around the DIV element.

4. Color

The color property is used to change the colour of text.

Example:

p {
    color: blue;
}

Explanation: It changes the paragraph text colour to blue.

We can also use background-color to change the background colour.

body {
    background-color: lightyellow;
}

Difference between Margin and Padding:

  • Margin = Space outside the border.
  • Padding = Space inside the border.

Q2. (b) With example code explain DIV with ID style and creating borders.

What is DIV?

DIV stands for Division. It is an HTML tag used to create a section or container on a webpage.

It helps to group different HTML elements together.

We can apply CSS styles to a DIV using an ID selector.

Example:

<html>
<head>
<style>
#mybox {
    width: 300px;
    height: 150px;
    background-color: lightblue;
    border: 3px solid red;
    padding: 20px;
    margin: 30px;
}
</style>
</head>

<body>

<div id="mybox">
    <h2>Welcome to CSS</h2>
    <p>This is my first DIV box.</p>
</div>

</body>
</html>

Explanation:

  1. #mybox selects the DIV using its ID.
  2. width sets the width of the DIV.
  3. height sets the height of the DIV.
  4. background-color sets the background colour.
  5. border creates a red border.
  6. padding creates space inside the DIV.
  7. margin creates space outside the DIV.

Output: A light blue box with a red border containing a heading and paragraph.

Q3. Explain Grouping Selectors and Universal Selector with example.

1. Grouping Selector

A grouping selector is used when we want to apply the same CSS style to multiple HTML elements.

We separate the element names using commas (,).

Example:

<html>
<head>
<style>
h1, h2, p {
    color: blue;
    font-family: Arial;
}
</style>
</head>

<body>

<h1>Welcome</h1>
<h2>Learn CSS</h2>
<p>This is a paragraph.</p>

</body>
</html>

Explanation:

  • h1, h2 and p are grouped together.
  • All three elements will have blue text.
  • All three elements will use Arial font.

Advantage: It reduces repeated CSS code and saves time.

2. Universal Selector

The universal selector is used to select all HTML elements on a webpage.

It is represented by the star symbol (*).

Example:

<html>
<head>
<style>
* {
    color: red;
    font-family: Arial;
}
</style>
</head>

<body>

<h1>Welcome to CSS</h1>
<p>This is a paragraph.</p>
<h2>Thank You</h2>

</body>
</html>

Explanation:

  • * selects all HTML elements.
  • All text will have red colour.
  • All elements will use Arial font.

Advantage: It is useful when we want to apply common styles to the whole webpage.

Difference between Grouping and Universal Selector:

Grouping SelectorUniversal Selector
Selects multiple specified elements.Selects all elements.
Uses comma (,).Uses star (*).
Example: h1, pExample: *

Q4. Explain how CSS works with Block Elements to control layout. Give example using DIV and P tag.

What are Block Elements?

Block elements are HTML elements that start on a new line and generally occupy the full available width of their parent container.

Examples of block elements:

  1. <div>
  2. <p>
  3. <h1>
  4. <h2>
  5. <section>

How CSS works with Block Elements?

CSS is used with block elements to control the layout and appearance of a webpage.

CSS can control:

  1. Width: Sets the width of an element.
  2. Height: Sets the height of an element.
  3. Margin: Adds space outside the element.
  4. Padding: Adds space inside the element.
  5. Border: Creates a border around the element.
  6. Background: Changes the background colour.

Example using DIV and P tags

<html>
<head>
<style>

div {
    width: 400px;
    background-color: lightblue;
    border: 2px solid blue;
    padding: 20px;
    margin: 20px;
}

p {
    color: red;
    font-size: 18px;
    background-color: lightyellow;
    padding: 10px;
}

</style>
</head>

<body>

<div>
    <h2>My Website</h2>

    <p>This is the first paragraph.</p>

    <p>This is the second paragraph.</p>

</div>

</body>
</html>

Explanation:

  • DIV creates a container on the webpage.
  • Width sets the width of the DIV to 400px.
  • Background colour makes the DIV light blue.
  • Border creates a blue border around the DIV.
  • Padding adds space inside the DIV.
  • Margin adds space outside the DIV.
  • P tag creates paragraphs inside the DIV.
  • Paragraph text is red and its background is light yellow.

Conclusion

CSS helps to control the position, size, colour, spacing and design of block elements. DIV is mainly used as a container, while P is used to display paragraphs.

Quick Revision – Important Points

TopicRemember
CSSCascading Style Sheets
Inline CSSUses style attribute
Internal CSSUses <style> tag
External CSSUses .css file
Element Selectorp { }
Class Selector.name { }
ID Selector#name { }
Grouping Selectorh1, p { }
Universal Selector* { }
DIVCreates a container
MarginSpace outside border
PaddingSpace inside border
BorderLine around element
ColorChanges text colour

Exam Preparation Tip

For each question, write the definition first, then explain the points and give one example program. This will make your answers easy to understand and well-presented in the examination.