Button not aligning properly after being relocated to the bottom of the container

I'm currently working on a form design where the submit button always appears at the bottom and centered. However, I am facing an issue with centering it properly.

To achieve this layout, I have been using text-align: center in the parent div and positioning the button at the bottom using position: absolute along with bottom: 20px. Despite implementing these styles, I noticed that the button is slightly off-center. I would appreciate some insight into why this is happening and how I can fix it.

HTML:


<div class='container'>
  <form>
    <p>
      <input name='group 1' type='radio' id='1' />
      <label for='1'>option 1</label>
    </p>
    <p>
      <input name='group 1' type='radio' id='2' />
      <label for='2'>option 2</label>
    </p>
    <p>
      <input name='group 2' type='radio' id='3' />
      <label for='3'>option 1</label>
    </p>
    <p>
      <input name='group 1' type='radio' id='4' />
      <label for='4'>option 1</label>
    </p>
    <button type='submit'>
      Submit
    </button>
  </form>
</div>

CSS:


.container {
  width: 400px;
  height: 400px;
  border: 2px solid red;
  text-align: center;
}

button {
  position: absolute;
  bottom: 20px;
}

Demo: https://jsfiddle.net/f9ktLn9o/3/

Answer №1

By using position: absolute;, you have successfully centered the element to the left of the button in the stack. To further refine the positioning, try translating it by 50% of the button's width to the left.

transform: translateX(-50%);

https://jsfiddle.net/x92jGhr7/2/

Answer №2

Upon analyzing the Computed styles, you'll notice the presence of left and right values for the position in the box model. It is essential to override these predefined values with your own adjustments to achieve the desired functionality.

Check out the updated JSFiddle here

Illustration of Code Snippet:

.container {
  width: 400px;
  height: 400px;
  border: 2px solid red;
  text-align: center;
  /* Additional */
  position: relative; /* position absolute child relative to parent */
}

button {
  position: absolute;
  bottom: 20px;
  /* Additional */
  left: 0;
  right: 0;
  display: block;
  margin: auto;
  width: 100px;
}
<div class='container'>
  <form>
    <p>
      <input name='group 1' type='radio' id='1' />
      <label for='1'>option 1</label>
    </p>
    <p>
      <input name='group 1' type='radio' id='2' />
      <label for='2'>option 2</label>
    </p>
    <p>
      <input name='group 2' type='radio' id='3' />
      <label for='3'>option 1</label>
    </p>
    <p>
      <input name='group 1' type='radio' id='4' />
      <label for='4'>option 1</label>
    </p><button type='submit'>
      Submit
    </button>
  </form>
</div>

Answer №3

Here's an alternative to your current CSS:

button {
  position: relative;
  margin-top: 40%;
}

This solution has been tested and works on Chrome, Firefox, Edge, and IE. Using position: absolute;, the button remains fixed on the page but not within the div element. It's recommended to use percentages over pixels whenever possible for better responsiveness in design.

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

How to locate the position of a specific word on a webpage using jQuery

In my div, I am struggling to search like an editor. Despite multiple attempts, I have not found a solution yet. Can someone please advise me on how to resolve this issue? <div id="content"> <p> Lorem Ipsum is simply dumm ...

Manipulate the content of any webpage without using external extensions by injecting your own HTML, CSS, and JavaScript code

I am currently following a tutorial on how to draw shapes on a local webpage using Canvas. I came across this helpful article on Html5 canvas paint. Everything is going smoothly, but now I am interested in replicating the drawing functions/CSS/HTML and "i ...

Tips for displaying an image that is embedded within a CSS file

I discovered an interesting background image embedded in a CSS file. Is there a way for me to actually view it? "iVBOR..." to "5CYII=" and saved it in a file named image.png - unfortunately, this method did not yield the desired outcome.</p> ...

What is the correct way to scroll to a specific div using jQuery?

Here is the button code I have: <button class="contactButton">XYZ</button> When the user clicks on this button, I want my page to smoothly scroll down to a specific section. I attempted to achieve this with the following jQuery code ...

Can Bootstrap CSS take precedence over a theme's styling?

