I\'m trying to create a piano keyboard that will keep it\'s elements ratios using flex box, but can\'t seem to make the black notes stay the same width or height once I star
Not sure if you need your black keys to be independent blocks but here is an example
<body>
<div class="note-container">
<div class="white-note black-note"></div>
<div class="white-note black-note"></div>
<div class="white-note"></div>
<div class="white-note black-note"></div>
<div class="white-note black-note"></div>
<div class="white-note black-note"></div>
<div class="white-note"></div>
<div class="white-note black-note"></div>
<div class="white-note black-note"></div>
<div class="white-note"></div>
</div>
body {
background: #000;
}
.note-container {
width: 100%;
display: flex;
flex-flow: row nowrap;
}
.white-note {
position: relative;
width: 10%;
height: 400px;
background: #fff;
margin: 0 2px;
}
.white-note.black-note:after {
content: "";
position: absolute;
top: 0;
right: -25%;
width: 50%;
height: 50%;
background: #000;
z-index: 1;
}
The position should be relative rather than absolute. Also you should set both the height of the containing divs to 100%.
Refer to the Fiddle link: http://jsfiddle.net/924sefae/6/
#kbd {
height: 100%;
...
}
#kbd {
height: 100%;
...
}
.black {
position: relative;
...
}
Flexbox is not required for this. A responsive keyboard layout can be accomplished with:
<div class="key white c" </div>
<div class="key black c_sharp"</div>
<div class="key white d" </div>
<div class="key black d_sharp"</div>
<div class="key white e" </div>
<div class="key white f" </div>
<div class="key black f_sharp"</div>
<div class="key white g" </div>
<div class="key black g_sharp"</div>
<div class="key white a" </div>
<div class="key black a_sharp"</div>
<div class="key white b" </div>
.a, .b, .d, .e, .g, .black{
margin: 0 0 0 (-($blackKey_Width / 2) - $blackKey_BorderWidth);
}
.key{
float: left;
position: relative;
}
z-index
for the black keys:.white{
z-index: 1;
}
.black{
z-index: 2;
}
vw
, vh
, vmin
, vmax
] for the (height
& width
) attributes of the (.white
& .black
) selectors. You can make this even more dynamic by sizing the black keys as a percentage of the white keys. Percentage height is calculated based on the height of the first positioned parent. In this case, the #keys
and #kbd
divs don't have a position
CSS rule. This means the black keys are scaled based on the width of the body, instead of their direct parent.
Add position: relative;
to the #keys
div to make it work properly.