Utilizing Bootstrap grid system to create varied column widths for content alignment

Looking to create a page with multiple entries that include text descriptions and associated icons of varying sizes, aligned neatly. (For simplicity, using letters "i" and "w" as placeholders for icons.)

For wider screens, the setup should feature a grid layout with a spacious left column and icons to the right, occupying minimal horizontal space. Check out this example with two "icons."

.container {
    display: grid;
    grid-template-columns: 1fr repeat(2, max-content);
}
.content {
    justify-self: center;
}
<div class="container">
    <div class="header">Some text</div>
    <div class="content">i</div>
    <div class="content">i</div>
    <div class="header">Some more text</div>
    <div class="content">w</div>
    <div class="content">w</div>
</div>

For smaller viewports, where the left column might shrink below 200px, the layout should responsively switch to a stacked display, like in this example.

.container {
    display: grid;
    grid-template-columns: 1fr repeat(2, max-content) 1fr;
}
.container > div {
    justify-self: center;
}
.header {
    grid-column-start: 1;
    grid-column-end: 5;
}
.content1 {
    grid-column-start: 2;
}
.content2 {
    grid-column-start: 3;
}
<div class="container">
    <div class="header">Some text</div>
    <div class="content1">i</div>
    <div class="content2">i</div>
    <div class="header">Some more text</div>
    <div class="content1">w</div>
    <div class="content2">w</div>
</div>

While this method is effective, there are areas I'm looking to enhance:

  • Considering my site relies on Bootstrap, utilizing their "row" and "col" features feels more appropriate than designing a grid from scratch. However, I struggle to implement this with Bootstrap, as discussed in this query.
  • The current setup necessitates a media query and employs two distinct layouts based on screen space, which seems overly complex. Is there a way to optimize grid responsiveness, allowing icons to flow beneath text automatically when the viewport shrinks? Exploring options like auto-fill could help, although adapting columns of differing sizes presents a challenge.
  • In the layout for smaller screens, the use of classes like content1, content2, leads to repetitive CSS rules for column placement. This issue would escalate with more icons; seeking a solution that minimizes redundancy is essential.

Answer №1

The display classes are responsive. This means that you have the flexibility to apply d-flex d-md-grid to the container. However, when it changes to display:flex, the grid-template-columns will not be applied.

.container {
    grid-template-columns: 1fr repeat(2, max-content);
}
.content {
    justify-self: center;
}

<div class="container d-flex d-md-grid flex-wrap align-items-center justify-content-center text-md-start text-center">
    <div class="header w-100">Some text</div>
    <div class="content">i</div>
    <div class="content">i</div>
    <div class="header w-100">Some more text</div>
    <div class="content">w</div>
    <div class="content">w</div>
</div>

Check out the demo on Codeply

You also have the option to use d-sm-grid, d-lg-grid, or d-xl-grid instead of d-md-grid to define the breakpoint according to your needs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to declare a function within the events onMouseOver and onMouseOut

<div align="right" style="border:1 #FF0 solid; background-color:#999" onMouseOver="javascript: function(){this.style.backgroundColor = '#DDD';}" onMouseOut="javascript: function(){this.style.backgroundColor = '#999';}"> Register & ...

Leveraging django model information within a bootstrap layout

Struggling with integrating Django model data into a Bootstrap 5 collapse function for an expanding list. After taking a long coding break, I'm finding it challenging to make the collapse function work with the data from Django. It works when using a ...

What is the best way to incorporate these elements into my Bootstrap 5 navbar design?

This is the current layout of my navbar, with the logo in the center. I now want to enhance it by adding a search bar and some icons similar to this example: ideal navbar Here is my current navbar for reference: My current navbar <header> <n ...

"Excessive overflow results in the displacement of the following block

When viewing on smaller screens, the blocks move down due to oversized images causing overflow. Is there a way to simulate collision in this scenario? <div class="image"> <img src="img/1.jpg" /> </div> <div class="image"> & ...

Tips on positioning the button right next to the bootstrap container

I'm using Bootstrap and facing an issue with aligning a button next to the container in the center, as shown in the image. I know that placing the button inside the container can partially solve this problem, but I prefer not to do so because it will ...

