CSS (Cascading Style Sheets) is the language that brings life to the web. It transforms plain HTML structures into visually stunning, interactive, and user-friendly websites. However, with its vast array of terminology, concepts, and techniques, CSS can sometimes feel overwhelming—especially for beginners or developers revisiting the language after some time. From mastering the cascade to understanding specificity and styling forms, CSS terminology is both powerful and intricate.
But here’s the good news, once you break it down into manageable pieces, CSS becomes not only approachable but also incredibly fun to work with. Whether you’re building a simple landing page or a complex web application, understanding CSS terminology is the key to unlocking your creativity and ensuring your designs look great across all devices.
1. The Objectives
The primary goal of this post is to refresh and deepen your understanding of CSS terminology. We’ll cover the foundational concepts and practical applications in a way that’s easy to follow, even if you’re new to CSS. By breaking down complex topics like selectors, properties, values, and interactivity into bite-sized categories, we could aim to make CSS more accessible and actionable for developers at all levels.
This guide isn’t just about theory—it’s packed with real-world examples and code snippets you can immediately apply to your projects. Whether you’re debugging a tricky layout issue or designing a responsive form, this post will equip you with the tools and knowledge you need to succeed.
1.1 The Scope
In this post, we’ll cover the following key areas:
- Core Principles of CSS: Understand the foundational concepts that govern how styles are applied, including the cascade, specificity, inheritance, and the box model.
- Syntax Elements: Learn the building blocks of CSS, such as selectors, declarations, rulesets, and at-rules, and how they work together to style HTML elements.
- Properties and Values: Explore common CSS properties and their corresponding values, grouped into categories like typography, layout, colors, and measurements.
- Styling Images and Forms: Discover techniques for styling images effectively and customizing forms to improve usability and aesthetics.
-
Adding Interactivity: Dive into mouse states like
:hover
,:focus
, and:active
to create dynamic and engaging user experiences. - Practical Examples and Implementation: Combine everything you’ve learned into complete, real-world examples that demonstrate how CSS works in action.
By the end of this post, you’ll have a solid grasp of CSS terminology and the confidence to tackle even the most challenging styling tasks. Let’s get started!
2. Core Principles of CSS
CSS operates based on foundational principles like the cascade, specificity, inheritance, and the box model. These principles determine how styles are applied and interact with each other, shaping the behavior of your web designs. Understanding these core concepts is essential for writing efficient, maintainable, and predictable CSS.
Whether you’re debugging a layout issue or optimizing for performance, mastering these principles will help you create clean and functional styles. In this chapter, we’ll explore each of these principles in detail, providing examples and actionable insights to solidify your understanding.
2.1 The Cascade
The cascade is one of the most fundamental aspects of CSS. It determines how styles are applied based on their order and source. When multiple rules target the same element, the cascade decides which rule takes precedence. Here’s how it works:
- Order of Rules: Later rules override earlier ones within the same source.
-
Sources of Styles: Styles can come from three sources:
- Browser Defaults: Styles applied by the browser (e.g., default margins on paragraphs).
- User Styles: Custom styles defined by the user (e.g., via browser settings or extensions).
- Author Styles: Styles written by the developer (you!).
Use Case: Understanding the cascade helps you predict and control how styles are applied, especially when dealing with conflicting rules.
Example:
p {
color: blue; /* Applied first */
}
p {
color: red; /* Overrides the previous rule */
}
In this example, all elements will appear red because the second rule overrides the first due to its position in the cascade.
Reference: MDN - The Cascade
2.2 The Specificity
Specificity is a priority system that determines which styles take precedence when multiple rules apply to the same element. It’s like a hierarchy that ranks selectors based on their "weight." Here’s how specificity works:
-
Hierarchy:
-
Inline styles (e.g.,
style="color: red;"
) have the highest specificity. -
IDs (e.g.,
#main-header
) come next. -
Classes, attributes, and pseudo-classes (e.g.,
.header
,[type="text"]
,:hover
) follow. -
Elements and pseudo-elements (e.g.,
p
,::before
) have the lowest specificity.
-
Inline styles (e.g.,
Key Difference: A more specific selector will always override a less specific one, regardless of the order in the stylesheet.
Example:
.header {
color: green;
}
#main-header {
color: red; /* Takes precedence due to higher specificity */
}
In this case, the element with the ID main-header
will appear red because IDs have higher specificity than classes.
Tip: Avoid overusing IDs in your CSS to prevent specificity conflicts, as they can make your styles harder to maintain.
Reference: MDN - Specificity
2.3 Inheritance
Some CSS properties are inherited by child elements from their parents. This means that if you define a property on a parent element, its children will automatically adopt the same value unless explicitly overridden. Inheritance helps reduce redundancy and ensures consistency across your design.
Common Inherited Properties:
color
font-family
line-height
text-align
Use Case: Inheritance is particularly useful for typography and text-related styles, as it allows you to define global styles on the Example: In this example, every element inside the Tip: Use the Reference: MDN - Inheritance The box model is a fundamental concept in CSS that defines how elements are structured and spaced within a layout. Every HTML element is treated as a rectangular box, consisting of four components: Understanding the box model is crucial for controlling layout and spacing effectively. Diagram: Example: In this example: Tip: Use the Reference: MDN - Box Model CSS syntax is the foundation of how styles are written and applied to HTML documents. It consists of selectors, declarations, rulesets, and at-rules, which together form the building blocks of CSS. Understanding these elements is essential for writing clean, maintainable, and effective styles. In this chapter, we’ll break down each component of CSS syntax and provide practical examples to help you grasp how they work. Selectors are the tools that target specific HTML elements for styling. They allow you to apply styles to individual elements, groups of elements, or elements in specific states. Let’s explore the most common types of selectors: The type selector targets elements by their tag name (e.g., Example: In this example, all The class selector targets elements based on their Example: Here, any element with the class The ID selector targets a single unique element by its Example: In this case, the element with the ID The attribute selector matches elements based on their attributes (e.g., Example: Here, only A pseudo-class targets elements in specific states, such as when they’re hovered over ( Example: In this example, the button’s background color changes to green when the user hovers over it. A pseudo-element styles specific parts of an element, such as the first line of a paragraph ( Example: Here, the first line of every Reference: MDN - Selectors A declaration consists of a property-value pair that defines a specific style. A ruleset groups one or more declarations within a block, associated with a selector. Together, declarations and rulesets form the core structure of CSS. Explanation: Example: In this example: Reference: MDN - CSS Syntax At-rules are special directives prefixed with Common Examples: Example: In this example: Reference: MDN - At-rules CSS properties define what aspect of an element is being styled, while values specify the exact style to apply. There are hundreds of standardized properties, each with its own set of valid values. These properties control everything from typography and layout to colors, spacing, and animations. In this chapter, we’ll explore common CSS properties grouped into categories for clarity: By the end of this chapter, you’ll have a solid understanding of how to use these properties effectively in your projects. Typography is one of the most important aspects of web design. Properly styled text improves readability, accessibility, and overall user experience. Let’s look at some key properties for styling text: Example: In this example: Reference: MDN - Typography Properties CSS layout properties allow you to control the structure and alignment of elements on the page. Modern CSS provides powerful tools like flexbox and grid for creating responsive and dynamic layouts. Example: In this example: Reference: Colors and backgrounds add visual appeal to your designs. CSS provides multiple ways to define colors, including static values, dynamic transparency, and variables. Static colors are defined using predefined formats like hex codes ( Example: Here: Dynamic colors allow you to create effects like transparency or dynamically adjust colors using variables. Example: In this example: Reference: MDN - Colors CSS supports a variety of units for defining sizes, spacing, and other measurements. Understanding these units is crucial for creating responsive and scalable designs. Basic units include absolute units like Example: Here: Modern CSS includes functions like Example: In this example: Reference: MDN - Units Styling images is a common yet essential task in CSS. Whether you’re working with inline In this chapter, we’ll explore two key techniques for styling images: By the end of this chapter, you’ll have the tools to style images effectively and ensure they look great across all devices. The Common Values: Example: In this example: Use Case: This technique is ideal for hero images, thumbnails, or gallery layouts where consistent sizing is important. Reference: MDN - Object-fit Using Key Properties: Example: In this example: Use Case: This technique is perfect for creating hero sections, banners, or decorative backgrounds that enhance the overall design. Reference: MDN - Background Properties Forms are a fundamental part of web development, enabling users to interact with websites by submitting data, logging in, or filling out surveys. Properly styled forms not only enhance the visual appeal of your website but also improve usability and accessibility. Well-designed forms guide users through the input process, reduce errors, and provide a seamless experience across devices. In this chapter, we’ll explore how to style common form elements like inputs, buttons, checkboxes, and dropdowns using CSS. We’ll cover: By the end of this chapter, you’ll have the tools to create functional, visually appealing, and accessible forms. Styling basic form elements like text inputs, buttons, and labels ensures readability and improves the overall user experience. Consistent padding, font sizes, and borders make forms easier to use. Example: Use Case: These styles create a clean and professional look for forms, making them easy to read and interact with. Focus states are essential for accessibility, as they highlight the currently active form element for users navigating with a keyboard. Using the Example: Accessibility Tip: Always ensure that focus states are visible and distinct. Avoid removing focus indicators entirely, as this can hinder users who rely on keyboard navigation. Default checkboxes and radio buttons often look outdated or inconsistent across browsers. By hiding the default styles and replacing them with custom designs, you can create a polished and cohesive look. Example: Use Case: Customized checkboxes and radio buttons align with your design system and improve the overall aesthetic of your forms. Aligning form elements neatly is crucial for creating a clean and professional layout. Tools like flexbox or grid make it easy to align labels, inputs, and buttons horizontally or vertically. Example: Use Case: This approach ensures that labels and inputs are aligned consistently, improving readability and usability. Forms must look good and function well on all screen sizes, from desktops to mobile devices. Using media queries and relative units, you can adapt form layouts for smaller screens. Example: Responsive Design Tips: Mouse states (e.g., hover, active, focus) are a powerful way to add interactivity to your designs. These states allow you to respond to user actions, such as hovering over a button or clicking an input field, by applying dynamic styles. Incorporating these effects not only enhances the visual appeal of your website but also improves the overall user experience by providing feedback and guiding interactions. In this chapter, we’ll explore how to use mouse states effectively: By the end of this chapter, you’ll know how to make your designs more dynamic and user-friendly. The Example: Use Case: Hover effects are ideal for buttons, navigation menus, and interactive elements. They provide visual feedback, encouraging users to interact with your design. Tip: Avoid overusing hover effects on touch devices, as they may not work consistently. Instead, ensure that your design remains functional without relying solely on hover states. The Example: Use Case: Accessibility Tip: Always ensure that focus states are visible and distinct. Avoid removing focus indicators entirely, as this can hinder users who rely on keyboard navigation. For more information on pseudo-classes and their applications, refer to the official documentation: Throughout this guide, we’ve explored the foundational principles of CSS, including syntax elements, properties, values, and interactivity. Now, it’s time to bring everything together into a complete, real-world example. This exercise will demonstrate how all the concepts you’ve learned—such as selectors, layout techniques, styling images, and adding interactivity—work harmoniously to create a polished and functional design. By combining these elements, you’ll see how CSS can transform a simple HTML structure into a visually appealing and interactive component. Below is a complete example of a styled card component. This example incorporates: This chapter brings together everything you’ve learned so far, showing how CSS principles and techniques can be applied to create a functional and visually appealing design. By experimenting with this example and customizing it further, you’ll gain confidence in using CSS to build your own projects. CSS is a powerful language that brings structure, style, and interactivity to the web. Throughout this guide, we’ve broken down CSS terminology into manageable categories, making it easier to understand and apply in your projects. Here’s a quick recap of what we’ve covered: By breaking CSS into these categories, we’ve shown that even though CSS terminology is vast, it becomes much more approachable when tackled step by step. Now it’s your turn! CSS is a versatile language, and there’s always more to learn and experiment with. Here’s how you can take your knowledge further: Remember, mastering CSS is a journey, and every project is an opportunity to learn something new. Keep experimenting, keep building, and most importantly, have fun with it! To deepen your understanding of CSS and stay up-to-date with the latest techniques, here are some trusted external resources for further reading: MDN Web Docs: The ultimate reference for all things CSS, with detailed explanations, examples, and browser compatibility information. CSS-Tricks: A treasure trove of CSS tips, tricks, and tutorials, including the popular CSS Almanac. W3C Specifications: The official CSS specifications from the World Wide Web Consortium (W3C), providing authoritative guidelines for CSS standards. CSS Grid and Flexbox Guides: Dive deeper into modern layout techniques with these comprehensive guides: Interactive Learning Platforms: Practice your CSS skills with interactive platforms like: These resources will help you expand your CSS knowledge and stay ahead of the curve as web development continues to evolve. or
elements and have them propagate throughout your document.
body {
font-family: 'Arial', sans-serif;
}
/* All child elements inherit this font-family */
will use the Arial font unless another rule specifies otherwise.
inherit
keyword to explicitly inherit a property from a parent element:
span {
color: inherit; /* Inherits the color from its parent */
}
2.4 Box Model
+---------------------------+
| Margin |
| +---------------------+ |
| | Border | |
| | +---------------+ | |
| | | Padding | | |
| | | +---------+ | | |
| | | | Content | | | |
| | | +---------+ | | |
| | +---------------+ | |
| +---------------------+ |
+---------------------------+
.box {
width: 200px; /* Content width */
padding: 10px; /* Space inside the border */
border: 5px solid black; /* Border thickness and style */
margin: 20px; /* Space outside the border */
}
.box
element is calculated as:
Width = Content Width + Padding (left + right) + Border (left + right)
Width = 200px + 20px (10px × 2) + 10px (5px × 2) = 230px
box-sizing: border-box;
property to include padding and borders in the element’s total width and height, making layouts easier to manage:
* {
box-sizing: border-box;
}
3. Syntax Elements
3.1 Selectors
3.1.1 Type Selector
p
, div
, h1
). It applies styles to all instances of the specified element.
p {
color: navy;
}
elements will have navy-colored text.
3.1.2 Class Selector
class
attribute. Classes are reusable and can be applied to multiple elements, making them ideal for styling components like buttons, cards, or sections.
.button {
background-color: #007bff; /* Blue background */
color: white; /* White text */
}
button
will have a blue background and white text.
3.1.3 ID Selector
id
attribute. IDs are more specific than classes and should be used sparingly to avoid specificity conflicts.
#main-header {
background-color: #333; /* Dark gray background */
}
main-header
will have a dark gray background.
3.1.4 Attribute Selector
type="text"
, disabled
). This is particularly useful for styling form inputs or other elements with specific attributes.
input[type="text"] {
border: 1px solid #ccc; /* Gray border */
}
elements with the
type="text"
attribute will have a gray border.
3.1.5 Pseudo-class
:hover
), clicked (:active
), or matched by a certain pattern (:nth-child()
). Pseudo-classes add interactivity and dynamic behavior to your styles.
button:hover {
background-color: green; /* Green background on hover */
}
3.1.6 Pseudo-element
::first-line
) or content before/after an element (::before
, ::after
). Pseudo-elements are powerful for fine-grained control over styling.
p::first-line {
font-weight: bold; /* Bold text for the first line */
}
element will appear in bold.
3.2 Declarations and Rulesets
color
, font-size
).blue
, 16px
).
h1 {
color: blue; /* Property: color, Value: blue */
font-size: 36px; /* Property: font-size, Value: 36px */
}
h1
selector targets all elements.
color
property sets the text color to blue.font-size
property sets the text size to 36 pixels.
3.3 At-rules
@
that enable conditional and dynamic styling. They allow you to apply styles based on conditions, import external stylesheets, or define animations.
@media
: Applies styles based on media queries (e.g., screen size).@keyframes
: Defines animations.@import
: Imports external stylesheets.
@media (max-width: 768px) {
body {
font-size: 14px; /* Smaller font size on smaller screens */
}
}
@media
rule applies styles only when the viewport width is 768px or less.body
text size is reduced to 14px for better readability on smaller devices.
4. Properties and Values
4.1 Typography
font-family
: Specifies the typeface(s) to use.font-size
: Defines the size of the text.line-height
: Controls the spacing between lines of text.text-align
: Aligns text horizontally (e.g., left, center, right).
body {
font-family: 'Arial', sans-serif; /* Default font */
font-size: 16px; /* Base font size */
line-height: 1.5; /* Spacing between lines */
text-align: center; /* Center-align text */
}
font-family
ensures a clean and readable typeface.font-size
sets a comfortable base size for body text.line-height
improves readability by adding vertical spacing.text-align
centers all text within the .
4.2 Layout
display
: Defines how an element behaves in the layout (e.g., block
, inline
, flex
, grid
).position
: Controls the positioning of elements (e.g., static
, relative
, absolute
, fixed
).flex
: Enables flexible layouts with flexbox.grid
: Creates grid-based layouts with rows and columns.
.container {
display: flex; /* Use flexbox for layout */
justify-content: space-between; /* Distribute items evenly */
align-items: center; /* Vertically align items */
}
display: flex
property enables flexbox.justify-content
distributes child elements evenly across the container.align-items
vertically centers the child elements.
4.3 Colors and Backgrounds
4.3.1 Static Colors
#ffffff
), RGB (rgb(255, 0, 0)
), or named colors (red
).
.card {
background-color: #f0f0f0; /* Light gray background */
color: #333; /* Dark gray text */
}
background-color
sets a light gray background.color
defines dark gray text for contrast.
4.3.2 Dynamic Colors
/* Semi-transparent red */
.card {
background-color: rgba(255, 0, 0, 0.5); /* Red with 50% opacity */
}
/* Variable-based color definitions */
:root {
--primary-color: 255, 100, 50; /* RGB values as variables */
}
.button {
background-color: rgb(var(--primary-color)); /* Dynamically use the variable */
}
rgba()
function creates a semi-transparent red background.--primary-color
) allow you to define reusable color values.
4.4 Units and Measurements
4.4.1 Basic Units
px
and relative units like em
, rem
, and %
.
.box {
width: 50%; /* Half the width of the parent container */
padding: 1rem; /* Relative to the root font size */
}
width
uses a percentage to make the box responsive.padding
uses rem
for consistent spacing based on the root font size.
4.4.2 Logical Operations
calc()
, clamp()
, min()
, and max()
for dynamic calculations and responsive designs.
/* Dynamic width calculation */
.box {
width: calc(100% - 20px); /* Subtract 20px from the full width */
padding: calc(1rem + 5px); /* Add 5px to the padding */
}
/* Responsive font size */
h1 {
font-size: clamp(1.5rem, 5vw, 3rem); /* Minimum 1.5rem, scales up to 3rem */
}
/* Dynamic spacing */
.container {
margin: max(10px, 5%); /* Use the larger of 10px or 5% */
}
calc()
function performs mathematical operations.clamp()
function ensures a responsive font size that scales within a range.max()
function selects the larger value for spacing.
5. Styling Images
tags or using images as backgrounds, CSS provides powerful tools to control how images are displayed and integrated into your layouts. Properly styled images improve the aesthetics, responsiveness, and usability of your designs.
object-fit
: Controls how an image fits within its container.background-image
and related properties.
5.1 Object-fit
object-fit
property controls how an image (or video) fits within its container. It’s particularly useful for responsive designs where images need to maintain their aspect ratio while fitting into different-sized containers.
fill
: Stretches the image to fill the container, potentially distorting it.contain
: Scales the image to fit within the container while maintaining its aspect ratio.cover
: Scales the image to cover the entire container, cropping any overflow.none
: Displays the image at its original size, ignoring the container dimensions.scale-down
: Scales the image down to fit within the container if necessary.
img {
width: 100%; /* Full width of the container */
height: 300px; /* Fixed height */
object-fit: cover; /* Ensures the image covers the area without distortion */
}
width: 100%
ensures the image spans the full width of its container.height: 300px
sets a fixed height for the image.object-fit: cover
ensures the image scales proportionally to cover the container, cropping any overflow.
5.2 Background Images
background-image
allows you to add decorative visuals to elements like headers, sections, or buttons. Combined with properties like background-size
, background-position
, and background-repeat
, you can create visually appealing designs that adapt to different screen sizes.
background-image
: Specifies the image to use as a background.background-size
: Controls the size of the background image (cover
, contain
, or specific dimensions).background-position
: Aligns the image within the container (e.g., center
, top
, bottom
).background-repeat
: Determines whether the image repeats (repeat
, no-repeat
).
.hero {
background-image: url('hero.jpg'); /* Path to the image */
background-size: cover; /* Ensures the image covers the entire element */
background-position: center; /* Centers the image */
background-repeat: no-repeat; /* Prevents the image from repeating */
height: 400px; /* Fixed height for the hero section */
}
background-image
adds a decorative image to the .hero
section.background-size: cover
ensures the image scales proportionally to cover the entire section.background-position: center
centers the image within the container.background-repeat: no-repeat
prevents the image from tiling.
6. Styling Forms
6.1 Basic Form Styling
for="name">Name:
type="text" id="name" name="name">
type="submit">Submit
/* General styles for inputs and buttons */
input, button {
padding: 10px; /* Adds space inside the elements */
font-size: 16px; /* Ensures readable text size */
border: 1px solid #ccc; /* Subtle border for definition */
border-radius: 5px; /* Rounded corners for a modern look */
}
/* Button-specific styles */
button {
background-color: #007bff; /* Blue background for buttons */
color: white; /* White text for contrast */
cursor: pointer; /* Pointer cursor indicates interactivity */
}
6.2 Focus States and Accessibility
:focus
pseudo-class, you can add visual cues like borders, outlines, or shadows to indicate focus.
input:focus {
border-color: #007bff; /* Changes border color on focus */
outline: none; /* Removes default browser outline */
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5); /* Adds a subtle glow effect */
}
6.4 Customizing Checkboxes and Radio Buttons
type="checkbox" name="subscribe">
Subscribe to newsletter
/* Hide default checkbox styles */
input[type="checkbox"] {
appearance: none; /* Removes default styling */
width: 20px; /* Sets a fixed width */
height: 20px; /* Sets a fixed height */
border: 2px solid #007bff; /* Adds a blue border */
border-radius: 4px; /* Rounds the corners */
cursor: pointer; /* Indicates interactivity */
}
/* Style for checked state */
input[type="checkbox"]:checked {
background-color: #007bff; /* Fills the checkbox with blue */
}
6.5 Aligning Form Elements
class="form-group">
for="email">Email:
type="email" id="email" name="email">
type="submit">Submit
/* Flexbox-based alignment */
.form-group {
display: flex; /* Enables flexbox layout */
align-items: center; /* Vertically aligns elements */
gap: 10px; /* Adds spacing between label and input */
}
label {
flex: 1; /* Allocates space for the label */
}
input {
flex: 2; /* Allocates more space for the input */
}
6.6 Responsive Design for Forms
/* Stack form elements vertically on small screens */
@media (max-width: 600px) {
.form-group {
flex-direction: column; /* Stacks elements vertically */
}
}
%
, em
, or rem
for widths and spacing.
7. Adding Interactivity with Mouse States
7.1 Hover Effects
:hover
pseudo-class applies styles when a user hovers over an element. Hover effects are commonly used on buttons, links, and cards to indicate interactivity and draw attention.
button:hover {
background-color: green; /* Changes background color on hover */
}
7.2 Active and Focus States
:active
pseudo-class applies styles when an element is being actively clicked or pressed, while the :focus
pseudo-class highlights elements that currently have keyboard focus. These states are essential for creating responsive and accessible designs.
/* Active State */
button:active {
transform: scale(0.95); /* Slightly shrinks the button when clicked */
}
/* Focus State */
input:focus {
border-color: #007bff; /* Changes border color on focus */
outline: none; /* Removes default browser outline */
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5); /* Adds a subtle glow effect */
}
:active
state provides immediate feedback when a user interacts with an element, such as clicking a button.:focus
state ensures that keyboard users can easily identify which element is currently active, improving accessibility.
Why These Techniques Matter
Reference
8. Putting It All Together
8.1 Example
object-fit
to ensure images scale proportionally.</span>
lang="en">
/* Global Styles */
body {
font-family: 'Arial', sans-serif; /* Sets a clean and readable font */
margin: 0; /* Removes default margin */
padding: 0; /* Removes default padding */
}
/* Container Layout */
.container {
display: flex; /* Enables flexbox layout */
justify-content: center; /* Centers content horizontally */
align-items: center; /* Centers content vertically */
height: 100vh; /* Full viewport height */
background-color: #f0f0f0; /* Light gray background */
}
/* Card Styling */
.card {
width: 300px; /* Fixed width for the card */
padding: 20px; /* Adds spacing inside the card */
background-color: white; /* White background for contrast */
border: 1px solid #ccc; /* Subtle border for definition */
border-radius: 10px; /* Rounded corners for a modern look */
text-align: center; /* Centers text inside the card */
}
/* Image Styling */
.card img {
width: 100%; /* Ensures the image spans the full width of the card */
height: 150px; /* Fixed height for consistency */
object-fit: cover; /* Ensures the image scales proportionally */
border-radius: 10px 10px 0 0; /* Rounds the top corners of the image */
}
/* Button Styling */
button {
background-color: #007bff; /* Blue background for buttons */
color: white; /* White text for contrast */
padding: 10px 20px; /* Adds space inside the button */
border: none; /* Removes default border */
border-radius: 5px; /* Rounded corners for a modern look */
cursor: pointer; /* Pointer cursor indicates interactivity */
transition: background-color 0.3s ease; /* Smooth transition for hover effect */
}
/* Hover Effect */
button:hover {
background-color: #0056b3; /* Darker blue on hover */
}
class="container">
class="card">
src="example.jpg" alt="Example Image">
Card Title
This is a sample card with CSS styling.
Click Me
How It Works
font-family: 'Arial', sans-serif;
ensures a clean and professional look for all text.text-align: center;
centers the text within the card.
.container
uses flexbox (display: flex
, justify-content: center
, align-items: center
) to center the card both horizontally and vertically within the viewport.height: 100vh
ensures the container spans the full height of the viewport.
.container
has a light gray background (background-color: #f0f0f0
) to provide contrast with the white card..card
uses a white background (background-color: white
) and a subtle border (border: 1px solid #ccc
) for definition.
width: 100%
and height: 150px
ensure the image scales proportionally while maintaining a consistent size.object-fit: cover
ensures the image fills the container without distortion.border-radius: 10px 10px 0 0
rounds the top corners of the image for a polished look.
button
includes a hover effect (background-color: #0056b3
) that changes the background color when the user hovers over it.transition: background-color 0.3s ease
adds a smooth animation for the hover effect.
9. Conclusion
calc()
and CSS variables.object-fit
and background properties, as well as how to create visually appealing and accessible forms.:hover
, :active
, and :focus
to enhance user experience.
9.1 Call to Action
10. Additional Resources