简体   繁体   English

div内部左右对齐

[英]Left and right alignment inside div

I am trying to design the layout in the below screenshot 我正在尝试在下面的屏幕截图中设计布局

div内部左右对齐

I tried to implement the same in fiddle 我试图在小提琴中实现同样的目的

http://jsfiddle.net/NNLct/1/ http://jsfiddle.net/NNLct/1/

<div  id="CorpDealerSearch" >
  <div class="left"> DealerName </div>
  <div class="left"> Region </div>
  <div class="left"> DealerCode </div>
  <div class="left"> Area </div>
  <div class="left">
    <input type="text"/>
  </div>
  <div class="left">
    <input type="text"/>
  </div>
  <div class="left">
    <input type="text"/>
  </div>
  <div class="left">
    <input type="text"/>
  </div>
</div>

Please help in suggesting proper css to get the design 请帮助建议正确的CSS以获得设计

Have a look at this FIDDLE 看看这个FIDDLE

HTML HTML

<div class='table'>
    <div class='row'>
        <div class='cell'>DealerName
            <input type='text' />DealerCode
            <input type='text' />
        </div>
        <div class='cell'>Region
            <input type='text' />Area
            <input type='text' />
        </div>
    </div>
</div>

CSS CSS

body{
    width:100%;
    padding:0;
    margin:0;
    box-sizing:border-box;
}
.table {
    display:table;
    width:100%;
}
.row {
    display:table-row;
}
.cell {
    display:table-cell;

    padding-right:20px;
}
input {
    display:block;
    width:100%;
}

Try something else: 尝试别的东西:

http://jsfiddle.net/Ta6Qk/ http://jsfiddle.net/Ta6Qk/

HTML HTML

<div class="main">
    <div>
        <label>Data</label>
        <input type="text" value="">
    </div>

    <div>
        <label>Data</label>
        <input type="text" value="">
    </div>

            <div>
        <label>Data</label>
        <input type="text" value="">
    </div>

    <div>
        <label>Data</label>
        <input type="text" value="">
    </div>
</div>

and CSS 和CSS

.main {
    width: 400px;
    position: relative;
}

.main div {
    border: none;
    display: inline-block;
    width: 44%;
    margin-right: 3%;    
}

input {
    width: 100%;
}

label {
    display:block
}

You have to include label and input in the same div: http://jsfiddle.net/NNLct/5/ 你必须在同一个div中包含标签和输入: http//jsfiddle.net/NNLct/5/

Don't forget you can also use label tags to associate the text to the related input. 不要忘记您也可以使用标签标签将文本与相关输入相关联。

 <div id="CorpDealerSearch" >

    <div class="left">
    DealerName <input type="text"/>
    </div>

    <div class="left">
    Region <input type="text"/>
    </div>

    <div class="left">
    DealerCode <input type="text"/>
    </div>

    <div class="left">
    Area <input type="text"/>
    </div>

</div>

CSS: CSS:

.left 
{
float:left;
}

Going by your existing markup, here is a : DEMO 按照现有的标记,这里是: DEMO

Float the elements and after every 2 element, clear the float s, as simple as that!! Float元素,每2个元素后, clear float s,就像那样简单! :) :)

CSS CSS

.left {
    float:left
}
.clr{clear:both}

HTML HTML

<div id="CorpDealerSearch">
    <div class="left">DealerName
        <br />
        <input type="text" />
    </div>
    <div class="left">Region
        <br />
        <input type="text" />
    </div>
    <div class="clr"></div>

    <div class="left">DealerCode
        <br />
        <input type="text" />
    </div>
   <div class="left">DealerCode
        <br />
        <input type="text" />
    </div>

</div>

See this fiddle: 看到这个小提琴:

http://jsfiddle.net/NNLct/16/ http://jsfiddle.net/NNLct/16/

Using display:table; 使用display:table; display:table-row; display:cell;

First I would change the html to: 首先,我将html更改为:

HTML HTML

<div  id="CorpDealerSearch" >
<div>
<label for="dealerName">DealerName</label>
<input type="text" id="dealerName" name="dealerName">       
<label for="Region">Region</label>
<input type="text" id="Region" name="Region">
</div>  
<div>
<label for="DealerCode">DealerCode</label>
<input type="text" id="DealerCode" name="DealerCode">       
<label for="Area">Area</label>
<input type="text" id="Area" name="Area">
</div>
</div>

And the css would be: 而css将是:

CSS CSS

#CorpDealerSearch label, input{
 float:left;
 clear:both;
}
#CorpDealerSearch div{
 display:block;
 float:left;
 width:200px;    
}

hope this will help. 希望这会有所帮助。

Here's the fiddle: Check this 这是小提琴: 检查一下

Here's the HTML Code: 这是HTML代码:

<div id="container">
    <div id="left" class="left">
        <div id="top-left" class="innerdiv">
            <div id="lbl-tl">
                Dealer Name:
            </div>
            <div id="txt-tl">
                <input type="text" style="width: 90%;" />
            </div>
        </div>
        <div id="bottom-left" class="innerdiv">
            <div id="lbl-bl">
                Dealer Code:
            </div>
            <div id="txt-bl">
                <input type="text" style="width: 90%;" />
            </div>
        </div>
    </div>
    <div id="right" class="right" >
        <div id="top-right" class="innerdiv">
            <div id="lbl-tr">
                Region:
            </div>
            <div id="txt-tr">
                <input type="text" style="width: 90%;" />
            </div>
        </div>
        <div id="bottom-right" class="innerdiv">
            <div id="lbl-br">
                Area:
            </div>
            <div id="txt-br">
                <input type="text" style="width: 90%;" />
            </div>
        </div>
    </div>
</div>

And here's the CSS: 这是CSS:

    *
    {
        margin: 0px;
        padding: 0px;
    }
    #container
    {
        width: 700px;
        min-height: 150px;
    }

    .left
    {
        width: 49%;
        min-height: 150px;
        float: left;
    }

    .right
    {
        min-height: 150px;
        width: 49%;
        float: right;
    }

    .innerdiv
    {
        height: 75px;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM