0

I'm making a fancy page that need to have some text centered, i can't use the text-align: center; thing because i have a position: absolute;. So now i need the be able to center the text horizontaly with the calc() function instead (also because i want to do some other fancy stuff with it).

This is what i tried and didn't work: CSS:


#center {
    position: absolute;
    margin-left:calc(100vw /2 - attr(length) /2);
}

HTML:

[...]

<div id="center">test</div>

[...]

As shown above I've tried using attr(), but I don't know which value to put inside to obtain what i want (and infact firefox refuse to make it work)

In the answer please consider that text will change on the fly. The text shoud be always centered.

hope you understood and can help

Correia7
  • 364
  • 3
  • 10

1 Answers1

1

you can try this. Hope it will be helpful to you.

#center {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}
<div id="center">test</div>
Showrin Barua
  • 1,737
  • 1
  • 11
  • 22