我需要在输入中放置图标以创建新用户.对于那些了解前端代码的人来说,这可能是一项非常容易的任务.但是我没有.这是线框,然后我显示我的代码.
如你看到的.输入左侧有图标.现在我在我的目录中有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>
Run Code Online (Sandbox Code Playgroud)
所以我有一个占位符,其中包含一个当前只打印该字符串的字符串.我需要把图标放在我认为的范围内吗?这是我用于图标的CSS.
.icon-email {
background-image: image-url('email-field.svg');
}
.icon-name {
background-image: image-url('name-field.svg');
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些类放在占位符中?
Sti*_*ers 23
您可以在<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;
}Run Code Online (Sandbox Code Playgroud)
<label>
<input type="text" placeholder="Search">
</label>Run Code Online (Sandbox Code Playgroud)
Ric*_*cky 10
您可以为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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)