html -::之前和::之后绝对位置就像固定位置一样

标签 html css

所以我试图用绝对位置定位我的伪元素,但相反,它的行为就像它的父元素是其他东西一样。

<!DOCTYPE html>
<html>
<head>
<style>
p::after { 
content: " - Remember this";
position: absolute;
top: 0;
}
</style>
</head>
<body>

<div style="position: absolute; top: 50px; left: 50px; border: 1px solid 
black;">
<p>My name is Donald</p>
<p>I live in Ducksburg</p>

<p><b>Note:</b> For this selector to work in IE8, a DOCTYPE must be declared, 
and you must use the old, single-colon CSS2 syntax (:after instead of 
::after).</p>
</div>

</body>
</html>

我从 w3school 得到了这个 + 一些编辑,因为我需要确定这不仅仅是我的 html 造成的。

我想要的是伪元素使用 p 标签作为其父元素而不是 div

最佳答案

位置为绝对的元素;相对于最近定位的祖先定位(而不是相对于视口(viewport)定位

w3schools example here

My Example here

  p{
       position:relative;
  }

关于html -::之前和::之后绝对位置就像固定位置一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52133597/

相关文章:

html - 嵌套表不占用父 td 的 100% 高度

html - 在 Div 中添加 inApp 浏览器

c++ - 用 C++ 解析 <li> 列表

html - 像 github 404 这样的响应式背景图片

css - 省略号不适用于长 URL

javascript - 仅在第一次加载页面时如何执行 Javascript 警报

javascript - 表单提交按钮在 Firefox 中不起作用,但在 Chrome 或 IE 中起作用

javascript - js给dom元素一一添加多个class

html - 如何使用 CSS/HTML 制作 Facebook 封面?

css - 带有边距和 ul 的基本 css 定位问题