Guidelines for aligning input buttons at the center of an HTML form

My form contains multiple input buttons, however, the inputs are not centered on the page. Currently, they are displayed in a single row within the div. I would like them to fill the page width with a 20px margin on each side and adjust to the browser screen size. How can I achieve this? Thank you for your assistance!

.main_buttons {
  width: 100%;
  text-align: center;
}

.btn-select {
  margin: 4px;
  background-color: #7a7979;
  border-radius: 4px;
  overflow: auto;
  width: 250px;
  height: 200px;
  margin-right: 5px;
}
<div class="container main_buttons">

  <div class="row">

    <div class="col-12">

      <form class="select_form" method="POST">
        {% csrf_token %}


        <div class="btn-select">
          <label>
                                <input type="checkbox" name="city_select" value="0"><span>sydney</span></input>
                            </label>
        </div>

        <div class="btn-select">
          <label>
                                <input type="checkbox" name="city_select" value="1"><span>tokyo</span></input>
                            </label>
        </div>

        <div class="btn-select">
          <label>
                                <input type="checkbox" name="city_select" value="2"><span>london</span></input>
                            </label>
        </div>

        <div class="btn-select">
          <label>
                                <input type="checkbox" name="city_select" value="3"><span>new york</span></input>
                            </label>
        </div>

        <div class="btn-select">
          <label>
                                <input type="checkbox" name="city_select" value="4"><span>paris</span></input>
                            </label>
        </div>

        <div class="btn-select">
          <label>
                                <input type="checkbox" name="city_select" value="5"><span>madrid</span></input>
                            </label>
        </div>





        <button type="submit" class="btn btn-secondary city_submit_button">SUBMIT</button>

      </form>

    </div>

  </div>

</div

Answer №1

One effective method is to utilize the position property. I enclosed the label and input fields within a div container named wrapper, and then applied a display style to the label.

.main_buttons {
  width: 100%;
  text-align: center;
  }

.wrapper {
    position: relative;
}

.inner {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%,-50%);
}
label {
  display:block;
}

.btn-select {
  margin:4px;
  background-color:#7a7979;
  border-radius:4px;
  overflow:auto;
  width: 250px;
  height: 200px;
  margin-right: 5px;
  
}
<div class="container main_buttons">

    <div class="row">

        <div class="col-12">

            <form class="select_form" method="POST">
            {% csrf_token %}                
                    <div class="btn-select wrapper">
                        <div class="inner">                          
                          <input class="" type="checkbox" name="city_select" value="0">
                          <label class=""><span>sydney</span></label>
                        </div>
                    </div>
              
                    <div class="btn-select wrapper">
                        <div class="inner">                          
                          <input class="" type="checkbox" name="city_select" value="0">
                          <label class=""><span>tokyo</span></label>
                        </div>
                    </div>              

                    <div class="btn-select wrapper">
                        <div class="inner">                          
                          <input class="" type="checkbox" name="city_select" value="0">
                          <label class=""><span>london</span></label>
                        </div>
                    </div>


                    <div class="btn-select wrapper">
                        <div class="inner">                          
                          <input class="" type="checkbox" name="city_select" value="0">
                          <label class=""><span>new york</span></label>
                        </div>
                    </div>

                    <div class="btn-select wrapper">
                        <div class="inner">                          
                          <input class="" type="checkbox" name="city_select" value="0">
                          <label class=""><span>paris</span></label>
                        </div>
                    </div>

                    <div class="btn-select wrapper">
                        <div class="inner">                          
                          <input class="" type="checkbox" name="city_select" value="0">
                          <label class=""><span>madird</span></label>
                        </div>
                    </div>               
          
                <button type="submit" class="btn btn-secondary city_submit_button">SUBMIT</button>
          
            </form>

        </div>
            
    </div>

</div>

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

What could be causing the incorrect value of the endpoint of an element when utilizing a function?

By adding angles individually and then using ttheta (without calling a function to add angles and then using ttheta), the problem is resolved. However, can anyone explain why using a function here is incorrect or identify the issue that this function is ca ...

Choose a specific <div> element from an external page using Ajax/PHP

I'm encountering a small issue. I am currently utilizing Ajax to dynamically load content from another page into a <div> element after detecting a change in a <select>. However, my specific requirement is to only load a particular <div& ...

Embedding an Inline Frame on Chrome while preserving Internet Explorer preferences

