Skip to content

Instantly share code, notes, and snippets.

@hulufei
Last active June 26, 2018 05:37
Show Gist options
  • Select an option

  • Save hulufei/9063571 to your computer and use it in GitHub Desktop.

Select an option

Save hulufei/9063571 to your computer and use it in GitHub Desktop.
Fixed element relative to it's parent

第一种方法

<div class="container">
  <div class="fixed">content</div>
</div>
.fixed {
  position: fixed;
  left: 50%;
  bottom: 100px;
  margin-left: (containerWidth / 2);
}

第二种方法

<div class="container">
  <!-- 兼容IE和Safari,多加一个div -->
  <div>
    &nbsp;<div class="fixed">content</div>
  </div>
</div>
.container {
  text-align: right;
}
.fixed {
  position: fixed;
  _position: absolute;
  bottom: 100px;
}

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment