Add image inside search box? - html

How can I add an image inside this search box? I'd like the image to be positioned inside and to the left of the palceholder text...
Here is the fiddle
HTML:
<div class="searchbar">
<h1>Welcome...</h1>
<div id="sb-search" class="sb-search">
<div class="search-wrapper">
<input class="search-input" placeholder="Search..." type="text" value="" name="search" id="search"/>
<img class="search-pic"src="img/search-icon.png"/>
</div>
</div>
</div>
thanks

Add this to your CSS which will position the image inside the input field.
.search-wrapper {
position: relative;
}
.search-wrapper img {
position: absolute;
top: 0px;
left: 0px;
}
Then you just need to use padding and change the top and left values to move everything about so it fits nicely and nothing is overlapped.
You might also need to set a width and height to the image so it's not too big for the input field.

You can solve it like this:
#mixin searchbar-font {}
#mixin searchbar-styles {
border: none;
outline: none;
color: $header-bg-color;
padding: 20px 65px 20px 20px;
background: transparent;
flex:1;
}
#mixin search-bar-input {
border: none;
outline: none;
}
.search-input {
border: none;
outline: none;
color: $header-bg-color;
padding: 20px 65px 20px 20px;
background: transparent;
flex:1;
}
.search-wrapper {
position: relative;
background: white;
display: flex;
align-items: center;
.search-input {
#include searchbar-font;
#include searchbar-styles;
}
.search-submit {
#include search-bar-input;
}
}
<div class="searchbar">
<h1>Welcome...</h1>
<div id="sb-search" class="sb-search">
<div class="search-wrapper">
<img class="search-pic" src="img/search-icon.png" />
<input class="search-input" placeholder="Search..." type="text" value="" name="search" id="search" />
</div>
</div>
</div>
Attached Fiddle

Technically you need to set your parent tag's position to relative, then set the image inside's position to absolute. Then you can overlay the image on your Input field. Also, one more thing to remember is you might want to set you z-index. Just in case, your image does not get behind of your input field. Make sure you are giving enough space to for your image by setting the input's padding left to somewhere around your image's width.
.search-wrapper{
.search-input{
padding-left: {image.width}px;
}
img{
position:absolute; left:0; top:0;
}
}
This should do the work.

<div class="search-container">
<input type="text" placeholder="Search...">
<img src="search-icon.png" alt="search icon">
</div>a
.search-container {
display: flex;
align-items: center;
position: relative;
}
input[type="text"] {
border: 1px solid gray;
border-radius: 20px;
padding: 10px 40px 10px 20px;
font-size: 16px;
width: 500px;
}
img {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
height: 20px;
width: 20px;
}
<div class="search-container">
<input type="text" placeholder="Search...">
<img src="search-icon.png" alt="search icon">
</div>
.search-container {
display: flex;
align-items: center;
position: relative;
}
input[type="text"] {
border: 1px solid gray;
border-radius: 20px;
padding: 10px 40px 10px 20px;
font-size: 16px;
width: 500px;
}
img {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
height: 20px;
width: 20px;
}

Related

Independant margin for the submit button and the text input

