In the following, when I combine font-size
and font-family
into font
, and then use JS to change the new-style
class, the line font-style: italic;
line is ignored:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Chapter 9, Example 5</title>
<style>
#divAdvert {
font: 12pt Arial;
}
.new-style {
text-decoration: underline;
font-style: italic;
}
</style>
</head>
<body>
<div id="divAdvert">
Here is an advertisement. Why doesn't this italicize?
</div>
<br><button onclick="clickMeh();">Click Meh</button>
<script>
function clickMeh() {
var divAdvert = document.getElementById("divAdvert");
divAdvert.className = "new-style";
}
</script>
</body>
</html>
But when I separate them, the line is italicized as expected:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Chapter 9, Example 5</title>
<style>
#divAdvert {
font-size: 12pt;
font-family: Arial;
}
.new-style {
text-decoration: underline;
font-style: italic;
}
</style>
</head>
<body>
<div id="divAdvert">
Here is an advertisement.
</div>
<br><button onclick="clickMeh();">Click Meh</button>
<script>
function clickMeh() {
var divAdvert = document.getElementById("divAdvert");
divAdvert.className = "new-style";
}
</script>
</body>
</html>
Why???????
UPDATE: Thanks to input from Evgeny and Michael, I managed to get it working simply by adding #divAdvert
in front of .new-style
(in the CSS) and I think that's the most correct answer. This preserves the ID and recognizes the fact that the new-style
class is entirely dependent on the ID. Codepen here.