Setting the margin to 0 auto to create a CSS Grid layout featuring an Image taking up the entire right side

I’ve been grappling with finding a consistent method to ensure uniform white space for all containers like this:

https://i.stack.imgur.com/wtk0G.png

Important: The grey rectangle signifies an image. My goal is for it to extend to the viewport's end, originating from its respective column.

This is what I have written so far:

HTML:

<div class="container">
    <h1>Title</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>


<div class="grid">
    <div class="grid__content-wrapper">
        <h2>Title</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>

    <div class="grid__image-wrapper">
        <img src="https://w.wallhaven.cc/full/9m/wallhaven-9mjoy1.png" alt="" class="grid__img"/>
    </div>
</div>

CSS / SCSS:

.container {
   max-width: 127rem;
   margin: 0 auto;
}


.grid {
   display: grid;
   grid-template-columns: repeat(2, 1fr);
   grid-gap: 3rem;

   &__content-wrapper {
   }

   &__image-wrapper {
   }

   &__img {
      display: block;
      width: 100%;
      height: auto;
   }
}

I’m aiming to find a solution that aligns the left margin of the .grid section consistently with the .container layout while allowing the image to stretch beyond the maximum width up to the grid's end. Any guidance on how to approach this challenge would be greatly appreciated!

Answer №1

For this solution to work effectively, it is important to ensure that the margin of your body element is set to 0. Failure to do so may cause the grid item to shift due to the margin width. Additionally, you will need to establish a CSS variable for the maximum width in your root element. This guideline should assist you in achieving the desired outcome. It is worth noting that to fully experience the effect, viewing it in full-screen mode is recommended as the snippet viewer behaves oddly in the previewer window.

:root {
  --max-width: 600px;
}

/* Added this */
body {
  margin:0;
}

.container {
  /* Utilize the CSS variable defined above */
  max-width: var(--max-width);
  margin: 0 auto;
}

.grid {
  /* These additions have been made */
  margin-block: 0;
  margin-left: max(0px, calc(50vw - calc(var(--max-width) / 2)));

  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 3rem;
}

.grid__img {
  display: block;
  width: 100%;
  height: auto;
}
<div class="container">
    <h1>Title</h1>
    <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>

<div class="grid">
    <div class="grid__content-wrapper">
        <h2>Title</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>

    <div class="grid__image-wrapper">
        <img src="https://w.wallhaven.cc/full/9m/wallhaven-9mjoy1.png" alt="" class="grid__img"/>
    </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

What happens when absolute positioning looks for "relative" inside nested divs?

I'm struggling to grasp the concept of nested divs and how they work together. I understand that when you have a "position absolute" element inside a "position relative" element, the former looks for the latter as its reference point. But what happens ...

What is the best way to align the items in the center of this container?

Check out this link: http://jsfiddle.net/zCXMv/18/ I'm having trouble getting this to work properly. Any assistance would be greatly appreciated. Here is the HTML code snippet: <div id="button" > <a class="button1 active" rel="1">&l ...

Guide on how to set the grid cell width to 0 for specific dimensions

What can be used in place of mdl-cell--0-col-phone to either disable an element or make its width zero? <div className="mdl-grid"> <div className="mdl-cell mdl-cell--2-col-phone mdl-cell--2-col-tablet mdl-cell--2-col-desktop"> <Left ...

How can I adjust a centered container div to fit properly on a mobile device

Exploring the centering of a particular div: #container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); height: 550px; width: auto; background: rgba(28, 28, 54, 0.70); padding: 15px; border: 1px solid #1c ...

What can be done to stop the Datepicker from exiting the Dialog box and causing it to malfunction?

While creating a form inside a dialog box, I encountered an issue with the date picker functionality. Whenever I try to select a date, it disappears and breaks, rendering the last days of the calendar inaccessible. You can view an example of this problem i ...

Incorporate a platform-specific button in a user interface with React Native

