Expand the background of columns to cover grid gutters

Imagine this...

You're working on a layout with three columns, all within a fixed-sized container that is centered using margin:0 auto.

The design requires the first column to have a background color that reaches the left edge of the browser window.

Do you know how to make this happen? I came up with a solution here, although it might not work well with certain types of image backgrounds.

HTML:

<main>
  <section>
    <div class="container">
      <header>
        <h1>Hello!</h1>
      </header>
      <div>
        <h2>Column 2</h2>
      </div>
      <div>
        <h2>Column 3<h2>
      </div>
    </div>
  </section>
</main>

CSS

main {
  min-width: 800px;
}

section {
  background-image: linear-gradient(to right, #dfdfdf 50%, #fff 50%);
  border-bottom: 1px solid #dfdfdf;
}

.container {
  width: 500px;
  margin:0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

section > * {
  min-height: 200px;
}

.container > div {
  background-color: #fff;
  padding-left: 30px;
}

Answer №1

You may also create the background using a pseudo-element.

To ensure the background extends beyond the screen, adjust the value of left accordingly. However, this approach might pose issues if precise positioning of the background image is required.

Another option to consider is using grid-gap instead of adding padding-left to the grid items.

body {
  margin: 0;
}

main {
  min-width: 800px;
}

section {
  border-bottom: 1px solid #dfdfdf;
}

.container {
  width: 500px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

section>* {
  min-height: 200px;
}

.container>div {
  background-color: #fff;
  padding-left: 30px;
}

header {
  position: relative;
}

header:before {
  content: '';
  background: lightgrey;
  display: block;
  position: absolute;
  left: -50vw;
  right: 0;
  bottom: 0;
  height: 100%;
  z-index: -1;
}
<main>
  <section>
    <div class="container">
      <header>
        <h1>Hey There!</h1>
      </header>
      <div>
        <h2>Column 2</h2>
      </div>
      <div>
        <h2>Column 3</h2>
      </div>
    </div>
  </section>
</main>

Answer №2

If you want to achieve three centered columns within a container that stretches the full width, you can use CSS grid and some alignment techniques.

.container {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-column-gap: 20px;
  min-height: 50px;
}

header {
  background-color: lightgray;
  display: flex;
  justify-content: flex-end;
}

section {
  border-bottom: 1px solid #dfdfdf;
}

body {
  margin: 0;
}

p { text-align: center;}
p > span { padding: 5px; background-color: aqua; }
<main>
  <section>
    <div class="container">
      <header>
        <h1>Hey There!</h1>
      </header>
      <div>
        <h2>Column 2</h2>
      </div>
      <div>
        <h2>Column 3</h2>
      </div>
    </div>
  </section>
</main>
<p><span>True Center</span></p>

Check out the codepen demo for reference

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 is the best way to conceal a bootstrap directive tooltip in Vue.js for mobile users?

Currently, I'm tackling a project with Vuejs and Laravel. There's a tooltip directive incorporated that relies on Bootstrap's functionality. Check it out below: window.Vue.directive("tooltip", function(el, binding) { // console ...

Style large and small text side by side with CSS styling

I've been trying to figure out how to position a smaller biography-style text next to this large title, but I'm having trouble finding a solution. I've experimented with float: left, float: right, and display: flex in my CSS code. Below is t ...

Position the Bootstrap Button on the right side of the navbar

Hey there, I'm completely new to the world of HTML/CSS and this is my first project ever. I welcome any constructive criticism that can help me improve in the future. Now onto my current dilemma: I've decided to use Bootstrap because it seems u ...

Which specific transitionend (or animationend) event should I use for this application?

I'm feeling a bit lost when it comes to using transitionend (or if I should be using animationend in this scenario). I'm not sure whether to utilize var, node, or box. Essentially, I am a complete beginner in this case. My goal is to have my div ...

What is the best way to implement media queries for mobile phones and desktop computers?

I've come across similar questions before but still can't wrap my head around it. Here's my dilemma: I want the index page of my website to display in desktop layout on desktops and mobile jquery on mobile devices. Currently, I have set up m ...

Is there a workaround utilizing calc() and vh specifically for Chrome browsers?

Is it possible to find a CSS-only solution for incorporating vh in calc() functions specifically for Chrome browsers? I am trying to implement calc(100vh - 25px) as the top margin in order to make a 25px bar stick to the bottom of the page, but I am facin ...

The collapsible section expands upon loading the page

Trying to implement Disqus as a plugin on my test webpage, I created a collapsible section with a "Show comments" button. The idea was to hide the plugin by default and reveal it only when users click on "Show comments". Despite using the w3schools example ...

What could be causing this addEventListener to fail when I am assigning elements to a class?

I've encountered an issue with my code where I have two text inputs and two date inputs. I tried to select all of them using QuerySelectorAll with a class, added a click listener that should change the textContent of a div element to "", but it's ...

Creating my website with a unique inverse color scheme

I'm looking to change the color scheme of my webpage so that it is inverse (white becomes black and black becomes white) similar to the Dark Reader chrome extension: https://chrome.google.com/webstore/detail/dark-reader/eimadpbcbfnmbkopoojfekhnkhdbiee ...

A guide on updating CSS content dynamically within ExtJS

In my resource folder, I have a CSS file that contains some values which I need to change or add based on certain conditions. However, I'm struggling to figure out how to achieve this. Here is an example of the code: triggers: { clear: { ...

Adding dynamic content to CSS in Next.JS can be achieved by utilizing CSS-in-JS

Currently diving into the world of Next.JS, I've managed to grasp the concept of using getStaticProps to retrieve dynamic data from a headless CMS and integrate it into my JSX templates. However, I'm unsure about how to utilize this dynamic conte ...

Particle JS - Taking over the entire screen

I have incorporated Particle JS into the banner using the link provided. It should be confined within the banner, with a white background underneath displaying the header text "hello there." However, the Particle.JS effect is currently taking over the enti ...

Say goodbye to tables and hello to CSS div layouts!

Is there a way to align three or four elements without relying on tables? I want them to follow this format: The reason I am asking is because my current method involves using tables, but they are not allowing me to set a specific height for the "test" bo ...

How to create a see-through background using three.js

I am new to working with three.js and recently came across a codepen that caught my attention. However, I am facing difficulties while trying to change the background color. After exploring various questions related to this issue, I attempted to add { alp ...

What is the best approach for scaling @material-ui Skeleton in a grid row with variable heights?

I am working on creating a grid of Avatar images with a transition state where I want to show a skeleton representation of the image. To achieve this, I am using @material-ui/lab/Skeleton. The issue I'm facing is that since my images are set to autos ...

What is preventing the div container from extending to its full width?

The content on the right side of this div container is struggling to reach full width for some unknown reason. Click here to see an image of the issue. The right column is not extending to full width. This snippet shows my CSS code. The 'main-conte ...

Creating a dynamic trio of graphs with HTML5, CSS3, and Vanilla JavaScript

I successfully created a tree graph using HTML5 and CSS3, but currently the nodes are static. I am looking to enhance the graph by making it dynamic. By dynamic, I mean that if the number of nodes increases or there are multiple children added, the graph ...

Ways to switch sidebar menu using only CSS?

How do I create a BS4 sidebar menu that toggles on click and replaces an icon? See the code below: .toggle { display: none; } .menu-head label { color: black; cursor: pointer; } .container { overflow: hidden; display: inline-block; } .si ...

Obtain a masterpiece by creating a canvas in React

Greetings! I have developed a drawing app using react and I am looking for a way to capture what the user has drawn on the canvas when they release the mouse button. This process should repeat continuously until the user stops drawing. Can anyone suggest h ...

How to efficiently center a jQuery Mobile Dialog on the screen using custom dimensions

I designed an app using jQuery Mobile and I'm looking to display a Dialog box when a button is clicked. The Dialog box should be centered on the screen with specified width and height. For reference, you can view the current version on jsfiddle. The ...