Splitting the screen into four sections using Bootstrap 4 Flexbox

Is there a way to split my screen into four sections with equal width and height using bootstrap 4 and flexbox?

Each section should occupy 50% of the screen's width and height.

Additionally, I need these sections to be responsive in terms of their content.

I have attempted the following code without achieving the desired result:

<div id="app" class="flex-container">
    <div class="flex-item" style="background-color:red"></div>
    <div class="flex-item" style="background-color:blue"></div>
    <div class="flex-item" style="background-color:yellow"></div>
    <div class="flex-item" style="background-color:grey"></div>
</div>


.flex-container {
    display: flex;
    flex-wrap: wrap;
}

.flex-item {
    flex-basis: 50%;
    flex-grow: 1;
    flex-shrink: 1;
}

Answer №1

To achieve a responsive layout, utilize min-width and height properties set to 50% within a flex container, while also ensuring the container takes up the full viewport height.

.flex-container {
  display: flex;
  flex-wrap: wrap;
  min-height: 100vh;
}

.flex-item {
  min-width: 50%;
  min-height: 50%;
}
<div id="app" class="flex-container">
  <div class="flex-item" style="background-color:red"></div>
  <div class="flex-item" style="background-color:blue"></div>
  <div class="flex-item" style="background-color:yellow"></div>
  <div class="flex-item" style="background-color:grey"></div>
</div>

Answer №2

Bootstrap 4 comes equipped with all the necessary flexbox utilities. It's important to ensure that the container spans the full height.

<div id="app" class="d-flex mvh-100 flex-wrap">
    <div class="w-50" style="background-color:red"></div>
    <div class="w-50" style="background-color:blue"></div>
    <div class="w-50" style="background-color:yellow"></div>
    <div class="w-50" style="background-color:grey"></div>
</div>

Check out the demo:

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

Please submit information that is not already filled out in the text fields

I am looking to create a table with text fields and buttons as shown below: <table border="1"> <tr> <td>name</td> <td>id</td> <td>icon</td> <td> </td> & ...

Issues with the styling of the side navigation bar

How can I center the paragraph content within the .main-content div located to the right of the side navigation? When I apply margin: 0 auto, nothing happens. I believe my CSS is incorrect, but I'm unsure how to correct it. Additionally, I would like ...

Maintaining the aspect ratio of images in Bootstrap Carousel: A complete guide

Using the default carousel from Bootstrap template has been working well for me. However, I've encountered an issue where resizing the browser window distorts the image aspect ratio by squishing it horizontally. I've tried various solutions to m ...

Clicking on an element in the Dropdown Form will directly redirect you to a link, without the need to press a

I'm having trouble with my dropdown form. I want the user to be directed to a specific URL when they click on an element, without needing to hit a submit button. Below is the code I've been working with: <select name="mydropdown" class="style ...

Attempting to create a functional action listener for a deck of cards game

I'm currently working on a game and want to make an image appear blank when clicked on, to simulate it disappearing. Specifically, this is for a tri peaks solitaire game. I have a function that tests the validity of playing a card, but I'm strugg ...

Problems with Displaying Content in HTML

Having trouble with displaying the apostrophe within my code: if (type == 'Survivor') { display = isProxy ? (finishedRegistration ? '' : 'Unfinished ') + (name.substr(name.length - 1) == 's' ? name + '&rsqu ...

The correct reading of JavaScript in HTML is a common source of confusion

After creating a basic application using the code provided in a forum thread and testing it on the worker sandbox MTurk site, I noticed a peculiar issue. The application runs smoothly when following the code from the forum answer directly. However, when at ...

Unable to establish connection with server through Ajax request on HTML page

I set up a NodeJs server using the Express module. However, I am facing issues with implementing an AJAX request from an HTML page using $.ajax when clicking a button. I want to retrieve data from the server in either JSON or text format but for some reaso ...

Having trouble adjusting the space between the footer and the bottom of the page

Looking to create a footer using multiple elements The first element should be positioned at: left: 944px; top: 9749px; The second element should be positioned at: left: 715px; top: 9819px; The third element should be positioned at: left: 718px; top: ...

Using custom CSS with Bootstrap 4 and Rails 5: A simple guide

Currently I am working with Bootstrap 4 and Rails 5, and have chosen to use render :layout => false in order to exclude my main app layout from the login page. The login page is styled with Bootstrap, and I have incorporated some custom CSS from a Boots ...

Exploring the Contents of an ASP ListView

I am attempting to align the odd-numbered items to the left using float:left, and the even-numbered items to the right using float:right. However, it seems like every item is being considered as odd AND element 1 regardless of the actual order in the list. ...

Tips to position two shortcode elements next to each other horizontally

I have some code here and I am looking to align the two elements at the bottom, "php echo do_shortcode(wcas-search-form);" and "php do_action('hamzahshop_custom_min_cart');" on the same line horizontally. I am new to this and struggling to figure ...

Is the div empty? Maybe jQuery knows the answer

I currently have a <div id="slideshow"> element on my website. This div is fully populated in the index.php file, but empty in all other pages (since it's a Joomla module). When the div is full, everything works fine. However, when it's emp ...

The header location functionality is not functioning properly on the live server

I have a single registration and payment page where the program flow goes from registration to confirmation and then to payment. Upon validation and calculation on the registration page, it should redirect to the confirmation page. This is my code: < ...

Create a sinusoidal wave and stream it through the web browser

I'm looking for a code snippet that can: create a sine wave (an array of samples) play the wave This should all be accomplished in a web browser using an HTML5 API in JavaScript. (I've tagged this with web-audio, but I'm not entirely ...

Using `vertical-align` on a `span` inside a flexbox container may not produce the desired result

Some users are experiencing issues with the vertical-align: middle property not working on a span. .tc__timezone-toggle { display: flex; } .tc__timezone-toggle span { vertical-align: middle; } .tc__timezone-toggle-ui { display: block; font-wei ...

What is the best way to dynamically shift the position of an image on a page while keeping it in place in real-time?

A group of friends and I are currently collaborating on an experimental project for a presentation. Our goal is to have multiple elements (such as images, text, gifs) displayed on a page that can be dragged around in real-time using a draggable script whi ...

Three fixed position divs arranged horizontally side by side

I am attempting to organize 3 divs in a row using Flex. ISSUE 1: The div that is centered is set with position: fixed. However, the two other divs on each side do not stay aligned with the centered fixed div when scrolling. If I change the centered div to ...

Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images. First screenshot below shows that it works ...

If the value of the input matches, set the checkbox to be

I have successfully implemented functionality that allows the value of an input to be changed by clicking a checkbox. This works without any issues. Now, I am facing the challenge of automatically checking the checkbox when the page loads, but only if the ...