Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align element to bottom of HTML Table Cell

I have multiple elements in a cell of an HTML table. I want some of the elements to be aligned to the bottom of the cell and some to be aligned at the top. I am having trouble getting the elements to align to the bottom. My table is:

    <tr>
        <td style="background-color: #007CE2">
            <p id="t1_list">test<br>another<br>testing</p>
            <input type="text" name="t1_input" id="t1_input">
            <button>
                Add
            </button>
        </td>
        <td style="background-color: #E54040">
            <p id="t2_list"></p>
            <div class="value_input2">
                <input type="text" name="t2_input" id="t2_input">
                <button>
                    Add
                </button>
            </div>
        </td>
    </tr>

However the elements within the div seem to want to stay centered in the cell, rather than stay at the bottom. I have tried two different methods so far with CSS:

div.value_input {
    position: absolute;
    bottom: 0;
}

which just takes the div down to the bottom of the page. And:

div.value_input2 {
    display: table-cell;
    vertical-align: bottom;
}

Which has no effect.

I have the code here in JSFiddle

What do I need to do to get the input box and button to align to the bottom of the cell?

like image 812
Pottsiex5 Avatar asked Mar 03 '17 12:03

Pottsiex5


People also ask

How do you align something to the bottom in HTML?

If position: absolute; or position: fixed; - the bottom property sets the bottom edge of an element to a unit above/below the bottom edge of its nearest positioned ancestor. If position: relative; - the bottom property makes the element's bottom edge to move above/below its normal position.

How do you align an element to the bottom of a div?

Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.

How do you align a table at the bottom of a page in HTML?

Use the bottom alignment in CSS. This will align the div only and not the table. Your table will align because it is within the div.

How to align text in table cells using CSS?

So, use CSS to align text in table cells. The text-align will be used for the <td> tag. We’re using the style attribute for adding CSS. The style attribute specifies an inline style for an element. The attribute is used with the HTML <td> tag, with the CSS property text-align.

How to align text inside a block in HTML?

Use the text-align property to align the inner content of the block element. Use the bottom and left properties. The bottom property specifies the bottom position of an element along with the position property.

Which HTML attribute supports the vertical alignment of cell content?

HTML valign attribute supports col, colgroup, tbody, td, tfoot, th, thead, tr elements. Where ElementName is any supported element. Predefined. Sets the vertical alignment of cell content top. Sets the vertical alignment of cell content center. Sets the vertical alignment of cell content bottom.

How do I use the vertical-align property in HTML?

The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or <td>. By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements). The following example sets the vertical text alignment to bottom for <td> elements:


Video Answer


3 Answers

You need height:somepx for vertical align to work in this.

like image 192
Ashwin Mothilal Avatar answered Oct 15 '22 14:10

Ashwin Mothilal


Make the table cell position: relative, and then you can try position: absolute on the div again...

table tr td {
  position: relative;
}

div.value_input2 {
  position: absolute;
  bottom: 0;
}

fiddle

like image 36
sol Avatar answered Oct 15 '22 15:10

sol


You need to set the parent elements position to relative position:relative in order to use absolute positioning. Here is a working snippet.

table {
    		border-collapse: collapse;
		}

		table, th, td {
		    border: 2px solid black;
        position:relative;
		}

		div.value_input {
			position: absolute;
      bottom: 0;
		}
    
    div.value_input2 {
      position:absolute;
      bottom:0;
    }
<table>
		<tr>
			<th style="background-color: #007CE2">
				Test
			</th>
			<th style="background-color: #E54040">
				Test
			</th>
		</tr>
		<tr>
			<td style="background-color: #007CE2">
				<p id="t1_list">test<br>another<br>testing</p>
				<input type="text" name="t1_input" id="t1_input">
				<button>
					Add
				</button>
			</td>
			<td style="background-color: #E54040">
				<p id="t2_list"></p>
				<div class="value_input2">
					<input type="text" name="t2_input" id="t2_input">
					<button>
						Add
					</button>
				</div>
			</td>
		</tr>

		<tr>
			<th style="background-color: #8BC34A">
				Test
			</th>
			<th style="background-color: #FF9800">
				Test
			</th>
		</tr>
		<tr>
			<td style="background-color: #8BC34A">
				<p id="t3_list"></p>
				<input type="text" name="t3_input" id="t3_input">
				<button>
					Add
				</button>
			</td>
			<td style="background-color: #FF9800">
				<p id="t4_list"></p>
				<input type="text" name="t4_input" id="t4_input">
				<button>
					Add
				</button>
			</td>
		</tr>
	</table>
like image 42
L L Avatar answered Oct 15 '22 14:10

L L