简体   繁体   English

如何居中对齐表格的 label

[英]how to center align the label for form

I am using my own classes and tags for HTML and CSS forms. I want to align the label text in middle.我正在为 HTML 和 CSS forms 使用我自己的类和标签。我想在中间对齐 label 文本。

I tried to use it using line height but it get messier when the text length is heavy.我尝试使用行高来使用它,但是当文本长度很长时它会变得更加混乱。

and is it possible to use vertical align: middle;是否可以使用vertical align: middle; Because that's not even working因为那甚至不起作用

HTML: HTML:

<div class="type-details">
     <div class="col-xs-12 no-padding text-group">
          <span class="col-md-4 no-padding form-label">Placeholder:</span>
          <div class="col-md-7 no-padding">
               <input class="form-text" type="text">
          </div>
      </div>
</div>

CSS: CSS:

.type-details {
    border-bottom: 1px solid #e9eef0;
    padding: 20px;
    width: 60%;
    float: left;
}
.form-label {
    float: left;
    min-width: 17px;
    font-size: 11px;
    text-transform: uppercase;
    color: #62696d;
    margin-right: 15px;
}
.no-padding {
    padding: 0px;
}
input.form-text {
    background: #fff;
    border: 1px solid #dae2e6;
    font-size: 12px;
    color: #62696d;
    padding: 6px 8px;
    box-shadow: none;
    border-radius: 0;
    line-height: normal;
    display: inline-block;
    width: 100%;
    margin-bottom: 0px;
}

Check this fiddle https://jsfiddle.net/kuascjpo/2/ I want to align the span="form-label" vertically center检查这个小提琴https://jsfiddle.net/kuascjpo/2/我想对齐 span="form-label" 垂直居中

There are a few ways you can do this. 有几种方法可以做到这一点。 Here is a fiddle: https://jsfiddle.net/sheriffderek/b9jg33b3/ 这是一个小提琴: https//jsfiddle.net/sheriffderek/b9jg33b3/

Markup 标记

<form action="" class="your-thing">

  <label class="input-w">
    <span class="label">First<br> name:</span>
    <input type="text" placeholder='sheriff'>
  </label>

  <label class="input-w">
    <span class="label">Last name:</span>
    <input type="text" placeholder='derek'>
  </label>

</form>


1. With display: inline-block; 1.带display: inline-block;

The trick is that you need the element to be display inline- block . 诀窍是你需要元素显示内联 This way, you can use the vertical-align: middle; 这样,你可以使用vertical-align: middle; property of inline elements - and have the other properties of block elements too. 内联元素的属性 - 并且还具有块元素的其他属性。 Also, you don't want to use floats in this case. 此外,在这种情况下,您不想使用浮动。 So, 所以,

.inline-block .input-w {
  display: block; // to stack them
  width: 100%;
}

.inline-block .label, .inline-block input {
  /* float: none; you may need this to overide previous float rules */
  display: inline-block;
  vertical-align: middle;
}


2. The other way would be to use flexbox display: flex; 2.另一种方法是使用flexbox display: flex;

This involves a little more concern about the markup and the parent of these inputs. 这涉及对标记和这些输入的父级的更多关注。

.flex-box {
  display: flex;
  flex-direction: column;
}

.flex-box .input-w {
  display: flex;
  flex-direction: row;
}

They both have their strengths and side-affects. 他们都有自己的优势和副作用。 : ) :)

A simple approach/hack that works is to wrap the text inside the <label></label> tags in <p></p> tags, at which point, the text-align: center;一个简单的方法/技巧是将文本包装在<p></p>标签中的<label></label>标签内,此时, text-align: center; property will work to center the text.属性将使文本居中。

 label{ text-align: center; }
 <label><p>Your Text</p></label>

https://jsfiddle.net/cmckay/2xydfs87/ https://jsfiddle.net/cmckay/2xydfs87/

.text-group {
display:flex;
align-items:center;
}

vertical-align: baseline|length|sub|super|top|text-top|middle|bottom|text-bottom|initial|inherit;

http://www.w3schools.com/cssref/pr_pos_vertical-align.asp

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

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