CSS Basic Coding | Learn CSS Basic
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. It allows you to control the layout, colors, fonts, and overall visual style of a web page. Here’s a basic introduction to CSS coding and some of the fundamental properties used to style HTML elements.
Basic Structure of a CSS File
CSS can be included in an HTML document in three ways: inline, internal, and external.
1. Inline CSS
Inline CSS is used to apply a unique style to a single HTML element. It uses the style attribute within the element’s tag.
<p style="color: blue; text-align: center;">This is a blue, centered paragraph.</p>2. Internal CSS
Internal CSS is used to define styles for a single HTML document. The styles are defined within a <style> element in the <head> section.
<!DOCTYPE html> <html> <head> <style> body { background-color: lightgray; } h1 { color: blue; text-align: center; } p { font-family: Arial, sans-serif; font-size: 14px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>3. External CSS
External CSS is used to define styles for multiple HTML pages. The styles are defined in an external .css file and linked to the HTML document using the <link> element.
style.css
body { background-color: lightgray; } h1 { color: blue; text-align: center; } p { font-family: Arial, sans-serif; font-size: 14px; }index.html
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html>Basic CSS Syntax
A CSS rule consists of a selector and a declaration block:
selector { property: value; property: value; }- Selector: Specifies the HTML element you want to style.
- Property: The aspect of the element you want to change (e.g., color, font-size).
- Value: The value you want to apply to the property (e.g., blue, 14px).
Common CSS Selectors
- Element Selector: Targets all instances of a given HTML element.
p { color: red; }- ID Selector: Targets a single element with a specific
idattribute. Prefixed with#.
#header { background-color: yellow; }- Class Selector: Targets elements with a specific
classattribute. Prefixed with..
.highlight { background-color: yellow; }- Universal Selector: Targets all elements.
* { margin: 0; padding: 0; }- Attribute Selector: Targets elements with a specific attribute.
[type="text"] { border: 1px solid black; }Common CSS Properties
- Color and Background
color: blue; background-color: lightgray;- Font
font-family: Arial, sans-serif; font-size: 16px; font-weight: bold;- Text
text-align: center; text-decoration: underline;- Box Model (Spacing)
margin: 20px; padding: 10px; border: 1px solid black;- Dimensions
width: 100px; height: 200px;- Display and Positioning
display: block; position: absolute; top: 50px; left: 100px;Example of Applying CSS
Here is a simple example to demonstrate the application of various CSS properties:
style.css
body { background-color: lightgray; font-family: Arial, sans-serif; } h1 { color: blue; text-align: center; margin-top: 50px; } p { font-size: 14px; color: black; line-height: 1.6; margin: 20px; } #main-content { background-color: white; padding: 20px; border: 1px solid #ccc; width: 80%; margin: auto; } .highlight { background-color: yellow; font-weight: bold; }index.html
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>Welcome to My Webpage</h1> <div id="main-content"> <p>This is a paragraph inside the main content area. Here is some <span class="highlight">highlighted text</span>.</p> <p>Another paragraph with some more content.</p> </div> </body> </html>By using CSS, you can significantly enhance the appearance and usability of your web pages.