I don't know why, but my text input and my submit button margin are linked, so it's ugly and very annoying.
I have this on my main html file :
.banner {
width: 100%;
height: 100px;
display: inline-flex;
margin: 0px;
background-color: #1b2936;
}
.logo {
width: 200px;
height: 100px;
}
input[type=text] {
background-color: #10151b;
border: #10151b 5px solid;
border-radius: 7px;
height: 50px;
width: 500px;
color: white;
font-family: jetbrainsRegular;
}
.search {
width: 50px;
height: 50px;
}
.searchbtn {
margin-top: 0px;
border: #1fa2f3 5px solid;
border-radius: 30px;
background-color: #1fa2f3;
}
.nomargin {
margin-top: 0;
}
<div class="banner">
<img class="logo" src="logo.png">
<form action="query.php">
<label>
<input type="text" id="query" placeholder="Mot" class="nomargin">
</label>
<label>
<button type="submit" class="searchbtn"><img src="search.png" class="search"/></button>
</label>
</form>
</div>
I am searching for a way to unlink their margins...
There is no margin, it's an issue with your form. Try this:
form{
display:flex;
}
Or (but I don't recommend it):
input[type=text]{
float:left;
}
Flex gives you much more flexibility (pun intended) and it's much easier to work with. https://css-tricks.com/snippets/css/a-guide-to-flexbox/

How do I make a css item position absolutely to the parent element?

I want to add a "view password" icon inside of an html input. To do that, I added the material icons font and used one of its components. I nested it inside of the input to allow me to position it according to the input. I set the positon property of the input as "relative" and the position property of the icon as "absolute," but somehow, it still positions absolutely relative to the whole page. How can i fix this?
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
position: relative;
}
i.material-icons {
position: absolute;
top: 0
}
<input type="password" name="password" id="password-input" class="auth-input">
<i class="material-icons">visibility</i>
</input>
You cannot have html inside an input. It is a self closing tag. You need to put both elements into a container.
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
position: relative;
}
.auth-input input {
width: 100%;
}
i.material-icons {
position: absolute;
top: 0
}
<div class="auth-input">
<i class="material-icons">visibility</i>
<input type="password" name="password" id="password-input" />
</div>
Wrap the two. See demo https://codepen.io/yifanai/pen/MWyvJRE
.password-input-group {
position: relative;
}
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
position: relative;
}
i.material-icons {
position: absolute;
top: 7px;
right: 10px;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel=" stylesheet">
<div class="password-input-group">
<input type="password" name="password" id="password-input" class="auth-input" />
<i class="material-icons">visibility</i>
</div>
.auth-input {
text-align: left;
padding: 7px 10px;
background-color: #f6f6f6;
border: none;
border-radius: 5px;
font-size: 1.05em;
width: 100%;
}
.layout {
position: relative;
}
i.material-icons {
position: absolute;
top: 0;
right: 20px;
z-index: 1;
}
<span class="layout">
<input type="password" name="password" id="password-input" class="auth-input"></input>
<i class="material-icons">visibility</i>
</span>
Input TAG not have child, try it.

How do I keep a button appended to an input even at a smaller page width?

I am trying to append a button to a text input.
It works when I am in desktop mode, but when you change the size to a mobile-viewing size the button separates from the input.
Here is my fiddle.
I am using purecss:
input {
font-size: 16px;
padding-right: 50px;
border-radius: 0px;
height: 30px;
}
.pure-button {
margin-left: -45px;
height: 30px;
width: 40px;
padding: 1px;
}
span {
cursor: pointer;
display: inline-block;
height: 11px;
width: 8px;
text-align: center;
white-space: nowrap;
align-self: flex-start;
background: red;
background-position: 0 0;
margin: 1px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/pure-min.css" rel="stylesheet"/>
<div class="pure-g">
<div class="pure-u-1">
<form class="pure-form">
<fieldset>
<input type="text" class="pure-input-1-2" />
<button type="submit" class="pure-button pure-button-primary">
<span></span>
</button>
</fieldset>
</form>
</div>
</div>
There is a media query that is applying tules to to the input and the button from 0px - 480px. If you remove or override these, your fields will behave the same way as > 480px screen widths.
The library you're using is adding display: block to the input in a media query. That's forcing the button to the next line.
Add this to your CSS:
input, button {
display: inline-block !important;
margin: 0 !important;
}
The !important is needed to override the PURECSS code. Otherwise, alter the code in the source.
I also removed all whitespace between the elements in the code:
<input type="text" class="pure-input-1-2" /><button type="submit" class="pure-button pure-button-primary"><span></span></button>
This is one method for removing spaces between inline-block elements.
revised fiddle
Please try the below code within your existing code, hope this will help you.
.pure-form fieldset{
position: relative;
width: 50%;
padding: 0 45px 0 0;
margin: 10px;
box-sizing: border-box;
}
input {
font-size: 16px;
border-radius: 0px;
height: 30px;
}
.pure-form .pure-input-1-2{
width: 100%;
}
.pure-button {
position: absolute;
right: 0;
top: 0;
height: 30px;
width: 40px;
padding: 1px;
margin: 0 !important;
}
span {
cursor: pointer;
display: inline-block;
height: 11px;
width: 8px;
text-align: center;
white-space: nowrap;
align-self: flex-start;
background: red;
background-position: 0 0;
margin: 1px;
}
<div class="pure-g">
<div class="pure-u-1">
<form class="pure-form">
<fieldset>
<input type="text" class="pure-input-1-2" />
<button type="submit" class="pure-button pure-button-primary">
<span></span>
</button>
</fieldset>
</form>
</div>
</div>

Inline block element, not inline in this case

I have set my search bar div element as inline-block, as well as the img.
However, the div element is below the image, instead on the same horizontal line.
Anyone can advise me why this is happening?
body {
margin: 0;
}
#header {
width: 100%;
height: 60px;
background-color: #f1f1f1;
}
#header img {
height: 56px;
display: inline-block;
margin-left: 20px;
}
#search {
display: inline-block;
}
#search input {
display: inline-block;
width: 584px;
height: 38px;
border: 1px solid #d9d9d9;
}
#search input:hover {
border: 1px solid black;
}
<div id="header">
<img src="http://orig04.deviantart.net/1d83/f/2013/087/5/6/google_icon_by_slamiticon-d5z7lrp.png" />
<div id="search">
<form>
<input type="text" name="search" />
</form>
</div>
</div>
Your styles are working as they should; the #search input element's width is just too wide! Try looking at the result on a wider screen, and you will see the elements appear inline as expected.
Anticipating your next question, you can prevent wrapping of inline elements using the rule (on the container, e.g. #header):
white-space: nowrap
Anticipating your next question (if I may be so bold), you will probably want to set CSS rule:
vertical-align: middle
on both #header img and #search to get the look you want.
Here is the code snippet with what you want. The problem here is width of input. Since its more it bring the div to next line.
body {
margin: 0;
}
#header {
width: 100%;
height: 60px;
background-color: #f1f1f1;
}
#header img {
height: 56px;
display: inline-block;
margin-left: 20px;
}
#search {
display: inline-block;
height: 100%;
position: relative;
top: -22px;
}
#search input {
display: inline-block;
width: 480px;
height: 38px;
border: 1px solid #d9d9d9;
}
#search input:hover {
border: 1px solid black;
}
<div id="header">
<img src="http://orig04.deviantart.net/1d83/f/2013/087/5/6/google_icon_by_slamiticon-d5z7lrp.png" />
<div id="search">
<form>
<input type="text" name="search" />
</form>
</div>
</div>
The total width of image and search input is greater than full width of screen. Try with lesser width either of image or search input.
try this one
body {
margin: 0;
}
#header {
width: 100%;
height: 60px;
background-color: #f1f1f1;
}
#header img {
height: 56px;
display: inline-block;
margin-left: 5px;
}
#search {
display: inline-block;
height: 100%;
position: relative;
top: -22px;
}
#search input {
display: inline-block;
width: 450px;
height: 38px;
border: 1px solid #d9d9d9;
}
#search input:hover {
border: 1px solid black;
}
<div id="header">
<img src="http://orig04.deviantart.net/1d83/f/2013/087/5/6/google_icon_by_slamiticon-d5z7lrp.png" />
<div id="search">
<form>
<input type="text" name="search" />
</form>
</div>
</div>

