简体   繁体   English

如何正确对齐标签和跨度

[英]how to align label and span properly

I put together this fiddle 我把这个小提琴放在一起

What I have is 2 divs and then one of them has 2 more divs in it. 我所拥有的是2个div,然后其中一个拥有2个div。 What I am trying to do it properly position label and span within second div 我正在尝试正确地将标签放置在第二个div内

  <div id="someotherdiv">

  </div>
  <div class="reportuserinfo">
        <div class="leftdivinfo">
             <label>Teacher:</label><span>Teacher Name</span><br/>
            <label>District:</label><span>District Name</span><br/>
            <label>School:</label><span>School Name</span>
        </div>
        <div class="rightdivinfo">
            <label>Class:</label><span>Class Name</span><br/>
            <label>Content:</label><span id="currcontent">Content</span><br/>
            <label>Unit:</label><span id="currunit"></span>
        </div>
    </div>

If you look in fiddle right now label is floating to the right and so is span but they look strange, what I am attempting to accomplish is something like this on both sides of second div: 如果您现在看小提琴,标签在右边浮动,那么跨度也在浮动,但它们看起来很奇怪,我试图在第二个div的两边完成以下操作:

 Teacher:   Teacher Name
District:   District Name
  School:   School Name

The way they look right now is 他们现在的样子是

Teacher:     Teacher Name
District:   District Name
School:    School Name

Thanks for your help. 谢谢你的帮助。

You should define a min-width style in your css apart from width:40% to ensure that the text you are trying to write does not scroll if browser width is decreased 您应该在CSS中定义一个宽度小于40%的最小宽度样式,以确保如果减小浏览器宽度,则您尝试编写的文本不会滚动

.leftdivinfo {
    float:left;
    min-width: 200px;
    width:40%;
    background: yellow;
    padding-left: 30px;
    padding-right: 30px;
} 

Have you tried using tables? 您是否尝试过使用表格? You could enclose this information in a table, then right-align the text in the left column: 您可以将此信息封装在表格中,然后在左栏中将文本右对齐:

 table { border: none; } td:first-child { text-align: right; } td { padding: 5px; } 
 <table> <tr> <td>Class Teacher:</td> <td>John Smith</td> </tr> <tr> <td>District:</td> <td>Smithsville</td> </tr> <tr> <td>School:</td> <td>Smith's High School</td> </tr> </table> 

Try setting width to auto and adding display:inline-block . 尝试将width设置为auto并添加display:inline-block

.leftdivinfo {
  float:left;
  width: auto;
  background: yellow;
  padding-left: 10px;
  padding-right: 10px;
  display:inline-block
}

Tables might be a better way to make this. 表可能是实现此目的的更好方法。

EDIT: I see you want the colons aligned, add min-width:70px; 编辑:我看到你想要冒号对齐,添加min-width:70px; to the label class. 标签类。

I just added width:30%; 我刚加宽度:30%; in your css class ".reportuserinfo label" and it fixed the issue ( fiddle ). 在您的css类“ .reportuserinfo标签”中,它解决了该问题( fiddle )。 You need to define width in order to fix the text alignment issue. 您需要定义宽度才能解决文本对齐问题。 You can adjust space margins as per your requirements. 您可以根据需要调整空间边距。

Here is how your code looks like to me now :- 这就是您的代码现在对我的样子:-

.reportuserinfo label {
    float: left;;
  margin-right: 30px;
  font-weight: bold;
  text-align: right;
  width:30%;
}

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

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