I am currently facing a situation where my webpage looks different on Internet Explorer compared to Chrome. I am considering loading the webpage within an iframe, but I want it to render as it does in Internet Explorer regardless of the browser being use ...

Iterate endlessly over CSS styles in Angular 4

I'm looking to create a website background 'screensaver' by looping through an array of background URLs with a delay between switches. Currently, I have the array stored in my .ts file and I am using the NgFor directive in my HTML. However, ...

Creating a custom Vue.js component for specific table cells within a table row - here's how!

My challenge is having related and neighboring table columns grouped within the same component in Vue.js. However, I'm facing a limitation with the template system where only one tag can be directly placed inside <template>. Typically, this wrap ...

What options are available for labels in Bootstrap v5.0.0-beta1 compared to BS 3/4?

How can I use labels in Bootstrap v5.0.0-beta1? In Bootstrap 3, the labels are implemented like this: <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" /> <span class="label label-success ...

Removing a Dom element using stage.removeChild( )

When the number 6 is typed and entered into the game, the function correct() in the code snippet below determines what action to take. I would like to remove the DOM element gg (the equation 3+3=input) from the stage after typing 6 and pressing enter. How ...

Align the text vertically in a Bootstrap button

Can anyone lend a hand with vertically aligning text in a Bootstrap button? When I use the align-middle class, the text is aligned as desired. However, if I use the align-top class, the text remains top-aligned no matter what I do. Any suggestions? Here& ...

Display outcome generated by JavaScript in the input box

I've successfully implemented a date/time picker in a form. Using JavaScript, I've created a script to calculate the difference between two date fields and display it in a third field labeled Course Duration. However, I'm encountering an i ...

The final Bootstrap radio element is missing from view

Presented below is the form in question: <!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, ...

Non-responsive image in Bootstrap 4

My struggle is with making my website fully responsive. Whenever I use the img tag with a width of 100% on a large screen, the image fits perfectly, but when the window is resized, a horizontal scroll appears, creating white spaces and causing the image ...

ColdFusion encounters an HTML form error, presenting the message: "There is an undefined element in the FORM."

I'm attempting to submit an HTML form that contains checkboxes for each day of the week. Whenever a checkbox is checked, I assign a value of 1 to it. To handle unchecked checkboxes, I set a default value of 0 using a CFPARAM tag in the form action pag ...

Transitioning from LESS to SASS involves adapting responsive breakpoints

Adapting from Less to Sass has been my current focus. I have defined responsive breakpoints in Less variables: /* Breakpoints */ @break1:~"(max-width: 570px)"; @break2:~"(min-width: 571px) and (max-width: 1002px)"; @break3:~"(min-width: 1003px)"; These ...

In Python using Selenium, the text property of a WebElement may cut off duplicate whitespaces

Today, I came across an interesting observation regarding the text property of WebElement. Here is a PDF link that sparked my curiosity: https://i.stack.imgur.com/1cbPj.png Upon closer inspection, I noticed that the file name contains triple whitespace. W ...

Exploring the Vertical Metrics of Various Typeface Styles

I'm encountering a problem and struggling to find a solution. I'm in the process of creating a website and incorporating various fonts. My goal is to ensure that every font appears visually similar to the others and align perfectly at the base. ...

Remove text on the canvas without affecting the image

Is there a way to remove text from my canvas element without losing the background image of the canvas? I'm considering saving the Image source and reapplying it to the Canvas Element after using clearRect, but I'm unsure how to execute this sol ...

Scaling Vuetify icons dimensions

During the development of an app using Vuetify, I encountered challenges in changing the default colors set by Vuetify. After some trial and error, I came across a solution on: https://github.com/vuetifyjs/vuetify/issues/299 The solution involved adding ...

The primary section does not occupy any vertical area

I am currently working on a new design that includes a header, side menu, and footer within the main section. My goal is to have the main section fill the remaining space with a minimum height, pushing the footer to the bottom of the page. I want the mai ...

JavaScript code to dynamically change the minimum value of an input field when the page

How can I use JavaScript to change the minimum value onload? I tried the following code but it didn't work: <script type="text/javascript">$(document).ready(function(){ $("#quantity_5c27c535d66fc").min('10'); });</script> ...

Adjust the size of an image and move its position both vertically and horizontally using Javascript

I am currently working on transforming an image both vertically and horizontally, as well as scaling it using JavaScript. While I have managed to resize the image successfully, it doesn't quite look how I want it to. I am aiming for a similar effect a ...