CSS for button positioning inspired by game controller layout, built to be responsive

Is there a way to arrange these 4 buttons in a layout resembling a game controller? Currently, they are displayed like this and I want them to be centered at the bottom of the screen, smaller, and responsive. What changes should I make? Would using #controls { position:relative;} help?

https://i.sstatic.net/iAd1o.png

Sample HTML Code:

<head><meta name="viewport" content="width=device-width, initial-scale=1.0"/>...</head>

<body><div id="controls">
    <button id="keyboard_key_up" class="movements_control"><img src="public/img/keyboard_key_up.png"/></button>
    <button id="keyboard_key_left" class="movements_control"><img src="public/img/keyboard_key_left.png" /></button>
    <button id="keyboard_key_down" class="movements_control"><img src="public/img/keyboard_key_down.png" /></button>
    <button id="keyboard_key_right" class="movements_control"><img src="public/img/keyboard_key_right.png" /></button>

</div>...</body>

Sample CSS Code:

#keyboard_key_up {
    position: absolute;
    right:300px;
    bottom: 500px;
    border-style: solid;
    border-color: black;
    background: white;z

}

#keyboard_key_left {
    position: absolute;
    right:575px;
    bottom: 250px;
    border-style: solid;
    border-color: black;

}

#keyboard_key_down {
    position: absolute;
    right:300px;
    bottom: 250px;
    border-style: solid;
    border-color: black;

}

#keyboard_key_right {
    position: absolute;
    right:25px;
    bottom: 250px;
    border-style: solid;
    border-color: black;

}

Answer №1

Support for flex and grid is quite robust these days, although it may not be compatible with all targets.

body {
  margin: 0;
  padding: 0;
}

#wrapper {
  display: flex;
  height: 100vh;
  justify-content: center;
}

#controls {
  display: inline-grid;
  grid-column-gap: 0;
  align-self: end;
}

.movements_control {
  border: solid 1px black;  
  margin: 0;
  padding: 0;
}

.movements_control img {
  width: inherit;
  height: inherit;
}

#keyboard_key_up {
  grid-column-start: 2;
}

#keyboard_key_left {
  grid-column-start: 1;
}

#keyboard_key_down {}

#keyboard_key_right {
  grid-column-start: 3;
}

@media (min-width: 721px) {
  .movements_control {
    width: 40px;
    height: 40px;
  }
}

@media (max-width: 720px) {
  .movements_control {
    width: 20px;
    height: 20px;
  }
}
<div id="wrapper">
  <div id="controls">
    <button id="keyboard_key_up" class="movements_control"><img src="public/img/keyboard_key_up.png"></button>
    <button id="keyboard_key_left" class="movements_control"><img src="public/img/keyboard_key_left.png"></button>
    <button id="keyboard_key_down" class="movements_control"><img src="public/img/keyboard_key_down.png"></button>
    <button id="keyboard_key_right" class="movements_control"><img src="public/img/keyboard_key_right.png"></button>
  </div>
</div>

Answer №2

Check out this CSS snippet:

#controls {
position: relative;
bottom: 10px;
margin: 0 auto;
}

If the above code doesn't give you the desired result, consider sharing your code on platforms like Plunker or CodePen for further assistance.

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

bootstrap modal dialog displayed on the edge of the webpage

I am facing an issue with a modal dialog that pops up when clicking on a thumbnail. The JavaScript code I used, which was sourced online, integrates a basic Bootstrap grid layout. The problem arises when half of the popup extends beyond the edge of the pa ...

New to using JavaScript and JQuery, attempting to position a form underneath an image at a specific URL for the

Hello, I'm having difficulties placing a form below an image with a specific URL. As someone who is still learning JQuery, I am unsure of what mistake I might be making. Is there anyone who can help me figure out what's wrong here? <html> ...

Is it possible to access an external website by clicking on a link within a DIV element?

I have a basic website set up like this sample.php <html> <head></head> <body> <a id="myLink" href="http://sample.com">Click!</a> </body> </html> I displayed this website within a DIV-Container on a ...

Wordpress theme's Bootstrap failing to display correctly on browser

I've been working on a WordPress theme with the bootstrap framework to style my page. First, I created a mock-up using standard HTML to ensure everything looked as desired: Prototype Code: <!DOCTYPE html> <html> <head> <meta c ...

