如下所示,您可以將-10px
的margin-top
添加到.blackkey
類,方法是將其他邊保留爲1px並只更改頂層值。
.blackKey {
background: #232323;
display: inline-block;
position: absolute;
width: 75%;
height: 20px;
color: #eee;
z-index: 10;
margin: -10px 1px 1px 1px; /*Add this*/
}
#keyboard {
\t width: 100%;
\t left: 0px;
\t position: relative;
\t display: inline-block;
\t background: black;
\t height: 100%;
}
.whiteKey {
\t background: #FCFBE3;
\t display: inline-block;
\t position: relative;
\t width: 100%;
\t margin: 1px;
\t height:30px;
\t text-align: right;
}
.blackKey {
\t background: #232323;
\t display: inline-block;
\t position: absolute;
\t width: 75%;
\t height: 20px;
\t color: #eee;
\t z-index: 10;
\t margin: -10px 1px 1px 1px;
}
<div id="keyboard">
\t <span class="whiteKey">C</span>
\t <span class="blackKey">C#</span>
\t <span class="whiteKey">D</span>
\t <span class="blackKey">D#</span>
\t <span class="whiteKey">E</span>
\t <span class="whiteKey">F</span>
\t <span class="blackKey">F#</span>
\t <span class="whiteKey">G</span>
\t <span class="blackKey">G#</span>
\t <span class="whiteKey">A</span>
\t <span class="blackKey">A#</span>
\t <span class="whiteKey">B</span>
</div>
感謝。這正是我所期待的,從經驗來看,我們有些奇怪,CSS不是針對訂單的。我能夠在位置前應用左/右屬性。 – Jacob
相同的屬性將始終覆蓋之前的聲明。在這種情況下,'margin:1px'只是四個聲明'margin-top:1px; margin-right:1px; margin-bottom:1px; margin-left:1px',並將始終覆蓋之前的保證金值,即「margin-top」。 – Marvin