Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to keep the width of the parent element when position: fixed is applied?

When we apply position:fixed to an element, it's taken out of the normal flow of the document, therefore it doesn't respect it's parent's element width. Are there ways to make it inherit it's parent's width if this is declared as a percentage ? (working use case below)

let widthis = $('.box').width();  $('.dimensions').text(`width is ${widthis}`);    $('button').on('click', function() {    $('.box').toggleClass('fixed');    let widthis = $('.box').width();    $('.dimensions').text(`width is ${widthis}`);  });
.container {    max-width: 500px;    height: 1000px;  }    .box {    background-color: lightgreen;  }    .fixed {    position: fixed;  }    .col-1 {    border: 1px solid red;    float: left;    width: 29%;  }    .col-2 {    border: 1px solid pink;    float: left;    width: 69%;  }
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css" rel="stylesheet"/>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <button>Click this to toggle position fixed for the left column</button>  <div class="container">    <div class="col-1">      <div class="box">        fixed content<br>        <span class="dimensions"></span>      </div>      </div>        <div class="col-2">      some other content      </div>    </div>
like image 982
George Katsanos Avatar asked Apr 15 '13 15:04

George Katsanos


People also ask

Can position fixed be relative to parent?

fixed : the element is removed from the flow of the document like absolutely positioned elements. In fact they behave almost the same, only fixed positioned elements are always relative to the document, not any particular parent, and are unaffected by scrolling.

When using position fixed What will the element?

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located.

How do you make a div fixed width?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.

When using position fixed position what will the element always be positioned relative to the nearest relative with wrapper element parent element the viewport?

To position an element "fixed" relative to a parent element, you want position:absolute on the child element, and any position mode other than the default or static on your parent element. This will position childDiv element 50 pixels left and 20 pixels down relative to parentDiv's position.


1 Answers

This is an interesting challenge. To approach this, we should first understand what fixed actually does.

Understand Fixed

Unlike absolute, fixed doesn't position itself from its closest relative parent. Instead, fixed positions itself relative to the viewport. The viewport will always stay fixed, which is why you get the effect that you do.

That being said, whenever you "inherit" any width it will be respective to the viewport. So it does us no good when we're trying set the width of our target element to the width of it's parent.

Learn more about the different behaviors of position.

Quick Solutions

There are two approaches to fix this.

Pure CSS

We can use pure CSS to fix this problem, but we would need to know the width in advance. Suppose that its parent element is 300px;

.parent{     width: 300px; }  .parent .fixed_child{     position: fixed;     width: 300px; } 

JS

Now with mobile devices, we don't really have the luxury of having set widths, especially anything over 300px. Using percentages won't work either, since it will be relative to the viewport and not the parent element. We can use JS, in this case with jQuery to achieve this. Lets take a look at a function that will always set the width of the parent at the given moment:

 function toggleFixed () {       var parentwidth = $(".parent").width();             $(".child").toggleClass("fixed").width(parentwidth);           } 

css:

.fixed{     position:fixed; } 

View in CodePen

Dynamic Widths

That's fine and dandy, but what happens if the width of the window changes while the user is still on the page, changing the parent element with this? While the parent may adjust its width, the child will stay the set width that the function set it. We can fix this with jQuery's resize() event listener. First we'll need to split the function we created into two:

function toggleFixed() {    adjustWidth();    $(".child").toggleClass("fixed");  }   function adjustWidth() {    var parentwidth = $(".parent").width();    $(".child").width(parentwidth);  } 

Now that we've separated each part, we can call them individually, we'll include our original button method that toggles the fixed and width:

$("#fixer").click(      function() {        toggleFixed();      }); 

And now we also add the resize event listener to the window:

 $(window).resize(      function() {        adjustWidth();      }) 

View in CodePen

There! Now we have a fixed element who's size will be adjusted when the window is resized.

Conclusion

We've tackled this challenge by understanding fixed position and it's limitations. Unlike Absolute, fixed only relates to the view port and therefore cannot inherit its parent's width.

To solve this, we need to use some JS magic, which didn't take very much with jQuery, to achieve this.

In some cases, we need a dynamic approach with scaling devices of varying widths. Again, we took the JS approach.

like image 119
Philll_t Avatar answered Sep 19 '22 07:09

Philll_t