2017-01-22 13 views

回答

0

確保添加margin-top: -10px更一般的margin: 1px後不會覆蓋它:

.blackKey { 
    /* ... */ 
    margin: 1px; 
    margin-top: -10px; 
} 

Thi s可以是combinedmargin: -10px 1px 1px 1pxmargin: -10px 1px 1px

Updated JSFiddle

+0

感謝。這正是我所期待的,從經驗來看,我們有些奇怪,CSS不是針對訂單的。我能夠在位置前應用左/右屬性。 – Jacob

+0

相同的屬性將始終覆蓋之前的聲明。在這種情況下,'margin:1px'只是四個聲明'margin-top:1px; margin-right:1px; margin-bottom:1px; margin-left:1px',並將始終覆蓋之前的保證金值,即「margin-top」。 – Marvin

0

如下所示,您可以將-10pxmargin-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>

+0

@Jacob希望它有效。 – frnt

+0

謝謝,它正常工作就像我需要它一樣。 – Jacob

+0

歡迎@Jacob做好投票。 – frnt

0

我更新了小提琴:因爲它更容易控制後使用:

#keyboard { 
 
    width: 100%; 
 
    background: black; 
 
} 
 
.whiteKey { 
 
    background: #FCFBE3; 
 
    width: 100%; 
 
    margin: 1px; 
 
    height:30px; 
 
    text-align: right; 
 
    position: relative; 
 
} 
 
.withBlack:after { 
 
    content: " "; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 20px; 
 
    background: #232323; 
 
    width: 75%; 
 
    height: 20px; 
 
    color: #eee; 
 
    z-index: 10; 
 
    margin: 1px; 
 
    text-align: left; 
 
} 
 
.keyC:after { 
 
    content: "C#"; 
 
} 
 
.keyD:after { 
 
    content: "D#"; 
 
} 
 
.keyF:after { 
 
    content: "F#"; 
 
} 
 
.keyG:after { 
 
    content: "G#"; 
 
} 
 
.keyA:after { 
 
    content: "A#"; 
 
}
<div id="keyboard"> 
 
    <div class="whiteKey withBlack keyC">C</div> 
 
    <div class="whiteKey withBlack keyD">D</div> 
 
    <div class="whiteKey">E</div> 
 
    <div class="whiteKey withBlack keyF">F</div> 
 
    <div class="whiteKey withBlack keyG">G</div> 
 
    <div class="whiteKey withBlack keyA">A</div> 
 
    <div class="whiteKey">B</div> 
 
</div>

+0

謝謝,作品非常好! – Jacob