html - 在什么情况下使用 position :absolute, position :relative, position:fixed 和 position:static in css

标签 html css

我刚开始使用 html5 和 css,我对使用 css position 有点困惑。

我可以从堆栈溢出中找到这些链接,

Difference between relative and absolute

When to use CSS positioning?

Position absolute but relative to parent

我仍然无法解决,我需要通过示例了解所有位置属性,以及在什么情况下必须使用哪个位置属性来开发网页。

先生,你能帮帮我吗?

任何帮助将不胜感激。提前致谢。

最佳答案

.parentClassDiv{
  position:relative;
  border-color:red;
  border-radius:4px;
  border-style:solid;
  height:150px;
  top:100px;
  }
.relativeClass{
  position:relative;
  border-color:green;
  border-radius:4px;
  border-style:solid;
  top:80px;
  left:30px;
  }
.absoluteClass{
  position:absolute;
  border-color:blue;
  border-radius:4px;
  border-style:solid;
  top:120px;
  }
.fixedClass{
  top:30px;
  position:fixed;
  border-color:yellow;
  border-radius:4px;
  border-style:solid;
  left:10px;
  }
.staticClass{
  top:10px;
  left:10px;
  position:static;
  border-color:brown;
  border-radius:4px;
  border-style:solid;
  }
<html>
  
  <head>
  <title>difference b/w relative,absolute,fixed,static</title>
  </head>
  <body>
  <div class='parentClassDiv'>
    <span class='relativeClass'>hello..I am relative..positioned top-40px and left:30px..relative to my parent div(red color)</span>
    <span class='absoluteClass'>hello..I am absolute</span>
    <span class='fixedClass'>hello..I am Fixed..I will be positioned from the page top(i.e:it parent always be html tag)</span>
    <span class='staticClass'>I am Static.even top is 100px.i will be positioned on my place in my parent tag(red color).i will not take top,bottom,left,right.and also i started here only because green color is relative (reserved it place and moved to bottom)</span>
  </div>
  </body>
  
</html>

关于html - 在什么情况下使用 position :absolute, position :relative, position:fixed 和 position:static in css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942504/

相关文章:

html - 导航栏覆盖切换按钮

javascript - 单击 div 元素后如何显示更多文本?

php - 为响应式媒体查询切换 php 字符限制

javascript - 删除非 SVG slider

css - 中间带圆圈的单杠

javascript - 图像相互叠加的动画

javascript - 我如何使用 Jquery 调整三 Angular 形的大小

html - Firefox 中的输入控件溢出 flex 容器

css - 选择部分显示所选选项

html - 通过代码我无法在 iFrame 中生成文本