Utilizing tables to perfectly position text and dropdown options within a form

Although I know using tables is not ideal, it seems to be the most practical solution in this case.

I'm attempting to align text and dropdown menus in a form. The challenge arises when the text can vary from one line to two lines, making it difficult to set a consistent height for alignment while keeping them on the same line.

Adding padding to accommodate single-line text throws off the alignment when there's a two-line text. Is there a way to create a relative solution that works for both scenarios?

HTML

<table>

<tbody>
<tr>
    <td style="width: 120px;" class="level-heading">
        <div style="position: absolute;">Client Name</div>

    </td>
    <td style="width: 120px">
        <div class="level">
            <select name="ctl00$MainContent$cmbClientName" id="MainContent_cmbClientName" style="width:140px;">
<option selected="selected" value="Select One">Select One</option>

</select>
        </div>
    </td>
    <td style="width: 120px;" class="level-heading">
        <div style="position: absolute;">Business Segment</div>

    </td>
    <td style="width: 120px">
        <div class="level">
            <select name="ctl00$MainContent$cmbBusinessSegment"  id="MainContent_cmbBusinessSegment" style="width:140px;">
<option selected="selected" value="Select One">Select One</option>

</select>
        </div>
    </td>
    <td style="width: 120px;" class="level-heading">Business Segment Category

    </td>
    <td style="width: 130px">
        <div class="level">
            <select name="ctl00$MainContent$cmbBusinessSubGroup" id="MainContent_cmbBusinessSubGroup" class="level-big" style="width:180px;">
<option selected="selected" value="Select One">Select One</option>

</select>
        </div>
    </td>
</tr>



<tr>
    <td style="width: 120px;" class="level-heading">Experience Test From

    </td>
    <td style="width: 120px">
        <div class="level">
            <select name="ctl00$MainContent$cmbFrom"  id="MainContent_cmbFrom" style="height:16px;width:140px;">
<option selected="selected" value="Select One">Select One</option>

</select>
        </div>
    </td>
    <td style="width: 120px;" class="level-heading">Experience Test To

    </td>
    <td style="width: 120px" align="justify">
        <div class="level">
            <select name="ctl00$MainContent$cmbTo" id="MainContent_cmbTo" style="width:140px;">
<option selected="selected" value="Select One">Select One</option>

</select>
        </div>
    </td>
    <td style="width: 120px">
        <span id="MainContent_lblPeriodTo"></span>
    </td>
    <td style="width: 130px"></td>
</tr>
<tr>
    <td colspan="6" style="height: 10px"></td>
</tr>

</tbody>
    </table>

CSS

This is the CSS related to the aforementioned issue.

.level-heading {
    font-size: 12px;
    padding-bottom: 12px;
}
.decision-page select {
    font-size: 12px;
}
.fullpage-width {
    width: 896px;
}

Answer №1

While the road you're on may lead to frustration, I can help answer your question:

Check out this fiddle for a solution: http://jsfiddle.net/7tyjx/

First step is to eliminate absolute positioning

<td style="width: 120px;" class="level-heading">
    <div>Client Name</div>
</td>

Next, ensure vertical alignment in your td's is set to middle

td {
    vertical-align: middle;
}

Then, get rid of padding from the .level-heading

.level-heading {
    font-size: 12px;
}

I've added borders in the fiddle to visualize the alignment and increased td height for clarity

Simplify by using class names to identify elements and styles, then move those styles to your stylesheet -- e.g. create a style like .level-heading instead of adding inline width styles.

.level-heading { width: 120px; }

and so forth...

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

Connect identical Microdata domains without including one inside the other

I am trying to connect the same Microdata domain in a different div, this is what I currently have: <div id="colleau" itemscope itemtype="http://schema.org/Organization"> <!-- Here I have information like name, foundingDate, founders, etc... -- ...

Transforming the table into a list by categorizing every 3 rows together

SharePoint tends to use nested tables excessively, causing issues when trying to implement a jQuery carousel on them. To resolve this, I discovered a piece of jQuery code that can convert a table into a list: var list = $("<ul/>"); $('#tab1&apo ...

Error: Attempting to access the 'main' property of an undefined object - React dilemma

For my React webpage, I decided to create a theme using createMuiTheme from material-ui, and here is the palette I defined: palette: { primary: { main: "#333333" }, secondary: { main: "#727171" }, background: { paper: " ...

The chosen option does not equal the value retrieved by using jQuery's

I am perplexed by the situation at hand. It seems that there is a discrepancy in the selected option of my select element. Despite appearing as the empty default option on top, the inspected element reveals it displaying a value of 13. https://i.sstatic.n ...

Dynamic extension of classes in SCSSORExtending classes

When working with Vue, I encountered a situation similar to :style="{ [`--chip-bg-hover`]: hoverColorValue, [`--chip-bg-active`]: activeColor, [`--chip-border-hover`]: borderHoverColorValue, }" Then, i ...

The uncertainty surrounding the usage of checkboxes in D3.js

I am faced with the challenge of adding checkboxes to multiple groups within an SVG, each containing a circle and text element. I have attempted to accomplish this by using D3 to append foreignObject tags and then add checkboxes to each group. The code I h ...

Restrict the amount of decimal places allowed in input text

Another question on Stack Overflow addresses limiting the number of characters allowed in a form input field. I am specifically looking to restrict the number of digits that can come after the decimal point to 2 digits. <input type="text" maxlength="2 ...

Grid element's height does not correspond to the responsive square child dimension

In my latest project, I am developing a web application using React and Material-ui. One of the challenges I have encountered is creating a responsive square div for a map component. Unfortunately, attempting to implement a solution from a trick on iamstev ...

What is the best way to elegantly display a block containing background and text using Angular and ngAnimate?

I'm a beginner when it comes to JavaScript and Angular. I am attempting to use ng-show and ng-hide for my background and text elements. However, I am experiencing an issue with my text: It smoothly hides, but when it is shown again, the text appears b ...

AngularJS is failing to remove elements following an update from version 1.0.8 due to issues with the if

Let's talk about this div: <div ng-if="myCheck"> content </div> In my AngularJs controller, I am controlling the value of myCheck which determines whether or not the div is displayed. By default, myCheck is set to false, hiding the div e ...

Choosing the right eldest offspring

I've been struggling to target the initial div with the 'section' class in my CSS code. Here's a simplified version of my HTML: <div id="page_container"> <div></div> // this div has content, but no id or class ...

The CSS classes from Bootstrap are failing to function properly despite my best efforts in

I have been experiencing issues with the row and col classes in Bootstrap. <!DOCTYPE html> <html lang="en> <head> <meta charset="utf-8> <meta http-equiv="X-UA-Compatible" content="IE-edge> <meta name="viewport" content=" ...

Does HTML elements come with a pre-set background color of white or transparency by default?

Feeling a bit confused about a straightforward issue. Can someone clarify for me - What is the original background color of HTML elements? Is it white by default or transparent? ...

Is it better to use multiple external style sheets?

Hello! I am looking to implement a pop-up login screen by integrating downloaded code. The issue I am facing is that the CSS file that accompanies it clashes with my current one. Is there a way to have a specific style sheet only affect certain div tags, ...

Tips for retrieving selected items in an array object

Within my UI, I have a select field that, on change, populates corresponding data in a div with nested ul and li elements. What I am attempting to achieve is to convert the selected items within the list (which include checkboxes) into an object of arrays ...

What measures can be taken to prohibit a user from accessing a client-side HTML file using express?

I am currently developing a task management application called "todolist." In my node.js code, I utilize express and grant it access to my directory named Client: var app = express(); app.use(express.static(__dirname + "/Client")); The contents of my Cl ...

Unable to process form submission using Ajax in ASP.NET MVC

I'm having trouble with submitting a form using ajax and opening a modal dialog after the ajax function is successful. Whenever I click the submit button, the process doesn't complete. Where could the issue be, in the ajax method or somewhere el ...

Having trouble getting CSS calc to work on a table cell? Wondering how to make two out of four columns the same width?

Is it possible that CSS calc doesn't work on a table cell? It seems that no matter what size I change 90px to, the result looks the same. Even after trying various calculations with calc, I cannot seem to achieve the desired number of 230. The goal i ...

What is the best way to add a custom color to a blank div that is designed with the Bootstrap grid system?

Let's analyze this scenario <div class="row"> <div class="col-md-3 col-sm-3 col-xs-3"></div> <div class="col-md-6 col-sm-6 col-xs-6"></div> <div class="col-md-3 col-sm-3 col-xs-3"></div> </div& ...

"Unlocking the full potential of sidebars.js: A guide to seamless

I recently attempted to implement THIS plugin on a small project of mine. Despite reviewing the USAGE section multiple times and examining the example files provided after downloading from git (unfortunately, none of them seemed to work!), I have construct ...