Aligning HTML form with CSS styling

After researching various strategies on how to achieve this, and implementing the suggestions provided, I am still encountering difficulties. While the text successfully centers, the actual elements remain aligned to the left of the screen.

Below is the form structure:

<div class="row">
        <div class="col-md-6">
            <div class="formdiv">
                <form id="my-form" method="post">
                    <div asp-validation-summary="All" class="text-danger"></div>
                    <div class="form-group">
                        <label asp-for="OrderEmail"></label>
                        <br />
                        <input asp-for="OrderEmail" />
                    </div>
                    <button id="update-profile-button" type="submit" class="btn btn-primary">Send</button>
                </form>
            </div>
        </div>
    </div>

Here is the corresponding CSS:

.formdiv {
    display: block;
    text-align: center;
}

#my-form {
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    text-align: left;
}

Answer №1

Here is a suggestion for you:

<div class="container">
 <div class="row">
  <div class="col-md-6 mx-auto">
   <div class="formdiv">
    <form id="treatment-form" method="post">
     <div asp-validation-summary="All" class="text-danger"></div>
      div class="form-group">
       <label asp-for="OrderEmail"></label>
       <br />
       <input asp-for="OrderEmail" />
      </div>
      <button id="update-profile-button" type="submit" class="btn btn-primary">Send Email</button>
     </form>
    </div>
   </div>
  </div>
</div>

Answer №2

If you're looking to center the button within a text input, I've got the solution right here. If not, just give me a shout.

<div class="row">
    <div class="col-md-6">
        <div class="formdiv">
            <form id="treatment-form" method="post">
                <div asp-validation-summary="All" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="OrderEmail"></label>
                    <br />
                    <input asp-for="OrderEmail" />
                </div>
                <button id="update-profile-button" type="submit" class="btn btn-primary">Send Email</button>
            </form>
        </div>
    </div>
</div>

And some CSS for ya:

#treatment-form {
     display: inline-block;
     margin-left: auto;
     margin-right: auto;
     text-align: left;
}
.formdiv {
     display: block;
     text-align: center;

}
#update-profile-button{
     margin-left:40px;
}

Answer №3

Looking to achieve something like this: https://i.sstatic.net/v5eEw.png

Well, you can accomplish it with flexbox. Here's how:

#treatment-form {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}
<div class="row">
  <div class="col-md-6>
    <div class="formdiv>
      <form id="treatment-form" method="post>
        <div asp-validation-summary="All" class="text-danger></div>
        <div class="form-group>
          <label asp-for="OrderEmail></label>
          <br />
          <input asp-for="OrderEmail />
        </div>
        <button id="update-profile-button" type="submit" class="btn btn-primary>Send Email</button>
      </form>
    </div>
  </div>
</div>

To center the form element, just add the flex properties. And if you want to center the text within an input field, use the text-align property with a value of center. Check out this codepen for reference - https://codepen.io/thoughtlessmind/pen/QWjvOZZ?editors=1100

Answer №4

To achieve this, utilize the position property.

.formdiv{
    top:0;
    right:0;
    bottom:0;
    left:0;
    position:absolute;
    margin:auto;
    width: 200px;
    height:100px;
}

#treatment-form {
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    text-align: left;
}
#update-button{
    display: flex;
    align-items: center;
    justify-content: center;
  
}
<div class="row">
        <div class="col-md-6">
            <div class="formdiv">
                <form id="treatment-form" method="post">
                    <div asp-validation-summary="All" class="text-danger"></div>
                    <div class="form-group">
                        <label asp-for="OrderEmail"></label>
                        <br />
                        <input asp-for="OrderEmail" />
                    </div>
                    <div id="update-button">
                    <button id="update-profile-button" type="submit" class="btn btn-primary">Send Email</button>
                    </div>
                </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

Navbar Growth Effect

I'm attempting to create an expanding effect in my navbar using Angular. When I click on "show information," the content should slide up. The issue is that the top part of my footer does not follow the effect. I have tried something like: <foot ...

The HTML dropdown menu is malfunctioning

I've been struggling to create a website with a dropdown menu. Despite following various guides and searching for solutions, the menu is behaving strangely. I've tried numerous tactics, so some lines of code may be unnecessary. The submenu is ap ...

The issue arises when attempting to use Bootstrap date and time components simultaneously

I am encountering an issue with the date picker and datetimepicker when used together in my form. If I only work on time or date individually, they function properly. However, when both are included, the time is not working correctly as it displays the dat ...

