假设我在 foo.html 文件中有这样的文本:
All the World's a Stage
All the world's a stage,
And all the men and women merely players;
They have their exits and their entrances,
And one man in his time plays many parts,
His acts being seven ages. At first, the infant,
Mewling and puking in the nurse's arms.
Then the whining schoolboy, with his satchel
And shining morning face, creeping like snail
Unwillingly to school. And then the lover,
Sighing like furnace, with a woeful ballad
Made to his mistress' eyebrow.
注意空格和换行符。我可以把它放到 div
中吗?并保留它与 css 的原样?而不是像放入 HTML 页面时那样将所有文本放在一起:
All the World's a Stage All the world's a stage, And all the men and women merely players; They have their exits and their entrances, And one man in his time plays many parts, His acts being seven ages. At first, the infant, Mewling and puking in the nurse's arms. Then the whining schoolboy, with his satchel And shining morning face, creeping like snail Unwillingly to school. And then the lover, Sighing like furnace, with a woeful ballad Made to his mistress' eyebrow.
我想要的是非常接近 <pre>
的行为标签,我想。
最佳答案
将 white-space: pre
应用到 div
元素上。
Sequences of whitespace are preserved, lines are only broken at newline characters in the source and at <br> elements.
文档:https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
关于html - 我可以用文本 "as-is"制作一个 css div 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28815926/