introduction to css
introduction to css
Selectors are used to target specific HTML elements and apply styles.
CSS Selectors
• Element Selector (Targets all elements of a type)
• Class Selector (Targets elements with a specific class)
• ID Selector (Targets a single element with an ID)
The declaration is also split into two parts, separated by a colon:
A property is the property of the selected element(s) that you want to affect, in
this case the width property.
A value is a specification for this property; in this case it is that table cells
should be 36 pixels wide.
This is like the way that HTML elements can carry attributes and how the
attribute controls a property of the element; the attributes’ value would
be the setting for that property. For example, a<td> element could have
a width attribute whose value is the width you want the table to be:<td
width="36"></td>
With CSS, however, rather than specifying the attribute on each instance of the
element, the selector indicates that this one rule applies to all elements in the
document.
HTML:
Below are examples of css code you can apply on your webpages feel free to try them out
button {background-color: green; color: Styles a button with a green background, white
white; border: none; padding: 10px text, no border, padding, and a cursor change
20px; cursor: pointer; } when hovered over.
img { width: 100px; height: auto; border: Ensures images are 100 pixels wide while
2px solid gray; } maintaining their aspect ratio, with a gray border.
td, th { border: 1px solid black; padding: Adds a border, padding, and centers text inside
8px; text-align: center; } table cells (<td>) and headers (<th>).
CSS ensures that web pages are visually appealing and user-friendly.
• Separation of Content and Design: HTML focuses on structure, while CSS handles
appearance, making websites easier to maintain.
• Consistency: A single CSS file can style multiple pages, ensuring uniformity across a
website.
• Faster Page Loading: CSS reduces the need for repetitive inline styles, leading to
cleaner and more efficient code.
• Responsive Design: CSS makes web pages adaptable to different screen sizes,
improving accessibility.
Example:
p {
color: blue;
font-size: 16px;
}
This rule applies a blue color and a font size of 16 pixels to all <p> elements.
• Best for quick styling but not recommended for larger projects due to limited
maintainability.
<style>
p {
color: green;
font-size: 20px;
}
</style>
• Suitable for single-page styling but not ideal for larger websites.
3. External CSS (Stored in a separate file and linked to the HTML document)
<link rel="stylesheet" href="styles.css">
/* styles.css */
p {
color: purple;
font-size: 22px;
}
Example Usage:
/* Element selector */
h1 {
color: navy;
}
/* Class selector */
.box {
background-color: lightgray;
padding: 10px;
}
/* ID selector */
#main-header {
font-size: 28px;
}
1. Display Property
2. CSS Flexbox
Flexbox is a powerful layout tool for aligning and distributing elements efficiently.
.container {
display: flex;
justify-content: center;
align-items: center;
}
3. CSS Grid
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
Analogy: Think of Flexbox as a row of adjustable seats in a bus, while CSS Grid is a
chessboard where elements fit into defined spaces.
CSS is a powerful tool that enhances the appearance of web pages. Understanding its
concepts allows developers to create visually appealing, responsive, and well-structured
websites. By mastering selectors, the box model, and layout techniques, you can build user-
friendly interfaces that function seamlessly across different devices.