Replicate the Bootstrap flexbox grid system with customized media breakpoints

I'm trying to replicate the flexbox grid system found in Bootstrap. Everything is working smoothly except for the media query aspect. The columns are stacking horizontally as intended, but when a specific breakpoint is reached, I aim to have the divs stack on a new line.

I considered using jQuery, but I want to first attempt achieving this with pure css. I attempted to analyze the source files of Bootstrap, but found it difficult to grasp.

.row {
  overflow: hidden;
  display: flex;
  width: 100%;
  background-color: burlywood;
  height: auto; 
}

.row > .col {
  padding: 14px;
  border: 2px solid black;
  flex: 1 1 auto; 
}

.col-md-4 {
  padding: 14px;
  border: 2px solid black;
  flex: 1 1 33.33333%; 
  
  @media (max-width: 768px) {
    .col-md-4 {
      //desired code here
  }
}
<div class="row">
    <div class="col-md-4" style="background-color:burlywood;">MyColumn 1</div>
    <div class="col-md-4" style="background-color:chartreuse;">MyColumn 2</div>
    <div class="col" style="background-color:crimson;">MyColumn 3</div>
    <div class="col" style="background-color:crimson;">MyColumn 4</div>
</div>

The inline styling is just for testing purposes.

Answer №1

The media query declaration was originally nested inside another CSS rule.

To resolve this issue, you can simply adjust the stacking direction of the .row element itself.

Note:

If you are just experimenting and learning, it's fine to continue. However, if you plan to use this code in a production environment, I would suggest extracting the grid definitions directly from Bootstrap rather than trying to create them yourself.

Keep in mind that Bootstrap follows a mobile-first approach. The original example provided does not. By incorporating flex-wrap and using a wildcard for the box-sizing property below, you will achieve most of the desired results.

*{box-sizing:border-box;} /* Ensure borders do not affect layout */
.row {
  display:flex;
  width:100%;
  background-color:burlywood;
  height:auto;
  flex-wrap:wrap; /* Allows columns to wrap on smaller devices */
}

.row>.col {
  padding:14px;
  flex:1 1 auto;
}

.col-md-4 {
  padding:14px 0;
  border:2px solid black;
  width:100%;
}

@media(min-width:768px) {/* Media query for larger screens */}
.col-md-4 {
  -webkit-box-flex:0;
  -ms-flex:0 0 33.333333%;
  flex:0 0 33.333333%;
  max-width:33.333333%;
}
<div class="row">
  <div class="col-md-4" style="background-color:burlywood;">MyColumn 1</div>
  <div class="col-md-4" style="background-color:chartreuse;">MyColumn 2</div>
  <div class="col-md-4" style="background-color:crimson;">MyColumn 3</div>
</div>

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 select the element based on a specific CSS property value in the inline style

As a novice in the world of Javascript, I am currently attempting to select an element within an array of images where the opacity is set to 1. Could someone please guide me on how to achieve this? I've tried using hasAttribute, but I'm unsure ho ...

Tips for accessing information from a FOR loop within DIV tags

Let's explore the following code snippet: Here is the HTML generated: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Website ...

The only functioning href link is the last one

Currently, I am in the process of constructing a website using HTML and CSS. My goal is to create a white rectangle that contains 4 images, each serving as a clickable link redirecting users to different sections on the page. The issue I am facing is that ...

Expandable Table displaying all sections upon clicking with Shopify linklists

My current setup includes a sidebar menu that iterates through Shopify linklists to display the parent and child collection titles and URLs. However, I am encountering an issue where clicking on one of the anchor tags opens all anchor links due to non-uniq ...

Having trouble aligning the form in the center?

Despite #container being centered, I am struggling to center the <form>. It is important for me to have all elements inside the form aligned horizontally and despite trying various techniques, it is not working as expected. <!-- Container Start - ...

PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery. I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery ...

What's causing this element to overlap the previous one (apologies, once more)?

This is the code: <form id="the_form" action="">ev </div> <!-- final pay_block --> <div class="linea"/> <div class="spacer"/> The "linea" element is currently overlapping the buy button because a margin-bottom of 15px has be ...

The CSS tag combination of 'position:relative;' and 'top:xx%;;' functions properly in Chrome, however, it does not work in any non-webkit browsers such as Firefox, Internet Explorer, Opera, etc

Encountering some strange behavior with a simple piece of code that I can't seem to figure out! I am trying to position a footer navigation at the bottom of my page using 'position:relative;' and it works perfectly in Chrome. However, in FF ...

Tips for effectively utilizing CSS classes with current-device

Attempting to lock the screen orientation in portrait mode for mobile and iPad devices using a CSS snippet found on css-tricks.com. @media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) { html { transform: rotate ...

When attempting to click on the dropdown in Bootstrap, there is no content displayed

I am currently practicing Bootstrap and focusing on implementing Dropdowns. However, I am facing an issue where upon clicking the Dropdown button, nothing appears on the screen. Preview when it's not clicked Preview when it's clicked Here is m ...

When hovering the mouse over, the text fails to appear

Is there something wrong with my overlay on this image? It darkens when I mouse over, but the text I want to display does not show up. Any ideas? http://jsfiddle.net/cryycw3n/ HTML <div class="square"> <div class="info"> <h2&g ...

Bottom-anchored horizontal navigation bar for seamless scrolling experience

Is there a way to create a navbar with horizontal scrolling at the bottom of the page without compromising its scrollability? When I use position: fixed;, the navbar becomes non-scrollable. div.scrollmenu { background-color: #333; overflow: auto; ...

Clicking on a flex card will trigger the opening of a new page where the parsed data will be displayed in a stylish manner using JavaScript

Currently, I am attempting to showcase the data retrieved from the following URL: . My objective is to format the parsed data with a specific style. However, I have encountered difficulties accomplishing this using `window.open()`. Any assistance in resolv ...

Alignment in HTML / CSS / ReactJS: Leveraging the Text-align attribute

My attempt to align the text to the right within my div element has been unsuccessful. Despite using text-align: right and width: 100%, the issue persists. It seems that the problem lies within the left-side and right-side attributes, but I am unable to pi ...

How to toggle classes on anchor tags using jQuery for Twitter Bootstrap buttons?

I built a single-page website. The navigation is made up of buttons styled with bootstrap 3. Since it's a single page, the buttons are anchor tags. I'm trying to figure out how to apply an .active class to the last clicked anchor tag. Here is t ...

Enhancing the appearance of your website by incorporating external stylesheets and utilizing multiple class names through

Is it possible to use external CSS frameworks like Bootstrap, Semantic UI, or Foundation and still generate base64 class names? This could be achieved if there was a way to apply multiple class names. Currently, you can only assign one class name like thi ...

The positioning of the Zorro table column remains flexible despite the presence of nzLeft

I am currently developing a project using Angular. Within one of my components, I have a table that displays some data. This table is generated by the ng-zorro table module. However, I've encountered an issue where setting a table column or header wi ...

Is it possible for the scroll event to be triggered while scrolling only when a div element is used?

While utilizing window.onscroll to track scroll events during scrolling, I noticed that in certain Android devices the scroll event is only triggered after the scroll has completed. However, when monitoring scroll events within a specific div element, it ...

Turn off Appbar padding at the top and bottom

I am looking to create a customized box within the Appbar that spans the full height, as illustrated in the image below: https://i.stack.imgur.com/CFMo0.jpg However, the default Appbar provides padding from all sides to its internal elements, leading to ...

Utilizing Jquery to create a carousel with looping functionality for flowing text

I am currently facing an issue with a carousel that contains multiple images and text. In order to make the text responsive, I have implemented a script called FlowText. The script works perfectly on the initial carousel image (the active one), but as soon ...