Get started with 33% off your first certification using code: 33OFFNEW

`-webkit-box` Explained

5 min read
Published on 21st August 2024

One of the more obscure yet powerful tools in CSS is -webkit-box, a property that was part of an early implementation of the Flexbox layout model. Though it's been mostly replaced by the modern Flexbox (display: flex), understanding -webkit-box is still valuable, especially if you're dealing with older codebases or need to ensure compatibility with legacy browsers.

This article delves into the -webkit-box property, explaining what it is, how it works, and when you might still encounter or use it today.

What is -webkit-box?

-webkit-box is an older implementation of a flexible box layout module that was introduced by the WebKit engine, which powers browsers like Safari and earlier versions of Chrome. It was an experimental feature designed to offer a more flexible way to align and distribute space among items in a container, which was a challenge in earlier CSS layout models.

In essence, -webkit-box was a precursor to the modern Flexbox layout, providing a way to control the flow, alignment, and distribution of elements within a container.

Basic Syntax

To use -webkit-box, you would typically apply it to a container element, turning that container into a flexbox. Here’s the basic syntax:

.container {
    display: -webkit-box;
    -webkit-box-orient: horizontal;
    -webkit-box-pack: start;
    -webkit-box-align: stretch;
}

Let's break down these properties:

  • display: -webkit-box;: This sets the container as a flexbox using the WebKit-specific implementation.
  • -webkit-box-orient: Controls the direction of the child elements inside the container. It can be set to horizontal (row) or vertical (column).
  • -webkit-box-pack: Aligns the child elements along the main axis (the direction set by -webkit-box-orient). Possible values include start, center, end, and justify.
  • -webkit-box-align: Controls the alignment of child elements along the cross-axis (perpendicular to the main axis). Values can be start, center, end, baseline, or stretch.

Understanding the Properties in Detail

Now that you have a basic understanding of the -webkit-box properties, let's dive deeper into each of them.

-webkit-box-orient

The -webkit-box-orient property defines the direction in which the child elements are laid out within the container. It’s akin to the flex-direction property in modern Flexbox.

  • horizontal: Child elements are placed side by side in a row.
  • vertical: Child elements are stacked one on top of the other in a column.

Example:

.container {
    display: -webkit-box;
    -webkit-box-orient: vertical;
}

.item {
    padding: 10px;
    border: 1px solid #ccc;
}
<div class="container">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
</div>

In this example, the items would be stacked vertically.

-webkit-box-pack

The -webkit-box-pack property defines how child elements are aligned along the main axis (which can be horizontal or vertical, depending on -webkit-box-orient).

  • start: Items are aligned at the start of the container.
  • center: Items are centered along the main axis.
  • end: Items are aligned at the end of the container.
  • justify: Items are spaced evenly across the main axis.

Example:

.container {
    display: -webkit-box;
    -webkit-box-orient: horizontal;
    -webkit-box-pack: center;
}

This would center the items within the container along the horizontal axis.

-webkit-box-align

The -webkit-box-align property determines how the child elements are aligned along the cross-axis (the axis perpendicular to the one set by -webkit-box-orient).

  • start: Items are aligned at the start of the cross-axis.
  • center: Items are centered along the cross-axis.
  • end: Items are aligned at the end of the cross-axis.
  • baseline: Items are aligned based on their baselines.
  • stretch: Items stretch to fill the container along the cross-axis.

Example:

.container {
    display: -webkit-box;
    -webkit-box-orient: horizontal;
    -webkit-box-align: stretch;
}

In this example, the items would stretch to fill the height of the container.

A Real-World Example

Let’s create a simple layout using -webkit-box that demonstrates a horizontal navigation bar with evenly spaced items.

.navbar {
    display: -webkit-box;
    -webkit-box-orient: horizontal;
    -webkit-box-pack: justify;
    -webkit-box-align: center;
    background-color: #333;
    padding: 10px;
}

.nav-item {
    color: white;
    padding: 10px;
    text-decoration: none;
}
<div class="navbar">
    <a href="#" class="nav-item">Home</a>
    <a href="#" class="nav-item">About</a>
    <a href="#" class="nav-item">Services</a>
    <a href="#" class="nav-item">Contact</a>
</div>

In this example:

  • The navbar container is set as a -webkit-box with horizontal orientation.
  • The items are evenly spaced across the width of the navbar, thanks to -webkit-box-pack: justify.
  • The items are also centered along the vertical axis using -webkit-box-align: center.

The Decline of -webkit-box

As web standards evolved, the -webkit-box model was gradually replaced by the more robust and standardized Flexbox model (display: flex). Flexbox provided greater flexibility, more consistent behavior across browsers, and more powerful layout capabilities.

Today, the use of -webkit-box is generally discouraged in favor of Flexbox. However, you might still encounter it in older projects or need to use it when maintaining legacy code that was built before Flexbox became widely supported.

Transitioning from -webkit-box to Flexbox

If you’re working with a codebase that still uses -webkit-box, transitioning to Flexbox is relatively straightforward. Here’s how you can map -webkit-box properties to their Flexbox equivalents:

  • display: -webkit-box;display: flex;
  • -webkit-box-orient: horizontal;flex-direction: row;
  • -webkit-box-orient: vertical;flex-direction: column;
  • -webkit-box-pack: start;justify-content: flex-start;
  • -webkit-box-pack: center;justify-content: center;
  • -webkit-box-pack: end;justify-content: flex-end;
  • -webkit-box-pack: justify;justify-content: space-between;
  • -webkit-box-align: start;align-items: flex-start;
  • -webkit-box-align: center;align-items: center;
  • -webkit-box-align: end;align-items: flex-end;
  • -webkit-box-align: stretch;align-items: stretch;

Here’s how you could convert the earlier navigation bar example to use Flexbox:

.navbar {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    background-color: #333;
    padding: 10px;
}

.nav-item {
    color: white;
    padding: 10px;
    text-decoration: none;
}

This achieves the same layout, but with more modern and widely supported CSS properties.

Conclusion: The Legacy of -webkit-box

While -webkit-box is largely a relic of the past, it’s an important part of the evolution of CSS layout techniques. Understanding it is crucial for those who maintain older web applications or need to ensure compatibility with legacy systems. However, for new projects, Flexbox is the recommended approach due to its superior flexibility and broader support across modern browsers.

Whether you’re working with legacy code or exploring the history of CSS, understanding -webkit-box provides valuable insight into how web standards have evolved and how modern tools like Flexbox came to be.

For further reading, consider these resources: