简体   繁体   中英

How can I vertically center an icon in an input?

I don't know how to center an icon vertically inside an input . This is my code:

<i-feather name="user" class="fea icon-sm icons"></i-feather>
<input type="text" class="form-control ps-5" placeholder="Name" name="s" required="">

Currently it looks like this:

图标略低于垂直中心的 HTML 输入

and I want to achieve an effect like this:

带有垂直居中图标的 HTML 输入

How do I center the icon vertically in the input ?

You can do it like this:

 <style>.input-wrapper { display: flex; position: relative; }.input-wrapper.icon { position: absolute; top: 50%; transform: translateY(-50%); padding: 0 10px; }.input-wrapper input { padding: 0 0 0 25px; height: 50px; } </style> <div class="input-wrapper"> <i class="icon">2</i> <input type="text" class="form-control ps-5" placeholder="Name" name="s" required=""> </div>

There are two styling options you can use. These are block and flex. Block will not be responsive while flex will be responsive. I hope this solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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