BIT251 ยท 2081 ยท 5 marks
Consider an HTML file that contains three <div> elements and two <p> elements. The first <div> should have border color red with border size 1px. The next two <div> elements should have font-family Arial and text color blue. Both of the <p> elements should have background color yellow and their disp
Consider an HTML file that contains three <div> elements and two <p> elements. The first <div> should have border color red with border size 1px. The next two <div> elements should have font-family Arial and text color blue. Both of the <p> elements should have background color yellow and their display property set to block type. Now write the equivalent external CSS for this case. [5]
Web Technology IChapter 5: CSS Styling and LayoutInline internal and external CSS methods
External CSS for the Given HTML Structure
HTML File (index.html)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="first-div">This is the first div</div>
<div class="styled-div">This is the second div</div>
<div class="styled-div">This is the third div</div>
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
</body>
</html>
External CSS File (styles.css)
/* Style for the first <div> - red border with 1px size */
#first-div {
border-color: red;
border-style: solid;
border-width: 1px;
}
/* Style for the next two <div> elements - Arial font and blue text */
.styled-div {
font-family: Arial;
color: blue;
}
/* Style for both <p> elements - yellow background and block display */
p {
background-color: yellow;
display: block;
}
Explanation of Key Concepts
| Selector Used | Target Element | Properties Applied |
|---|---|---|
#first-div (ID selector) | First <div> | border-color, border-style, border-width |
.styled-div (Class selector) | 2nd and 3rd <div> | font-family, color |
p (Element selector) | Both <p> elements | background-color, display |
Why External CSS?
- The CSS rules are written in a separate
.cssfile (styles.css). - It is linked to the HTML file using the
<link>tag inside the<head>section. - This is called External CSS and is preferred for maintaining separation of structure and style.
Note:
border-style: solidis required alongsideborder-colorandborder-widthfor the border to be visible in browsers.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.