How to align a search icon inside a search box [duplicate] - html

This question already has answers here:
Put icon inside input element in a form
(21 answers)
Closed 1 year ago.
I would like to know how to align a search icon, within the text box, same as Google's search bar
Like this:
This is what i got, but idk How to do it:
I am trying to avoid using bootstrap or any library. I want to keep it vanilla as much as I can.
#search-bar {
width: 32%;
color: white;
border-radius: 24px;
border-style: solid;
border-width: 1px;
border-color: white;
background-color: #2d2d2d;
height: 44px;
}
<form class="search-bar-form">
<input id="search-bar" type="text" href="#" />
<img src="https://via.placeholder.com/24" />
</form>

You can place the image as the background of the textbox:
#search-bar {
width: 32%;
color: white;
border-radius: 24px;
border-style: solid;
border-width: 1px;
border-color: white;
background-color: #2d2d2d;
height: 44px;
background-image: url(https://via.placeholder.com/24);
background-position: 10px center;
background-repeat: no-repeat;
padding-left: 40px;
}
<form class="search-bar-form">
<input id="search-bar" type="text" value="text goes after the padding" />
</form>

You can use position: absolute.
Like this:
form {
position: relative;
}
img {
position: abosolute;
top: 10px;
right: 20px;
}
You should update top and right values.
This is only example.

Add some padding-left to the search bar, to move the text more to the right side.
Then add a class to the image and define it like that:
.mySearchIconImage {
position: absolute;
top: 10px;
left: 10px;
Also add position: relative to the form itself, to make sure the img isn't positioned outside of your form.

There are several ways. One way is with absolute position inside a relative position parent...
body {
margin: 0;
}
#search-bar {
width: 32%;
color: white;
border-radius: 24px;
border-style: solid;
border-width: 1px;
border-color: white;
background-color: #2d2d2d;
height: 44px;
position: relative;
padding-left: 44px;
}
.search-icon {
position: absolute;
left: 0;
}
<form class="search-bar-form">
<input id="search-bar" type="text" href="#" />
<img src="https://img.icons8.com/material-outlined/50/000000/search.png" class="search-icon" />
</form>
Demo
Another way is using relative position and a negative margin. For this, put the img before the input in your markup.
#search-bar {
width: 32%;
color: white;
border-radius: 24px;
border-style: solid;
border-width: 1px;
border-color: white;
background-color: #2d2d2d;
height: 44px;
padding-left: 44px;
}
.search-icon {
position: relative;
margin-right: -60px;
z-index: 1;
vertical-align: middle;
}
<form class="search-bar-form">
<img src="https://img.icons8.com/material-outlined/50/000000/search.png" class="search-icon" />
<input id="search-bar" type="text" href="#" />
</form>

Related

How to stick image to div block in CSS?

