/    Sign up×
Community /Pin to ProfileBookmark

best practice: Elements with different font-families

Hello there!

I have a question regarding best practices when it comes to deal with HTML elements with different font-families.

Would you use one class per family and apply them to the HTML elements or would you group the HTML elements by font-families?

This is an example using one class per font-family.

[code]
<head>
<style>
.roboto {
font-family: ‘Roboto’, sans-serif;
}

.alegrya {
font-family: ‘Alegrya’, sans-serif;
}

.anek {
font-family: ‘Anek’, sans-serif;
}
</style>
</head>
<body>
<h1 class=”heading-1 anek”>Heading</h1>
<span class=”description roboto”>A description</span>
<p class=”paragraph alegrya”>More text</p>
<span class=”colophon roboto”></span>
<h2 class=”heading-2 anek”>Main titles</h2>
<p class=”long-paragraph roboto”>Much more text</p>
</body>
[/code]

And this is an example grouping HTML elements by family

[code]
<head>
<style>
.heading-1, .heading-2 {
font-family: ‘Anek’, sans-serif;
}

.description, .colophon, .long-paragraph {
font-family: ‘Roboto’, sans-serif;
}

.paragraph {
font-family: ‘Alegrya’, sans-serif;
}
</style>
</head>
<body>
<h1 class=”heading-1″>Heading</h1>
<span class=”description”>A description</span>
<p class=”paragraph”>More text</p>
<span class=”colophon”></span>
<h2 class=”heading-2″>Main titles</h2>
<p class=”long-paragraph”>Much more text</p>
</body>
[/code]

I personally prefer the first approach, but I want to know your opinion and if you have any other better approach.

Thanks a lot!

to post a comment
CSS

5 Comments(s)

Copy linkTweet thisAlerts:
@tracknutJan 19.2023 — As a general rule, class names should not tie to the styling. The common example is:

.green {color:green}
[/quote]

Instead, the class names should correspond to the content being displayed, maybe:

.environmental {color:green}
[/quote]

So I would always lean toward your second option unless by some fluke you are developing a "font site" where those headings and paragraphs are describing different font styles.

Note, in your second version you also, with "heading-1 and 2" are breaking a rule, and can likely just apply the styles to h1 and h2 or some more relevant heading name.
Copy linkTweet thisAlerts:
@NogDogJan 19.2023 — Not my area of expertise, but I'd prefer the 2nd approach, on the assumption that if someone decides at some point to change the font for a set of elements, you would just need to change one thing in the style section, as opposed to either changing the classes of a bunch of HTML tags, or having those class names be misleading. I.e., consider maintenance costs as well as initial development costs. :) YMMV
Copy linkTweet thisAlerts:
@spinhigh20Jan 20.2023 —  HTML elements that can have different font-families include the following:

<p> - paragraph

<h1> - heading level 1

<h2> - heading level 2

<h3> - heading level 3

<h4> - heading level 4

<h5> - heading level 5

<h6> - heading level 6

<span> - a small portion of text within a paragraph

<div> - a container for HTML elements, which can have its own CSS properties

<a> - a link

You can set the font-family for these elements using CSS. For example, p { font-family: Arial; } will set the font-family of all paragraphs to Arial.
Copy linkTweet thisAlerts:
@hebrerilloauthorJan 26.2023 — Hi guys, thank you for your responses!!

@tracknut#1650067 , what did you mean by "you also, with heading-1 and 2 are breaking a rule". What kind of rule am I breaking?
Copy linkTweet thisAlerts:
@tracknutJan 26.2023 — @jessPHP#1650222 I'm having trouble coming up with the right words for it this morning, so my apologies in advance if this rambles...

A class called "heading-1" is adding no information other than to tell you that it applies to something you're calling the first heading. You already have that, it's the h1 element itself. So if you really want to style all heading-1's, just style the h1 and don't use a class at all.

On the other hand, if you only want to style a few of your h1's, then I ask "what differentiates those few from the others?". Maybe they's headings for recipes, but not for prose? If that's the case, then I would likely have html that looks like:
<i>
</i>&lt;section class="recipe"&gt;
&lt;h1&gt;Chicken Soup&lt;/h2&gt;
etc.
&lt;/section&gt;

with CSS:
<i>
</i>.recipe h1 {color:blue}


So the point is that class (and ID) names should correspond to the content on the page, and not to the layout or styling settings themselves.

I hope that helps.
×

Success!

Help @hebrerillo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...