如何使用CSS設計文字段落的首字母?


我們在許多書籍的各個章節中都看到過關於如何使用CSS設計單詞首字母的指導。在CSS中,我們可以使用`::first-letter`偽元素來設計文字段落的首字母。這允許我們將特定樣式應用於塊級元素第一行的首字母。例如,我們可以使首字母比其餘文字更大、使用不同的字型或不同的顏色。

語法

p::first-letter{
   property_name : value_name;
}

`first-letter` − `first-letter`被定義為用於設計段落首字母的偽元素。

使用的屬性

示例中使用的屬性如下:

  • `font-style` − 定義文字的樣式。

  • `color` − 定義文字的顏色。

  • `text-align` − 定義文字的水平對齊方式。

  • `font-size` − 定義文字的大小。

示例

在這個例子中,我們將使用內聯CSS的屬性來設計文字段落的首字母。`p`元素將在HTML中建立段落。段落的主題名稱將使用`h1`元素提及,並透過`font-style`、`color`和`text-style`設定屬性。為了設計段落中的首字母,它將使用偽元素,即`p::first-letter`。這個偽元素使用`font-size`和`color`屬性建立更大的首字母。

<!DOCTYPE html>
<html>
   <title>Design initial letter of text paragraph</title>
   <head>
      <style>
         h1{
            font-style: italic;
            color: darkblue;
            text-align: center;
         }
         p{
            font-size: 20px;
         }
         p::first-letter{
            font-size: 80px;
            color: #FF0000;
         }
         p.new_style:first-letter {
            font-size: 100px;
            display: block;
            float: left;
            line-height: 0.5;
            margin: 15px 15px 10px 0;
            color: blue;
         }
         h3{
            color: darkgreen;
            font-style: italic;
         }
      </style>
   </head>
   <body>
      <h1>The story of Anne Frank</h1>
      <p><i>
         Anne Frank was born on June 12, 1929, in Frankfurt am Main, Germany. Edith 
         and Otto Frank were her parents. Anne spent her first five years of life in 
         a flat on the outskirts of Frankfurt with her parents and older sister, 
         Margot. Otto Frank moved to Amsterdam in the Netherlands, where he had 
         links in the business world, after the Nazis took over in 1933. The 
         remainder of the Frank family arrived shortly after, with Anne arriving 
         last in February 1934 after spending time with her grandparents in 
         Aachen.</i></p>
      <h2>One more new format style of first letter</h2>
      <p class="new_style"> Anne Frank was born on June 12, 1929, in Frankfurt 
         am Main, Germany. Edith and Otto Frank were her parents. Anne spent her 
         first five years of life in a flat on the outskirts of Frankfurt with her 
         parents and older sister, Margot. Otto Frank moved to Amsterdam in the 
         Netherlands, where he had links in the business world, after the Nazis took 
         over in 1933. The remainder of the Frank family arrived shortly after, with 
         Anne arriving last in February 1934 after spending time with her 
         grandparents in Aachen.<p>
      <h3>@tutorialspoint.com</h3>
   </body>
</html>

結論

我們看到了如何使用文字段落首字母的樣式。`::first-letter`被稱為偽元素,我們透過使用幾種樣式(例如更大的字型大小、不同的字型或不同的顏色)使首字母脫穎而出。因此,我們就是這樣使用CSS設計文字段落的首字母。

更新於:2023年5月10日

2K+ 閱讀量

啟動你的職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.