css - Issue with Margins Not Aligning Correctly Despite Using Inline-Block and Parent Element with a Fixed Width

I'm having trouble centering a pair of buttons inside a form. These buttons are enclosed within a <div> with display: inline-block, so the <div> is only as wide as the buttons themselves. The parent element to this <div> is a <form> with a fixed width of 290px.

Despite setting

margin-left: auto; margin-right: auto
on the <div>, it doesn't seem to center within the <form>. I even tried applying display: block to the <form> without success.

What am I missing here?

Here's the HTML structure:

<form id="schemeForm">
    <!--Other HTML elements-->
    <div id="formButtons">
        <input type="button" value="Back" />
        <input type="submit" value="Submit" />
    </div>
</form>

And the corresponding CSS:

#schemeForm {
    width: 290px;
    display: block;
}

#formButtons {
    display: inline-block; /*to make it only as wide as buttons*/
    margin-left: auto;
    margin-right: auto;
}

Check out this example code snippet for reference:

#schemeForm {
    width: 290px;
    display: block;
    background-color: black;
}

#formButtons {
    display: inline-block; /*to make it only as wide as buttons*/
    margin-left: auto;
    margin-right: auto;
    background-color: orange;
}
<html>
<body>
<form id="schemeForm">
    <!--Other HTML elements-->
    <div id="formButtons">
        <input type="button" value="Back" />
        <input type="submit" value="Submit" />
    </div>
</form>
</body>
</html>

Answer №1

To center the content of #schemeForm, try adding text-align: center;

<html>
    <style>
        #schemeForm {
            width: 290px;
            display: block;
            background-color: black;
            text-align: center;
        }

        #formButtons {
            display: inline-block; /*to make it only as wide as buttons*/
            background-color: orange;
        }
    </style>
<body>
    <form id="schemeForm">
        <div id="formButtons">
            <input type="button" value="Back" />
            <input type="submit" value="Submit" />
        </div>
    </form>
</body>
</html>

Answer №2

Ensure that #formButtons remains display: block and adjust to have text-align: center.

#schemeForm {
  width: 290px;
  display: block;
}

#formButtons {
  display: block;
  text-align: center;
}
<html>
<body>
<form id="schemeForm">
    <!--Other HTML elements-->
    <div id="formButtons">
        <input type="button" value="Back" />
        <input type="submit" value="Submit" />
    </div>
</form>
</body>
</html>

Answer №3

You seem to be in need of using display:block; instead of display:inline-block;

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

Utilizing Bootstrap 3 to eliminate the blue border surrounding the Contact Button

Recently, I integrated Bootstrap 3 into my website and encountered an issue. After selecting the Contact button and closing the popup window, it remains highlighted which is not desirable. https://i.sstatic.net/pzfKy.png I want to modify my CSS file so t ...

Troubleshooting VueJS: Issues with V-for display behavior

Is there a way to use v-for to arrange table data in a format similar to the code below: <tr> <th scope="col">Name</th> <th scope="col">Price</th> <th scope="col">Product ID</th> <th></th& ...

What is the Process of Rendering CSS/HTML in Web Browsers?

I have a simple question that I haven't been able to find an answer for despite my efforts on Google and Wikipedia. Perhaps I'm not wording it correctly. My query is regarding how the combination of CSS and HTML is displayed on-screen and in a b ...

The rotation feature for legends in Highcharts does not function properly on Internet Explorer 10 and 11 if the document mode is

When using Internet Explorer 10 or 11, rotation works perfectly fine if we select edge mode, as shown in the image. This information was taken from the High Chart Demo Charts. However, if we select Document mode 8 in IE 10 or IE 11, the rotation does not ...

The "placeholder" attribute effectively populates the form input in HTML, but when using Angular 2+, the "value" attribute does not fill the input field as expected

Within my Angular UI, I have implemented an edit button that allows users to transform a member's name heading into a form for editing and updating the name. The challenge lies in ensuring that the form is pre-filled with the current name. My issue a ...

Conceal the scrollbar when the expansion panel is in its collapsed state

