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

Block Display


The display property in CSS is a fundamental property that specifies how an element is rendered in a document. The block value for the display property makes an element a block-level element. A block-level element always starts on a new line and takes up the full width available, pushing subsequent elements to the next line.


Example 1: Basic Block Behavior

/* Selects all <p> elements and makes them block-level */
p {
  display: block; /* This is the default for paragraphs */
  background-color: lightblue;
  margin-bottom: 10px;
}

Explanation: This code explicitly sets paragraph elements to display: block. Each paragraph will start on a new line and span the full width of its container, with a light blue background to visualize the space it occupies.


Example 2: Converting Inline to Block

/* Changes inline <span> elements to block-level */
span {
  display: block;
  background-color: lightgreen;
  width: 200px; /* Now we can set width */
  height: 50px; /* And height */
}

Explanation: By default, <span> is an inline element. This example changes it to a block-level element, which allows us to define its width and height, properties that are not applicable to inline elements.


Example 3: Block-Level Links for Navigation

/* Styles navigation links to be block-level for easier clicking */
nav a {
  display: block;
  padding: 15px;
  background-color: #333;
  color: white;
  text-decoration: none;
}

Explanation: This turns anchor tags within a <nav> element into block-level elements. This makes the entire rectangular area of each link clickable, not just the text, improving user experience.


Example 4: Structuring a Header

/* Ensures header elements are treated as distinct blocks */
header h1, header p {
  display: block;
  text-align: center;
}

Explanation: Here, both the <h1> and <p> elements inside a <header> are set to display: block. This ensures they stack vertically and each takes up the full width, which is ideal for a centered header layout.


Example 5: Creating Vertical Button Groups

/* Styles buttons to appear one above the other */
.button-group button {
  display: block;
  width: 100%;
  margin-bottom: 5px;
  padding: 10px;
}

Explanation: This example styles buttons within a .button-group to be block-level elements. Setting width: 100% makes each button fill the horizontal space of its parent container, creating a stacked button layout.


Example 6: Using Block for Form Elements

/* Displays form labels and inputs on separate lines */
form label, form input {
  display: block;
  margin-bottom: 8px;
}

Explanation: By setting both <label> and <input> elements to display: block, they each occupy their own line. This creates a clean, organized, and easy-to-read vertical form structure.


Example 7: Controlling List Item Display

/* Explicitly setting list items to block */
ul li {
  display: block;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  padding: 5px;
}

Explanation: While <li> elements are block-level by default, this example explicitly sets display: block. This can be useful to override other styles or to emphasize that each list item is a distinct block-level container.