如何在不接触任何 HTML 的情况下使用 :before :after 使用 CSS 设置 <Blockquote> 元素的样式

How to style <Blockquote> element with CSS using :before :after WITHOUT touching any HTML

提问人:Sam 提问时间:6/19/2022 最后编辑:Sam 更新时间:6/21/2022 访问量:891

问:

给定一个 HTML 引号,我希望 CSS 在不触及 HTML 的情况下自动换行在开始和结束引号内。

<blockquote><p>
I love you not only for what you are, but for what I am when I'm with you.
I love you not only for what you have made of yourself, but for what you are making of me.
I love you for ignoring the possibilities of the fool in me, and for accepting the possibilities of the good in me.
I love you for helping me to construct of my life not a tavern, but a temple.
</p></blockquote>

以下 CSS 有效,但仅部分有效:

blockquote {font-style: italic;}
blockquote:before {content: '“'; float:left;}
blockquote:after  {content: '”'; float:right;}

开头引号的位置正确,但结束引号不直接放在点所在的引号末尾之后。此外,报价在下一行看起来垂直错误。

以下是该问题的一个例子,引用了最近一部电影中的一句话:

enter image description here

如何以优雅的方式解决这个问题?谢谢!
随意在您的答案中尝试不同的简单或疯狂的布局,使这个布局成为社区使用的永恒解决方案。

html css 块引用

评论

1赞 Temani Afif 6/19/2022
删除 float 并添加到 p 元素display:inline
0赞 Sam 6/19/2022
@TemaniAfif太棒了!这解决了我的问题!谢谢。请添加为答案,以便我可以按答案投票!另外:如果您愿意,请随时提供一两个花哨的变体,说明您将如何为永恒的 css 定义 css 规则<blockquote>
0赞 Temani Afif 6/19/2022
你说的“永恒”是什么意思?
0赞 Sam 6/19/2022
我的意思是,你认为在2020年与1995年或2040年一样好的东西。
1赞 Temani Afif 6/19/2022
如果 HTML 代码不会更改,那么您可以将我的解决方案视为“永恒的”,因为我依赖于非常古老的属性,这些属性将永远以相同的方式工作

答:

2赞 Temani Afif 6/19/2022 #1

保持引号内联并使元素内联p

blockquote {
  font-style: italic;
}
blockquote > p {
  display:inline;
}
blockquote:before {
  content: '“';
}
blockquote:after {
  content: '”';
}
<blockquote>
  <p>
    I love you not only for what you are, but for what I am when I'm with you. I love you not only for what you have made of yourself, but for what you are making of me. I love you for ignoring the possibilities of the fool in me, and for accepting the possibilities
    of the good in me. I love you for helping me to construct of my life not a tavern, but a temple.
  </p>
</blockquote>

或以元素为目标p

blockquote {
  font-style: italic;
}
blockquote > p:before {
  content: '“';
}
blockquote > p:after {
  content: '”';
}
<blockquote>
  <p>
    I love you not only for what you are, but for what I am when I'm with you. I love you not only for what you have made of yourself, but for what you are making of me. I love you for ignoring the possibilities of the fool in me, and for accepting the possibilities
    of the good in me. I love you for helping me to construct of my life not a tavern, but a temple.
  </p>
</blockquote>

评论

0赞 Sam 6/19/2022
太棒了,谢谢!使用代替有什么好处?第一个会因为具有更高的特异性而具有更高的 CSS 处理速度吗?blockquote > p {}blockquote p {}>
0赞 Temani Afif 6/19/2022
@Sam两者都具有相同的特异性,这里没有特别的好处。我只是想有一个精确的选择器并针对直接的孩子