Arrange the divs as though they were keys on a piano

For the past few hours, I've been attempting to align 4 div elements as they appear on a keyboard layout, but I'm struggling to get it right...

I prefer not to use absolute positioning since I have 3 groups of four divs. These divs are meant to be positioned on the left side of the page next to a canvas, serving as instructions for my JavaScript game.

Here's the snippet of my code:

p.keyboard {
           display: inline-block;
           color: #5E5E5E;
           font: bold 10px arial;
           text-decoration: none;
           text-align: center;
           margin: 10px auto;
           padding: 7.5px 10px;
           background: #EFF0F2;
           border-radius: 4px;
           border-top: 1px solid #F5F5F5;
           box-shadow: inset 0 0 25px #E8E8E8, 0 1px 0 #C3C3C3, 0 2px 0 #C9C9C9, 0 2px 3px #333;
           text-shadow: 0px 1px 0px #F5F5F5;
       }
<div id="green">
         <p class="keyboard">Z</p>
         <p class="keyboard">Q</p>
         <p class="keyboard">S</p>
         <p class="keyboard">D</p>
       </div>
       <div id="blue">      
         <p class="keyboard">Flèche haut</p>           
         <p class="keyboard">Flèche gauche</p>
         <p class="keyboard">Flèche bas</p>
         <p class="keyboard">Flèche droite</p>
       </div>
       <div id="red">
         <p class="keyboard">O</p>
         <p class="keyboard">K</p>
         <p class="keyboard">L</p>
         <p class="keyboard">M</p>
       </div>

I've experimented with various layouts, like using flexbox and multiple nested div tags, but unfortunately, it seems that I just can't get it right. I simply want the "Z," the "O," and the "flèche haut" (up arrow) to be centered above the other three elements.

Any assistance would be greatly appreciated. Thank you!

Answer №1

At first, I recommend avoiding the use of <p> tags as most browsers automatically add styles to them that may need resetting.

It's best to opt for a more generic tag like <span> or <div>.

Here is my approach using flexbox:

