2078 3

CSC329 ยท 2078 ยท 10 marks

How external CSS is inserted in an HTML page? Create an HTML page with two paragraph tags with id p1 and p2, and one div tag with id dv1. Write an external CSS file as per the description. the margin of the paragraphs should be 100px from the top and 80px from the left. Both of paragraphs should hav

How external CSS is inserted in an HTML page? Create an HTML page with two paragraph tags with id p1 and p2, and one div tag with id dv1. Write an external CSS file as per the description. the margin of the paragraphs should be 100px from the top and 80px from the left. Both of paragraphs should have the display set to block. The dv1 should have padding left and right set to 30px and 25px respectively, its background color should be blue and the font color should be red with the font type Verdana. Set the display of dv1 to none.[10]

Web TechnologyChapter 3: Cascading Style SheetsInserting CSS

Quick answer

External CSS is a separate .css file that contains all the style rules. It is linked to an HTML page using the <link> tag inside the <head> section of the HTML document.

External CSS in HTML - Complete Answer

How External CSS is Inserted in an HTML Page

Syntax:

<link rel="stylesheet" type="text/css" href="filename.css">
  • rel="stylesheet" defines the relationship between the HTML file and the linked file.
  • type="text/css" specifies the media type (optional in HTML5).
  • href="filename.css" specifies the path to the external CSS file.

Advantages of External CSS:

  • One CSS file can control the style of multiple HTML pages.
  • Changes made in one file reflect across all linked pages.
  • Keeps HTML structure and styling separate (clean code).

HTML File (index.html)

<!DOCTYPE html>
<html>
  <head>
    <title>External CSS Example</title>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>

    <p id="p1">This is Paragraph 1 (id = p1)</p>

    <p id="p2">This is Paragraph 2 (id = p2)</p>

    <div id="dv1">
      This is a div block (id = dv1)
    </div>

  </body>
</html>

External CSS File (style.css)

/* Styles for paragraph with id p1 */
#p1 {
    margin-top: 100px;
    margin-left: 80px;
    display: block;
}

/* Styles for paragraph with id p2 */
#p2 {
    margin-top: 100px;
    margin-left: 80px;
    display: block;
}

/* Styles for div with id dv1 */
#dv1 {
    padding-left: 30px;
    padding-right: 25px;
    background-color: blue;
    color: red;
    font-family: Verdana;
    display: none;
}

Explanation of CSS Properties Used

PropertyElementValueDescription
margin-top#p1, #p2100pxSets top margin to 100px
margin-left#p1, #p280pxSets left margin to 80px
display#p1, #p2blockDisplays paragraphs as block elements
padding-left#dv130pxSets left padding to 30px
padding-right#dv125pxSets right padding to 25px
background-color#dv1blueSets background color to blue
color#dv1redSets font/text color to red
font-family#dv1VerdanaSets font type to Verdana
display#dv1noneHides the div element completely

Note: The # selector in CSS is used to target elements by their id attribute. Since id values are unique per page, #p1, #p2, and #dv1 target exactly those specific elements.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.