我需要在输入中放置图标以创建新用户。对于了解前端代码的人来说,这可能是一项非常简单的任务。但我不这么认为。这是线框,然后我展示我的代码。
如你所见。输入左侧有图标。现在我的目录中有 SVG 并准备好了,我只需要知道如何将它们放置在输入中。这是表格的代码
<label clas="name-label">
<%= f.text_field :name, placeholder: "Name", class: "form-labels" %>
</label>
<label class="email-label">
<%= f.text_field :email, placeholder: "Email", class: "form-labels" %>
</label>
所以我有一个带有字符串的占位符,当前仅打印该字符串。我需要将图标放入其中吗?这是我的图标 css。
.icon-email {
background-image: image-url('email-field.svg');
}
.icon-name {
background-image: image-url('name-field.svg');
}
有没有办法可以将这些类放在占位符中?
您可以在
<label>
标签中添加伪元素,并使用一些 position
和 padding
视觉技巧。使用 svg 作为背景与使用图像是一样的。
label {
position: relative;
}
label:before {
content: "";
position: absolute;
left: 10px;
top: 0;
bottom: 0;
width: 20px;
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='25' height='25' viewBox='0 0 25 25' fill-rule='evenodd'%3E%3Cpath d='M16.036 18.455l2.404-2.405 5.586 5.587-2.404 2.404zM8.5 2C12.1 2 15 4.9 15 8.5S12.1 15 8.5 15 2 12.1 2 8.5 4.9 2 8.5 2zm0-2C3.8 0 0 3.8 0 8.5S3.8 17 8.5 17 17 13.2 17 8.5 13.2 0 8.5 0zM15 16a1 1 0 1 1 2 0 1 1 0 1 1-2 0'%3E%3C/path%3E%3C/svg%3E") center / contain no-repeat;
}
input {
padding: 10px 30px;
}
<label>
<input type="text" placeholder="Search">
</label>
您可以为 svg 图标创建 SVG spritesheet。
label {
position: relative;
}
label > .icon {
position: absolute;
top: 50%;
left: 10px;
transform: translateY(-50%);
color: silver;
}
label > input {
padding-left: calc(1em + 10px + 8px); /* icon width + icon padding-left + desired separation*/
height: 2em;
}
/*
SVG SpriteSheet
*/
.spritesheet {
display: none;
}
.icon {
display: inline-block;
width: 1em;
height: 1em;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
<label clas="name-label">
<svg class="icon icon-user">
<use xlink:href="#icon-user"></use>
</svg>
<input type="text" placeholder="Name">
</label>
<label clas="name-label">
<svg class="icon icon-envelop">
<use xlink:href="#icon-envelop"></use>
</svg>
<input type="text" placeholder="Email">
</label>
<svg class="spritesheet">
<symbol id="icon-user" viewBox="0 0 32 32">
<title>user</title>
<path d="M18 22.082v-1.649c2.203-1.241 4-4.337 4-7.432 0-4.971 0-9-6-9s-6 4.029-6 9c0 3.096 1.797 6.191 4 7.432v1.649c-6.784 0.555-12 3.888-12 7.918h28c0-4.030-5.216-7.364-12-7.918z"></path>
</symbol>
<symbol id="icon-envelop" viewBox="0 0 32 32">
<title>envelop</title>
<path d="M29 4h-26c-1.65 0-3 1.35-3 3v20c0 1.65 1.35 3 3 3h26c1.65 0 3-1.35 3-3v-20c0-1.65-1.35-3-3-3zM12.461 17.199l-8.461 6.59v-15.676l8.461 9.086zM5.512 8h20.976l-10.488 7.875-10.488-7.875zM12.79 17.553l3.21 3.447 3.21-3.447 6.58 8.447h-19.579l6.58-8.447zM19.539 17.199l8.461-9.086v15.676l-8.461-6.59z"></path>
</symbol>
</svg>
这是使用占位符属性中设置的 fontawesome 图标的示例。
textarea,
input {
padding: 10px;
font-family: FontAwesome, "Open Sans", Verdana, sans-serif;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
text-align: center;
}
<link href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<textarea placeholder=''></textarea>
<br>
<input type='text' placeholder=''/>
您可以在输入中放置背景图像,并将该图像设为 svg。这是一个示例(我使用了堆栈溢出徽标,因为只要它存在,它就可能保持不变):
.search-input {
width: 100%;
height: 48px;
padding: 12px 8px 12px 36px;
border-radius: 4px;
border: 1px solid gray;
/* replace this svg with the one you like */
background-image: url('https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.svg');
background-repeat: no-repeat;
/* update the position so it shows */
background-position: -33px -30px;
}
<input
type="search"
class="search-input"
placeholder="Find a product or service"
/>
可能重复:https://stackoverflow.com/a/917636/2329657
现在回答你的问题。我发现您的设置存在一些问题。
<%= %>
语法相当通用。通常输入元素是使用 <input>
标签创建的。了解该框架将有助于识别其实现输入元素的方式中可能存在的任何奇怪之处。class="icon-email"
和 class="icon-name"
属性,但我没有看到此类属性。同样,这可能是框架处理的内容并在其他地方定义。我相信您尝试以错误的方式执行此操作,使用
<label>
而不是直接在输入本身上使用 CSS 处理此问题。如果您能提供以上信息,我就能提供更合适的解决方案。