Building a custom 2-column layout with Flexbox for a specific screen size: What you need to know

I'm currently working on using Flex to adjust the layout of my grid when it reaches a specific breakpoint. I've included a visual example of what I want it to look like at 900px and above.

However, I'm struggling to get the columns to align properly and stack beneath each other.

Below is the code snippet and a link to a fiddle for reference: http://jsfiddle.net/3wk9yepj/

https://i.sstatic.net/XH8Z0.png

.wrapper {
  display: flex;
  flex-wrap: wrap;
  margin: 32px 0;

  @media (min-width: 600px) {
    flex-wrap: nowrap;
  }
  
  @media (min-width: 900px) {
    flex-wrap: wrap;
    // Here's where the new layout from the attached screenshot should be implemented
  }
}

.box-1 {
  background-color: red;
  color: white;
  padding: 40px 32px;
  width: 100%;
}

.box-2 {
  width: 50%;
}

.box-3 {
  width: 50%;
  background: grey;
}

img {
  width: 100%;
  /* object-fit: contain; */
  height: 100%;
  transition: 0.3s;
  opacity: 0.9;
}
<div class="wrapper">
  <div class="box-1">
    <h4>
      Title goes here
    </h4>
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
    </p>
  </div>

  <div class="box-2">
    <img src="https://www.audi.com/content/dam/gbp2/models/1920x1080-desktop-models-teaser-A211437.jpg?imwidth=1920&imdensity=1" />
  </div>

  <div class="box-3">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.
    </p>
  </div>
</div>

Answer №1

When working within a grid layout, you have the ability to define the number of columns using the grid-template-columns property.

In the example below, I've utilized this property to create a layout with two columns. The first column takes up 60% of the width of the .wrapper element, while the second column occupies 40%. If you desire equal width columns, you can use grid-template-columns: 1fr 1fr;.

grid-template-columns: 60% 40%;

To make .box-1 span across two rows in terms of height, you can employ the grid-row: span 2 property.

To ensure equal heights for all rows, you can apply grid-auto-rows: 1fr;.

If you wish to allow .box-1 to wrap based on screen size, a media query like the one below can be used:

@media screen and (max-width: 400px) {
  .wrapper {
    grid-template-columns: 1fr;
  }
}

...Snippet code here...

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

PHP Solution: I'm working on a page that is accessed using the post method. However, I need to defer the execution of certain code until after the page has finished

Apologies for the confusing title, but I'm struggling to succinctly explain my issue: I have a web page that is accessed through a button. When this button is clicked, specific data (a unique identification name) is sent to the page I want to view. Ho ...

What is the process for generating a GET request for selected checkboxes and subsequently showcasing the data on an HTML webpage?

Currently working on an app project and need assistance with implementing a feature. I have successfully set up a POST method for the checkboxes that are checked, but I am unsure how to retrieve this data and display it on my HTML page using a GET method ...

Every time an input field is clicked, an email is sent

I'm struggling with the contact form on my website. Instead of sending an email after clicking on each input field, I want it to send only one email after hitting the submit button. Currently, it sends a total of 5 emails - 1 with user inputs and 4 wi ...

Tips for increasing the size of a textarea

I'm attempting to extend a textarea by adjusting the margin-top property, but it doesn't seem to be working as intended. Here is the code snippet in question: #sqlcontainerLoggedInPage2 { margin-top: 60px; } <div class="container-fluid" i ...

Understanding the float property in CSS

Check out this CSS example: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Jenware | Personalized Gifts</title> <style type="text ...

jquery function that switches image after the fifth mouseover event

I just can't seem to figure this out. I need a mouseover event that switches between two images until the fifth time you mouseover, then it changes to a third image. Here is my code so far: <!doctype html> <html> <head> <meta c ...

Securing containers with CSS styling

I'm currently working on a website and I've encountered an issue with positioning. Within the content section, I have two main text boxes - one in the top left corner and one in the bottom right corner. However, when the browser window is resized ...

What is the process for extracting the differences between two or three files and merging them together in Node.js?

Recently, I've been experimenting with webpack and successfully managed to output and transform es5 build to include nomodule in the tags directly from HtmlWebpackPlugin using hooks. Similarly, for the es6 build, I added type="module". Howe ...

An hour ago, the Python and Selenium code was functional, but now it has suddenly ceased to work

Hello to all fellow community members, I recently attempted to locate the next button in Google search, specifically on the bottom right corner. Here is the HTML code snippet that pertains to this: <td aria-level="3" class="d6cvqb" ...

Problem with the menu button overflowing

I'm exploring the process of designing burger menus for the mobile version of my site. I've successfully created a basic burger menu with some JavaScript assistance, but I encountered an overflow issue. The burger menu functions properly, but the ...

The issue with the left border color not functioning in JavaScript

Attempting to alter the border-left-color property using this script is resulting in a Uncaught SyntaxError: Unexpected token -. Is border-left-color actually supported? Javascript function logoChange() { var description = new Array (); description[0] ...

extracting data from checkbox to use in an angular function upon being selected

Currently, I am creating a list of checkboxes with distinct string values: heading, paragraph, list, table, visualtitle. I believe the current approach using (change)="onChange('heading', $event.target.checked) may not be the best way to han ...

scribbling at the heart of the page

I have been struggling to get this text centered using the following code: echo "<div class='pagination' align ='center'>"; Unfortunately, the text still appears on the left side. How can I properly center it? Additionally, I h ...

The new FormData(form) method unexpectedly returns an empty object

In this scenario, I am aiming to retrieve key-value pairs. The form on my page looks like this: <form id="myForm" name="myForm"> <label for="username">Enter name:</label> <input type="text" id="username" name="username"> ...

The Bootstrap drop-down feature refuses to open

I seem to be having trouble with a basic issue. I used the standard template from the bootstrap website, but for some reason, when I click on the dropdown menu in the navbar, it's not opening. Can someone help me figure out what's going wrong? & ...

What is the best way to divide widgets in a Wordpress Sidebar?

I am looking to customize the spacing between widgets in my Wordpress sidebar, similar to what is shown on this example site. Currently, my sidebar does not have the desired spacing. I have tried various CSS codes found online, but none of them seem to be ...

Jsoup's :has() selector is giving unexpected results

Attempting to extract specific information from an HTML table that contains various cells structured like this: <td id="topic1234"> <a name='1234'></a> <b><a href='/url'>Title</a><b> ...

Using jQuery to make an Ajax post request that will load the current page with additional form data appended to the

Below is the HTML code for my registration form: <form id="registerForm"> <input type="text" pattern="[A-Za-z]{1,20}" placeholder="First Name" name="guestFName" title="Up to 20 alphabetical characters" required> <inp ...

Unable to execute specific php function using ajax

I have created a script that utilizes an ajax request. This script is triggered when a user clicks a button on the index.php page, like so: Risorse.php <form method='post'> Name <input type='text' name='nome&apo ...

Locate a DOM element by its attribute identifier

Is there a way to use jQuery to find a DOM element based on the name of its attribute, rather than the attribute value? For instance: <div id="div1"> <div id="div2" myattr="myvalue"> </div> </div> I want to locate all element ...