I'm trying to stick an image to div block in CSS. I couldn't move 'image' using margin... What can I do? Advice is appreciated. Thank you.
What I want to implement
.bottalk {
background-color: white;
height: 100px;
width: 200px;
margin-top: 20px;
margin-left: 280px;
border-radius: 1.5em;
}
.bottalk p {
padding-top: 5px;
padding-left: 15px;
}
.bot .bottalkwhite {
height: 40px;
margin-left: 250px;
margin-top: 0px;
}
.bottalk button {
background-color: yellow;
color: purple;
padding: 5px 5px;
border: none;
margin-left: 50px;
box-shadow: 3px 3px 2px #666666;
}
<div class="col-6 bot">
<div class="bottalk">
<p>Ready to get started?</p>
<button>Let's talk</button>
</div>
<img src="./img/bottalk.png" alt="bottalk" class="bottalkwhite" />
</div> </div>
Current view
Please ignore the background color: I snipped it from the second image!
I have moved the position of the image inside the div with class bottalk, then I absolutely positioned the image, then all you need to do is to set the top and left position based on the image, (Cropped the image online so please ignore the quality of the output), So now you can position this anywhere. Also I have added background-color:pink to the body to show the image, please ignore this too.
So to summarize. I set the parent div element with class bottalk as position:relative and the child image with class bottalkwhite to position:absolute so that it can be positioned inside the parent. Position absolute will take the position relative to the immediate parent with position:relative, I hope I made my summary clear.
body{
background-color:pink;
}
.bottalk {
position: relative;
background-color: white;
height: 100px;
width: 200px;
margin-top: 20px;
margin-left: 280px;
border-radius: 1.5em;
}
.bottalk p {
padding-top: 5px;
padding-left: 15px;
}
.bot .bottalkwhite {
height: 40px;
position: absolute;
top: 80%;
left: -30px;
}
.bottalk button {
background-color: yellow;
color: purple;
padding: 5px 5px;
border: none;
margin-left: 50px;
box-shadow: 3px 3px 2px #666666;
}
<div class="col-6 bot">
<div class="bottalk">
<p>Ready to get started?</p>
<button>Let's talk</button>
<img src="https://i.stack.imgur.com/7i9bY.gif" alt="bottalk" class="bottalkwhite" />
</div>
</div> </div>
You can use the position: relative; and adjust the values of the top and left properties, like the follow code:
.bottalk {
position: relative;
left: -5px;
top: 10px;
background-color: white;
height: 100px;
width: 200px;
margin-top: 20px;
margin-left: 280px;
border-radius: 1.5em;
}
.bottalk p {
padding-top: 5px;
padding-left: 15px;
}
.bot .bottalkwhite {
height: 40px;
margin-left: 250px;
margin-top: 0px;
}
.bottalk button {
background-color: yellow;
color: purple;
padding: 5px 5px;
border: none;
margin-left: 50px;
box-shadow: 3px 3px 2px #666666;
}
<div class="col-6 bot">
<div class="bottalk">
<p>Ready to get started?</p>
<button>Let's talk</button>
</div>
<img src="./img/bottalk.png" alt="bottalk" class="bottalkwhite" />
</div> </div>
In order to put a image into a exact position relative to its ancestor, you can set position property to absolute then using left-right-top-bottom properties, you can determine its exact position. like this:
.bottalkwhite{
position: absolute;
left: 250px;
top: 0px;
}
though in such a particular css rule definition using id selector instead of class selector sounds more appropriate.
Use position:relative on the wrapper element of the image and position the image via position: absolute, left: 0 and bottom: 0 in the bottom-left corner. Then adjust it's position via transform: translate, to get the desired effect.
Note: I moved the image into the div.botttalk container to position it relative to its parent.
Like this:
body {
background: #715886;
font-family: Open Sans, Arial, sans-serif;
}
.bottalk {
background-color: white;
width: 200px;
margin-top: 20px;
margin-left: 100px;
border-radius: 8px;
padding: 24px 16px;
position: relative;
text-align: center;
color: #715886;
}
.bottalk .bottalkwhite {
position: absolute;
left: 0;
bottom: 0;
height: 40px;
color: white;
transform: translate(-100%, 100%) translate(16px, -16px);
}
.bottalk h4 {
line-height: 1;
margin: 0 0 24px 0;
}
.bottalk button {
cursor: pointer;
color: #715886;
display: inline-block;
background-color: #fbcb33;
font-weight: bold;
padding: 0 32px;
border: none;
line-height: 40px;
font-size: 14px;
box-shadow: 0px 1px 2px #666666;
}
<div class="col-6 bot">
<div class="bottalk">
<h4>Ready to get started?</h4>
<button>Let's talk</button>
<img src="https://i.imgur.com/oeUdlld.png" alt="bottalk" class="bottalkwhite" />
</div>
</div>

how to allow a hidden button to be shown when focused using CSS only?