The div's height is being disrupted by the accordion

Currently, I am facing an issue with the accordion component in Angular and ng-bootstrap. The problem arises when I place an accordion within a div that has a specified max-height. The accordion extends beyond this height limit, whereas I would like it to ...

Accurate understanding of URL parameters and hashtags

Imagine an HTML document containing two blocks, where the content of only one block can be displayed at a time by toggling between them using menu buttons and/or URL parameters with the same JavaScript functions provided below: <div class="block1&q ...

What is the best way to increase padding beyond p-5 for specific screen sizes?

I need help creating a webpage that is responsive in design. Below is a snippet of the specific section I am working on: <div class="container-fluid"> <div class="row p-3 p-lg-5"> <div class="col"&g ...

Splitting Arrays in Laravel DatabaseExploding arrays stored in a Laravel

Within my database, there exists a text field featuring: "6:00 AM Registration Opens; Day of Race registration (if available), check-in, packet pick up. 7:30 AM - First Wave Start. 10:00 AM - Awards Ceremony (time is approximate)." My goal is to create b ...

We're facing some technical difficulties with the form for the school project. Furthermore, I need to ensure that the answers are

Upon inspection, it seems that the HTML code is fine, but there appears to be an issue with the JavaScript. I also need to store the answers in an array, which is a task for later. <form> <fieldset> <legend>Content:</lege ...

Issue with activating a specific element with jQuery

I am currently working on a feature that enables a multi selector only when a checkbox is checked. Despite generating an alert, my code seems to be failing in enabling the selector. Here is the HTML and script for the selector and checkbox: https://i.sst ...

I am having trouble with the hover feature on the image tag. Does anyone know how to troubleshoot this issue?

h1{ color:red; font-size: 100px; } img :hover { background-color: gold; } .bacon{ background-color: green; } .broccoli{ background-color: red; } /* .circular{ border-radius: 100%; } */ #heading{ background-color: aquamarine; ...

Javascript eval function providing inaccurate results

I have encountered a problem while using eval() for a calculator I am developing. When I input the following code: console.log(eval("5.2-5")); The output is: 0.20000000000000018 I am confused as to why this is happening. Thank you for your assistance. ...

"Adjusting the height of a div element while considering the

I have 2 elements with different heights and I want to make them equal: var highestEl = $('#secondElement').height(); $('.element').first().height(highestEl); I understand that the second element is always taller than the first one. W ...

Tips for converting a styled MS-Access report to HTML with a template document

I am having trouble formatting my MS Access report using an HTML template. After doing some research, I discovered the following tags and tokens for MS Access HTML templates: <!--AccessTemplate_Body--> <!--AccessTemplate_FirstPage--> ...

CSS gallery slideshow with images that smoothly fade-in and fade-out at a specified location

In the image below, at position 4 (marked by yellow circle) from the left where picture 6 is displayed, I want a cross-fade (fade-in/fade-out) gallery of images to take place. Specifically at position 4, I am looking for a cross-fade (fade-in/fade-out) ef ...

Ways to update the value in localstorage with the click of a button

After a user inputs a numerical value on one screen of my web app, the value is stored in local storage. On the next screen, the user can click a button to increment the stored value by a specified amount, such as adding 5. For example, if the variable X i ...

Updating a class within an AngularJS directive: A step-by-step guide

Is there a way to change the class (inside directive) upon clicking the directive element? The current code I have updates scope.myattr in the console but not reflected in the template or view: <test order="A">Test</test> .directive("test", ...

Using identical CSS for two separate layouts

Imagine having to work with an html page that cannot be altered in any way. The only tool at your disposal is CSS. The page is loaded and shown through an iframe from a different domain. This particular page serves as a payment gateway, displaying either ...

Is it beneficial to modify the title/alt attribute of an image for SEO purposes?

In my application, I utilize Angular and have implemented the ng-repeat directive for images. This means that in my HTML, I only have one img tag for all the image objects fetched from the server. Should I also include unique titles and alt tags for thes ...

Troubleshooting CSS issues for Compatibility View on Internet Explorer

I have been using CSS to create a dropdown navigation menu without the use of JavaScript. It seems to be working perfectly in most browsers, however, I am encountering some issues with certain versions of Internet Explorer. I've spent hours trying var ...

Easy ways to manipulate the style of array components in Vue.js

Hey there all you programmers, I'm reaching out to see if any of you have insight on how I can modify the style of a specific component within an Object array. This is the code snippet: <template> <div> <ul> <li v-fo ...