0

I need a header 1 element that stays fixed at the top of the screen and has a relative position. Here are the HTML elements:

<h1 class="sticky">Text goes here</h1>

And here is the CSS:

.sticky {
    top: 0;
    text-align: center;
    position: relative;
    position: fixed;
    /* I know I cannot add multiple */
    /* position properties, so my */
    /* question really is what should */
    /* I do from here? */
}

For some reason, that doesn't seem to work. I either use sticky, or relative. Is there a fix for this?

1 Answers1

0

The text is being centred... the problem is that using position:fixed makes the div the same width as the text, so there is no extra space around it so it doesn't appear to be centred.

All you need to do is make the div the full width of the screen, then the text will be centred across the full width rather than in a smaller container.

.sticky {
    text-align: center;
    position: fixed;
    /* rest of your CSS here... */
}

You can see it in this Working Snippet:

.sticky {
    top: 0;
    width:100%;
    text-align: center;
    position: fixed;
}
<h1 class="sticky">Text goes here</h1>
FluffyKitten
  • 13,824
  • 10
  • 39
  • 52