Setting dynamic image dimensions within a div element

Is there a way to display the div width in percentage even when the image is not loaded? The image sizes are mentioned in percentage and they load dynamically, making it impossible to fix their size. How can we ensure that the div maintains the correct wid ...

Having trouble getting the show/hide div feature to work in a fixed position on the

Trying to show/hide a div using jQuery isn't working when the div is wrapped in a position:fixed element. However, changing it to position:relative makes the show/hide function work again. You can see an example of the working version with position:r ...

Experience the smooth CSS3 transition effects on iOS devices such as iPhones and iPads. Elevate the appearance of DOM objects with

I have a basic image contained within a div tag. I use JavaScript to add a transition effect to the div element: <div style="transition: opacity 0.8s linear; opacity: 0.5;"><img src="..." /></div> At the end of the transition duration ...

Change HTML to PDF with the ability to split text at page breaks

I recently attempted to convert an HTML/CSS document into a PDF file using the pdflayer.com API. Everything seemed to be working well, but I encountered a problem with line breaks splitting lines, as shown in this photo: https://i.sstatic.net/1tqKM.jpg I ...

What is the best way to add prefixes to my SCSS style sheets?

After attempting to add prefixes to my scss files, I came across the autoprefixer tool. However, I discovered that it only supports CSS files. Is there a way to utilize autoprefixer with scss files? Here are the commands for Autoprefixer: npm install post ...

Collapsed Grid System

Is it possible to create a CSS grid system that meets the following requirements: The grid should have arbitrary height (with consistent width) and when a grid overflows, it should stack vertically underneath the preceding grid, regardless of other grid ...

What could be causing the video to extend beyond the limits of the container?

When extending the result window, the video ends up overlapping the section below it. I am looking to keep the video within the confines of the section's height, which is set to height:100vh. Is there a way to accomplish this? Check out this jsFiddl ...

What is the best method to align these images in the center of this div?

Struggling to center the images inside the div...any suggestions? I know it's basic, just getting back into it. Hopefully someone understands what I'm trying to achieve here? Tryin' to get those images centered in the div but can't fig ...

Showcasing the values of JavaScript

How can I resolve the issue depicted in the second picture? I need the value of 3 only in the USD column, with all others having a value of zero. <div class="span3"> <ul class="nav nav-tabs nav-stacked" > <?php foreach ( ...

What is the best way to add rounded corners to tables in Bootstrap 3?

Is there a way to give the top and bottom corners of a table rounded edges? I am currently using Bootstrap 3 tables, but they have no corner radius by default. How can I achieve this effect? ...

Can Javascript be used to identify the number of td elements in a table and specify column widths within the table tags?

Is there a way to automatically add the "col width" tag based on the number of td tags within a Table tag? For example, if there are 2 td's, then it should add 2 "col width", and if there are 3 then, 3 "col width", and so on. <!DOCTYPE html> &l ...

The image shape at the bottom of the screen may disappear on certain resolutions

I've added a unique curvy shape at the bottom of the image on a bootstrap card I designed using bootstrap 5. Everything looks great on resolutions above 1400px, but on smaller screens, the shape disappears, leaving the image rectangular. I've tri ...

Creating a Jira-inspired table layout in Angular by embedding components in a structured format

I'm attempting to recreate the functionality of Jira where I can create a "gadget" that can be added to a board, resized, moved, and deleted. (For those unfamiliar with why this might be useful, think of gadgets like the "Gantt" gadget. You can add a ...

Using a JSON file as a database for a project featuring HTML, CSS, and Vanilla JavaScript

Our task was to create a project that exclusively utilized JSON files for data storage. The data structure we were working with resembles the following: [ { "aircraftName": "Boeing 747", "departDate": 1640173020000, ...

Ways to incorporate CSS padding into a website displayed in a webView

I have been trying to figure out a way to apply padding-bottom (Custom CSS) to a webpage fetched from RemoteConfig (URL provided). Unfortunately, I do not have direct access to the webpage's source files for editing. Below is the code snippet in ques ...