apply css in html
Code snippet for how to apply css in html with sample and detail explanation
Applying CSS in HTML
Utilizing Cascading Style Sheets (CSS) is a fundamental skill in web development. In this article, we’ll discuss how to apply CSS to your HTML files to make your websites more appealing and functional.
Code Snippet for Applying CSS in HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 30px;
}
</style>
</head>
<body>
<h1>This is a Heading</h1>
</body>
</html>
Code Explanation for Applying CSS in HTML
Let’s break down this code to understand how CSS is applied in HTML:
-
DOCTYPE html: This is the document type declaration and it is used to tell the browser that the document is an HTML5 document.
-
html: This is the root of the HTML document.
-
head: This element represents a container for metadata (data about data) and it is always placed between the
<html>
and<body>
tags. -
style: CSS is placed inside the
<style>
tags. This is one of the ways to include CSS in your HTML files known as internal styling. -
body: Inside the
<style>
tag, we declare styles for thebody
andh1
tags. For thebody
, we set a light blue background color. -
h1: For the
h1
tag, we set the text color to navy and pushed the heading 30 pixels from the left margin. -
After the
head
section, we move to thebody
section where the actual content of the web page resides. Here, we simply have a heading defined by theh1
tag and the styling for this tag has been defined in thestyle
section of thehead
.
This is a simple example of implementing CSS styling in your HTML files, making your web page look more attractive. You may specify as many style definitions as required within the style
block for different elements.