简体   繁体   English

CSS:设置文本框样式以仅显示一个段落

[英]CSS: style a text box to only display one paragraph

I have a text box on a page that I'm going to style to dynamically include content via dust.js : 我在页面上有一个文本框,我将对其设置样式,以通过dust.js动态包含内容:

<div class="box">
   {Lorem}
</div>

It will be populated with content that will already have HTML tags in it: 它将填充其中已经具有HTML标签的内容:

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

I want to style the box class so that only the first paragraph of the loripsum text is displayed. 我想为box类设置样式,以便仅显示loripsum文本的第一段。 Is there a way to do that easily in Stylus/CSS? 有没有办法在Stylus / CSS中轻松做到这一点?

Use this selector: 使用此选择器:

div.box p:first-child

Example: (shows only the first paragraph) 示例:(仅显示第一段)

<style>
    div.box p:not(:first-child){display:none;}
</style>
<div class="box">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

EDIT 编辑


Try this: 尝试这个:

.box > p:first-of-type
    visibility visible

Although the other answer picked the obvious solution, while testing certain styles were not being applied correctly. 尽管另一个答案选择了明显的解决方案,但在测试某些样式时未正确应用。 This must be something browser specific. 这必须是特定于浏览器的。 So keep that in mind. 所以记住这一点。 The first-of-type selector seems to work best for me. first-of-type选择器似乎最适合我。 But again, it may not work on every browser. 但同样,它可能无法在所有浏览器上都起作用。 Also, I recommend you set everything in box as hidden and use either selector to make the first paragraph as visibility of visible . 另外,我建议您将box所有内容都box为隐藏,然后使用任一选择器将第一段设置为visible Fiddle of what I mean included as an example. 我的意思是小提琴作为例子。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM