The form's label occupies excessive space beyond what is necessary

I am facing an issue with the layout of my form. It is structured in three columns: the first column for labels, the second column for inputs, and the third column for units (such as cm or kg). All components are placed inside a table within the form. However, the first column is occupying more horizontal space than necessary even though it has no padding, margin, or border. Below is the code snippet:

body {
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
color: #222222;
}

table {
width: 100%;
white-space: nowrap;
}

input {
background-color: #DACBDF;
padding: 8px 10px 8px 10px;
width: 91.5%;
font-family: Georgia;
font-size: 13px;
border: 1px solid #EEEEEE;
}

I have attempted to address this issue by setting td {width: auto}, but it does not seem to resolve the problem. Any insights on why this might be happening?

UPDATE: Upon further examination, I realize that my question may lack clarity. My ultimate goal is for the width of the td tags to adjust dynamically based on the content inside them. Can anyone provide guidance on achieving this functionality?

Answer №1

It seems that what you are trying to achieve is not feasible.

If your table has a set width:100%, then the tds will automatically expand to fill the available space in order to maintain the table's specified width.

You might need to specify which columns should have flexible widths and which ones should remain fixed. Unfortunately, you cannot have both options simultaneously.

You can try something like this:

tr > td:last-of-type, tr > td:first-of-type {
  width: 50px !important; /* adjust width as necessary to accommodate longer words */
}

/* changed 'td' selector to 'input' */
input{
    width: 95%;
    /* other properties */
}

I've simplified your code into a MCVE.

Check out the live demonstration here:

table {
  width: 100%;
  white-space: nowrap;
}

input {
  background-color: #DACBDF;
  padding: 8px 10px 8px 10px;
  width: 95%;
  font-family: Georgia;
  font-size: 13px;
  border: 1px solid #EEEEEE;
}

input:hover {
  border: 1px solid green;
}

input:focus {
  background-color: #DACBFF;
  border: 1px solid #FF0000;
}

input[type="submit"] {
  font: 13px "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  padding: 8px 15px 8px 15px;
  width: auto;
  border: 1px solid #EEEEEE;
  color: #222222;
}

input[type="submit"]:hover {
  background: #4691A4;
  box-shadow: none;
  -moz-box-shadow: none;
  -webkit-box-shadow: none;
}


tr > td:last-of-type, tr > td:first-of-type {
  width: 50px !important; /* adjust width as necessary to fit longest word */
}
<table>
  <tr>
    <td><label>Height:</label></td>
    <td><input type="text" id="height" name="height" value="" maxlength="5" /></td>
    <td>cm</td>
  </tr>
  <tr>
    <td><label>Navel:</label></td>
    <td><input type="text" id="navel" name="navel" value="" maxlength="5" /></td>
    <td>cm</td>
  </tr>
  <tr>
    <td><label>Neck:</label></td>
    <td><input type="text" id="neck" name="neck" value="" maxlength="4" /></td>
    <td>cm</td>
  </tr>
  <tr>
    <td><label>Weight:</label></td>
    <td><input type="text" id="weight" name="weight" value="" maxlength="4" /></td>
    <td>kg</td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" id="calculate" name="calculate" value="Calculate" /></td>
    <td></td>
  </tr>
</table>

Answer №2

If you include the CSS property border-collapse: collapse; in the styling for the table element, it can improve the layout to some extent:

body {
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
color: #222222;
}

table {
width: 100%;
white-space: nowrap;
    border-collapse: collapse;
}
... (CSS styling continues)
<!DOCTYPE HTML>
<html>
<head>
<title>Body Fat Calculator</title>
... (HTML content begins) 

Answer №3

table{table-layout: auto;}
td:first-of-type{width: custom-size;}

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

placing additional containers at the bottom rather than on the right side

Hey there! I'm currently working on a simple website and I'm having trouble getting the duplicated form rows to appear aligned below the previous one. Here's what I have right now: var i = 0; var original = document.getElementById("dupl ...

Tips on reducing the gap between two tags inside a div element

I'm facing an issue with the spacing between a H1 and H2 tags inside a "welcome" div. I want to reduce the gap between the word welcome and the second line of text, but I can't seem to figure it out. Any suggestions on how to solve this simple pr ...

How can I set a minimum height for a table on my website?

Is there a way to make a table that is at least 50px in size, but can still enlarge if there is a lot of text inside? I'm not sure if this even makes sense, but any help would be appreciated. Thank you! ...

Easily adjustable centered div on the screen with a width to height ratio of 3:8

Feeling a bit lost here. I've been struggling for 2 days trying to tackle what seems like a simple task. I want to create a widget that resembles a calculator, but not exactly. The main goal is to have a black colored div with a width/height ratio of ...

Establish breakpoints for columns in Bootstrap 4

I have been working on creating a grid layout using rows and columns, but I am struggling with setting up responsive breakpoints using Bootstrap. Specifically, I want the layout to display three columns on desktop screens, but collapse into a single column ...

What is the best way to extend the width of an element within a Bootstrap column beyond the column itself?

Apologies for any language errors, but I have a query regarding Bootstrap. I am currently working on making a website responsive and I have a row with 4 columns set up like this: The "seeMore" div is initially hidden and on clicking the boxToggle element ...

Image Positioned Outside Border in HTML

I'm working on creating a personalized homepage for my browser and I want to display 3 images with captions evenly spaced within a divider. Although everything seems to be in place, the border of the outermost divider is not covering the images. How c ...

What steps can be taken to solve the JavaScript error provided below?

My objective is to create a new variable called theRightSide that points to the right side div. var theRightSide = document.getElementById("rightSide"); Once all the images are added to the leftSide div, I need to use cloneNode(true) to copy the left ...

Having trouble with setting padding to 0 for Accordion Answers

I am facing an issue with the padding in my 'panel' element. Even though I have set the padding to 0, there is still some unwanted white space visible (highlighted in red blocks in the image below). Can anyone help me identify where this padding/ ...

What is the method to retrieve this content using PHP's Simple HTML dom parser?

If I have the following HTML snippet in an HTML document: <div class="info"> <p><span class="strong">Score</span>4.32/5</p> <p><span class="strong">Type</span>TV</p> <p><span class ...

Storing `this` in a variable that is assigned by an element using inline JavaScript is applicable for either all elements or specifically the active element that contains the assigned value

Consider this scenario: if we include the this attribute in an element's onclick event like onclick="a=this", are we selecting the "p" element or can we use the variable a as an alternative to this? Take a look at this code snippet- <p onclick=" ...

Issue with Registration Form Redirection

This registration form has been created using a combination of HTML, PHP, and MYSQL. However, after filling out the form and submitting it, the page simply reloads. I'm unsure if there is an issue with my PHP or HTML code. I'm at a loss as to w ...

Problems encountered with the navigation on mobile devices

Seeking advice on debugging a non-functioning mobile side menu implementation. The main site, a WordPress site, works perfectly. However, the offsite white label version does not. All HTML has been copied to the white label and necessary CSS and scripts h ...

Chrome's Surprising Cross-Domain File URL Problem

Is anyone else experiencing the issue with Chrome throwing a Cross Domain Error when using file URLs? I'm trying to embed an SVG document into an HTML page using the object tag with a relative path in the data attribute. Upon the onload event, I need ...

Unable to move up and down in my iframe

I am currently working on an Instagram page that is visible through an iframe. Everything seems to be functioning fine on my iMac, but I am facing an issue with scrolling on my iPhone and iPad? After reviewing my code, I couldn't find any hidden over ...

Unlocking the Secret to Rotating a GroundOverlay on Google Maps

I am currently working on a map project that involves adding shapes and locations onto the map and saving it. However, I am encountering an issue with groundoverlay rotation. Even though there is no built-in rotation property in the Google Maps documenta ...

Having difficulties in loading the CSS file for my real-time chat application built with Express and Socket.io

Hi everyone, I've been struggling with an issue for the past couple of days where I can't seem to load my CSS for my Express app. If anyone has a solution, I would greatly appreciate the help. Below is my server.js code: const express = requir ...

Display or conceal content seamlessly without the need to refresh the page

I am working on a project where I have three different content boxes that I need to toggle between using controls. The HTML code for the content boxes is as shown below: <div id="leermat1"> Content here <a class="pag-next">Next</a> ...

PHP - the button remains unchanged after submission

My Objective: Upon button press, I want to update the database table. Additionally, the button should change its color and text. Issue at Hand: The button fails to change unless I manually refresh the page. I attempted using echo "<meta http-eq ...

Exploring the benefits of utilizing CSS Variables for adjusting position offsets in

I've been learning about using native CSS variables to declare colors, but I'm curious if it's possible to create a variable for adjusting top and left positions. In my CSS template, text boxes are overlaid on an image, and the layout change ...