Inline styles in CSS are applied directly to an HTML element using the style attribute. Here are some basic examples for beginners:
<p style="color: blue;">This is a blue paragraph.</p>
<p style="font-size: 20px;">This text has a larger font size.</p>
<div style="background-color: yellow;">This div has a yellow background.</div>
<p style="border: 2px solid red;">This paragraph has a red border.</p>
<div style="text-align: center; padding: 20px;">
This text is centered with padding.
</div>
<p style="font-family: Arial, sans-serif;">This text is in Arial font.</p>
<p style="font-weight: bold; font-style: italic;">Bold and italic text.</p>
<div style="width: 200px; height: 100px; background-color: lightblue;">
This div has a fixed width and height.
</div>
<p style="text-shadow: 2px 2px 5px gray;">This text has a shadow.</p>
<div style="background-color: lightgreen; padding: 20px; border-radius: 10px;">
This box has rounded corners.
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document level - CSS3</title>
<style type="text/css">
h1 {
border-width: 2px;
border-style: solid;
border-color: black;
color: white;
text-align: center;
background-color: goldenrod;
font-size: 28px;
padding: 10px;
border-radius: 8px;
}
p {
font-weight: bold;
font-style: italic;
color: white;
background-color: darkblue;
font-size: 18px;
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Heading</h1>
<p>
I've learned that people will forget what you said, people will forget what you did, but people will never forget how you
made them feel.” ― Maya Angelou
</p>
<h1 style="color:red;">Heading</h1>
<p>
“Always forgive your enemies; nothing annoys them so much.” ― Oscar Wilde
</p>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat, neque nulla, harum ipsum ea quae dolore laborum quis rem voluptates illo non inventore pariatur? Nisi, perspiciatis. Quo, sapiente hic. Dicta!</p>
</body>
</html>
<style> tag in the HTML file).styled_page.html.<style> tag in the <head> section.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Styled Page</title>
<style>
/* Document-Level CSS */
body {
background-color: lightblue;
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
color: darkblue;
text-transform: uppercase;
}
p {
color: darkgreen;
font-size: 18px;
}
button {
background-color: orange;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
}
button:hover {
background-color: darkorange;
}
</style>
</head>
<body>
<h1>Welcome to My Styled Page</h1>
<p>This is a simple paragraph with some styling.</p>
<button>Click Me</button>
</body>
</html>
background-color of the page to another color.h1 and p colors to different shades.background-color, padding, and hover effect.
button:hover {
background-color: darkblue;
transform: scale(1.1); /* Slightly increases the size */
}
border property to give paragraphs a border.
External CSS is a method of applying styles to a webpage by linking an external stylesheet file. This approach keeps the HTML structure clean and makes it easier to maintain and update styles across multiple pages.
.css extension (e.g., styles.css).<link> tag inside the <head> section of the HTML file.index.html):<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS Example</title>
<link rel="stylesheet" href="styles.css"> <!-- Linking the external CSS file -->
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is an example of external CSS.</p>
</body>
</html>
styles.css):/* Styling for the heading */
h1 {
color: blue;
text-align: center;
}
/* Styling for the paragraph */
p {
color: green;
font-size: 18px;
}
index.html file and sees the <link> tag pointing to styles.css.styles.css and applies the styles to the corresponding HTML elements.<h1> tag turns blue and is centered, while the <p> tag appears green with a font size of 18px.See also:
Create an HTML file and an external CSS file to style a simple webpage.
task.html) with the following elements:
h1) with the text “My First Styled Page”p) with a short description of yourself.button) with the text “Click Me”task-styles.css) and apply the following styles:
lightgray.h1) red and center-aligned.p) with blue text and a font size of 16px.greenwhite10px 20px5px<link> tag in the <head> section.Modify the button style to change its background color to dark green when hovered.
When you open the task.html file in a browser, you should see a well-styled page where the heading is red, the paragraph is blue, and the button is green with a hover effect.