Align Icon to The Right of an Input Field No Matter Width - html

I have three input fields, each with a set width, and want an icon aligned to the right of each.
Right now, it appears it's aligning to the right of the parent container.
Take a look at this codepen to see what I mean.
.input-container {
width: fit-content;
position: relative;
margin-top: 16px;
}
.input-field {
padding: 8px 12px;
border: 1px solid gray;
position: relative;
}
.input-field.short {
width: 100px;
}
.input-field.medium {
width: 200px;
}
.input-field.long {
width: 400px;
}
.icon {
position: absolute;
right: 8px;
top: 8px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/#fortawesome/fontawesome-free#5.15.4/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<div class="input-container">
<input class="input-field short" type="text">
<i class="fa fa-user icon"></i>
<div>
<div class="input-container">
<input class="input-field medium" type="text">
<i class="fa fa-user icon"></i>
<div>
<div class="input-container">
<input class="input-field long" type="text">
<i class="fa fa-user icon"></i>

You're not closing the div tag that you were using. An opening <div> should be closed by it's closing counterpart </div>. Your code works, it's just that you forgot to use the closing tag for each div you were using. See the snippet below to see what I mean.
.input-container {
width: fit-content;
position: relative;
margin-top: 16px;
}
.input-field {
padding: 8px 12px;
border: 1px solid gray;
position: relative;
}
.input-field.short {
width: 100px;
}
.input-field.medium {
width: 200px;
}
.input-field.long {
width: 400px;
}
.icon {
position: absolute;
right: 8px;
top: 8px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/#fortawesome/fontawesome-free#5.15.4/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<div class="input-container">
<input class="input-field short" type="text">
<i class="fa fa-user icon"></i>
</div>
<div class="input-container">
<input class="input-field medium" type="text">
<i class="fa fa-user icon"></i>
</div>
<div class="input-container">
<input class="input-field long" type="text">
<i class="fa fa-user icon"></i>
</div>
I've corrected your codepen code here as well.

Related

How to add search and mic button in input text?

I want to add a search icon on the left side of the input text and a speaker icon on the right side.
Something like this
Currently my I have :
Here is my code :
.search-bar {
width: 20%;
position: relative;
}
.search-bar input {
width: 100%;
height: 30px;
text-align: center;
border: none;
border-radius: 18px;
outline: none;
}
.search-ico {
position: absolute;
left: 10px;
}
<!--Search Bar-->
<div class="search-bar">
<input type="text" name="search" placeholder="Search" />
<i class="fa-solid fa-magnifying-glass search-ico"></i>
</div>
How can I achieve this? Thanks.
In addition to the answer above you can add pointer for the icon for simulate mouse with cursor:pointer; like:
body {
background: #333;
marging: 0;
}
.search-bar {
width: 30%;
position: relative;
}
.search-bar input {
width: calc(100% - 60px);
height: 30px;
text-align: center;
border: none;
border-radius: 18px;
outline: none;
padding: 0 30px;
}
.search-ico {
position: absolute;
left: 10px;
top:8px;
cursor: pointer; // add cursor
}
.microphone-ico {
position: absolute;
right: 10px;
top: 8px;
cursor: pointer; // add cursor
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!--Search Bar-->
<div class="search-bar">
<input type="text" name="search" placeholder="Search" />
<i class="fa fa-search search-ico"></i>
<i class="fa fa-microphone microphone-ico"></i>
</div>
If you use bootstrap 4 you can use input group like:
.input-group-text i{
cursor:pointer;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-search search-ico"></i></span>
</div>
<input type="text" class="form-control" aria-label="Search here">
<div class="input-group-append">
<span class="input-group-text"><i class="fa fa-microphone microphone-ico"></i></span>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
You are going the right way, using position: absolute set the icons
body {
background: #333;
marging: 0;
}
.search-bar {
width: 30%;
position: relative;
}
.search-bar input {
width: calc(100% - 60px);
height: 30px;
text-align: center;
border: none;
border-radius: 18px;
outline: none;
padding: 0 30px;
}
.search-ico {
position: absolute;
left: 10px;
top:8px;
}
.microphone-ico {
position: absolute;
right: 10px;
top: 8px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!--Search Bar-->
<div class="search-bar">
<input type="text" name="search" placeholder="Search" />
<i class="fa fa-search search-ico"></i>
<i class="fa fa-microphone microphone-ico"></i>
</div>

How do i hover a icon and it displays a input

<div id='searchParent'>
<!--icon to be hovered-->
<i id="search" class="fas fa-search"></i>
<!--icon to be hovered-->
<div id="inputParent">
<!--input to be shown-->
<input type="text" placeholder='Pesquise' autocomplete='off' id="input-bar">
<!--input to be shown-->
</div>
</div>
I´ve been working in this for a really long time, and the best piece of css hover i could get was this:
#search:hover ~ #inputParent #input-bar {
width: 200px;
}
But the problem is that when you hover the input it just drag it way out and you cant just write anything.
The icons are in my local machine but i will prolably put them in there
EDIT: I´m going to show some of my poor css and what i was intending to do.
input {
border: none;
outline: none;
border-bottom: 0.3px solid black;
}
#search {
padding-left: 180px;
display: inline-block;
}
#inputParent {
margin-left: auto;
}
#input-bar {
margin-right: 1vw;
width: 0;
transition: 0.8s ease-in-out;
}
#search:hover ~ #inputParent #input-bar {
width: 200px;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
</head>
<body>
<div id="searchParent">
<i id="search" class="fas fa-search"></i>
<div id="inputParent">
<input type="text" placeholder="Pesquise" autocoplete="off" id="input-bar">
</div>
</div>
</body>
</html>
What i really wanted to do was to when i hovered the input it stay there.
I really dont know why the bar is going from the left to the right, it was suposed to go backwards
Hi If you are able to add a new div to your HTML and add a class to your inputParent this is quite easy. Please see my code:
.dropdown {
position: relative;
display: inline-block;
}
#inputParent {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover #inputParent {
display: block;
}
<div id='searchParent'>
<!--icon to be hovered-->
<div class="dropdown">
<i id="search" class="fas fa-search"></i>Icon
<!--icon to be hovered-->
<div id="inputParent" class="parentc">
<!--input to be shown-->
<input type="text" placeholder='Pesquise' autocomplete='off' id="input-bar">
<!--input to be shown-->
</div>
</div>
</div>

