CSS Basics and Text Styling in HTML
1. What is CSS?
CSS, which stands for Cascading Style Sheets, is a language used to style and layout
web pages. It allows developers to control the appearance of HTML elements by
defining colors, fonts, spacing, alignment, and more. CSS makes it easier to maintain
consistent styling across multiple web pages by separating design from content.
2. How to Use CSS in HTML
There are three main ways to use CSS in HTML:
1. Inline CSS: CSS can be written directly within an HTML element's 'style' attribute.
Example: <p style="color: blue; font-size: 20px;">This is a styled paragraph.</p>
2. Internal CSS: CSS can be written within a <style> tag inside the <head> section of
an HTML document.
Example:
<head>
<style>
p{
color: green;
font-size: 18px;
</style>
</head>
3. External CSS: CSS can be written in a separate file (e.g., styles.css) and linked to
the HTML document using a <link> tag.
Example:
<head>
<link rel="stylesheet" href="styles.css">
</head>
External CSS is the recommended method for larger projects as it keeps HTML and
styling separate, making maintenance easier.
3. Text Styling Properties with CSS in Detail
CSS provides various properties to style text. Here are some commonly used ones:
1. color: Sets the text color. Example:
p{
color: red;
2. font-size: Defines the size of the text. Example:
p{
font-size: 16px;
3. font-family: Specifies the font of the text. Example:
p{
font-family: Arial, sans-serif;
}
4. font-weight: Sets the thickness of the font. Example:
p{
font-weight: bold;
5. font-style: Sets the style of the font. Example:
p{
font-style: italic;
6. text-align: Aligns text within its container. Example:
p{
text-align: center;
7. text-decoration: Adds decorative elements. Example:
p{
text-decoration: underline;
8. text-transform: Controls capitalization. Example:
p{
text-transform: uppercase;
9. letter-spacing: Adjusts the space between characters. Example:
p{
letter-spacing: 1px;
10. line-height: Sets the spacing between lines. Example:
p{
line-height: 1.5;
11. word-spacing: Controls the space between words. Example:
p{
word-spacing: 2px;
12. text-shadow: Adds shadow to text. Example:
p{
text-shadow: 2px 2px 5px gray;
These properties allow you to control how text appears on a webpage, enhancing
readability and visual appeal.