Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a pseudo-element fixed within a scrolled element?

How can I get this effect:

<pre class="default prettyprint prettyprinted" data-codetitle="homepage.html" style=""><code>
body{
  position: relative; 
  @include background(linear-gradient(left, lighten($me, 11%), lighten($me, 11%) 
    $half, $darkbackground $half, $darkbackground));
  color: $text;
  width: 100%;
  height: 100%;
  @include breakpoint(baby-bear) {
    @include background(linear-gradient(left, lighten($me, 11%), lighten($me, 11%) 
    45%, $darkbackground 45%, $darkbackground));
  }
}
</span></code></pre>

I need to use the data tag as a heading:

.prettyprint {
    margin: 0 0 0 0.5%;
    border-left: 2px solid #ce8f80;
    overflow: auto;
    position: relative;
    width: 300px;
}
.prettyprint:before {
    content: attr(data-codetitle);
    background: #ce8f80;
    display: block;
    width: 100%;
}

This is the result. The problem is that when you scroll, the :before element scrolls as well. Is there a way to keep this element fixed. I tried different variations but I cant get it to work.

Thanks

like image 711
aurel Avatar asked Mar 30 '13 18:03

aurel


People also ask

How do you make an element not move on scroll?

And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

How do I keep my Div fixed when scrolling?

Use position: fixed instead of position: absolute .

Which position will keep the element in the same place regardless of scrolling?

An element with position:fixed is fixed with respect to the viewport. It stays where it is, even if the document is scrolled.

Are pseudo elements clickable?

The answer is no.


1 Answers

For browsers that supports position: sticky

.prettyprint {
  margin: 0 0 0 0.5%;
  border-left: 2px solid #ce8f80;
  overflow: auto;
  position: relative;
  width: 300px;
}

.prettyprint:before {
  position: sticky;
  left: 0;
  content: attr(data-codetitle);
  background: #ce8f80;
  display: block;
  width: 100%;
}
<pre class="default prettyprint prettyprinted" data-codetitle="homepage.html" style="">
  <code>
        body{
          position: relative; 
          @include background(linear-gradient(left, lighten($me, 11%), lighten($me, 11%) 
          $half, $darkbackground $half, $darkbackground));
          color: $text;
          width: 100%;
          height: 100%;
          @include breakpoint(baby-bear) {
            @include background(linear-gradient(left, lighten($me, 11%), lighten($me, 11%) 
            45%, $darkbackground 45%, $darkbackground));
          }
        }
  </code>
</pre>
like image 60
yrstyre Avatar answered Nov 09 '22 17:11

yrstyre