Why isn't my easypie number displaying on the inside?

I am currently utilizing the easypie plugin, but I am facing an issue where the number is not displaying inside the pie chart. Despite trying multiple solutions, I haven't been able to resolve this. How can I successfully display the number inside the ...

AngularJS Validation does not verify the step limitation of an HTML number input field

Why isn't AngularJS validating the step="0.01" attribute in the second input? It seems to be working fine for the min and max attributes, but not for step. For instance: When entering 0.005, it still evaluates submitNewEntryForm.submitNewEntryAmount. ...

What is the best method for adding anchors or appending items in aspx pages?

I have created code for an image gallery on my webpage, "gallery.aspx". The situation is that I have an external aspx page with various links. When a user clicks on one of these links, I want them to be redirected to the image gallery but to a specific ima ...

Having trouble setting background images for CSS ID, but it's working perfectly for CSS Body

When working with my .CSS file: Initially, applying the following styling: body { font-size: .85em; font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif; color: #232323; background-color: #fff; background: url('Images/Login_Background.jpg'); ...

Achieving Vertical Alignment of Content in Bootstrap 5

I've experimented with different approaches based on search results, StackOverflow suggestions, and examining existing code snippets. Here is the snippet of code I am working with: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a ...

There seems to be an issue with the behavior of the click-to-toggle menu implemented with jQuery

I can't seem to figure out why my pop-up menu in jQuery is acting so strange and unpredictable. The menu is supposed to appear on a toolbar located at the bottom of the window when clicked (not hovered). It should then disappear again when: a) a menu ...

Feeling anxious about the abundance of fields within the table

Currently in the process of planning my upcoming project, a text-based MMORPG game. I'm tackling the design of certain aspects of the database and have encountered a new challenge. One feature of the game involves allowing players to purchase cars and ...

Why is inner HTML returning input/textbox instead of the value?

I need help extracting the value from an input/textbox within a table cell. Although the rest of my code is functioning correctly, I'm struggling to retrieve the value from this particular input element. I've attempted to access the value using ...

What is the best way to reveal the following div while concealing the one before it?

Just starting out with Javascript, I'm currently developing a quiz solution that utilizes divs and buttons to navigate through the questions. I've written some JavaScript code for this functionality but I'm encountering an issue where it doe ...

Make the div disappear after a specified time

Does anyone know of a code snippet that can make a couple of div elements fade out after a specific time period? Currently, I have the following example: <div id="CoverPop-cover" class="splash"> <div id="CoverPop-content" class="splash-center"> ...

Could one potentially append an attribute to a script that has been loaded by nuxt?

Is it feasible to include an attribute to a script loaded through nuxt? https://i.sstatic.net/UXDYW.png Situation: I am in need of adding an attribute to make Cookiebot () disregard my script. Currently, it is blocking a few scripts that send client-side ...

Find the dominant color within the project's style sheets

Is there a method to extract the main color from an angular material theme and apply it in the css files of an angular project? Within a project, we can specify the primary color in a *.scss document as shown below: @import '~@angular/material/themi ...

On a desktop view, the content is displayed in 4 columns, while on smaller

In my simple block, I am trying to arrange 4 items in a row on desktop and 2 items per row on smaller devices like tablets and mobile. The desired layout is shown below: https://i.sstatic.net/3i799.png To see a live demo, visit: jsfiddle Below is the HTM ...

Utilizing media queries and JQuery to display a variety of menus based on screen

I have created a hamburger menu specifically for smaller screens, and it is functioning properly on small screens. However, I am facing an issue where if the menu is not open before resizing the screen to a larger size, the menu does not show at all. When ...

Changing between two images using HTML and CSS

I am currently designing a WordPress theme and I would like to create an effect where two different thumbnail images switch on hover. The code that I have come up with so far looks something like this : <a class="thumb" href="posturl"> <img src= ...

Looking for a way to customize it using @emotion/styled? Dealing with the deprecated makeStyles from MUI problem?

I have been working on setting up my styling using @emotion since makestyles were removed in React 18. Despite making changes as per my research, I am not seeing any updates reflected on my page. The expected result looks like this: https://i.stack.imgur. ...