html 将段落的第一个字母放大

nafvub8i  于 2023-09-28  发布在  其他
关注(0)|答案(3)|浏览(122)

我正在做我的HTML作业。这是基本的CSS东西(外部样式表),我几乎完成了。一切都在按它应该的方式工作,除了一部分,我不知道为什么。

<article>
  <h2>About the Course</h2>
  <p>
    The Civil War and Reconstruction
    explores the causes and consequences of the American
    Civil War, covering American history from 1840 through 1876 in
    great detail. My primary goal is to interpret the multiple
    threads that run through this epic event and consider how these
    threads still engage the politics and culture of the
    present day. In this course we will rely heavily on primary
    texts, interpreting the events of the day through the words of
    those men and women who experienced it. We'll examine four main
    points of interest:
  </p>
</article>

因此,在本作业的21个要求中,#15是:
对于article元素中<h2>标题之后的第一个段落,创建一个样式规则,以32像素的字体大小显示第一个字母。
所以我是这样做的:

article h2 p:first-of-type:first-letter {
    font-size: 32px;
}

这不起作用,所以我把它改为:

article > h2 p:first-of-type:first-letter {
   font-size: 32px;
}

但还是不管用

jvlzgdj9

jvlzgdj91#

该段落不是<h2>的后代,而是在它之后。把你的CSS改成:

article h2 + p:first-letter {
    font-size: 32px;
}

+(相邻兄弟)选择器将直接选择h2之后的第一个p元素。有一个很好的StackOverflow答案进一步详细解释了这一点here
此外,您也不需要:first-of-type伪选择器,因为+只选择第一个元素。

hzbexzde

hzbexzde2#

article h2 + p:first-letter {
    font-size: 32px;
}
iyfamqjs

iyfamqjs3#

当使用font-size: 32px拉伸内容时,您可能会注意到第一句话和其余内容之间存在轻微的间隙。

因此,建议使用initial-letter CSS属性来避免这种额外的间距。
请注意目前的limited browser support

article h2+p:first-letter {
  font-size: 32px;
}

p:nth-of-type(2):first-letter {
  /* Initial letter occupies 2 lines and sinks 1 line */
  initial-letter: 2 1;
  color: red;
}
<article>
  <h2>About the Course</h2>
  <p>
    The Civil War and Reconstruction explores the causes and consequences of the American Civil War, covering American history from 1840 through 1876 in great detail. My primary goal is to interpret the multiple threads that run through this epic event and
    consider how these threads still engage the politics and culture of the present day. In this course we will rely heavily on primary texts, interpreting the events of the day through the words of those men and women who experienced it. We'll examine
    four main points of interest:
  </p>
  <p>
    The Civil War and Reconstruction explores the causes and consequences of the American Civil War, covering American history from 1840 through 1876 in great detail. My primary goal is to interpret the multiple threads that run through this epic event and
    consider how these threads still engage the politics and culture of the present day. In this course we will rely heavily on primary texts, interpreting the events of the day through the words of those men and women who experienced it. We'll examine
    four main points of interest:
  </p>
</article>

相关问题