One issue I am encountering is that the scroll-bar appears when the expansion panel is collapsed, but not when it's expanded. Here are the references: Collapsed Expanded <mat-expansion-panel class="panel"> <mat-expansion-panel-he ...

Leveraging the power of Required (html5) feature in Internet Explorer

What should I do if I want to make a textbox required on my webpage, but the issue is that it uses an HTML5 attribute? Is there a solution or alternative available? This is how I create my TextBox: @Html.TextBoxFor(model => model.SubChoiceText, new { ...

Error: Attempting to assign a value to the 'fillStyle' property of a null object in HTML5 Javascript

I've been experimenting with HTML5 canvas and JavaScript to draw shapes. While I can successfully draw shapes directly within the HTML file, I encounter an Uncaught Type Error when attempting to do so through an external JavaScript file. My goal is si ...

What allows CSS to interact with pseudo-elements?

While experimenting in my class, I made an interesting discovery that CSS can be applied to custom elements. For example: imsocool { color:blue; } <imsocool>HELLO</imsocool> Surprisingly, when my professor noticed me using these custom ...

Tips for keeping the menu open even when you're not hovering over it with your cursor

I put together a stylish drop-down menu based on some web examples, but my manager pointed out that it can be inconvenient to use because the menu closes when the mouse moves off of it. I've tried various workarounds as outlined here, but none have in ...

What seems to be the issue with my code for Javascript Math functions?

Welcome to the number game slider! If you roll above 50, you will get double the amount bet. Use the slider to choose your desired betting amount. Issue 1: After a win, the score does not update correctly. Instead of showing increments of 5, it displays s ...

Accordion-style elements arranged in a grid format, smoothly displacing surrounding content

I am facing an issue with a grid of accordions, as demonstrated in the codesandbox linked below. The problem arises when I open one accordion, causing all the accordions in the row below to shift down. Ideally, only the accordion directly below should be p ...

What is the best way to incorporate six rows of paragraphs using bootstrap?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" con ...

The script fails to function on elements contained within an infinite scrolling feature

I am facing an issue with my code after implementing infinite scroll. The like script seems to be broken as the page gets refreshed when I try to like a post. Here is the Like-btn script: <script> $(document).ready(function(){ ...

Issues arising in Microsoft Edge due to multiple media queries being utilized

Currently, I am in the process of developing a webpage that needs to be responsive across various screen sizes. To achieve this, I have incorporated CSS media queries within my stylesheets. While everything functions properly when tested on Firefox and Ch ...

Can you assist in resolving this logical problem?

exampleDEMO The code above the link demonstrates how to control input forms based on a button group selection. In this scenario, when a value is inputted on the 'First' Button's side, all input forms with the same name as the button group ...

Issue with Applying CSS Flexbox Justify-Content: Flex End

I'm having trouble achieving the design I created in Figma: https://i.sstatic.net/aH6Cy.png Instead, my actual code output looks like this (dummy text): https://i.sstatic.net/z5L0y.png This is the CSS and SCSS for my page: .navbarcont { margin-t ...

Looking for guidance on integrating Forms with VueX and script setup? Are you utilizing v-model in your implementation?

UPDATE: I'm contemplating whether or not to abandon VueX entirely due to its outdated status, with Pinia being the preferred choice nowadays. Can someone confirm this? https://stackblitz.com/edit/vue-script-setup-with-vuex-hmrk5d?file=src/store.ts ...

Is there a way to achieve a straightforward three-column stacked layout in Bootstrap 5 that spans 100% of the viewport height?

Today I decided to try out Bootstrap 5 for the first time. However, I am facing a challenge in creating a 3 stacked column layout with box-1, box-2, and box-3 on top of each other, taking up 100vh. I have shared a redacted version of the code below, wher ...

CSS: customize the color of a highlighted row within a table

I am struggling to implement a new feature in my table. Specifically, I want the row to change color (#FFCF8B) when a user selects it. I attempted using #newspaper-b tbody tr.selected td, but it was not successful. #newspaper-b { border-colla ...