Languages

CSS - issue with position: fixed element inside position: fixed element

6 points
Asked by:
Kevin
797

Is it possible to use position: fixed element inside other position: fixed element in the way position: absolute works?

As I noticed, position: fixed style sets element position according to the document - not according to the parent element that has position: fixed also. It causes covering one element by second one messing my layout.

What I want (screenshot):

What I get (sample code):

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    body {
        height: 100px;
    }

    .outer {
        position: fixed;
        left: 10px; top: 10px; right: 10px; bottom: 10px;
        background: red;
    }

    .inner {
        position: fixed;
        left: 10px; top: 10px; right: 10px; bottom: 10px;
        background: blue;
    }

  </style>
</head>
<body>
  <div class="outer">
    <div class="inner">
    </div>
  </div>
</body>
</html>

 

1 answer
5 points
Answered by:
Kevin
797

There is only one solution: use position: absolute for the inner element.

Example:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    body {
        height: 100px;
    }

    .outer {
        position: fixed;
        left: 10px; top: 10px; right: 10px; bottom: 10px;
        background: red;
    }

    .inner {
        position: absolute;
        left: 10px; top: 10px; right: 10px; bottom: 10px;
        background: blue;
    }

  </style>
</head>
<body>
  <div class="outer">
    <div class="inner">
    </div>
  </div>
</body>
</html>

 

0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join