简体   繁体   English

在div中仅将一个元素居中

[英]Center only one element within a div

I have this HTML code: 我有这个HTML代码:

    <div>
        <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required>                    
        <span>Proper format "exampel@something.com"</span>
    </div>

I've configured 'text-align: center' on the div but unfortunately span text is also centered (see the image below). 我在div上配置了“ text-align:center”,但不幸的是, span文本也居中(请参见下图)。 I need span to be near it and not centered. 我需要span要靠近它而不是居中。

在此处输入图片说明

You probably should center your parent element, and not div inside form: 您可能应该将父元素居中放置,而不要在表单内部放置div

 .center-form { text-align: center; } .center-form form { display: inline-block; text-align: left; } 
 <div class="center-form"> <form action=""> <div> <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required> </div> <div> <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required> <span>Proper format "exampel@something.com"</span> </div> <div> <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required> </div> <button>Submit</button> </form> </div> 

Or if you want span not to affect form position, use position: absolute : 或者,如果您希望span不影响表单位置,请使用position: absolute

 .center-form { text-align: center; } .center-form form { display: inline-block; text-align: left; } .center-form form div { position: relative; } .center-form form div span { position: absolute; left: 100%; top: 0; /* for demo */ white-space: nowrap; } 
 <div class="center-form"> <form action=""> <div> <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required> </div> <div> <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required> <span>Proper format "exampel@something.com"</span> </div> <div> <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required> </div> <button>Submit</button> </form> </div> 

You should apply 'text-align: center;' 您应该应用'text-align:center;' on the input#cemail, not on the div. 在输入#cemail上,而不是在div上。

What about a nice - 怎么样呢?

position: absolute
  • You get a free relative with the div. 您可以与div取得一个免费的亲戚。 Then the error will never get in the way of the input box as its absolutely positioned. 这样,错误将永远不会挡住输入框的​​位置。

Of course you will need to fix the top, left, right, bottom to conform to your design. 当然,您需要固定顶部,左侧,右侧,底部以符合您的设计。

You can also do something like this, to allow wrapping of text. 您也可以执行类似的操作以允许自动换行。

 body { font-family: sans-serif; } input { width: 160px; position: relative; left: 50%; margin-left: -80px; box-sizing: border-box; } p { clear: right; } .legend { float: right; width: 50%; } .legend small { background: #ccc; display: block; margin-left: 80px; word-wrap: break-word; padding: 0.5em; } 
 <form> <p> <input type="text" placeholder="Your Last Name"> <span class="legend"> <small>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam consectetur libero magna, ultrices vehicula dui hendrerit ut. Mauris dictum urna nec justo tristique, id rutrum sapien blandit. Ut pellentesque, augue ornare pellentesque dictum, sem lectus hendrerit massa, nec gravida elit mauris sit amet lorem.</small> </span> </p> <p> <input type="text" placeholder="Your User Name"> </p> <p> <input type="email" placeholder="Your Email"> <span class="legend"> <small>Proper format "exampel@something.com"</small> </span> </p> <p> <input type="text" placeholder="Your Password"> </p> <p> <input type="submit" value="Add Me Now"> </p> </form> 

  <div>
     <input id="cemail" type="email" name="email" placeholder="Your E-Mail" required>                    
     <span class="float-right">Proper format "exampel@something.com"</span>
  </div>

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

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