I have a container div and it has a hidden button inside it and it appears only when focusing on the container div. I want to make the button visible when focused (I want to make it focusable). Here's a fiddle
HTML:
<div class="ads" tabindex="0">
<button class="close" tabindex="0">X</button>
</div>
CSS:
.ads{
position: relative;
display: inline-block;
border-radius: 0.5625rem;
border-style: solid;
border-width: 0.03125rem;
border-color: lightgrey;
background-color: #ffffff;
width: 100%;
height: 80px;
}
.close{
display: none;
padding: 0;
background: transparent;
border: none;
margin-top: 0.5rem;
margin-right: 0.5rem;
float: right;
width: 0.5rem;
height: 0.5rem;
background-image: url('delete.png');
background-size: 100% 100%;
}
div.ads:focus{
background-color: #ebeded;
}
div.ads:focus .close{
display:inline-block;
}
button.close:focus{
display:inline-block;
}
How can I achieve that?
Thank you.
At any given moment of time only one element can be in focus or none.
But your solution assumes that there are two elements matching :focus in the document at the same time.
Here is sequence of events when you press TAB on focused div:
Your div looses focus so is does not match :focus;
Button gets hidden as it has not got focus yet;
as nothing visible/focusable inside the div focus moves to something else but not to the button.
You should find other solution.
Update: possible CSS only hack is to use opacity:0 instead of display:none.
Hack here is that opacity:0 element is considered as still displayed element so focusable.
input{
display: block;
width: 100%;
}
.ads{
position: relative;
display: inline-block;
border-radius: 0.5625rem;
border-style: solid;
border-width: 0.03125rem;
border-color: lightgrey;
background-color: #ffffff;
width: 100%;
height: 80px;
}
.close{
opacity: 0;
padding: 0;
background: transparent;
border: none;
margin-top: 0.5rem;
margin-right: 0.5rem;
float: right;
width: 0.5rem;
height: 0.5rem;
background-image: url('delete.png');
background-size: 100% 100%;
}
div.ads:focus{
background-color: #ebeded;
}
div.ads:focus .close{
opacity: 1.0;
}
button.close:focus{
opacity: 1.0;
}
<input type="text" placeholder="press on me and tab two times">
<div class="ads" tabindex="0">
<button class="close" tabindex="0">X</button>
</div>
<p>
by the second tab you should see focused button ,but you don't
</p>

CSS: Click button to transition and keep style open

That might sound odd, but essentially, I'm trying to make it so when i click on a designated button/spot on a page, it opens up a CSS border box that contains information. I know how to make it hidden, but like when you hover and it appears using the :hover attribute, i want to make it stay permanently visible after the hover transition is complete. Can this be done with CSS? Or is it going to require Javascript? Here is the code I'm using as a starter base.
#information {
border: solid 2px #FF8000;
border-radius: 20px;
position: absolute;
height: 48%;
width: 24%;
left: =0.6%;
top: 0.7%;
padding: 0.4%;
color: #FFFFFF;
background-color: rgba(114, 70, 0, 0.3);
overflow: hidden;
}
#information:hover {
left: 74.6%;
}
<div id="information">
<div style=" height: 325px; overflow-x: hidden;" align="left">
<i>Information</i>
<br><br>
</div>
</div>
Sorry, I'm new to the site, and I'm still working out how to format my posts.
#information {
border: solid 2px #FF8000;
border-radius: 20px;
position: absolute;
height: 48%;
width: 24%;
left: =0.6%;
top: 0.7%;
padding: 0.4%;
color: #FFFFFF;
background-color: rgba(114, 70, 0, 0.3);
overflow: hidden;
}
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
left: 74.6%;
}
<input type="radio" id="box">
<label id="information" for="box">
<div style=" height: 325px; overflow-x: hidden;" align="left">
<i>Information</i>
<br><br>
</div>
</label>
My solution is similar to what #Michael Coker suggested. We can use radio button instead to disable click on label after first click. I removed the inner div inside #information to make HTML W3C valid.
I'm trying to make it so when i click on a designated button/spot on a page, it opens up a CSS border box that contains information.
You can use the "checkbox hack" to pull off changes like this in CSS. https://css-tricks.com/the-checkbox-hack/
#information {
border: solid 2px #FF8000;
border-radius: 20px;
position: absolute;
height: 48%;
width: 24%;
left: =0.6%;
top: 0.7%;
padding: 0.4%;
color: #FFFFFF;
background-color: rgba(114, 70, 0, 0.3);
overflow: hidden;
}
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]:checked + label {
left: 74.6%;
}
<input type="checkbox" id="box">
<label id="information" for="box">
<div style=" height: 325px; overflow-x: hidden;" align="left">
<i>Information</i>
<br><br>
</div>
</label>

overlap buttons in div tag using css

