Is it possible to make side elements expand to occupy the remaining screen space beyond the container?

This is what it's supposed to look like:

The goal here is to have the red element on the left expand to fill the remaining space beside its content, as well as the grey on the right. I am utilizing bootstrap which means the center elements occupy 1170px, or 1140px if we exclude the padding. My attempt involved adding before and after children to the first and last elements and using absolute positioning in an effort to stretch them all the way to the screen margins. However, I'm struggling with a reliable method for calculating that length, and I can't use overflow due to the main element containing dropdowns. Any suggestions?

This is the HTML structure:

<div class="body-width-container">
  <div class="container">
    <div class="battery-finder battery-finder--inactive">
      <div class="battery-finder__label">
        Choose Battery
      </div>

      <div class="battery-finder__icon">
        <i class="icon icon-battery"></i>
        <div class="battery-finder__icon::after"></div>
      </div>

      <div class="dropdown battery-finder__select battery-finder__select--fuel">
        <button class="dropdown-toggle" type="button" data-toggle="dropdown">
          Fuel Type
                            <i class="icon icon-fuel"></i>
          <span class="battery-finder__select__arrows">
            <i class="fa fa-angle-up"></i>
            <i class="fa fa-angle-down"></i>
          </span>
        </button>
        <ul class="dropdown-menu">
          <li><a href="#">Test 1</a></li>
          <li><a href="#">Test 2</a></li>
          <li><a href="#">Test 3</a></li>
        </ul>
      </div>

     <!-- Add rest of the HTML code-->

      <button class="battery-finder__button">
        <i class="icon icon-magnifier"></i>
      </button>
    </div>
  </div>
</div>

Here is the CSS used:

.container .battery-finder__label:before{
    content: "";
    display: block;
    height: 100%;
    position: absolute;
    left: -1000px;
    right: 100%;    
    background-color: inherit;
}

.container .battery-finder__button:before{
    content: "";
    display: block;
    height: 100%;
    position: absolute;
    right: -1000px;
    left: 100%;    
    background-color: inherit;
}

.body-width-container {
    overflow: hidden;
}

Answer №1

You could consider utilizing a table for this scenario:

<table style="width:100%; table-layout: fixed;">
  <tr>
    <td style="width: 50%; background-color: blue">&nbsp;</td>
    <td style="width: 1170px;">
      main content
    </td>
    <td style="width: 50%; background-color: red">&nbsp;</td>
  </tr>

Answer №2

If you want to restrict the width of an element, you can achieve this by applying a fixed display style.

For example, I have set .battery-finder__content with a max-width:1170px, which ensures that it does not exceed 1170px and allows the other two items to occupy the remaining space. The container also has a width of 100%.

To make the width fixed instead of maximum, simply replace max-width with width in the CSS snippet below:


.body-width-container,
.container{
  width:100%
}
.battery-finder{
  width:100%;
  display:table;
  table-layout:fixed;
  height:50px;
 }
.battery-finder__label{
  background: #d00;
  display:table-cell;
}
.battery-finder__content{
  background: #888;
  display:table-cell;
  max-width:1170px; /* Change this line */
}
.battery-finder__buttonWrap{
  background: #555;
  display:table-cell;
  border:none;
}

Incorporate this HTML structure within your code for implementing the above CSS changes:


<div class="body-width-container">
  <div class="container">
    <div class="battery-finder battery-finder--inactive">
      <div class="battery-finder__label">
        Choose Battery
      </div>

      <div class="battery-finder__content">
        STUFF GOES HERE
      </div>
      
      <div class="battery-finder__buttonWrap">
        <button class="battery-finder__button">
          <i class="icon icon-magnifier"></i>
        </button>
      </div>
    </div>
  </div>
</div>

Answer №3

Using a pseudo-element was the perfect solution.

You can find more information about the basic techniques in this Stack Overflow question and answer

