Mastering 2 Column Grid CSS with Flexbox and Grid

Author

Posted Oct 22, 2024

Reads 482

View of city skyline framed by a modern metal grid structure, showing architectural innovation.
Credit: pexels.com, View of city skyline framed by a modern metal grid structure, showing architectural innovation.

Flexbox is a powerful layout tool that can be used to create a 2 column grid with just a few lines of CSS. By setting the display property to flex and the flex-direction property to row, you can easily create a two-column layout.

To make the columns equal in width, you can use the flex-basis property to set the width of each column to 50%. This will ensure that the columns are evenly spaced and take up half of the available space.

In the example, we used the flex-basis property to set the width of each column to 50%. This resulted in a 2 column grid with equal width columns.

Grid Layout Options

You can create a responsive grid using flexbox, which is simpler than using float grid. Flexbox automatically divides up the grid space equally between columns, regardless of how many columns you have.

To use flexbox, declare that the parent should use flexbox and set the flex property on the child column divs. The flex: 1 declaration means each column div will have the same width as the other columns, kind of like a ratio.

Flexbox is pretty intelligent, so you can easily change the ratio of column widths by adjusting the flex value. For example, setting the first column's flex to 2 and the second column's flex to 1 will make the first column double the width of the other.

Important Terminology

Credit: youtube.com, CSS Grid Layout Course #05: Terminology (Grid Line, Track, Area etc)

To truly master Grid Layout Options, it's essential to understand the terminology involved. The Grid specification defines several key terms that can be easy to confuse with one another if you don't first memorize their meanings.

There aren't many Grid terms to memorize, so don't worry if it seems daunting at first.

Flexbox Options

Bootstrap 4 is built with flexbox, but not every element's display has been changed to display: flex, as this would add many unnecessary overrides and unexpectedly change key browser behaviors.

Most components are built with flexbox enabled, but you may need to add display: flex to an element using classes like .d-flex or its responsive variants.

To enable flexbox on an element, simply add the .d-flex class or one of its responsive variants, such as .d-sm-flex, to the element's HTML.

Flexbox options are a powerful tool for building responsive grids, and with Bootstrap 4, you can easily create complex layouts with ease.

You can use flexbox utilities for sizing, alignment, spacing, and more, but you'll need to enable flexbox on the element first.

By using flexbox, you can create responsive grids that adapt to different screen sizes and devices, making it easier to build complex layouts.

Mix and Match

Credit: youtube.com, Match Your Modular Grid to your and Baseline!

The beauty of grid layouts lies in their flexibility, and one of the most powerful features is the ability to mix and match different column configurations for each breakpoint.

You can use a combination of different props for each tier as needed, which allows you to create unique layouts that adapt to different screen sizes.

The Responsive Attributes system, for example, uses simple data-attributes to define the columns for each breakpoint, making it easy to create custom layouts.

Don't be afraid to experiment and try out different combinations to see what works best for your project.

With a little creativity, you can create complex and beautiful layouts that engage your users and enhance their experience.

Making a Grid

To create a grid, you would give multiple elements a float property, which would make all of them align to one side, either right or left.

Floats aren't terrible, they were all we had for quite a while.

Credit: youtube.com, Simplify your CSS with these 3 grid layout solutions

You can fix the problem of the parent div collapsing to the height it would be if it had no children by adding a display: table ruleset to the parent element or by clearing the floats with an :after pseudo-element.

To create a responsive grid using flexbox, you'll need to declare that the parent should use flexbox and set the flex property on the child column divs.

Flexbox is pretty intelligent and will automatically divide up the grid space equally between the columns no matter how many you have.

To make the grid responsive, you'll need to set your default styles for mobile first, just like you would for a float grid.

Flexbox is simpler than using floats, and it's a great alternative for creating responsive grids.

The flex: 1 declaration means that each column div will have the same width as the other columns, kind of like a ratio.

If you wanted the first column to be double the width of the other column, you would set the first column's flex to 2, and the second column's flex value would be half of that, 1.

Grid Styling

Credit: youtube.com, Build Layouts with CSS Grid #2 - Multi-Column Layout

Grid styling is key to creating a visually appealing two-column grid. A two-column photo love heart design, like the Love Heart 2 Column Layout, can be achieved with creative border radius settings.

This layout is designed to work with a square image background, which is a great starting point for grid styling. By using a square image, you can create a clean and symmetrical design that's perfect for showcasing your content.

To take your grid styling to the next level, consider experimenting with different border radius settings, like those used in the Love Heart 2 Column Layout.

Static

Static layouts can be achieved using CSS grid, which allows columns to stay side-by-side, equal-width, and equal-height even on small mobile screens.

Using CSS grid, you can create a two-column layout that adapts to different screen sizes. The columns will always be next to each other, regardless of the screen resolution.

One benefit of CSS grid is that it eliminates the need for custom tags to define the layout, unlike some other systems like Responsive Columns, which use tiny custom tags for this purpose.