Add span to side of center align element

I'm trying to implement some designs in CSS, but having a bit of trouble figuring out how to align this <span> correctly.
I am trying to achieve the <input> and <button> elements being centrally aligned, but the <span> element being absolutely to the right of the <input>, example:
It's important to make sure that the <span> does not affect the alignment of the other elements. The <input> and <button> should always be exactly in the middle of the parent.
Would be great if I could do this in CSS only. This is what I have so far:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: block;
width: 20px;
height: 20px;
background: red;
position: absolute;
top: 0; /* Not sure how to calculate these? */
right: 0; /* Input.X + Input.Width + 15px ?? */
}
<div>
<input placeholder="Enter code" maxlength="8" size="8"/><br />
<span class="verify"></span>
<button>Register</button>
</div>
Additional Info:
It only has to work in Chrome
I can make all elements a fixed width if required
I can make DOM changes if required
I would prefer not to hardcode X/Y co-ordinates for the <span>...I might want to change the input/button dimensions later
Wrap the input and the span inside a div with position: relative and display:inline
The span .verify will get absolutely positioned, leaving the input element to it's original position (centered aligned)
By giving a top:50% and then margin-top: -10px (half of its height), it will get in the middle of it's parent height.
.wrp {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
.inpWrp {
display: inline;
position:relative;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: block;
width: 20px;
height: 20px;
background: red;
position: absolute;
margin-top: -10px;
top: 50%; /* Not sure how to calculate these? */
right: -20px; /* Input.X + Input.Width + 15px ?? */
}
<div class="wrp">
<div class="inpWrp">
<input placeholder="Enter code" maxlength="8" size="8"/>
<span class="verify"></span>
</div>
<br />
<button>Register</button>
</div>
You can wrap your input element in span and using pseudo-element :after to create the square. No need of position absolute:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
vertical-align: middle;
}
.verify:after {
content: "";
width: 20px;
height: 20px;
background: red;
display: inline-block;
vertical-align: middle;
}
<div>
<span class="verify">
<input placeholder="Enter code" maxlength="8" size="8"/>
</span>
<br />
<button>Register</button>
</div>
After #kapantzak comment you can use position absolute like:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify:after {
content: "";
width: 20px;
height: 20px;
background: red;
display: inline-block;
position: absolute;
top: 17px;
}
<div>
<span class="verify">
<input placeholder="Enter code" maxlength="8" size="8"/>
</span>
<br />
<button>Register</button>
</div>
Try this...
.main_container {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: inline-block;
width: 20px;
height: 20px;
background: red;
position: relative;
top: 2px;
left: 10px;
}
<div class="main_container">
<div><input placeholder="Enter code" maxlength="8" size="8"/>
<span class="verify"></span></div>
<button>Register</button>
</div>