.keys > span {
    display: inline-block;
    color: #5E5E5E;
    font: bold 10px arial;
    text-decoration: none;
    margin: 3px 2px;
    padding: 7.5px 10px;
    background: #EFF0F2;
    border-radius: 4px;
    border-top: 1px solid #F5F5F5;
    box-shadow: inset 0 0 25px #E8E8E8, 0 1px 0 #C3C3C3, 0 2px 0 #C9C9C9, 0 2px 3px #333;
    text-shadow: 0px 1px 0px #F5F5F5;
    text-align: center;
    clear: both;
}
.keyboard {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
.keys {
  display: flex; 
  flex-wrap: wrap;
  justify-content: center;
  flex: 0 1 100%;
  padding: .5rem;
}
.keys > span:first-child {
  flex: 0 0 0;
  margin-left: 50%;
  margin-right: 50%;
  white-space: nowrap;
}

#green { background-color: rgba(0,255,0,.35);}
#red { background-color: rgba(255,0,0,.35);}
#blue { background-color: rgba(0,0,255,.35);}
<div class="keyboard">
  <div id="green" class="keys">
    <span>Z</span>
    <span>Q</span>
    <span>S</span>
    <span>D</span>
  </div>
  <div id="blue" class="keys">
    <span>Flèche haut</span>           
    <span>Flèche gauche</span>
    <span>Flèche bas</span>
    <span>Flèche droite</span>
  </div>
  <div id="red" class="keys">
    <span>O</span>
    <span>K</span>
    <span>L</span>
    <span>M</span>
  </div>
</div>

The larger keys appear misaligned because the 2nd and 4th keys are not equal in width. The first key is actually centered.
I also optimized your markup slightly by applying classes to their parent elements for styling purposes.

.parent-class > child {}

If you require further assistance with layout, feel free to share a jsFiddle link so I can help position elements if needed.

Don't forget to autoprefix.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm looking to customize the text displayed on the screen from my JSON file. Is there a way to hide this text?

I am currently working with a JSON data file that contains values and displays them on the screen alongside a CSS property width. However, I do not want the output to be the actual numerical data; instead, I aim to connect the JSON file so that the value s ...

Tips for creating a section that perfectly stretches to cover the entire view port in HTML

After browsing through this website, I was inspired by its design and wanted to replicate something similar. The unique approach they took to ensure each section fits 100% of the viewport caught my eye. They utilized a "div section-background" within each ...

What is the best method for transforming bullet points into a horizontal list?

Is there a way to display disc bullet points in a horizontal list? I've encountered an issue where setting display: inline; in CSS prevents the class definition list-style-type: disc from working. My workaround involves manually inserting disc bulle ...

Exploring z-indices in event bubbling

JSFiddle: https://jsfiddle.net/uLap7yeq/19/ Issue Let's examine a scenario where there are two elements, canvas and div, positioned in the same location using CSS. The div has a higher z-index compared to the canvas, but how can we make sure events ...

Transitioning colors in the renderer using ThreeJS and GSAP

I'm attempting a basic color transition using gsap to modify the renderer's background color. The issue arises when trying to transition from white to black – it simply changes the color abruptly without any fade effect. Conversely, transition ...

JQuery for creating sleek scale animations

I am currently working on a page where I have an image with a pointer down icon. When the user hovers over it, a list of products should be displayed and the pointer should compress. Everything is functioning correctly except for the animation aspect. I am ...

What is the best way to insert a space between my element and pseudo-element?

I'm currently utilizing the ::before selector to insert an image before each item on my menu list. I am aiming to include a blank space between the image and the text. Below is the HTML markup <ul> <li>menu 1</li> <li>me ...

Challenge encountered when trying to add overlay hover effect on image cards

Yesterday I faced a challenge with a tracking problem while trying to implement code on a w3schools webpage. The issue was with a hover effect triggering an overlay when the mouse was placed anywhere on the line, rather than just within the card area. I&ap ...

Unnecessary additional spacing caused by inline blocks in Firefox

On my webpage, I have organized my div elements using inline-block properties. Strangely, Firefox is adding extra spacing between two specific divs, while Safari and Chrome display the design consistently. Check out this example here. #main { display ...

How can I address the problem of a responsive menu not displaying properly within a container?

I have a question regarding the menu placement within a container. In mobile view, I noticed that the menu is not displayed in full width because it's inside a container. How can I address this issue and make the menu extend to the full view? In the ...

How to Align Floats in Bootstrap 4 Without Using Clearfix

Unfortunately, Bootstrap did not successfully align the div boxes next to each other. There are inconsistencies with spacing between the boxes, and we suspect it has something to do with clearfix. Is there a way for us to achieve proper 'float' ...

How to enhance the design with a box-shadow for a :after pseudo element

One of the challenges I'm facing involves a CSS design with a div called .testimonial-inner that has an arrow created using the :after pseudo element. The issue is making them appear as one cohesive element, especially when applying a box-shadow. Her ...

Is it possible to apply inherited styles while using createStyles?

Here is a createStyles statement in my code: const useStyles = makeStyles((theme: Theme) => createStyles({ menuOpen: { zIndex: 1, width: 200, height: '100%', position: 'fixed', top: 48, trans ...

Adjust the table to line up perfectly, center the content within the table cell (td), and align the

I'm currently working on aligning a table by centering the td elements and left justifying the text inside them. My initial attempt was to use flex for center alignment, which worked to some extent but I couldn't get the left alignment right. I ...

What could be causing the significant gap at the bottom of my Flexbox?

Whenever I resize the page to fit a mobile device, there is always a large gap at the bottom. I want the content to stack normally when it shrinks, without leaving a huge gap at the bottom. There is no gap at the bottom when viewed on desktop, but as soo ...

The utilization of the <span> tag featuring a {float:right] property causes container expansion in Internet Explorer 7

Within my HTML code, I have an A tag button that contains a Span element to hold icons. This setup functions correctly in most browsers, except for IE7. When I attempt to float the Span to the right side using CSS, it causes issues specifically in IE7, cau ...

Responsive Line Breaks in Bootstrap 4

I can't seem to find any information in the documentation to guide me in the right direction here. I'm hesitant to use columns as it could result in gaps between the text. Currently, I have the date displayed at the top of my website like this u ...

Create a fixed content scroll effect with an inset box-shadow

I recently stumbled upon a solution at Stack Overflow that effectively created an inset box shadow beneath content. However, I encountered an issue when the outer container div needed to be scrolling due to overflow - the inner div with the box shadow woul ...

Creating a custom design for ng-bootstrap accordion using CSS styling

I've encountered an issue with my Angular 2 component that utilizes an accordion from ng-bootstrap. While the functionality works perfectly, I'm facing a problem with applying custom styles using the .card, .card-header, and .card-block classes o ...

Expand the width of the navbar brand icon

Currently working on a landing page project and noticed that my logo appears too small, utilizing Bootstrap 5 style.css ... .navbar-brand { width: 3.5rem; padding: 0 0; margin: 0.3rem 0; position: relative; } .navbar-brand img { width: 100%; ...