The issue of Bootstrap 4 fixed position not working with inherited width

Exploring content and sidebar:

.border {
   border: 1px solid black;
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <div id="app">
       <div class="container">
           <div class="row">
              <div class="col-md-7 col-lg-9 border">
                <div class="content">
                  .....
                  </div>
              </div>
              <div class="col-md-5 col-lg-3 border position-relative">
                 <div class="position-fixed border" style="    width: inherit;">
                   .....
                 </div>
              
              </div>
           </div>
           
       </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

Trying to figure out why the width of the position-fixed element is not inheriting from its parent in this scenario. Any insights on how to make it inherit the width from the parent element would be much appreciated. If I set width: 20%, it does work for the position-fixed element, but why is inherit width not functioning properly?

Answer №1

Things are functioning correctly, however, it is important to remember that when using position:fixed, the width is relative to the viewport. This means that if you set 20% to the parent element, the fixed element will inherit that 20% and not the actual computed pixel value of its parent's width.

Therefore, both elements will have 20%, but the percentage references may not be the same for both (i.e. they may not share the same containing block).

The CSS keyword inherit instructs the specified element to inherit the computed value of a property from its parent element. source

 

... However, for certain properties (such as width, margin-right, text-indent, and top), percentage values specified become percentage-computed values. source

Let's demonstrate this with a simple example:

.box {
  border:2px solid red;
  min-height:50px;
  width:100%;
}
.box > div {
  position:fixed;
  width:inherit;
  min-height:50px;
  border:2px solid green;
  left:0;
}

body {
  padding:0 100px;
}
<div class="box">
  <div></div>
</div>

In this example, both elements have width:100%. The fixed element has 100% width of the viewport, while the static element has 100% of the body width minus padding.

An element's position and size can be calculated relative to a certain rectangle, known as the containing block of the element. The containing block is defined as follows:

...

  1. For other elements, if the element's position is 'relative' or 'static', the containing block is formed by the content edge of the nearest block container ancestor box.

  2. If the element has 'position: fixed', the containing block is established by the viewport in continuous media or the page area in paged media. source


Another point to consider is the absence of a left value in your code, which can be misleading and cause overflow issues. Here is the previous code without the left value:

.box {
  border:2px solid red;
  min-height:50px;
  width:100%;
}
.box > div {
  position:fixed;
  width:inherit;
  min-height:50px;
  border:2px solid green;
}

body {
  padding-left:100px;
}
<div class="box">
  <div></div>
</div>

It may seem like the fixed element has the same width as the static one, but in reality, the fixed element is actually overflowing.

Related: Why aren't my absolutely-positioned elements located where I expect?

Answer №2

When determining the percentage of a desired column to be fixed, I calculate it and then apply it to the CSS by setting the width percentage. For instance, if I want the fixed element to be equivalent to col-md-2, this would mean setting the width in the CSS to 2/12 = 16% !important; as shown below:

.fixedelement{width:16% !important;}

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

Setting the child elements of a CSS div to have the same width and be larger than the

Welcome everyone to my inaugural post! I hope I've followed the right steps. I'm encountering an issue where I have child divs that need to have equal widths. The #content can sometimes exceed the browser window's width (hence the 3000px), ...

What is the best way to target CSS only to elements that do not have a parent with a specific class?

Striving to preserve the existing CSS in the codebase. .my-new-class { .existing-class { ...implementing new styles } } .existing-class { ...maintaining old styles } I attempted a technique that I know won't work and understand why: ...

What's the best way to continuously increase my counter using the data received from my AJAX calls?

On the final step of my first ajax project, I have implemented a thumbs-up icon that increments a column in the database when clicked. The following code achieves this: Viewable Page HTML and jQuery: <div id="comment_id">+1</div> <div id=" ...

Issue with Bootstrap 'align-content-around' not functioning as expected

The align-content: space-around property does not seem to be working in this scenario. What is the solution? How can columns 3 and 4 be aligned to the bottom? .row { background: #f8f9fa; } .col { border: solid 1px red; padding: 10px; height: 20 ...

Using JavaFX to create a StackedAreaChart and implementing the setLowerBound method

I am encountering some issues with the Class StackedAreaChart and setLowerBound function. When I enable the autoRangingProperty to true, everything works fine. However, when I disable autoRanging and apply certain parameters, I encounter some bugs. imp ...

Symfony 2 lacks the ability to automatically create the web/bundle/framework structure

I encountered a major issue with Symfony. After installing Symfony 2.7.5 via the command line: $ symfony new my_project The problem arose in the generated project directory: /web/bundle In this folder, I found two empty files (not directories!) named fr ...

Is there a way to create a table cell that occupies zero space?

I have a very intricate table setup that resembles a tree structure, with only a few cells on the left and many on the right. I am looking to completely hide specific cells, along with their entire rows, using JavaScript based on their class. Using visibi ...

Determining the true width of a span element using jQuery

I am trying to determine the actual width of a span element within a div, but when I attempt to do so, it gives me the width of the entire div instead. Here is the code I am working with: <div class="content"> <span class="type-text" id="ta ...

Tips for inserting an image within a horizontal list of items

Struggling to find a solution here. I have a menu with a unique bar design between list elements, featuring breaks at the top and bottom. I attempted to use li:after in the CSS to insert an image, but encountered issues with padding and margin. Below is t ...

What is the best way to insert an HTML data attribute string into a SCSS mixin using attr()?

I'm currently working on setting up a color scheme in SCSS that allows me to use the following HTML structure: <div class="swatch" data-bg="green">...</div> In my SCSS file, I have defined a mixin like this: @function color($key: ' ...

Angular.js, require.js, and jQuery Plugin all combined to create a uniquely customized directive

As a newcomer to Angular and Require, I am seeking assistance with the following code snippet. Despite successfully integrating Angular and Require, I am facing an issue where the bootstrap-switch element fails to initialize: define(['myapp.ui/module ...

Adjusting the width of an element by modifying its border-collapse property

Is there a way to implement "border-collapse: collapse" without affecting the width of the element? The Issue: Without using the border-collapse property, the right and left borders appear bold because they are adjacent. Using the border-collapse propert ...

I must update a bootstrap class name within multiple layers of divs by referring to the parent class

My code structure is set up like this: <div id="main-parent"> <div class="child2"> <div> child2 </div> </div> <div>child3</div> - - - - <div class="ch ...

Properly extending the functionality of an HTML widget

Understanding HTML and CSS layout is still a mystery to me, so any guidance on what I'm trying to achieve would be greatly appreciated. I am currently developing a JavaScript API for our company's "widget" so that users can integrate it into the ...

Encountering an issue with retrieving the nth element using Python Selenium

<div class="booking-classes"> <h3 style="text-align: center;"> <p><a href="https://foreupsoftware.com/index.php/booking/20290#/teetimes" style="background-position:0px 0px;"><b> ...

Exploring the world of sass with compound mathematical statements

There's a slight issue that I'd like to circumvent. $var: 30px; font: 25px/25px font,font,font; //Works font: ($var - 5)/($var - 5) font, font, font; //Works not font: ($var - 5px)/($var - 5px) font, font, font; //Works no ...

Adjusting text alignment to span the entire width with consistent automatic spacing

Imagine if I have a collection of N words. To illustrate, consider the following: Dog Peanut Oranges Lemon Disciplinary All these words vary in length. Let's assume there is a container with a variable width X (the container may adjust its size ac ...

Merge jQuery with the jetpack infinite scroll feature

Utilizing the jQuery code below for an accordion effect on all my WordPress posts on the front end. (function($) { function initAccordion() { var $ele = $('.entry-content').hide(); $(".entry-header").unbind('click&apos ...

Issue with printing error messages for JavaScript form validation

I have implemented the following code for a form to handle validation and display errors below the fields when they occur: <!DOCTYPE html> <html> <head> <style type="text/css"> .errorcss { background-color: yellow; color:re ...

Using Jquery for conditional statements: if versus else

$("#bottomWatch").click(function(){ var doops = $(".videoMove").display; if (doops == "none") { $("#video").css("left","15%", "display: block"); } else { $(".videoMove").cs ...