skip to Main Content

I try to align the bottom border of an input element and a textarea so that both bottom borders of the input and the textarea are exacty aligned and on the same line. Both the input element and the textarea are inside a table. I build my table with divs (display: {table, table-row-groupe, table-row and table-cell}. So far, I have not been able to solve the problem and when I view the page in Firefox and Microsoft Edge, the bottom borders which I indicated with white color (on a black background) are not horizontally aligned.

Therefore I’m going to provide here a working test case, so that you might be able to reproduce easily and instantly my problem.

Note : For this test case to work, both CSS and HTML file must be in the same directory.

The HTML file (test.html):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Alignment example</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Input and Textarea align example">
  <link rel="stylesheet" href="styles.css">
</head>
<body style="background-color: #000000;">
<div style="width: 60%;">
    <div class="DivTab">
        <div class="DivTabBody">
            <div class="DivTabRow">
                <div class="DivTabCell" style="width: 30%;">
                    <input id="paramKey" type="text" class="KeyClass">
                </div>
                <div class="DivTabCell" style="width: 70%;">
                    <textarea id="paramValue" rows="1" 
                        spellcheck="false" class="ValueClass"></textarea>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

The CSS file (styles.css):

.DivTab {
    display: table;
    width: 100%;
    height: 100%;
    color: #ffffff;
    background-color: #000000;
}

.DivTabBody {
    display: table-row-group;
}

.DivTabRow {
    display: table-row;
    background-color: #000000;
}

.DivTabCell {
    display: table-cell;
    vertical-align: middle;
    text-align: start;
    padding-top: 0px;
    padding-bottom: 0px;
    color:  #cfd0ce;
    background-color: #000000;
}

.KeyClass {
    background-color: #000000;
    color: #ffffff;
    padding-left: 5px;
    padding-right: 5px;
    border: none;
    border-bottom: solid #ffffff 1px;
    width: 100%;
    font-family: Consolas;
}

.ValueClass {
    background-color: #000000;
    color: #ffffff;
    padding-left: 5px;
    padding-right: 5px;
    border: none;
    border-bottom: solid #ffffff 1px;
    margin-left: 5px;
    resize: none;
    width: 95%;
    font-family: Consolas;
    overflow-y: hidden;
}

Could you kindly indicate, where is my mistake and how can I make these two bottom borders aligned?

Thanks in advance.

2

Answers


  1. The issue with the alignment of bottom border of the input and textarea element
    use box-sizing: border-box in these classes .KeyClass, .ValueClass and also use vertical-align: middle; in class .DivTabCell
    Here is working example on my side: enter image description here

    test.html

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="utf-8">
        <title>Alignment example</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Input and Textarea align example">
        <link rel="stylesheet" href="styles.css">
    </head>
    
    <body style="background-color: #000000;">
        <div style="width: 60%;">
            <div class="DivTab">
                <div class="DivTabBody">
                    <div class="DivTabRow">
                        <div class="DivTabCell" style="width: 30%;">
                            <input id="paramKey" type="text" class="KeyClass">
                        </div>
                        <div class="DivTabCell" style="width: 70%;">
                            <textarea id="paramValue" rows="1" spellcheck="false" class="ValueClass"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    
    </html>
    

    styles.css

    .DivTab {
           display: table;
           width: 100%;
           height: 100%;
           color: #ffffff;
           background-color: #000000;
        }
        
       .DivTabBody {
           display: table-row-group;
        }
        
        .DivTabRow {
           display: table-row;
           background-color: #000000;
         }
        
         .DivTabCell {
            display: table-cell;
            /* add changes */
            vertical-align: middle; 
            text-align: start;
            padding: 0;
            color: #cfd0ce;
            background-color: #000000;
         }
        
        .KeyClass,
        .ValueClass {
            background-color: #000000;
            color: #ffffff;
            padding-left: 5px;
            padding-right: 5px;
            border: none;
            border-bottom: solid #ffffff 1px;
            font-family: Consolas;
            /* changes */
            box-sizing: border-box;
            line-height: 1.5;
            vertical-align: bottom;
        }
        
        .KeyClass {
            width: 100%;
        }
        
        .ValueClass {
            margin-left: 5px;
            resize: none;
            width: calc(100% - 5px);
            overflow-y: hidden;
            height: 22px;
        }
    

    This is working on my side hopefully that’s works for you, Thanks.

    Login or Signup to reply.
  2. If your requirement is to align elements side by side then using flex will be much better option. Talking about issue if you inspect the textarea then you will observe that it is floating to top side of its parent div and that is causing issue of input and textarea not aligning horizontally to fix that just add display: flex; next to width: 70%;

    But if you want to make your code more flexible then add display: flex to DivTabRow & DivTabCell. You can ignore using display: table-cell; if you don’t have specific need.

    That’s all. Below is working example.

    body{
      margin: 0;
      padding: 0;
      background-color: black;
    }
    
    .DivTab {
        display: table;
        width: 100%;
        height: 100%;
        color: #ffffff;
        background-color: #000000;
    }
    
    .DivTabBody {
        display: table-row-group;
    }
    
    .DivTabRow {
        display: flex;
        background-color: #000000;
    }
    
    .DivTabCell {
        display: flex;
        vertical-align: middle;
        text-align: start;
        padding-top: 0px;
        padding-bottom: 0px;
        color:  #cfd0ce;
        background-color: #000000;
    }
    
    .KeyClass {
        background-color: #000000;
        color: #ffffff;
        padding-left: 5px;
        padding-right: 5px;
        border: none;
        border-bottom: solid #ffffff 1px;
        width: 100%;
        font-family: Consolas;
    }
    
    .ValueClass {
        background-color: #000000;
        color: #ffffff;
        padding-left: 5px;
        padding-right: 5px;
        border: none;
        border-bottom: solid #ffffff 1px;
        margin-left: 5px;
        resize: none;
        width: 95%;
        font-family: Consolas;
        overflow-y: hidden;
    }
    <div style="width: 60%;">
        <div class="DivTab">
            <div class="DivTabBody">
                <div class="DivTabRow">
                    <div class="DivTabCell" style="width: 30%;">
                        <input id="paramKey" type="text" class="KeyClass">
                    </div>
                    <div class="DivTabCell" style="width: 70%;">
                        <textarea id="paramValue" rows="1" 
                            spellcheck="false" class="ValueClass"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search