After working with various WordPress theme templates, I am now ready to take on the challenge of creating my own custom themes. One aspect I would like to incorporate is utilizing Bootstrap to ensure my website is responsive. However, I have some questions ...

Differentiate pointer for checkbox HTML

Just starting out with html and css, and I'm working with a checkbox: <input type="checkbox" name="vehicle" value="Bike">I have a bike<br> My goal is to display a hand icon when hovering over the checkbox. Ho ...

Provide the email address to use on the forum

Is there a way to code an email address in HTML that will be invisible and undetectable by forums? Thank you in advance. I'm looking for a more advanced solution like this one! <h>mariaburkke76</h><h><h><h><h>< ...

The width of the Ion-slide is dynamically determined by the styling

After transitioning my Ionic 2 project to Ionic 3, I encountered issues with ion-slides which are affecting my app's functionality. Upon app loading, specific widths are being defined in the style tags on the slides, disrupting the intended styling. ...

Override the default HTML style overflow to hidden with Material UI Swipable Drawer

Currently, I'm utilizing the Material UI SwipableDrawer component which has an unexpected drawback of causing the scrollbar to disappear when the drawer is displayed. This issue overrides my specified style of "overflow-y: scroll" on the Html tag and ...

Customize the order of elements in Bootstrap for mobile devices

How can I create a responsive Bootstrap layout with 2 columns and a nested row in the right column? The desired layout is as follows: Desktop Version: --------- | 2 || 1 | | || | | |------- | || 3 | | || | | ...

Tips for adjusting the CSS of each <p> element within a designated datafield selector

My goal is to include additional text following each numerical weight measurement. Here's the structure of the HTML: <table class="shop_attributes"> <div class="field" data-field-id="product_size"> ...

Transferring HTML elements styled with CSS into Libre Office

Looking for a solution to transfer my HTML page, which is beautifully styled with CSS, into Libre Office Writer for editing. However, I am facing an issue where only the HTML content gets copied over without the accompanying style. Even after trying "Paste ...

Spacing is missing between Months and Years in the Bootstrap Datepicker

Currently, I am utilizing the Bootstrap Date picker plugin from . In their example, they showcase a grid-style format with spacing between the months and years that appears quite visually appealing. However, when implementing their JavaScript and CSS file ...

The pop-up fails to appear

Could you assist me in identifying the issue with my code? <script type="text/javascript"> function PopupCenter(pageURL, title,w,h) { var left = (screen.width/2)-(w/2); var top = (screen.height/2)- ...

What is the process for changing the border color of a Select Component?

Is there a way to change the border color of a select component in Material UI library without directly setting the property in inspect elements? I can see it working when I set the property that way, but how can this be achieved using class override? htt ...

trouble retrieving file path with PHP inside an image tag

I have created a form for uploading an image file to a folder using PHP. The upload process is successful and I can locate the uploaded file in the directory. However, when attempting to retrieve the URL of the uploaded image to display it in a specific lo ...

I'm having trouble getting this text to align properly next to the image. Plus, I can't seem to achieve a horizontal screen alignment either

There seems to be an answer available, but I'm clearly not understanding it, so I've come here seeking further explanation and assistance. CSS can be quite complex, right? Here's a screenshot of my current project: https://i.stack.imgur.com/ ...

Above, there are two components with a scrollbar, while below there is a fixed div that remains in place when the screen

Just a heads up: below is an example with code of how I envision my interface to look, but I'm not sure if it's valid? I've been trying to create an HTML5/CSS interface that resembles the MIRC fullscreen layout (check out the image below) a ...

Applying a CSS class to a jeditable input field

Is there a way to apply a simple class tag to a jeditable input field? The developer's instructions on their website () suggest using the following code snippet under "How to style elements?": $('.editable').editable('http://www.exampl ...

Encountering the error message "Failed - File incomplete" in Google Chrome while attempting to download a file from MongoDB

I have developed a JavaScript application that enables users to download a file. The page displays a clickable link to the file, which is stored in MongoDB. However, when I attempt to download the file by clicking on the link, I encounter an issue where Ch ...