For Flexbox front end design, the left side should be contained within a container while the right side should seamlessly stretch across the

https://i.sstatic.net/3RYY6.png

I'm facing a challenge in designing a website with a unique layout that has left me puzzled.

I've created a codepen example to showcase what I'm trying to achieve. Specifically, I need to implement a Hero Banner on top of a standard 1200px container. The layout requires a 400px wide container on the left side that aligns parallel to another 400px container below it. The red container should line up with the purple container below, while the right side needs to extend to the end.

The difficulty lies in making this design responsive. Despite trying various flex options, I have yet to find a solution.

Is there a way to accomplish this using only CSS and HTML without relying on JavaScript?

You can view my code pen here: https://codepen.io/hundredbillion/pen/jOPRdgr

Here is my code snippet:


    <div class="text-image-banner">
      <div class="text-flex">
        <div class="text-content">
          <h5>This needs to be 400px wide and the right and left edges must match the right and left edges of the orange bar below for all screen widths</h5>
        </div>
      </div>
      <div class="img-flex">
        <div class="img">
          <p>This needs to stretch all the way to the right, but the left edge of the red bar must match the left edge of the purple bar for all screen widths. An image will go here</p>
        </div>
      </div>  
    </div>
    <div class="container">
      <p class="center">this is a 1200px container</p>
      <div class="flex">
         <p class="four">text container</p>
        <p class="eight">img container</p>
      </div>

    </div>

And here is my CSS code:


    .text-image-banner {
      display: flex;
      margin-left:auto;
      margin-right:auto;
    }

    .text-flex {
      background: blue;
      display:flex;
      justify-content:flex-end;
      flex: 1;
    }
    .text-content{
      background: yellow;
      display:flex;
      width:400px;
    }
    .img-flex{
      background: red;
      flex-basis: 1030px;
    }


    /*---- this is a plain vanilla hero banner ----*/
    .container{
      max-width:1200px;
      margin-left:auto;
      margin-right:auto;
      background:pink;

    }
    .four{
      background:orange;
      text-align:center;
      width:400px;
      height:200px;
    }
    .eight{
      background:purple;
      text-align:center;
      color:white;
      width:800px;
    }
    .flex{
      display:flex;
    }
    .center{
      text-align:center;
    }

Answer №1

After some experimentation, I discovered that utilizing css grid-template-areas can yield the desired outcome. Here's a potential solution that may assist others facing the same issue.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Custom Title</title>
    <style>
      *{
        overflow: hidden;
      }

      .container {
        display: grid;
        grid-template-columns: auto 400px 800px auto;
        grid-template-areas: ". header-text header-image header-image"
                            ". body-text body-image. ";
        background-color: #2B3D41;
      }
      .header-text {
        grid-area: header-text;
        background-color: #F9B9F2;
      }
      .header-image {
        grid-area: header-image;
        background-color: #BCA0BC;
      }
      .body-text {
        grid-area: body-text;
        background-color: #4C5F6B;
      }
      .body-image {
        grid-area: body-image;
        background-color: #83A0A0;
      }
      @media screen and (max-width: 1200px) {
        .container {
          grid-template-columns: 33.3% 66.6%;
          grid-template-areas: "header-text header-image"
                              "body-text body-image";
        }
      }
      @media screen and (max-width: 768px) {
        .container {
          display: block;
        }
      }

    </style>
  </head>
  <body>
    <div class="container">
      <div class="header-text">
        <h5>This section should be 400px wide with matching edges to the orange div below, regardless of screen size.</h5>
      </div>
      <div class="header-image">
        <p>This section should expand fully. An image will be inserted here.</p>
      </div>

      <p class="body-text">text container</p>
      <p class="body-image">img container</p>

    </div>

  </body>
</html>

Answer №2

CSS grid is recommended over pure flex for this scenario. Here's an example:

.wrapper {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 400px 1fr;
  grid-template-rows: auto;
  background-color: #fff;
  color: #444;
}

.eight {
  background: purple;
  text-align: center;
  color: white;
  width: 800px;
}

.footer {
  grid-column: span 2;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 10px;
  font-size: 150%;
}

.header,
.footer {
  background-color: #999;
}
<div class="wrapper">
  <div class="box sidebar">left</div>
  <div class="box sidebar">right</div>
  <div class="text-container">Content
    <br /> Updated content goes here.</div>
  <div class="eight">img container</div>
  <div class="box footer">Full width footer</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

When inline elements are positioned right next to block elements

Can inline and block elements be placed on the same level without wrapping in HTML? Does it matter if they are wrapped or not? For example: <div class="name"> <span class="name__text">List name</span> </div> <div class="li ...

A step-by-step guide on integrating a Django template tag and HTML element into an HTML page using Javascript

