Search code examples
htmlcsscentertext-align

Text align left in a centered div with input fields


I have input fields which are supposed to be shown centered and then the texts to these input fields are supposed to be aligned left and "start" with the input fields.

http://jsfiddle.net/tfbatp5v/2/

.inputdes {
    color: #9b9b9a;
    font-size:20px;
    height: 200px;
}
.blue {
    height: 70px;
}
<div align="center" id="parent">
    <div class="welcome">Welcome</div>
    <div class="inputdes">
        <div class="blue">text1<br><input id="inputfield1" /></div>
        <div class="blue">text2<br><input id="inputfield2" /></div>
        <div class="blue">text3<br><input id="inputfield3" /></div>
    </div>
</div>

However, no matter what I do, every time when I use text-align: left; it automatically aligns the inputfields left as well. I tried to group the text areas together with class names but it doesn't work. Does anyone know the answer?

Thanks !


Solution

  • Try something like the following. The idea is that we limit the width of the .inputdes div, then put the text in a nested div that has text-align: left. That way we can have the inputs centered but the text aligned left within its div.

    .inputdes{
        color: #9b9b9a;
        font-size:20px;
        height: 200px;
        width: 200px;
    }
    .inputdes > div > div {
        text-align: left;
        margin: 0 15px;
    }
    .blue{
        height: 70px;
    }
    <div align="center" id="parent">
        <div class="welcome">Welcome</div>
        <br>
        <div class="inputdes">
            <div class="blue" ><div>text1</div>   
                <input id="inputfield1"/></div>
            <div class="blue" ><div>text2</div>
                <input id="inputfield2" /></div>
            <div class="blue" ><div>text3</div>
                <input id="inputfield3" /></div>
        </div>
    </div>