简体   繁体   English

如何在输入字段中添加按钮

[英]How to add button inside input field

Trying to copy some front end mentor challenges I am right now styling input field.试图复制一些前端导师挑战,我现在正在设计输入字段。 I want to do 3 things right now.我现在想做3件事。

  1. I want to display the button inside the input field (even though I made it inside it is not perfect fit)我想在输入字段内显示按钮(即使我把它放在里面并不完美)
  2. display the error icon inside the input field.在输入字段内显示错误图标。
  3. display the message "please provide a valid Email" below the textbox.在文本框下方显示消息“请提供有效的电子邮件”。

Right now it displays like this现在它显示如下

在此处输入图像描述

 .left button { background-image: linear-gradient(135deg, hsl(0, 80%, 86%), hsl(0, 74%, 74%)); border: none; border-radius: 50px; width: 100px; margin-left: -104px; margin-top: -30px; height: 48px; }.left input { background-color: transparent; border: 1px solid hsl(0, 36%, 70%); border-radius: 15px; padding: 13px; width: 60%; }.left.error {}
 <form action="#"> <input type="text" placeholder="Email Address"> <img src="/images/icon-error.svg" alt="" class="error"> <button type="submit"><img src="/images/icon-arrow.svg " ></button> <small>please provide a valid Email</small> </form>

They have different border radius - the button and the input.它们具有不同的边框半径 - 按钮和输入。 How could they be perfect match?他们怎么可能是绝配? Also different height.还有不同的高度。 As for the "hint" line, just place it under in it's own div .至于“提示”行,只需将其放在它自己的div下。 I've tweaked a little your example using float:left and some adjustments.我使用float:left和一些调整对您的示例进行了一些调整。 It's not perfect implementation but looks ok.这不是完美的实现,但看起来还不错。 Note height + padding = 48px .注意height + padding = 48px

 .my-input-group { position: relative; }.left input { background-color: transparent; border: 1px solid hsl(0, 36%, 70%); border-radius: 15px; padding: 13px; padding-right: 113px; width: 60%; height: 22px; float: left; }.left button { background-image: linear-gradient(135deg, hsl(0, 80%, 86%), hsl(0, 74%, 74%)); border: 1px solid transparent; border-radius: 15px; width: 100px; height: 48px; float: left; position: relative; left: -101px; top: 1px; }.left.error {} my-input-group
 <form action="#" class="left"> <div class="my-input-group"> <input type="text" placeholder="Email Address"> <button type="submit"><img src="/images/icon-arrow.svg "></button> <div style="clear:both"></div> <div class="error"> <small>please provide a valid Email</small> </div> </div> </form> <img src="/images/icon-error.svg" alt="" class="error">

Just a simple use of css display: flex will solve your problem, also I have made a container to store your input, button & image and gave that container a border so now it visually looks like they are inside the input box .只需简单地使用 css显示:flex将解决您的问题,我还制作了一个容器来存储您的输入、按钮和图像并给该容器一个边框,所以现在看起来它们在输入框内

 .container { border: 1px solid hsl(0, 36%, 70%); display: flex; height: 30px; background-color: transparent; border-radius: 15px; padding: 5px; width: 50%; justify-content: space-evenly; align-items: center; } button { background-image: linear-gradient(135deg, hsl(0, 80%, 86%), hsl(0, 74%, 74%)); border: none; border-radius: 50px; width: 80px; height: calc(100% + 5px); cursor: pointer; } input { border: none; outline: none; height: 100%; } img { height: calc(100% + 5px); } button img { height: 100%; filter: invert(); } small { margin-left: 10px; }
 <form action="#"> <div class="container"> <input type="text" placeholder="Email Address"> <button type="submit"> <img src="https://cdn-icons-png.flaticon.com/512/2989/2989988.png" > </button> <img src="https://cdn-icons-png.flaticon.com/512/7068/7068033.png" alt="image" class="error"> </div> <small>please provide a valid Email</small> </form>

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

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