Fixed position relative to parent container

undefinedtoken

Im trying keep an element fixed within a container but the fixed element seems to be positioning itself according to the screen but not the parent element.

My code :

<div class="relative">
  <div class="absolute">
    <div class="fixed"></div>
  </div>
</div>

Css :

.relative{
  position: relative;
  height:800px;
  width: 400px;
  background: #000;
}

.absolute{
  height:60px;
  width: 60px;
  position: absolute;
  top:0;
  right:0;
  background: #ccc;
  -webkit-transform: translateZ(0); 
}

.fixed{
  height:20px;
  width: 20px;
  background: red;
  position: fixed;
  top:0;
  right:0;
}

I want the red box to be fixed inside the grey box . but now when i scroll the box scrolls and doesnt remains fixed.

See it live : http://codepen.io/undefinedtoken/pen/abhgc

aniskhan001

The problem here is with the -webkit-transform.

Chrome cannot render position:fixed on elements under a transformation.

Read here

You can try removing the transform from .absolute div and set a margin-left to the .fixed div after calculating it's parents width. in your case it's 40px.

Example:

.absolute{
  height:60px;
  width: 60px;
  position: absolute;
  top:0;
  right:0;
  background: #ccc;
  /* -webkit-transform: translateZ(0);  */
}

.fixed{
  height:20px;
  width: 20px;
  background: red;
  position: fixed;
  margin-left: 40px;
}

JSFiddle DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Fixed position but relative to container

From Dev

How to have fixed position of element but relative to container

From Dev

EXT JS: css and position relative to parent container

From Dev

Make position: fixed element respect parent with position: relative

From Dev

Make position: fixed element respect parent with position: relative

From Dev

Set width of the fixed block relative to parent div (container)

From Dev

Programatically position a scollbar of a div relative to its parent container

From Dev

position relative overlapping position fixed

From Dev

Position:fixed element within a position:relative parent. Which browser renders correctly?

From Java

Position absolute but relative to parent

From Dev

position elements relative to parent

From Dev

position relative to the bottom of parent

From Dev

position relative to the bottom of parent

From Dev

Position elements relative to parent of parent

From Dev

How to position container to fixed position?

From Dev

Position fixed 100 of parent

From Dev

Position fixed relative to a block, not to the viewport

From Dev

Is position: fixed z-index relative to its parent's z-index?

From Dev

Position relative container white space

From Dev

Position relative to bottom + container height

From Dev

How to position objects relative to their parent

From Dev

Position absolute height of relative parent

From Dev

WPF place container on fixed position

From Dev

In chrome version 57, the css position fixed is behaving odd when it has a parent container whose left is changing

From Dev

How to position a relative element after a fixed element

From Dev

Relative positioned div inside of fixed parent

From Dev

Two divs (left, right) in parent, right with fixed width, left fill free space, both in same line. (without position relative/absolute)

From Dev

CSS - Safari - fixed positioned element is not relative to it's relative container

From Dev

Position fixed element within fixed element relative to page

Related Related

  1. 1

    Fixed position but relative to container

  2. 2

    How to have fixed position of element but relative to container

  3. 3

    EXT JS: css and position relative to parent container

  4. 4

    Make position: fixed element respect parent with position: relative

  5. 5

    Make position: fixed element respect parent with position: relative

  6. 6

    Set width of the fixed block relative to parent div (container)

  7. 7

    Programatically position a scollbar of a div relative to its parent container

  8. 8

    position relative overlapping position fixed

  9. 9

    Position:fixed element within a position:relative parent. Which browser renders correctly?

  10. 10

    Position absolute but relative to parent

  11. 11

    position elements relative to parent

  12. 12

    position relative to the bottom of parent

  13. 13

    position relative to the bottom of parent

  14. 14

    Position elements relative to parent of parent

  15. 15

    How to position container to fixed position?

  16. 16

    Position fixed 100 of parent

  17. 17

    Position fixed relative to a block, not to the viewport

  18. 18

    Is position: fixed z-index relative to its parent's z-index?

  19. 19

    Position relative container white space

  20. 20

    Position relative to bottom + container height

  21. 21

    How to position objects relative to their parent

  22. 22

    Position absolute height of relative parent

  23. 23

    WPF place container on fixed position

  24. 24

    In chrome version 57, the css position fixed is behaving odd when it has a parent container whose left is changing

  25. 25

    How to position a relative element after a fixed element

  26. 26

    Relative positioned div inside of fixed parent

  27. 27

    Two divs (left, right) in parent, right with fixed width, left fill free space, both in same line. (without position relative/absolute)

  28. 28

    CSS - Safari - fixed positioned element is not relative to it's relative container

  29. 29

    Position fixed element within fixed element relative to page

HotTag

Archive