Responsive text field with icons and counter field

I am trying to create an input field where user can increment or decrement the number. The input field should look like following design in a responsive way
However, it looks weird when I did the following way
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<div class="input-with-icon">
<div class="labelled-input">
<div class="labelling">
<div class="content">
<i class="fa fa-user fa-lg fa-fw" aria-hidden="true"></i>
<div class="text-wrapper">
<p class="adult">Adult</p>
<p>16+year</p>
</div>
</div>
</div>
<div class="augment">
<i class="fa fa-minus"></i>
</div>
<input type="text" class="input" disabled/>
<div class="augment">
<i class="fa fa-plus"></i>
</div>
</div>
</div>
Here is the demo
https://jsbin.com/fiwabararo/edit?html,css,output
Please check this code snippet
function counter(i){
var el =document.getElementsByClassName('input');
(i == '+')
? el[0].value = parseInt(el[0].value) + 1
: el[0].value = parseInt(el[0].value) - 1;
}
.input-with-icon {
position: relative;
width: 100%;
display: flex;
}
.labelled-input {
position: relative;
font-weight: 400;
font-style: normal;
display: inline-flex;
border: solid 1px #99acbd;
border-radius: 2px;
background:#eceef2;
}
.labelling {
display: inline-block;
line-height: 1;
vertical-align: baseline;
background-image: none;
color: rgba(0, 0, 0, 0.6);
text-transform: none;
font-weight: 700;
border: 0 solid transparent;
flex: 0 0 auto;
margin: 0;
}
.labelling:first-child {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.content {
display: flex;
align-items: center;
padding:0 12px
}
.augment {
background: #ccd5de;
text-align:center;
cursor: pointer;
height: 40px;
width: 40px;
border-radius: 50%;
margin: 10px;
margin-top:18px
}
.augment:hover {
background: #478dcf;
}
.augment i {
line-height:2.6;
color:#fff
}
.input {
margin: 0;
border: none;
outline: 0;
box-shadow: none;
font-size: 22px;
font-weight: bold;
text-align: center;
}
.input:disabled {
background: #fff;
}
#media(max-width:480px){
.input {
width:56%;
}
.augment {
width: 14%;
}
}
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<div class="input-with-icon">
<div class="labelled-input">
<div class="labelling">
<div class="content">
<i class="fa fa-user fa-lg fa-fw" aria-hidden="true"></i>
<div class="text-wrapper">
<p class="adult">Adult</p>
<p>16+year</p>
</div>
</div>
</div>
<div class="augment" onclick="counter('-')">
<i class="fa fa-minus"></i>
</div>
<input type="text" class="input" disabled value="0" />
<div class="augment" onclick="counter('+')">
<i class="fa fa-plus"></i>
</div>
</div>
</div>

Centering div does not work properly with font awesome