I need to display a button that is only shown on iOS, not on android. I have tried using the following style, but it doesn't seem to be working. ...Platform.select({ android: { display:'none', }, The code for my button is: <Bu ...

The appearance of my sidebar items' right border varies depending on the web browser being used

My sidebar item's right border appears differently in Mozilla and Chrome. How can I resolve this issue? Here are the images from both browsers: https://i.stack.imgur.com/YGkkz.png https://i.stack.imgur.com/JxNs3.png "use client"; import { ...

Bootstrap - Border padding excessively wide

For a project exercise using bootstrap, I'm working on recreating a page that can be found here: https://codepen.io/freeCodeCamp/full/NNvBQW I've hit a roadblock in trying to put a border around the image. Here's the code I have so far: htt ...

What is the best way to apply styles to a React component that is passed as a prop?

I'm currently working on a component that is passed as a label prop. In order for me to utilize justify-content: space between within my div, I must ensure it has a width of 100%. You can see how this appears in the developer tools by clicking here. r ...

What is the best way to arrange two divs inside a main div so they remain fixed in place at all times?

Struggling with aligning two smaller divs within a main wrapper for my website. Despite confining them to the main div, they don't stay fixed or aligned properly. How can this issue persist even when contained within a main div? I've attempted s ...

"Experience the power of Angular.js through seamless animations created by blending the capabilities

I want to smoothly fade out the old view and fade in the new view. The challenge is that the new content must be positioned absolutely until the old one fades out. I also want to set a specific top position and height for the new content, but when I try to ...

How do I make a div's height equal to 100% of the height of its parent

I am trying to make a button stick to the bottom of a card no matter how much content is on it. I used the Bootstrap class mt-auto, which worked well. However, when I set the height of all divs to 100%, the row in the card body overflows from the lg breakp ...

Reloading a page will display [object CSSStyleDeclaration]

Upon editing content and saving changes, instead of displaying my updated content when refreshing the page, it shows [object CSSStyleDeclaration]. function newElement() { let li = document.createElement("li"); let inputvalue = document.querySelector ...

Struggling with the installation of SimpLESS

I encountered an issue while attempting to install SimpLESS on my Windows 7 64bit PC. Upon running the installer, I was met with an error message stating: "Could not query info: Invalid HTTP Status Code (403)". Despite my efforts to search for a solution o ...

Hovering Div Troubles

I've been working on creating a hover effect for the blocks on my website. Everything was going smoothly until I reached the last block. The hover effect appears to be displaced, as shown in this sample link. Although the CSS code I'm using seem ...

Eliminate the gaps between the columns of the table

Is there a way to eliminate the gap between the day, month, and year columns in this table? <form action='goServlet' method='POST'> <table style="width:1000px" style="text-align:center"> <tr style="text-ali ...

Is there a way to turn off Emmet's CSS Abbreviations feature in Sublime Text 2?

One thing I really enjoy is how Emmet can generate HTML using 'CSS-like' strings. However, I prefer not to use their CSS Abbreviations. For example, when I write the following line of CSS: li a| I expect to get a tab when I press 'TAB&apos ...

Utilizing the Bootstrap grid system for responsiveness is optimized for a first word that is precisely 15 characters

Trying to achieve responsiveness using basic Bootstrap. The layout becomes responsive only when the initial word in a sentence contains more than 15 characters, as illustrated below. https://i.stack.imgur.com/wyErA.png <!-- Headers --> <div clas ...

What's the best way to add line numbers to source code on an HTML webpage after parsing?

I am currently working with AngularJS and MongoDB. In my MongoDB data, there are some text elements that contain a \n, causing each line to be displayed on a new line based on the occurrence of \n. However, I also want to add line numbers to each ...

Is the sidebar hidden only in Internet Explorer 7 and specifically not shown on one particular page?

So, I've been working on a website using Wordpress and created some custom page templates. However, I recently discovered that the sidebar on this specific page doesn't show up in IE 7. This page is using the lawyer.php template. I'm not su ...