There are a few CSS selectors or properties that I think are rarely used in the wild, but they actually have been in existence since the days of CSS1; some them are including the
:first-line
and :first-letter
pesudo-elements.
How to use?
These pseudo-elements basically work similar to their siblings – :before and :after – and I think they are also quite straightforward. The
:first-letter
will target the first letter or character of a selected element, this pseudo-element is commonly used to create a typographical effect like a drop cap. Here is how it’s done.
1
2
3
| p:first-letter { font-size : 50px ; } |
This code results in the following presentation.
A few things should be noted, however, this effect will only apply when the first character is not preceded by another element, for instance, an image. Additionally, when we have some of the same targeted elements in a row, all of them will also be affected.
Further, in terms of the
:first-line
, this pseudo-element will target the first line of targeted element, this example below shows how we bold the first line of paragraph.
1
2
3
| p:first-line { font-weight : bold ; } |
Like the previouss code of
:first-letter
, this will also affect all of the first lines in paragraph elements there are in the page.
So, in real cases, when we generally want to add drop cap or alter the first lineonly on the first paragraph we need to be more specific – either by adding an extra class attribute or applying these pseudo-elements along with
:first-child
or :first-of-type
selector, like so.
1
2
3
4
5
6
| p:first-child:first-letter { font-size : 50px ; } p:first-child:first-line { font-weight : bold ; } |
There we go, the affected paragraph is now only the first one.
Paragraph Dropcap with CSS’s :first-line and :first-letter Elements
Reviewed by JohnBlogger
on
3:44 PM
Rating:
No comments: