Preserve font spacing for a line of text split into separate HTML elements
P粉258083432
P粉258083432 2023-09-10 18:37:01
0
1
585

I'm trying to recreate a common technique of splitting words into separate HTML elements so that they can be positioned individually via animation.

The problem I'm having is that I lose the spacing between the words when they are split into separate elements. I can add spacing by rendering withafter each word, or by adding padding to each element, but I'd like to know if there is a way to do this without manually adding spacing/ Curving them onto the container and retaining the original spacing?

I know there are plugins like gsap SplitText, but I was hoping for a non-plugin solution.

Code pen is here.

const App = () => { const line = 'this is a line of text.'; const renderLine = line.split(' ').map((word, i) => { return ( {word}  ) }) return ( 

{renderLine}

) }

P粉258083432
P粉258083432

reply all (1)
P粉384679266

You can use the regular expression/\b/This will search for word boundaries and split lines by words and spaces.

function App() { const line = "this is a line of text."; const renderLine = line.split(/\b/).map((word, i) => { return {word}; }); return 

{renderLine}

; } ReactDOM.createRoot(document.getElementById("root")).render();
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!