How to keep a pseudo-element fixed within a scrolled element?
Asked Answered
A

2

8

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

About answered 30/3, 2013 at 18:9 Comment(0)
J
-4

Try this:

.prettyprint:before {
    content: attr(data-codetitle);
    background: #ce8f80;
    display: block;
    position: fixed;
    width: inherit;
}

Set position: fixed to keep the element from following the scroll, and then set width: inherit to keep the pseudo-element the same width as the parent element.

Fiddle reference: http://jsfiddle.net/audetwebdesign/r8aNC/2/

Josejosee answered 30/3, 2013 at 18:37 Comment(2)
Fixed positioning attaches the element to the viewport, not it's relative parent.Overtop
@Overtop You are correct on this point. In my fiddle, the content did not cause the page to scroll so I did not notice the side effect. I will give it a second look.Josejosee
P
5

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>
Patch answered 11/10, 2019 at 14:14 Comment(0)
J
-4

Try this:

.prettyprint:before {
    content: attr(data-codetitle);
    background: #ce8f80;
    display: block;
    position: fixed;
    width: inherit;
}

Set position: fixed to keep the element from following the scroll, and then set width: inherit to keep the pseudo-element the same width as the parent element.

Fiddle reference: http://jsfiddle.net/audetwebdesign/r8aNC/2/

Josejosee answered 30/3, 2013 at 18:37 Comment(2)
Fixed positioning attaches the element to the viewport, not it's relative parent.Overtop
@Overtop You are correct on this point. In my fiddle, the content did not cause the page to scroll so I did not notice the side effect. I will give it a second look.Josejosee

© 2022 - 2024 — McMap. All rights reserved.