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

Divider displayed between images in Internet Explorer 8

On my website, I have arranged four images in a square using the code below: <div id="tempo_main"> <div id="tempo_content"> <div style="text-align: center;z-index: 3;position: absolute;right:350px; left:350px; t ...

Is there a way to allow an HTML page rendered by node.js to communicate back to its corresponding node.js file?

I am currently in the process of developing a registry system using node.js and HTML. However, I have encountered an issue where my HTML page is rendered by node.js, but when trying to call it back to the node.js file, it appears that the JS file cannot be ...

React JS progress circle bar is a simple and effective way to visualize

Currently, I am in the process of developing a progress circle bar that will function as a timer alongside sliders. Each slide is intended to have its own corresponding progress bar. While I have managed to create the bars individually, I am facing challe ...

What is the best way to transfer data from an HTML input field to a PHP script?

Within my patient_display.php file, I am calling the patient_history.php script and passing along the P_ID. Here is an outline of my code. patient_display.php: echo '<form name="Patient" action="patient_history_display.php" method="get">'; ...

Ways to Press the Enter Key on Different Browsers

Looking for a solution to get the keypress action working properly? I have a chat feature where I need to send messages. Here is what I have in the Form-tag (JSP based): function SendMessage() { if (event.keyCode===13) { ale ...

Is your Jquery validation malfunctioning?

I am currently facing a challenge with required validation on an asp.net page. In order to validate the inputs, I have implemented multiple controls which can be hidden or displayed based on certain conditions. These controls include checkboxlists, dropdow ...

Ways to conceal and reveal content within div elements

I have a code snippet that is currently operational, but I am looking to enhance it by displaying one div at a time. If you would like to view the code, please visit my CodePen link below: https://codepen.io/danongu/pen/YzXvpoJ Due to the extensive amou ...

Is there a way to eliminate the transform style from a draggable element?

I am looking to enhance the CDK drag and drop example by adding a preview of the dragged element with specific left and top positions, without utilizing the transform style. HTML <div class="example-boundary"> <div class="example- ...

Background image not visible on Firefox and Chrome when using a DIV element

I'm encountering an odd issue where my DIV background isn't showing up in Firefox and Chrome, but it looks fine in IE. I've searched on several places like Stackoverflow for solutions to this problem, but none of them have worked for me so f ...

Remember to follow the camel case convention when utilizing element.tagName(String) with jSoup

Looking to change the name of an HTML tag using Jsoup in the following way - element.tagName("p:panelGroup"); But when I run this code, the output changes the case and I end up with p:panelgroup. Is there a method to preserve the camel case in the resul ...

An absolutely positioned element will always move within its relative container

After extensive searching on Google and Stack Overflow, I finally found what seemed like the perfect solution. The logic behind it made sense and I understood it perfectly. But when I implemented it, my absolute positioned divs kept moving whenever I resiz ...

Arranging containers in a fixed position relative to their original placement

A unique challenge presents itself in the following code. I am attempting to keep panel-right in a fixed position similar to position: relative; however, changing from position: relative to position: fixed causes it to move to the right side while the left ...

The relationship between elements and text input positioning

I am faced with a unique challenge. As a user types in an input field, I need to display a reset button positioned center right of the input. I must adhere to the current HTML structure for the label, input, button, and error message and I prefer not to r ...

Tips for updating images automatically

I have a HTML file where I display images on a popup window using the code below: <div id="divCheckbox" style="display: none;"> <a class="fancybox" rel="gallery01" title= "BONOS ALQUILER 2/6" href="eventos/Bonos_alquiler.jpg">o ...

Increase transparency of scrollbar background

Is it possible to adjust the opacity of a scrollbar track? I attempted to use opacity:0.5, but it didn't have any effect. I am using material UI styled, although I don't believe that is causing the issue. const Root = styled('div')(({ ...

How can I display a PHP variable in JavaScript?

I am having trouble displaying a PHP variable in Javascript; Below is the code I am using: <script type="text/javascript> $(document).ready(function (){ var n=<?php echo json_encode($count)?>; for(var i=0;i<n;i++){ var div ...

Tips for creating horizontal scrolling in the div element

I'm working with a div element that looks like this: <div id="tl" style="float:right;width: 400px; height:100px; background-color:Green; overflow-x: scroll;overflow-y: hidden;"> <div id='demo' style="float:left;height ...

Tips for adding an offset to a #link located on a different webpage

I have a website that consists mainly of an index.html file with a few additional subpages. The index.html file is divided into sections, but the navigation has a fixed position and a height of 100px, which requires a 100px offset for links like <a hre ...

Steps for aligning floating DIVs in the center of a parent DIV

Why is the text alignment not working as expected when I have three divs within a parent div that need to be center aligned? If you'd like to see the issue in action, check out this Demo fiddle <div class="container_alt"> <div class= ...

CSS for a Dropdown Menu with Multiple Layers

I've successfully implemented a dropdown menu, but now I'm facing the challenge of adding another level to it. Specifically, I want a second level to drop down when hovering over "Test3". What do I need to add or modify in the code to achieve thi ...