CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Internal Styles


Internal styles are CSS rules embedded directly within an HTML document's <head> section using the <style> tag. This method is suitable for styling single HTML pages, providing a quick way to apply styles without external files.

Example 1: Basic Text Styling

<!DOCTYPE html>
<html>
<head>
<title>Internal Styles Example</title>
<style>
  /* Selects all paragraph elements */
  p {
    color: blue; /* Sets text color to blue */
    font-size: 16px; /* Sets font size to 16 pixels */
  }
</style>
</head>
<body>
  <p>This paragraph will be blue and 16px.</p>
</body>
</html>

Explanation This example demonstrates how to set the color and font size for all <p> elements on the page. The CSS rules inside the <style> tag are applied globally to the HTML document.


Example 2: Styling a Specific Element by ID

<!DOCTYPE html>
<html>
<head>
<title>Internal Styles ID Example</title>
<style>
  /* Selects the element with id "header-title" */
  #header-title {
    text-align: center; /* Centers the text */
    background-color: lightgray; /* Sets background color */
    padding: 10px; /* Adds 10 pixels of padding around the text */
  }
</style>
</head>
<body>
  <h1 id="header-title">Welcome to Our Website</h1>
</body>
</html>

Explanation Here, we target a specific <h1> element using its id attribute. This allows for precise styling of individual elements within the HTML structure.


Example 3: Styling Multiple Elements by Class

<!DOCTYPE html>
<html>
<head>
<title>Internal Styles Class Example</title>
<style>
  /* Selects all elements with the class "highlight" */
  .highlight {
    background-color: yellow; /* Sets background to yellow */
    font-weight: bold; /* Makes the text bold */
  }
</style>
</head>
<body>
  <p class="highlight">This text is highlighted.</p>
  <p>This text is normal.</p>
  <span class="highlight">And this span is also highlighted.</span>
</body>
</html>

Explanation This example showcases how to apply the same style to multiple elements using a CSS class. Elements with the highlight class will have a yellow background and bold text.


Example 4: Combining Element and Class Selectors

<!DOCTYPE html>
<html>
<head>
<title>Internal Styles Combined Example</title>
<style>
  /* Selects paragraph elements that also have the class "important" */
  p.important {
    color: red; /* Sets text color to red */
    border: 1px solid red; /* Adds a red border */
    padding: 5px; /* Adds padding inside the border */
  }
</style>
</head>
<body>
  <p class="important">This is an important paragraph.</p>
  <p>This is a regular paragraph.</p>
</body>
</html>

Explanation This code demonstrates a more specific selector, targeting only <p> elements that also possess the important class. This allows for fine-grained control over your web page's design.


Example 5: Using Media Queries for Responsive Design

<!DOCTYPE html>
<html>
<head>
<title>Internal Styles Responsive Example</title>
<style>
  body {
    font-family: Arial, sans-serif; /* Sets a common font */
  }

  /* Styles applied when screen width is 600px or less */
  @media (max-width: 600px) {
    p {
      font-size: 14px; /* Reduces font size on smaller screens */
      color: green; /* Changes text color to green */
    }
  }
</style>
</head>
<body>
  <p>This text will change style on smaller screens.</p>
</body>
</html>

Explanation This example illustrates using a media query within internal styles to create responsive designs. The paragraph's font size and color will adjust when the screen width is 600 pixels or less, optimizing for various devices.