Is there a safe way to insert HTML that includes Django template tags using JavaScript in my project? For instance, if my template contains the following code: <div id="some-element"> <span id="my-tag">{{ my_data }}</sp ...

Discovering unmarked content using Jsoup

I have been struggling to find a solution to my problem, but so far I have not found any clear answers in my search results. I have spent a considerable amount of time trying different suggestions from various posts that seemed somewhat related. Now, let&a ...

Display and conceal elements within predetermined time intervals using jQuery, while ensuring that the final element remains visible

Is there a way to automatically hide div1 and show div2 after a set amount of time, let's say 10 seconds or 15 seconds? I came across this thread: Show and hide divs at a specific time interval using jQuery, However, the solution provided in the po ...

The dimensions of the HTML table have expanded excessively following the execution of mysql_fetch_assoc

I have encountered an issue where, after inserting the database values into an HTML table, the table becomes too large. I've tried using CSS code to adjust it, but it doesn't help. Should I consider changing something in the database value type? ...

Ending Current Tab using angularJS Controller

Is there a way to automatically close the current browser tab after a successful action in AngularJS? ...

Scroll down 1 page using Javascript and Jquery

Hey there! I'm looking to create a website where a small scroll on the mouse wheel will take the site to the next anchor. It's kind of hard to explain, but one good example is the Tesla Model 3 website on the US site here. Does anyone have any t ...

The alignment of the navigation menu disrupts the functionality of the dropdown sub-menus

I'm finding it challenging to center the navigation menu while maintaining the functionality of the drop-down li elements. Here is my demo: http://jsbin.com/tosayosino/1/edit?html,css,js,output Removing the float:left; property from .jetmenu li succ ...

Creating input fields similar to the Mail app on OSX

Is there a way to create input fields similar to those in the Mail App on OSX? Take a look at this screenshot - I'm looking to group text as shown in the image above. Instead of having multiple individual input fields (e.g. like in the mail app for ...

Did I accidentally overlook a tag for this stylish stripe mesh Gradient design?

I've been attempting to replicate the striped animated Gradient mesh using whatamesh.vercel.app. I've set up the JS file, inserted all the gist code into it, and placed everything in the correct locations, but unfortunately, it's not functio ...

Dynamic Bootstrap Table with Fixed Header

I am customizing a Bootstrap table by adding CSS for a sticky header. My code snippet includes the following: .table-sticky th { background: #fff; position: sticky; top: -1px; z-index: 990; } <div class ...

What causes RangeError: Maximum call stack size exceeded when Element UI event handlers are triggered?

I'm currently working on setting up a form and validating it with Element UI. Despite closely following the documentation, I am encountering an issue where clicking or typing into the input boxes triggers a RangeError: Maximum call stack size exceeded ...

Floating with Bootstrap Utility

Have you ever wondered about the proper usage of bootstrap float utility? Check out this example: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div class="row"> <div class=" ...

Tips for delivering a variable to a React Native Stylesheet

Is there a way to pass a variable to the "shadowColor" property in my stylesheet from an array declared in the code above? I keep encountering a "Can't find name" error. Attempting to use a template literal has not resolved the issue. Any assistance w ...

Guide on retrieving data from two separate tables within a single database using PHP

I am currently working on a shopping website where I have a table called "products" that stores all the product data. Additionally, there is a separate table named "cart" which contains the products added to the cart, including product ID and user ID. On t ...

Troubleshooting: My Bootstrap collapse navbar refuses to cooperate

Hi there! I'm working on creating a responsive navbar with Bootstrap, but I'm having trouble getting the collapse feature to work. Can someone please assist me? Here are the lines of code I have: <nav class="navbar navbar-expand-md navba ...

Using a MySQL statement within a conditional statement

Looking to modify this code to display different information based on the values retrieved, here is the current code: $sql_doublecheck = mysql_query("SELECT * FROM adminpage WHERE setting='main' AND close_site='1'"); $doublecheck = mys ...

Improve the design of the email newsletter

Having some trouble coding a particular element in my email newsletter layout. This is the desired outcome: View Screenshot Here's what I currently have: View Screenshot Any idea what could be going wrong here? Check out the code snippet below: &l ...

What is the best way to differentiate between two CSS elements with the same class name?

I ran into an issue where two elements share the same class name, specifically "img" Is it possible to apply different styles to these elements that are nested under different parent classes, despite having the same class name? I am trying to customize t ...

Guide on using PHP to assign the value of an array to an input field

Here is an array I have: $texts = [ "maybank2u.com", "Open BillPayment", "Status: Successful", "Reference number 2950211545", "Transaction date: 01 Feb 2016 13:09:17", "Amount: RM100.00", ...