HTML is a markup language used to create the structure of a web page. It consists of a series of elements, which are represented by tags. For example, the <html>
tag is the root element of an HTML document, the <head>
tag contains meta - information about the page, and the <body>
tag holds the visible content.
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to my page</h1>
<p>This is a paragraph of text.</p>
</body>
</html>
CSS is a style sheet language used to describe the presentation of an HTML document. It allows you to control the layout, colors, fonts, and other visual aspects of the web page. CSS uses a set of rules, each consisting of a selector and a declaration block. The selector targets one or more HTML elements, and the declaration block contains one or more property - value pairs.
CSS can be applied to HTML elements in different ways. When a CSS rule is applied to an HTML element, the browser interprets the rule and renders the element according to the specified styles. For example, you can use CSS to change the color of all <h1>
headings on a page.
Inline CSS involves adding style attributes directly to HTML tags. This method is useful for applying unique styles to a single element.
<!DOCTYPE html>
<html>
<body>
<h1 style="color: blue; font - size: 36px;">This is a blue heading</h1>
</body>
</html>
Internal CSS is defined within the <style>
tag in the <head>
section of an HTML document. This method is suitable for applying styles to multiple elements within a single page.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: green;
font - size: 32px;
}
p {
color: gray;
}
</style>
</head>
<body>
<h1>Green Heading</h1>
<p>This is a gray paragraph.</p>
</body>
</html>
External CSS involves creating a separate CSS file (usually with a .css
extension) and linking it to the HTML document using the <link>
tag in the <head>
section. This method is ideal for applying consistent styles across multiple pages.
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Styled with External CSS</h1>
<p>Paragraph styled by external CSS</p>
</body>
</html>
styles.css
h1 {
color: purple;
font - size: 28px;
}
p {
line-height: 1.6;
}
CSS selectors are used to target specific HTML elements. There are different types of selectors, such as element selectors, class selectors, and ID selectors.
/* Element selector */
p {
color: brown;
}
/* Class selector */
.my - class {
font - weight: bold;
}
/* ID selector */
#my - id {
background - color: yellow;
}
The box model is a fundamental concept in CSS. Every HTML element is considered a rectangular box, consisting of content, padding, border, and margin.
div {
width: 300px;
padding: 20px;
border: 1px solid black;
margin: 10px;
}
Floats are used to position elements to the left or right of their containing element, allowing other elements to flow around them. Clears are used to prevent elements from floating around other elements.
<!DOCTYPE html>
<html>
<head>
<style>
.left - float {
float: left;
width: 100px;
height: 100px;
background - color: pink;
}
.clear {
clear: both;
}
</style>
</head>
<body>
<div class="left - float"></div>
<p>This is some text that will flow around the floated element.</p>
<div class="clear"></div>
<p>This text will start below the floated element.</p>
</body>
</html>
It is best to separate the content (HTML) from the presentation (CSS). Using external CSS files makes the code more maintainable and easier to update. This way, changes to the styles can be made in one place without affecting the HTML structure.
With the increasing use of different devices, it is essential to create responsive web pages. CSS media queries can be used to apply different styles based on the device’s screen size.
/* For screens smaller than 600px */
@media only screen and (max - width: 600px) {
body {
font - size: 14px;
}
}
Minimize the use of inline CSS as it can make the HTML code cluttered. Also, use shorthand properties whenever possible to reduce the amount of code. For example, instead of writing padding - top: 10px; padding - right: 20px; padding - bottom: 10px; padding - left: 20px;
, you can use padding: 10px 20px;
.
CSS - enabled HTML is a powerful combination that allows web developers to create visually appealing and user - friendly web pages. By understanding the fundamental concepts, usage methods, common practices, and best practices, developers can write efficient and maintainable code. Whether you are a beginner or an experienced developer, mastering CSS - enabled HTML is essential for creating modern web applications.