hello just a quick question about overlapping two buttons with a div tag, how do i do it? i tried position and margin but both do not move the buttons.
heres the html:
<div class="container-fluid">
<div style="margin:auto;">
<button type="submit" class="btn-login">Log in</button>
</div>
<div class="signup-form">
<label style="text-align:center;margin-bottom:20px">Create an Account</label>
<div>
<button type="submit" class="btn-login">Sign Up Free</button>
</div>
<label style="text-align:center;margin-bottom:20px;margin-top:20px">or</label>
</div>
</div>
<div class="social_media">
<button type="submit" class="btn-facebook"></button>
<button type="submit" class="btn-gmail"></button>
</div>
</body>
heres the css:
.signup-form {
top:-40px;
position:relative;
box-sizing: border-box;
margin: 25px auto;
margin-bottom:0px;
width: 100%;
max-width:400px;
background-color: white;
padding: 50px 50px 50px 50px;
box-shadow: 1px 5px 2px #330000;
z-index: -1;
}
.social_media {
text-align:center;
}
.btn-facebook {
margin-bottom:60px;
padding-left:30px;
background-image: url(fb.gif);
background-color: #3b5998;
padding: 0px;
border: 3px solid white;
border-radius: 5px;
box-shadow: 1px 2px 2px grey;
background-size: contain;
background-repeat: no-repeat;
height: 70px;
width: 70px;
font-family: sans-serif;
font-size: 16px;
color: white;
}
.btn-gmail {
margin-bottom:60px;
top:50%;
padding-right:30px;
background-image: url(g+.gif);
background-color: #dc4a38;
padding: 0px;
border: 3px solid white;
border-radius: 5px;
box-shadow: 1px 2px 2px grey;
background-size: contain;
background-repeat: no-repeat;
height: 70px;
width: 70px;
font-family: sans-serif;
font-size: 16px;
color: white;
}
also the first screenshot is what it looks like now and the second is from a photoshopped image on what im trying to replicate
Set the position property value for the parent to relative (to control overlap) and that of the button DIV to absolute. Now place your button inside the button DIV, then use negative values for left, right, top or bottom to position the button DIV where you want.
.parent {
width: 150px;
height: 150px;
margin: 0 auto;
background: red;
position: relative;
}
.button {
width: 100px;
height: 45px;
line-height: 45px;
text-align: center;
position: absolute;
background: darkOrange;
border-radius: 5px;
bottom: -22.5px;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%)
}
<div class="parent">
<div class="button">
<input type="button" value="my button" />
</div>
<div>
try adding to the btn classes
position: absolute;
then move them to desired position
you may need to change the z index also.
Set the position on the social_media div to relative and then use the top property to move them as needed. For example:
.social_media {
text-align:center;
position: relative;
top: -74px;
}
jsFiddle example

Why inner div container overflows?

From below code,
.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
/* url(some img)*/
padding-left: 15px;
padding-top: 10px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
}
.customercardtype {
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
position: relative;
height: 8%;
margin-top: 5px;
}
.customercardtype .formlabel {
display: block;
height: 20%
}
.customercardtype .cardtypecontainer {
position: absolute;
width: 100%; /* Any problem here? */
top: 40%;
height: 50%;
border: 1px solid red;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step3: Card details
<div class="customercardtype">
<label class="formlabel">Cardtype:</label>
<div class="cardtypecontainer">
</div>
</div>
</form>
I would like to understand,
Why inner div container overflows?
This is because the width of an element is actually width + left padding + right padding + left border + right border.
As your width is 100% and additional to this will push it over 100%, making it overflow its parent.
If you use box-sizing: border-box, that will fix this issue.
That's a quick summary, lots more in depth info here: https://css-tricks.com/box-sizing.
The reason it overflows is because position absolute visually speaking, positions your element outside the normal flow of the site. This is intentional and powerful if you use it correctly. However in your case, the parent container of cardtypecontainer was not taking control of the absolute positioned element, therefore it overflowed outside its container.
Then, I changed cardtypecontainer to have relative position, which will work as you intended it to, because relative position does not change the intended layout of the element. For your case it means, cardtypecontainer will stay within the bounds of its parent container.
.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
/* url(some img)*/
padding-left: 15px;
padding-top: 10px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
}
.customercardtype {
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
position: relative;
height: 8%;
margin-top: 5px;
}
.customercardtype .formlabel {
display: block;
height: 20%
}
.customercardtype .cardtypecontainer {
position: relative;
margin-top: 10px;
width: 100%;
height: 50%;
border: 1px solid red;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step3: Card details
<div class="customercardtype">
<label class="formlabel">Cardtype:</label>
<div class="cardtypecontainer">
</div>
</div>
</form>