What is CSS?

CSS (Cascading Style Sheets) is a language used to style and format HTML elements on a webpage.

Why Use CSS?

How CSS Works

CSS applies styles using selectors, properties, and values.

p { color: blue; font-size: 16px; }

Types of CSS

Note:

Linking style.css to index.html

To apply external styles to an HTML page, you need to link a CSS file to your HTML document.

1. Using the <link> Tag

The best way to link CSS is by using the <link> tag inside 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 Website <link rel="stylesheet" href="style.css"> </head> <body> <h1>Welcome to My Website </body> </html>

2. Ensuring the Correct File Path

If the CSS file is inside a folder, update the path:

<link rel="stylesheet" href="css/style.css">

3. Alternative: Using an Internal Style

If linking is not working, try internal CSS:

<style> body { background-color: lightblue; }

Fonts in CSS

Fonts in CSS control the appearance of text, including its size, weight, spacing, and style.

1. Setting Font Family

The font-family property specifies the font of the text.

p { font-family: Arial, sans-serif; }

2. Using Google Fonts

Google Fonts allow you to use web fonts in your design.

@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap'); body { font-family: 'Roboto', sans-serif; }

3. Setting Font Weight

The font-weight property adjusts the thickness of the text.

h1 { font-weight: bold; }

4. Changing Font Style

The font-style property applies italic styling to text.

span { font-style: italic; }

5. Setting Letter Spacing

The letter-spacing property increases or decreases spacing between characters.

h2 { letter-spacing: 3px; }

6. Adjusting Line Height

The line-height property controls the height between lines of text.

p { line-height: 1.5; }

7. Text Transformation

The text-transform property changes capitalization style.

h3 { text-transform: uppercase; }

8. Text Decoration

The text-decoration property adds underlines, overlines, or strikethroughs to text.

a { text-decoration: underline; }

9. Applying Multiple Font Properties

You can apply multiple font properties in a single declaration.

p { font: italic bold 20px Arial, sans-serif; }

10. Responsive Font Sizes

The font-size can be defined in relative units for responsiveness.

h1 { font-size: 5vw; }

11. Font Variant

The font-variant property is used for small caps styling.

p { font-variant: small-caps; }

12. Font Smoothing

The -webkit-font-smoothing property improves text rendering on webkit browsers.

body { -webkit-font-smoothing: antialiased; }

13. Custom Font Loading

The @font-face rule allows loading of custom fonts.

@font-face { font-family: 'MyCustomFont'; src: url('myfont.woff2') format('woff2'); }

14. Applying Different Fonts to Elements

Different font families can be applied to specific elements.

h1 { font-family: 'Times New Roman', serif; } h2 { font-family: 'Courier New', monospace; }

15. Key Takeaways

Borders in CSS

Borders in CSS define the outline of elements, allowing for customization of width, style, and color.

1. Basic Border

The border property defines a border around an element.

div { border: 2px solid black; }

2. Different Border Styles

CSS provides various border styles.

div { border-style: dotted; } div { border-style: dashed; } div { border-style: double; }

3. Border Width

The border-width property adjusts the thickness of a border.

div { border-width: 5px; }

4. Border Color

The border-color property sets the color of the border.

div { border-color: blue; }

5. Individual Border Sides

Borders can be applied to specific sides.

div { border-top: 3px solid red; }

6. Rounded Borders

The border-radius property rounds the corners of an element.

div { border-radius: 10px; }

7. Border Shorthand

All border properties can be combined into one shorthand.

div { border: 3px dashed green; }

8. Border Image

The border-image property allows an image to be used as a border.

div { border: 10px solid; border-image: url('border.png') 30 round; }

9. Border Collapse (For Tables)

The border-collapse property controls whether table borders collapse into a single border or remain separate.

table { border-collapse: collapse; }

10. Key Takeaways

Background in CSS

Background properties in CSS allow you to set colors, images, gradients, and patterns behind elements.

1. Setting Background Color

The background-color property sets the background color of an element.

div { background-color: lightblue; }

2. Using Background Images

The background-image property sets an image as the background.

div { background-image: url('background.jpg'); background-size: cover; }

3. Background Repeat

The background-repeat property controls how a background image is repeated.

div { background-image: url('pattern.png'); background-repeat: no-repeat; }

4. Background Position

The background-position property specifies the starting position of a background image.

div { background-image: url('image.png'); background-position: center center; }

5. Background Attachment

The background-attachment property controls whether a background image scrolls with the page or stays fixed.

div { background-image: url('bg.jpg'); background-attachment: fixed; }

6. Background Size

The background-size property defines how a background image fits inside an element.

div { background-image: url('image.jpg'); background-size: contain; }

7. Background Gradient

The background property can be used to create gradients.

div { background: linear-gradient(to right, red, yellow); }

8. Multiple Backgrounds

CSS allows multiple background images to be layered.

div { background: url('top.png') top left no-repeat, url('bottom.png') bottom right no-repeat; }

9. Background Clip

The background-clip property specifies how far the background extends.

div { background: lightblue; background-clip: padding-box; }

10. Background Shorthand

The background property allows multiple background properties to be set in one line.

div { background: url('image.jpg') no-repeat center/cover fixed; }

11. Key Takeaways

Margins in CSS

Margins in CSS create space around elements, separating them from other elements on the page.

1. Setting Margin

The margin property controls the space around an element.

div { margin: 20px; }

2. Individual Margins