Problem: JSFiddle
I want to center the whole form and created a class called 'center' with the given attributes. But after adding the center class to the form in JSFiddle, the fontawesome-icons stay on the left and do not move. They should be positioned "inside" the input element left to the text. Therefore, to make place for the icon, I also added a padding on the left.
Can somebody give me the solution to fix this problem that both icons stay on the left because of centering? I really cannot find out why it is not working and it already took so much time :/
Thanks.
.center{
text-align: center;
margin-left: auto;
margin-right: auto;
}
.box {
position: relative;
margin-bottom: 20px;
}
.box .fa {
position: absolute;
top: 10px;
left: 5px;
}
.box input[type="text"],
.box input[type="password"] {
border: 0px;
border-bottom: 1px solid #ccc;
text-decoration: none;
width: 180px;
padding: 10px;
padding-left: 25px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="center">
<form action="/login" method="POST" class="form">
<div class="box">
<i class="fa fa-user" aria-hidden="true"></i>
<input required name="username" type="text" placeholder="user">
</div>
<div class="box">
<i class="fa fa-key" aria-hidden="true"></i>
<input required name="password" type="password" placeholder="pass">
</div>
<div class="box">
<input name="submit" type="submit" value="Login">
</div>
</form>
</div>
The .box elements are divs and therefore naturally have a width of 100% of the parent element.
A simple solution would be to give them a fixed width equal to that of your inputs and centre them using left and right margins of auto:
.center{
text-align: center;
margin-left: auto;
margin-right: auto;
}
.box {
position: relative;
margin: 0 auto 20px;
width: 180px;
}
.box .fa {
position: absolute;
top: 10px;
left: 5px;
}
.box input[type="text"],
.box input[type="password"] {
border: 0px;
border-bottom: 1px solid #ccc;
text-decoration: none;
width: 180px;
padding: 10px;
padding-left: 25px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="center">
<form action="/login" method="POST" class="form">
<div class="box">
<i class="fa fa-user" aria-hidden="true"></i>
<input required name="username" type="text" placeholder="user">
</div>
<div class="box">
<i class="fa fa-key" aria-hidden="true"></i>
<input required name="password" type="password" placeholder="pass">
</div>
<div class="box">
<input name="submit" type="submit" value="Login">
</div>
</form>
</div>
it occurs because
.box .fa {
position: absolute;
top: 10px;
left: 5px;
}
you can use position initial for it.

Hyperlink not clickable using position and z-index

I am working on a contact form.
I want to make a form same as in image
i use position absolute in social media wrapper and z-index:-1;
but facing a problem with hyperlink on social media icon.
html and css code is given below
html
<div class="section text-center c-index">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-md-offset-3 col-sm-offset-3 col-xs-offset-3 form-wrapper-main">
<div class="form-wrapper">
<h3 class="contact-head">Contact Us</h3>
<div class="form-inside">
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<textarea name="" id="" cols="10" rows="1" class="form-control" placeholder="Message"></textarea>
</div>
<div id="checkbox" class="checkbox">
<label>
<input type="checkbox"> Agree with terms & conditions
</label>
</div>
<button type="submit" class="btn contact-submit">Submit</button>
</form>
</div>
<div class="social-media-wrapper">
<ul>
<li>
<a href="#" target="_blank">
<i class="fa fa-twitter fa-2x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="#" target="_blank">
<i class="fa fa-facebook-official fa-2x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="#" target="_blank">
<i class="fa fa-linkedin-square fa-2x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="#" target="_blank">
<i class="fa fa-google-plus-official fa-2x" aria-hidden="true"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
Css Code
.form-wrapper {
background-color: #ffffff;
-webkit-box-shadow: 0px 10px 30px 0 rgba(0, 1, 1, .4);
box-shadow: 0px 10px 30px 0 rgba(0, 1, 1, .4);
-webkit-border-radius: 10px;
border-radius: 10px;
z-index: 3;
}
.contact-head,
.form-inside {
text-align: left;
}#checkbox {
padding-left: 55px;
padding-top: 10px;
padding-bottom: 10px;
}.contact-submit {
margin-left: 55px;
margin-top: 10px;
margin-bottom: 20px;}
.contact-head {
padding-top: 20px;
padding-bottom: 20px;
padding-left: 55px;
}
.form-inside form textarea {
resize: none;
}
.social-media-wrapper {
position: absolute;
top: 60px;
right: -80px;
padding-right: 40px;
padding-left: 40px;
padding-top: 20px;
padding-bottom: 20px;
background-color: #00a0ff;
border-radius: 10px;
z-index: -1;
}
.c-index {
z-index: 1;
}
.social-media-wrapper ul {
list-style: none;
padding: 0;
position: relative;
z-index: 10;
}
.social-media-wrapper ul li {
padding-top: 20px;
padding-bottom: 20px;
position: relative;
z-index: 10;
}
.social-media-wrapper ul li a {
color: #ffffff;
z-index: 10;
position: relative;
}
You can wrap both form and social media with a container div. then set the wrapper div to relative and rest two are absolute. then position it as you want. I think this should do the trick.
Here is a codepen link you can explore Click to see the pen
body {
padding-top: 45px;
background: #D2ECFF;
}
.contact-us-form {
box-shadow: 0px 3px 32px 2px rgba(0, 0, 0, 0.2);
width: 85%;
padding: 15px 30px;
border-radius: 15px;
z-index: 3;
position: absolute;
}
.contact-us-form .submit-form {
background: #FF6600;
padding-left: 45px;
padding-right: 45px;
color: #fff;
}
.contact-us-form h2.title {
font-size: 2em;
margin-bottom: 30px;
}
.form-control {
height: 50px;
border-radius: 25px;
font-size: 1.25em;
padding: 0 20px;
background: #F8FAFB;
border: 0;
box-shadow: none;
-webkit-box-shadow: none;
}
textarea.form-control {
padding-top: 15px;
resize: none;
}
.social {
background: #00A0FF;
width: 15%;
text-align: center;
position: absolute;
top: 66px;
right: 30px;
padding-left: 25px;
z-index: 1;
border-radius: 15px;
}
.social li {
padding: 15px;
}
.social li:first-child {
padding-top: 30px;
}
.social li:last-child {
padding-bottom: 30px;
}
.social li a {
color: white;
font-size: 2.25em;
}
.wrap {
position: relative;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-md-6 col-md-offset-3">
<div class="wrap">
<div class="panel panel-default contact-us-form">
<div class="panel-body">
<h2 class="title">Contact Us</h2>
<form action="">
<div class="form-group">
<input type="text" class="form-control" placeholder="Name"/>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email" />
</div>
<div class="form-group">
<textarea rows="4" class="form-control" placeholder="Message"></textarea>
</div>
<div class="form-group">
<label>
<input type="checkbox" />
Agree with terms & condition
</label>
</div>
<button type="submit" class="btn btn-lg submit-form">Submit</button>
</form>
</div>
</div>
<div class="social">
<ul class="list-unstyled">
<li><i class="fa fa-twitter-square"></i></li>
<li><i class="fa fa-facebook-square"></i></li>
<li><i class="fa fa-linkedin-square"></i></li>
<li><i class="fa fa-google-plus-square"></i></li>
</ul>
</div>
</div> <!-- /wrap -->
</div>
You have added unnecessary position: relative and z-index properties on many elements. Remove all of them.
Add position: relative on the container having your social media div.
Draw required background with :before or :after pseudo element with lower z-index. This will enable the social icons to remain clickable.
.form-wrapper {
background-color: #ffffff;
-webkit-box-shadow: 0px 10px 30px 0 rgba(0, 1, 1, .4);
box-shadow: 0px 10px 30px 0 rgba(0, 1, 1, .4);
-webkit-border-radius: 10px;
border-radius: 10px;
position: relative;
}
.contact-head,
.form-inside {
text-align: left;
}
#checkbox {
padding-left: 55px;
padding-top: 10px;
padding-bottom: 10px;
}
.contact-submit {
margin-left: 55px;
margin-top: 10px;
margin-bottom: 20px;}
.contact-head {
padding-top: 20px;
padding-bottom: 20px;
padding-left: 55px;
}
.form-inside form textarea {
resize: none;
}
.social-media-wrapper {
position: absolute;
top: 60px;
right: -100px;
padding-right: 40px;
padding-left: 40px;
padding-top: 20px;
padding-bottom: 20px;
}
.social-media-wrapper:before {
background-color: #00a0ff;
border-radius: 10px;
position: absolute;
content: '';
z-index: -1;
bottom: 0;
right: 0;
left: 0;
top: 0;
}
.social-media-wrapper ul {
list-style: none;
padding: 0;
}
.social-media-wrapper ul li {
padding-top: 20px;
padding-bottom: 20px;
}
.social-media-wrapper ul li a {
color: #ffffff;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="section text-center c-index">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6 col-md-offset-3 col-sm-offset-3 col-xs-offset-3 form-wrapper-main">
<div class="form-wrapper">
<h3 class="contact-head">Contact Us</h3>
<div class="form-inside">
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<textarea name="" id="" cols="10" rows="1" class="form-control" placeholder="Message"></textarea>
</div>
<div id="checkbox" class="checkbox">
<label>
<input type="checkbox"> Agree with terms & conditions
</label>
</div>
<button type="submit" class="btn contact-submit">Submit</button>
</form>
</div>
<div class="social-media-wrapper">
<ul>
<li>
<a href="#" target="_blank">
<i class="fa fa-twitter fa-2x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="#" target="_blank">
<i class="fa fa-facebook-official fa-2x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="#" target="_blank">
<i class="fa fa-linkedin-square fa-2x" aria-hidden="true"></i>
</a>
</li>
<li>
<a href="#" target="_blank">
<i class="fa fa-google-plus-official fa-2x" aria-hidden="true"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
Seems working fine, except you have given # inside href attribute, currently it is clickable, you have to provide proper url instead of the #
Fiddle Demo