I'm using CSS to create a piano layout. I really like this "button pressed" effect, but it changes surrounding elements. How to avoid this situation?
<main> <div id="o-2" class="octave"> <button id="C2" class="C key"></button> <button id="C2-sharp" class="C-sharp key-black"></button> <button id="D2" class="D key"></button> <button id="D2-sharp" class="D-sharp key-black"></button> <button id="E2" class="E key"></button> </div> </main>
main { width: 100%; height: 100vh; display: flex; justify-content: center; align-items: center; } .octave { display: grid; grid-template-rows: 25% 25% 25% 25%; grid-template-columns: repeat(14, auto); } button { border: 1px solid black; margin: 1px; box-shadow: 5px 5px 5px black; cursor: pointer; } button:active { margin-left: 4px; margin-top: 4px; box-shadow: 1px 1px 5px black; }
The positioning of buttons is achieved using grids. Thanks!
You can change the button element type. Let me share it below.
You can use the
transform
attribute instead of adjusting themargin
in the :active
pseudo-class. Thetransform
property allows you to scale the button slightly and move it downwards when pressed, without affecting the layout of surrounding elements.