Margins can be set separately for each side.

div { margin-top: 10px; margin-right: 15px; margin-bottom: 20px; margin-left: 25px; }

3. Auto Margin for Centering

The margin: auto is commonly used to center block elements.

div { width: 50%; margin: 0 auto; }

4. Percentage-Based Margins

Margins can be defined in percentages relative to the parent element.

div { margin: 10%; }

5. Margin Collapsing

When two vertical margins meet, they collapse to the larger margin.

div { margin-top: 30px; margin-bottom: 20px; }

6. Negative Margins

Negative values can be used to pull elements closer together.

div { margin-top: -10px; }

7. Margin Shorthand

The margin shorthand can be used to define all sides at once.

div { margin: 10px 15px 20px 25px; }

8. Margin for Inline Elements

Margins behave differently on inline elements compared to block elements.

span { margin: 10px; }

9. Key Takeaways

Float in CSS

The float property is used to position elements to the left or right, allowing text or inline elements to wrap around them.

1. Basic Float

The float property allows an element to be positioned on one side.

img { float: right; }

2. Floating Left

Elements can also be floated to the left.

div { float: left; }

3. Floating Multiple Elements

Floated elements can be placed side by side.

.box { width: 30%; float: left; }

4. Clearing Floats

The clear property is used to prevent floating elements from affecting other elements.

div { clear: both; }

5. Float Within a Container

Floated elements inside a container may cause issues; use overflow: hidden; to fix this.

.container { overflow: hidden; }

6. Floats and Inline Elements

Floating inline elements helps structure content.

span { float: left; }

7. Float Shorthand

Float values include left, right, and none.

div { float: none; }

8. Key Takeaways

Position in CSS

The position property in CSS determines how an element is positioned in the document.

1. Static Positioning

The default positioning method.

div { position: static; }

2. Relative Positioning

The element is positioned relative to its normal position.

div { position: relative; top: 10px; left: 20px; }

3. Absolute Positioning

The element is positioned relative to the nearest positioned ancestor.

div { position: absolute; top: 50px; left: 100px; }

4. Fixed Positioning

The element is positioned relative to the viewport.

div { position: fixed; top: 0; right: 0; }

5. Sticky Positioning

The element toggles between relative and fixed based on scrolling.

div { position: sticky; top: 20px; }

6. Z-Index

The z-index property controls stacking order.

div { position: absolute; z-index: 10; }

7. Key Takeaways

Shadows in CSS

CSS provides properties to add shadows to elements and text, enhancing visual appeal.

1. Box Shadow

The box-shadow property applies a shadow to an element.

div { box-shadow: 5px 5px 10px gray; }

2. Multiple Box Shadows

Multiple shadows can be added by separating values with commas.

div { box-shadow: 3px 3px 5px red, -3px -3px 5px blue; }

3. Inset Box Shadow

The inset keyword creates an inner shadow.

div { box-shadow: inset 5px 5px 10px rgba(0,0,0,0.5); }

4. Text Shadow

The text-shadow property applies shadows to text.

h1 { text-shadow: 2px 2px 5px black; }

5. Multiple Text Shadows

Like box shadows, multiple text shadows can be applied.

h1 { text-shadow: 2px 2px 4px red, -2px -2px 4px blue; }

6. Key Takeaways

Transform in CSS

The transform property allows you to apply 2D and 3D transformations to elements.

1. Translate (Move Elements)

The translate() function moves an element along the X and Y axes.

div { transform: translate(50px, 20px); }

2. Rotate Elements

The rotate() function rotates an element.

div { transform: rotate(45deg); }

3. Scale Elements

The scale() function resizes an element.

div { transform: scale(1.5, 1.5); }

4. Skew Elements

The skew() function skews an element.

div { transform: skew(20deg, 10deg); }

5. Combining Multiple Transforms

Multiple transforms can be applied together.

div { transform: rotate(30deg) scale(1.2) translate(50px, 0); }

6. Transform Origin

The transform-origin property sets the point around which transformations occur.

div { transform-origin: top left; }

7. 3D Transformations

The perspective() function adds depth to 3D transformations.

div { transform: perspective(500px) rotateY(45deg); }

8. Key Takeaways

Animations in CSS

CSS animations allow elements to transition between different styles smoothly over a defined duration.

1. Basic Animation

The @keyframes rule defines an animation.

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } div { animation: fadeIn 2s; }

2. Animating Position

Elements can move smoothly using animations.

@keyframes moveRight { 0% { transform: translateX(0px); } 100% { transform: translateX(200px); } } div { animation: moveRight 3s linear; }

3. Looping Animations

The infinite keyword makes animations repeat endlessly.

div { animation: moveRight 3s linear infinite; }

4. Delay in Animation

The animation-delay property delays the animation start.

div { animation: fadeIn 2s; animation-delay: 1s; }

5. Animation Iteration Count

Defines how many times the animation should repeat.

div { animation: fadeIn 2s 3; }

6. Changing Animation Timing

The animation-timing-function controls the animation speed curve.

div { animation: fadeIn 2s ease-in-out; }

7. Combining Multiple Animations

Multiple animations can be applied to an element.

div { animation: fadeIn 2s, moveRight 3s; }

8. Animation Fill Mode

The animation-fill-mode property determines the final state of an animated element.

div { animation: fadeIn 2s forwards; }

9. Key Takeaways

Blog

Trying My Best in cybersecurity, tutorials, and more!