Given a region where the line-height and any margins are n
, and the region has a height that is a multiple of n
, and the scrollTop is increased by mult
It appears this has to do with descenders (qjpg) "sticking out" down below the line-height box. Firefox and Safari seem to agree on how to do this - characters are allowed to stick out. However by exaggerating the sizes x 10, we notice something interesting for sans-serif
.
In Mac OS X, both Safari and Firefox chose helvetica
as the typeface for sans-serif
. But Firefox moves that particular typeface upwards in the line-height box, so the bottom doesn't "stick out". Compare with arial
- microsoft's bastardization of helvetica
where both browsers let it stick out.
I think the solution to your problem is to find a "reasonable" negative margin to offset the content upwards. It seems both helvetica
and arial
have some "wiggle room" at the top of the box. I would use #wrapper #content { margin-top: -1px; }
(selector extra strong to overcome #wrapper *
) for the specific font-size
/line-height
in your example.
Here's my test code. It shows that the "sticking out" can be much worse for geneva
and verdana
.
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>descender issue</title>
<style>
* {
margin: 0;
padding: 0;
}
.content {
margin-bottom: 30px;
background: #ffdd88;
font-size: 190px;
line-height: 210px;
}
#content1 {
font-family: sans-serif;
}
#content2 {
font-family: arial;
}
#content3 {
font-family: geneva;
}
#content4 {
font-family: helvetica;
}
#content5 {
font-family: 'trebuchet ms';
}
#content6 {
font-family: verdana;
}
#content7 {
font-family: serif;
}
#content8 {
font-family: times;
}
</style>
</head>
<body>
<h1>descender issue</h1>
sans-serif
<div class="content" id="content1">
lfgjpq
</div>
arial
<div class="content" id="content2">
lfgjpq
</div>
geneva
<div class="content" id="content3">
lfgjpq
</div>
helvetica
<div class="content" id="content4">
lfgjpq
</div>
trebuchet ms
<div class="content" id="content5">
lfgjpq
</div>
verdana
<div class="content" id="content6">
lfgjpq
</div>
serif
<div class="content" id="content7">
lfgjpq
</div>
times
<div class="content" id="content8">
lfgjpq
</div>
</body>
</html>
Have you tried increasing line height by 1px? I tried it in Chrome and it works:
line-height: 22px;
height: 220px; /* A multiple of line height */
and:
document.getElementById('wrapper').scrollTop += 220;
Alternatively you can reduce the font size by 1px which also works for me:
/* alternative solution: */
font-size: 18px;