Responsive Columns, on the other hand, uses its own system to handle structural CSS, making it a convenient option for some developers. However, it may not be the best choice for everyone, especially those who prefer a more straightforward approach like CSS grid.

Installing Gutters

Interior of aged church with columns bas reliefs and sculptures depicting biblical scenes
Credit: pexels.com, Interior of aged church with columns bas reliefs and sculptures depicting biblical scenes

Grid systems can be a bit finicky, but one thing to keep in mind is that Flexbox is smart and can automatically adjust the size of gutters between columns.

To make sure our gutters work as intended, we need to explicitly set the correct width to each column. This is especially important if we're using a gutter size that's not a percentage.

If we have gutters set to 2rem, each column width will be 50% minus 1rem. This means we need to use the same units in our calc as we do in our column-gap declaration.

Padded Boxes with Headings

Padded boxes with headings are a versatile and flexible layout option. You can add as many boxes as you like, and they will simply wrap to multiple rows of two boxes wide.

Each box has a colored heading and a padded grey box of content, making it easy to distinguish between different sections. The gutter between boxes adds a touch of visual separation.

On mobile devices, the boxes are stacked vertically, but on tablets and bigger screens, they sit side-by-side, allowing for a clean and organized layout.

Margin and Padding

Credit: youtube.com, The Ultimate Guide to MUI Grid Spacing, Padding, and Margin

Bootstrap 4 includes a five-level scale for spacing utilities, based on a 1rem value default SASS $spacer variable.

You can choose values for all viewports, or pick responsive variants to target specific viewports. For example, use .mr-3 for margin-right: 1rem.

The margin and padding utilities allow you to control how elements and components are spaced and sized. This is particularly useful for designing responsive layouts.

To force sibling columns away from one another, use margin utility classes like .mr-auto. This is especially useful in grid layouts where you want columns to take up equal space.

Justify Self

Justify Self is a property that aligns a grid item inside a cell along the inline (row) axis. It's the opposite of align-self, which aligns along the block (column) axis.

The justify-self property can be set to four different values: start, end, center, and stretch. These values determine how the grid item is aligned within the cell.

Credit: youtube.com, The justify-self property with CSS Grid layout

Here are the possible values for justify-self:

  • start – aligns the grid item to be flush with the start edge of the cell
  • end – aligns the grid item to be flush with the end edge of the cell
  • center – aligns the grid item in the center of the cell
  • stretch – fills the whole width of the cell (this is the default)

The stretch value is the default for justify-self, which means that grid items will fill the entire width of the cell unless you specify otherwise.

Helpful AI Assistant

As a helpful AI assistant, I've learned that responsive design is key to creating a user-friendly experience. This is achieved by specifying the number of columns for different breakpoints, such as 1 column for mobile and 2 columns for tablet.

I've seen how the Responsive Attributes system works by adding data-attributes to HTML tags. This allows for a two-column layout that stacks on mobile and sits side-by-side on tablet.

To create a responsive design, it's essential to understand how to work with different breakpoints. By specifying the number of columns for each breakpoint, you can ensure a smooth user experience.

The Responsive Columns system is another way to achieve a two-column layout. This system also stacks columns on mobile and sits them side-by-side on tablet.

Alignment

Credit: youtube.com, How to Create a Responsive 2 Column Layout with CSS

Alignment is a crucial aspect of designing a two-column grid. You can use flexbox alignment utilities to vertically and horizontally align columns.

In fact, Internet Explorer 11 does not support vertical alignment of flex items when the flex container has a min-height. You can check out Flexbugs #3 for more details.

To vertically align all grid cells in a row, you can use the align-v prop on . This prop accepts values like 'start', 'center', 'end', 'baseline', and 'stretch'.

You can also use the align-self prop on for individual grid cell vertical alignment. This prop also accepts the same values as the align-v prop.

Here's a summary of the possible values for align-self and align-v props:

  • start – aligns the grid item to be flush with the start edge of the cell
  • end – aligns the grid item to be flush with the end edge of the cell
  • center – aligns the grid item in the center of the cell
  • baseline – aligns the grid item to the baseline of the cell
  • stretch – fills the whole height of the cell (this is the default)

The align-self and align-v props are powerful tools for aligning your grid cells. With these props, you can create a wide range of layouts and designs.

Grid Sizing and Units

When working with CSS Grid, you'll often find yourself using fractional units, like 1fr. These units essentially mean "portion of the remaining space", making them super flexible and friendly to combine with other units.

Credit: youtube.com, FR Units in CSS Grid — 2/3 Flexibility

Fractional units are great because they don't get rigid like percentage values do. For example, if you add padding to percentage-based columns, you'll break the 100% width. But with fractional units, you can add padding without worrying about the width.

Here are some key differences between fractional units and percentage values:

Fractional units are also super useful when combined with other units, making them a great choice for grid layouts.

Container Sizes

Container sizes can vary depending on the breakpoint and the type of container. There are five breakpoints to consider: extra small, small, medium, large, and extra large.

The default container maximum width at the extra small breakpoint is 100%. This means that the container will take up the full width of the screen.

