Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

html - ::before and ::after position absolute acting like position fixed

So I am trying to position my pseudo element with position absolute, but instead, it is acting like its parent is something else.

<!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>

i got that from w3school + some editing, because I needed to be sure it wasn't just my html that caused this.

what i want is for the pseudo elements to use the p tag as their parent and not the div

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport

w3schools example here

My Example here

  p{
       position:relative;
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...