I’m trying to write a simple HTML page to output music chords. I display randomly generated chords to practice chord changes (like using flashcards).
When I output a chord containing a "flat" symbol, the whole chord appears to jump down — it increases the vertical height of the enclosing <div>
and appears at the bottom. The Other chords (with a "sharp" or without) appear as expected.
JSFiddle to demonstrate: https://jsfiddle.net/blatwell9/qjfcLtdv/3/
Is there a way to prevent the shift down from happening?
Shift down example
The code to demonstrate the effect. Note the effect when the backgrounds of the <div>
and the chord <span>
are displayed:
body {
font-family: sans-serif;
}
.chord-div {
font-size: 3em;
background: lightgreen;
margin: 0.2em;
float: left;
}
.chord-div span {
background: lightblue;
border: 1px blue solid;
}
<div class="chord-div">
<span>A</span>
</div>
<div class="chord-div">
<span>A♯</span>
</div>
<div class="chord-div">
<span>A♭</span>
</div>
2
Answers
You need to define
line-height
. In this case1
works.Instead of wasting time explaining why not to use float see this post. The example has two
<section>
s:The first
<section>
contains only inline elements (ex.<span>
,<mark>
,<strong>
, etc.). They naturally sit next to each other.The second
<section>
contains block elements (ex.<div>
,<p>
,<section>
, etc.). They naturally take up the whole line they sit in so they stack one on top off the other.float
is supposed to arrange them inline.As you can see the floating
<div>
s are cock-eyed and the ♭ symbol has nothing to do with it. Just use inline elements, flexbox or grid layouts.