CSS Styling to Align Form in Table Cell

I have some old HTML code without a CSS framework, so I'm using tables for layout. My current challenge is trying to horizontally center my form, while keeping the form fields left-aligned. Essentially, I want to achieve the following design:

+--------------------------------------------------+
|                     Welcome                      |
|           Please tell us about yourself          |
|                                                  |
|     first name            last name              |
|     +----------------+    +----------------+     |
|     |                |    |                |     |
|     +----------------+    +----------------+     |
|                                                  |
|     email address                                |
|     +--------------------------------------+     |
|     |                                      |     |
|     +--------------------------------------+     |
|     [Submit]                                     |
|                                                  |
+--------------------------------------------------+

This is how my current HTML code look like:

<table style="width:100%;">
  <tr>
    <td style="width:10%;"></td>
    <td style="width:80%; text-align:center;">
      <h1>Welcome</h1>    
      <p>Please tell us about yourself</p>
      <div>
        <form>
          <table style="text-align:left;">
            <tr>
              <td>first name</td>
              <td>last name</td>
            </tr>
            <tr>
              <td><input id="firstName" /></td>
              <td><input id="lastName" /></td>
            </tr>
            <tr><td colspan="2">email address</td></tr>
            <tr><td colspan="2"><input id="emailAddress" /></td></tr>
            <tr><td colspan="2"><input type="submit" value="Submit" />
          </table>
        </form>
      </div>
    </td>
    <td style="width:10%;"></td>
  </tr>
</table>

Although everything renders on the screen correctly, with "Welcome" and "Please tell us about yourself" centered as expected, the form itself remains aligned to the left edge. How can I centralize the form within the second cell?

Answer №1

Should you apply the margin: 0 auto style to your table?

<table style="width:100%;">
  <tr>
    <td style="width:10%;"></td>
    <td style="width:80%; text-align:center;">
      <h1>Hello There</h1>    
      <p>Please introduce yourself</p>
      <div>
        <form>
          <table style="text-align:left; margin: 0 auto;">
            <tr>
              <td>first name</td>
              <td>last name</td>
            </tr>
            <tr>
              <td><input id="firstName" /></td>
              <td><input id="lastName" /></td>
            </tr>
            <tr><td colspan="2">email address</td></tr>
            <tr><td colspan="2"><input id="emailAddress" /></td></tr>
            <tr><td colspan="2"><input type="submit" value="Submit" />
          </table>
        </form>
      </div>
    </td>
    <td style="width:10%;"></td>
  </tr>
</table>

Answer №2

To easily align the form in the center, you can achieve this by applying a style to the form element setting it to display as inline-block, as shown below:

form {
  display: inline-block;
}

Answer №3

Give this a shot

fieldset {display: block; margin-left: auto;}

https://jsfiddle.net/ahp8j2rb/1/

margin-left: auto may not always be necessary, but it can provide additional flexibility for layout purposes.

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

I am having trouble with the dropdown button in my Bootstrap - it just won't seem

I've exhausted all YouTube tutorials and followed the entire Bootstrap documentation, but I still can't seem to get it to work. Please, someone, help me figure out why it's not functioning properly :( This is the basic HTML bootstrap code I ...

Placing a picture within a grid

Hi, I'm new to "programming" and seeking assistance here on stackoverflow. I'm encountering a slight issue trying to fit an image inside my grid layout. For example, when I have an image with a specific size, it works fine. However, if the imag ...

Steps to redirect to an HTML page using Express and then utilize node modules within the HTML document

Looking for advice on how to best redirect from an index.js file containing Express and other modules, to an HTML page using Express while still being able to utilize node modules within that HTML file. Any suggestions? ...

How can I target specific screen sizes using JQuery instead of @media queries?

I am currently utilizing @media to define certain CSS styles, however now I require an equivalent check for screen size in JQuery. @media ( min-width: 33em ) { } The goal is to hide a button when the screen size is larger than 33em, and to display it wh ...

Highlight a section of the innerHTML code

