/    Sign up×
Community /Pin to ProfileBookmark

Font size change

In the following FAQ code,
why does the font size not get changed in the ‘.ans’ class display?

I have the ‘.faq’ class font size set larger than the ‘.ans’ font size,
but it appears that the ‘.ans’ class inherits the larger setting.
Is that as expected or what am I doing wrong to have
the answer font size smaller than the question font size.

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />

<title> css FAQ </title>
<style>
p { color: black; }
div.faq { margin-top: 1em; cursor: pointer; font-size: 1.25em; }
.ans { font-size: 1em; color: red; }
.hide { display: none; }
</style>

</head>
<body>
<h1>FAQ Page (no nesting)</h1>

<div class=”faq”> FAQ 1 <div class=”hide ans”>Answer 1</div> </div>

<div class=”faq”> FAQ 2
<div class=”hide ans”>Answer 2
<p> <i>Additional</i> information about answer </p>
<p> <b>And even more</b> <i>additional</i> information about answer </p>
</div>
</div>

<div class=”faq”> FAQ 3 <div class=”hide ans”>Answer 3</div> </div>

<div class=”faq”> FAQ 4 <div class=”hide ans”>Answer 4</div> </div>

<div class=”faq”> FAQ 5 <div class=”hide ans”>Answer 5</div> </div>

<script>
function init() {
var sel = document.querySelectorAll(‘.faq’);
for (var i=0; i<sel.length; i++) {
sel[i].addEventListener(‘click’, function() { this.firstElementChild.classList.toggle(‘hide’); });
}
} init();

</script>

</body>
</html>
[/code]

to post a comment
CSS

4 Comments(s)

Copy linkTweet thisAlerts:
@cootheadMay 26.2017 — Hi there JRMKER,

[indent]

you need a different mindset when using the [i]"em unit"[/i]

with respect to it's child elements. ?

Setting a child element's font-size to 1em in the CSS,

is to give it the the same size as that of it's parent

In your code example the [i]font-size[/i] requirement of the

child element is 4/5ths of it's parent's [i]font-size[/i].

This means that it should be set to [i]font-size: 0.8em[/i].

This example may help...

[color=navy]
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;style media="screen"&gt;
body {
font: 1em/ 1.62em verdana ,arial, helvetica, sans-serif;
}

.test1, .test2 {
padding: 0.5em;
margin: 0.5em 0;
border: 0.062em solid #999;
font-size: 1.25em;
}
.test2 {
font-size: 0.8em;
}

#child {
font-size: 1em;
}

#child1 {
font-size: 0.8em
}

#child2 {
font-size: 1.25em
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div&gt;This is font-size: 1em&lt;/div&gt;

&lt;div class="test1"&gt;
This is font-size: 1.25em
&lt;div id="child"&gt;This child is font-size: 1.25em, because it is set to 1em&lt;/div&gt;
&lt;/div&gt;

&lt;div class="test1"&gt;This is font-size: 1.25em
&lt;div id="child1"&gt;This child is font-size: 1em, because it is set to 0.8em&lt;/div&gt;
&lt;/div&gt;

&lt;div class="test2"&gt;This is font-size: 0.8em
&lt;div id="child2"&gt;This child is font-size: 1em, because it is set to 1.25em&lt;/div&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;[/color]


[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@JMRKERauthorMay 26.2017 — Thank you,, 'coothead'.

That was most informative.

Just when I think I know what I'm doing,

as my wife often points out, I'm wrong.

?
Copy linkTweet thisAlerts:
@cootheadMay 26.2017 — Hi there JMRKER,

[indent]

I don't have a wife, but I do have [i]"deathshadow"[/i]

to point out the error of my ways. ?

The [i]"em unit"[/i] does take a little while to master,

but the effort should be rewarding. ?

[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@auntniniMay 26.2017 — could % percentages and/or keywords help?

see https://css-tricks.com/almanac/properties/f/font-size/

The font-size property specifies the size, or height, of the font. font-size affects not only the font to which it is applied, but is also used to compute the value of em, rem, and ex length units.

p {

font-size: 20px;

}

font-size can accept keywords, length units, or percentages as values. It&#8217;s important to note however that when it&#8217;s declared as part of the font shorthand property, font-size is a mandatory value. If it is not included in the shorthand, the entire line is ignored.

Length values (e.g. px, em, rem, ex, etc) which are applied to font-size cannot be negative.

#Absolute keywords and values

.element {

font-size: small;

}

It accepts the following absolute keyword values:

xx-small

x-small

small

medium

large

x-large

xx-large

These absolute values are mapped to specific font sizes as computed by the browser. But you may also use two keyword values which are relative to the font size of the parent element.

Other absolute values include mm (millimeters), cm (centimeters), in (inches), pt (points) and pc (picas). One point is equal to 1/72 of an inch whilst one pica is equal to 12 points &#8212; these values are typically used for printed documents.

#Relative keywords

.element {

font-size: larger;

}

larger

smaller

For example, if the parent element has a font size of small, a child element with a defined relative size of larger will make the font size equal to medium for the child element.

#Percentage values

.element {

font-size: 110%;

}

Percentage values, such as setting a font-size of 110%, are also relative to the parent element&#8217;s font size as shown in the demo below:


#The em unit

.element {

font-size: 2em;

}

The em unit is a relative unit based on the computed value of the font size of the parent element. This means that child elements are always dependent on their parent to set their font-size. For example:

<div class="container">

<h2>This is a heading</h2>

<p>This is some text.</p>

</div>

.container {

font-size: 16px;

}

p {

font-size: 1em;

}

h2 {

font-size: 2em;

}

In the example above, the paragraph will have a font-size of 16px because 1 x 16 = 16px whereas the heading will be 32px because 2 x 16 = 32px. There are many advantages to scaling type up depending on the font-size of the parent element, namely we can wrap elements within a container and know that all of the children will always be relative to one another:


#The rem unit

In the case of rem units, however, the font-size is dependent on the value of the root element (or the html element).

html {

font-size: 16px;

}

p {

font-size: 1.5rem;

}

In the above example, the rem unit is equal to 16px (because it is inherited from the html/root element) and thus the font size for all paragraph elements will compute to 24px (1.5 x 16 = 24). Unlike em units, the paragraph will ignore the styling of all its parents besides the root.
[/quote]
×

Success!

Help @JMRKER 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.29,
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,
)...