.body-width-container {
  overflow: hidden;
}
.container {
  width: 80%;
  margin: auto;
}
header {
  height: 75px;
  background: grey;
  position: relative;
}
header::before {
  content: '';
  position: absolute;
  width: 50vw;
  height: 100%;
  top: 0;
  right: 100%;
  background: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="body-width-container">

  <div class="container">
    <header></header>
  </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

App.post is returning a set of empty curly braces as the response

I am currently working on an express.js application that retrieves data from a MySQL database and displays it on the screen. I am also trying to implement an insert functionality so that I can add data to the database via the browser. However, when I post ...

How can I retrieve text within a p tag using xpath and xpath axes?

<div class="details"> <p><b>Compatibility:</b> All versions</p> <p><b>Category:</b> Entertainment</p> <p><b>Updated:</b> Apr 2, 2014</p> <p><b>Version ...

Header that sticks to the top of a container as you scroll through it

Many questions arise regarding sticky elements in the DOM and the various libraries available to handle them, such as jquery.pin, sticky-kit, and more. However, the issue with most of these libraries is that they only function when the entire body is scro ...

Tips for evenly and fully stretching a set number of horizontal navigation items across a designated container

I want to evenly distribute 6 navigation items across a 900px container, with equal amounts of white space between each item. Here is an example: ---| 900px Container |--- ---| HOME ABOUT BASIC SERVICES SPECIALTY SERVICES OUR STAFF CONTACT ...

Position the Bootstrap button on the right side

this is the code I am working with: <p class="h1 p-3 mb-2 bg-danger text-white text-danger">Lagerbestand unter Mindestmenge! </p> <br /> <div class="border text-center"> <p class="h5 text-center">Durc ...

How can you capture the VIRTUAL keyCode from a form input?

// Binding the keydown event to all input fields of type text within a form. $("form input[type=text]").keydown(function (e) { // Reference to keyCodes... var key = e.which || e.keyCode; // Only allowing numbers, backspace, and tab if((key >= 48 && ke ...

Utilize jQuery to dynamically swap out a CSS stylesheet in response to changes in the body class

In the head section of my HTML, I have the following stylesheet: <link rel="stylesheet" href="/templates/jooswatch-v3-5/css/bootswatch/superhero/bootstrap.min.css"> I want to replace this stylesheet with different ones based on changes in the body# ...

Center align the division within the list item

Check out the fiddle provided here: https://jsfiddle.net/5jnnutg8/ I am looking for a way to center align and display inline the "something #" list items. While the title "Hi" can be centered using text-align: center in css, this doesn't seem to wor ...

Exploring the fundamentals of Django with a touch of creativity in CSS

As a newcomer to Django, I am currently working on setting up a basic Django application. I have progressed to chapter 5 in the Django online book: Before delving into databases, my goal is to incorporate some simple CSS and JS directly into the applicat ...

Receiving an inaccurate value from the input with type='number' attribute

There is an input field where users can enter a string, and I need to capture the value entered into it. $("#test").on("change", function(){ console.log($(this).val()); }) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery ...

Is it possible to remove the left margin on an li element by placing a td element inside it using HTML and

Here is my custom HTML code including the CSS styles: <!DOCTYPE html> <html> <head> <style> table td { border: 1px solid black; } li { margin-left: 3 ...

What method can I use to adjust the font style when it overlays an image?

Sorry if this is a bit unclear, but I'm trying to figure out how to change only a section of my font when it overlaps with an image while scrolling. If you visit this example website, you'll see what I'm referring to: For a visual represen ...

Tips on styling HTML using a query value in string format

When using ASP.NET razor and SQL Server 2008 R2, I have a local variable declared in the following manner: var tsmMtd = db.QueryValue(" SELECT b.Name, SUM(subtotal) totalSUM FROM DR_TRANS a INNER JOIN Staff b ON a.Salesno = b.Staffno WHER ...

What is a way to create a colored <hr> element without increasing its height at all?

I'm facing a dilemma - I created an hr element and it's currently grey, but I want to change the color to black. The issue is that when I do this, it appears thicker and ruins the overall styling of my page. Does anyone have any ideas on how I ca ...

Is there a way to create an input field that accepts only numbers and behaves like a password field simultaneously?

I am attempting to develop an input field for entering a PIN. I would like the PIN to be masked on mobile devices, similar to how passwords are obscured in password input fields. I came across a suggestion on stackoverflow regarding this, but unfortunately ...

Tips for synchronizing JavaScript rendering time with Python requests?

My goal is to retrieve the HTML content of a JavaScript-generated website so that I can extract information using BeautifulSoup. However, when I attempt to request the HTML, the data I receive is from before the page fully loads. Below is the code snippet ...

Elastic canvas to always match the full size of the container or screen

I am facing an issue with my responsive canvas that should resize to fit the screen window within a div. The canvas is intended to be 1100x1100 but needs to scale based on the screen size while maintaining a square aspect ratio. The problem arises when th ...

An issue arises in vue.js where the v class binding takes precedence over other bindings and fails to properly remove

I have a game with a punching bag where I want to incorporate an animation class each time the bag is clicked. Once the health meter hits zero, I intend to replace the bag image with one depicting a burst bag. Things are running smoothly up until the poin ...

Steps for importing selenium web elements:

Can all of these ... tags be extracted simultaneously? I aim to retrieve every ... from that particular webpage! Web address: 'https://school.programmers.co.kr/learn/challenges?order=recent&page=1' ...

Is the Application Wizard failing to display the card's content on the second attempt?

Hey there, I'm currently using the bootstrap application wizard and encountering a problem. Whenever I try to load the wizard for the second time, the card doesn't initialize properly and the content disappears. Here's a snippet of my code: ...