Currently, I am working on creating a table that will showcase the dates of the upcoming 10 days. In order to populate the table with the day, date, month, and year, I have implemented the following JavaScript code: document.getElementById(i).innerHTML = ...

Tips for including text and a button within the footer of a Bootstrap modal

I'm facing an issue with my Bootstrap modal footer where the output shows two divs side by side, which is not the desired result. Below is the code snippet in question: <div class="modal fade" id="itiModal" role="dialog&quo ...

CSS: elements that are only visible when positioned above specific elements

Can we create an element that is only visible above specific other elements? For instance, in the following code snippet, I want the .reflection element to be visible only above the .reflective elements (located at the top and bottom) and not visible on t ...

Updating dynamic content in IBM Worklight V6.1 with jQuery Mobile v1.4.3 requires waiting for the DOM to load

I need to dynamically update the content of a div within my HTML page structure. <!DOCTYPE HTML> <html> ... <body> <div data-role="page"> <div data-role="header"> //Image </div> <!-- Th ...

Navigating through multiple overlapping scenes in Three.js using Orbit Controls

Seeking assistance with a dilemma involving the use of Orbit Controls with Multiple Overlapping Scenes. To showcase the issue, I made adjustments to the example found at multiple scenes and developed a test specific to my needs. The code can be accessed h ...

Using pixel values to define CSS gradient color stops from the end

Currently, I am working on a project involving HTML/CSS/JS where the application has a fixed size and requires precise positioning of elements according to the provided designs. Working with pixel dimensions in CSS is not a concern due to the fixed window ...

Prevent scrolling within input field

I have a text entry field with background images that separate each letter into its own box. Unfortunately, an issue arises when I reach the end of the input: the view automatically scrolls down because the cursor is positioned after the last letter enter ...

The anchor for the circular image is oversized

Currently, I have a PHP code that displays a user's profile picture, and when clicked, it takes them to their own profile. The image is displayed correctly, and the link works fine. However, there seems to be an issue with the area covered by the anch ...

Tips for eliminating dots from material icons CSS

Recently, I incorporated a stylesheet from Google's Material Icons into my web project. However, upon implementing the HTML code provided to display the icon, I encountered an unexpected issue - a dot appeared beside the icon, which I do not want to b ...

Strategies for Increasing Index Values in JavaScript

I attempted to modify the data attribute in my code snippet below. After clicking on the square, the data attribute should be incremented. However, it appears that the incrementing is not happening as expected. How can I resolve this issue? Additionall ...

What could be the reason for my ng-init not functioning properly?

<body ng-init="user=${userID};month=${month};curPageNum=${currentPage}"> Using JSP, I set initial values in the body tag. However, in the controller, I have the following code: console.debug($scope.user + " "+$scope.month} The issue is that only ...

Implementing a dynamic display of images using JavaScript: a step-by-step guide

I have a set of 4 light bulbs: <div id="bulb1" class="lightbulb"><img src=".\images\bulb.png" /></div> <div id="bulb2" class="lightbulb"><img src=".\images\bulb.png" /></div> <div id="bulb3" class ...

Select a number from an input box using Selenium in Python

Having trouble getting Selenium to select an option for me. I attempted the following: phone = Select(driver.find_element(By.NAME, "PhoneCountry")) phone.select_by_data-value(self, US) Unfortunately, it did not work as expected. Here is the HTM ...

Condense card displays according to the radio button chosen

I have been working on this .cshtml page and I am trying to implement a new functionality. The goal is to collapse cards based on the selection of a radio button. If an Existing User is selected, then "collapseone" should open; if a New User is selected, t ...

Display the div at the top - it doesn't seem to be functioning properly in the Chrome

My div has a z-index of 99999 and is displaying correctly on Firefox and Safari. However, when testing it on Chrome, the footer does not appear as the top element. What steps should I take to ensure it is the highest element? Do I need to make any additio ...

Show an HTML image encoded in base64 from its origin

Is there a way to embed a base64 image in HTML without having to paste the entire code directly into the file? I'm looking for a more efficient method. For example: <div> <p>Image sourced from an online repository</p> <img src=" ...