Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 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 Answer

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;
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...