At the small breakpoint and above, the default container maximum width is 540px. This is a good size for small screens and devices.

As we move to the medium breakpoint and above, the default container maximum width increases to 720px. This provides more space for content on medium-sized screens.

Credit: youtube.com, CSS GRID: Sizing Grid Items — 9 of 25

On large screens and above, the default container maximum width is 960px. This is a good size for larger screens and devices.

Finally, on extra large screens and above, the default container maximum width is 1140px. This provides ample space for content on the largest screens.

Here's a breakdown of the default container maximum widths at each breakpoint:

Fr Units

Fr units are a type of unit in CSS Grid that represent a portion of the remaining space.

You can use a lot of fractional units in CSS Grid, like 1fr. They essentially mean "portion of the remaining space".

Fractional units are much more friendly in combination with other units.

They don't break the 100% width like percentage values do, assuming a content-box box model.

Sizing Functions

Sizing Functions are a powerful tool in Grid Layouts. They help us control how much space our grid items take up.

The fit-content() function is a great example of this. It uses the space available, but never less than min-content and never more than max-content.

Credit: youtube.com, Min & Max Content Sizing in CSS Grid — 1/3 Flexibility

This is useful when you want a column to be able to shrink, but not disappear. For instance, if you have a grid with a lot of empty columns, you can use fit-content() to make sure they take up the minimum amount of space.

The minmax() function is similar, but it sets a minimum and maximum value for what the length is able to be. This is useful for setting a minimum size for a grid item, while still allowing it to expand to fill the available space.

Here are some key differences between auto-fill and auto-fit:

Grid Utilities

Bootstrap's grid system is incredibly powerful, and it's made even more flexible with the help of grid utilities.

You can use these utilities to show or hide content across specific viewports, making it easier to create responsive designs.

For example, you can use Bootstrap's display utilities to responsively toggle common values of the display property.

This is especially useful when you need to show or hide content based on the screen size.

Mixing grid utilities with the grid system or content allows for a lot of creative freedom.

Grid Media Queries

Credit: youtube.com, How To Build 2 Column Responsive CSS Layout? | CSS Grid + Media Queries

Grid media queries are a crucial part of making a 2-column grid responsive.

To make the grid responsive, we'll use media queries to tell the grid to have a specific layout at certain device widths.

We want both main and sidebar elements to be 100% width for mobile.

For tablet devices, we'll float the columns to the left and make them 50% width.

On desktop, we'll change the widths of the main and sidebar content to 2/3 and 1/3.

Grid Browser Support

Grid Browser Support is crucial for a seamless user experience. A number indicates that browser supports the feature at that version and up.

Chrome supports CSS Grid from version 57. Firefox supports it from version 52. Safari supports it from version 10.1.

Project Planning and Implementation

Planning out your grid is a crucial step in building a 2-column layout. You need to decide how it will look responsively, meaning how it will change when viewed on different devices.

Credit: youtube.com, Learn CSS Grid - A 13 Minute Deep Dive

First, identify the breakpoints at which the layout will change. For a basic two-column grid, you might have breakpoints for mobile, tablet, and desktop. Write down how the design will change at each breakpoint.

For example, on mobile, you might want the columns to stack with the main content on top and the sidebar underneath, both taking up 100% width. On tablet, you might want the columns to be side by side with each taking up 50% width.

Here's a rough guide to help you plan your grid:

Having a clear plan will save you time and effort in the long run.

Sources

  1. 2 Column Layouts (Responsive, Flexbox & CSS Grid) (matthewjamestaylor.com)
  2. browsers (caniuse.com)
  3. codepen.io/mindplay-dk/pen/oNXmwej (codepen.io)
  4. codepen.io/mercmobily/pen/KKddYNJ (codepen.io)
  5. row vs column (wlearnsmart.com)
  6. grid design (tutsplus.com)
  7. Bootstrap (getbootstrap.com)
  8. Zurb Foundation (zurb.com)
  9. media queries (mozilla.org)
  10. Float Grid (codepen.io)
  11. Mozilla (mozilla.org)
  12. Flex Grid (codepen.io)
  13. Basic concepts of grid layout (mozilla.org)
  14. Grid by Example (gridbyexample.com)
  15. flexbox (mozilla.org)
  16. bugs around flexbox (github.com)
  17. responsive display utilities (getbootstrap.com)
  18. Safari flexbox bug (github.com)
  19. level 2 of the CSS Grid specification. (w3.org)
  20. repeat() notation (digitalocean.com)
  21. this article by DigitalOcean (digitalocean.com)
  22. needs (meyerweb.com)
  23. Caniuse (caniuse.com)
  24. CSS Grid Layout: Introduction (digitalocean.com)
  25. Learning CSS Grid Layout (rachelandrew.co.uk)
  26. Grid by Example (gridbyexample.com)
  27. CSS Grid Layout Module Level 1 (w3.org)

Ann Predovic

Lead Writer

Ann Predovic is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for research, she has established herself as a go-to expert in various fields, including technology and software. Her writing career has taken her down a path of exploring complex topics, making them accessible to a broad audience.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.