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

Inline Block Display


The CSS display property with the value inline-block is a powerful tool for controlling the layout of elements. It allows an element to behave like an inline element, flowing with text, while also possessing block-level properties like width, height, margin, and padding. This makes it ideal for creating navigation bars, grids, and other layout components where you need more control than inline provides but don't want the full line break of a display: block element.


Example 1: Basic Navigation Menu

CSS

/* style.css */
.nav-item {
  display: inline-block; /* Allows setting width and padding while staying in a line */
  width: 100px;
  padding: 15px;
  background-color: #f0f0f0;
  text-align: center;
  border: 1px solid #ccc;
}

Explanation

This code styles navigation links. By setting display: inline-block, we can define a specific width and padding for each link, which wouldn't be possible if they were just inline. They remain on the same line, forming a horizontal menu.


Example 2: Creating a Simple Grid

CSS

/* style.css */
.grid-item {
  display: inline-block; /* Each item can have dimensions and sits next to others */
  width: 30%;
  margin: 1.5%;
  height: 150px;
  background-color: #3498db;
  color: white;
}

Explanation

Here, inline-block is used to create a flexible grid of items. Each .grid-item has a percentage-based width and margin, allowing them to sit side-by-side and wrap to the next line if the container is too narrow.


Example 3: Vertically Aligning Different Sized Items

CSS

/* style.css */
.aligned-item {
  display: inline-block;
  width: 100px;
  background-color: #e74c3c;
  vertical-align: middle; /* Aligns items to the middle of the line */
}

Explanation

This example demonstrates how vertical-align works with inline-block elements. The .aligned-item elements, which may have different heights, are vertically centered next to each other, which is a common layout challenge solved by this property.


Example 4: Form Inputs and Labels

CSS

/* style.css */
.form-field {
  display: inline-block; /* Allows labels and inputs to have consistent spacing */
  margin: 10px;
  width: 200px;
}

Explanation

Using display: inline-block on a container for a label and its input field allows for consistent alignment and spacing. This helps in creating clean and organized forms where each field is treated as a single horizontal unit.


Example 5: Image with a Caption

CSS

/* style.css */
.image-container {
  display: inline-block; /* Container shrinks to fit the image and caption */
  border: 1px solid #ddd;
  text-align: center;
}
.caption {
  display: block; /* Caption appears below the image */
  padding: 5px;
}

Explanation

In this scenario, the .image-container is set to inline-block so it only takes up the necessary space. This allows multiple image containers to sit next to each other, behaving like images themselves while containing both an image and its caption.


Example 6: Product Cards in an E-commerce Layout

CSS

/* style.css */
.product-card {
  display: inline-block; /* Each card is a block with set dimensions, arranged inline */
  width: 220px;
  padding: 20px;
  margin: 10px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  vertical-align: top;
}

Explanation

This code is perfect for an e-commerce site. Each .product-card is an inline-block, allowing them to line up horizontally. We can control their size, spacing, and vertical alignment, ensuring a clean and consistent product listing.


Example 7: Button Grouping

CSS

/* style.css */
.button-group .btn {
  display: inline-block; /* Allows buttons to sit side-by-side */
  padding: 10px 20px;
  background-color: #2ecc71;
  color: white;
  border: none;
}

Explanation

By applying display: inline-block to buttons within a .button-group, they are rendered next to each other without any space in between if the HTML has no whitespace. This is a common technique for creating segmented controls or button toolbars.