Expanding search bar exanding to the left and show search button - html

I am using following code and code work fine. but i wanna that when user click on search button then search button will also show like stackoverflow search bar has.
HTML :-
<form>
<input type="text" name="search" placeholder="Search..">
</form>
CSS
input[type=text] {
display:block;
margin: 0 0 0 auto;
width: 250px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 100%;
}

Follow these steps
Create a button
Place that button on the search bar
set display property of that button to none
now whenever you focus on the search bar, change display property of that button to block
try implementing this code on your own first ...
input[type=text] {
display:block;
margin: 0 0 0 auto;
width: 250px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 90%;
}
input[type=text]:focus + .but{
display: block;
}
.but{
cursor:pointer;
width: 100px;
height: 50px;
position: absolute;
right:5px;
top:5px;
display:none;
}
<form>
<input type="text" name="search" placeholder="Search.."><input class="but" type="button" value = "search">
</form>

Related

Issue when creating an expanding search bar using :focus-within

I want to make a search bar as this website's search bar: https://www.arrivalguides.com/en.
I tried with this code: (Here's a codepen)
HTML:
<div class="col-md-4 d-flex justify-content-end all-buttons">
<div class="col-md-4 d-flex justify-content-end search-bar">
<a nbButton outline class="text-decoration-none add-btn" id="add-btn-hide" >
<i class="nb-plus fa-1x"> </i> Add User
</a>
<button nbButton class=" multi-delete-btn" id="delete-btn-hide" >
<i class="nb-trash fa-2x"></i>
Delete All
</button>
<input type="text" class="search-input" id="search-input" fullWidth nbInput placeholder="Enter Search" (keyup)="onKey($event)" />
<button nbButton class="search-btn" nbTooltip="Quick Search " nbTooltipPlacement="top" status="primary">
<!-- <nb-icon icon="search-outline"></nb-icon> -->icon
</button>
</div>
</div>
CSS:
body{
margin:40px;
}
.all-buttons {
height: 30px;
padding-right: 0;
}
.info-btn {
padding: 0.4rem;
border-radius: 0.25rem;
margin-right: 3px;
background-color: blue;
border-color: blue;
}
.advanced-filters {
background-color: green;
padding: 0.6rem 0 0 0;
border-radius: 0.5rem;
margin-left: 1px;
}
.multi-delete-btn {
margin: 0 8px 0 3px;
color:white;
background-color: orange;
border-color:orange;
border-radius: 0.25rem ;
padding: 0 5px 0 0 ;
opacity: 1 !important;
transition: width 0.5s ease-out;
}
.add-btn {
border-color:blue ;
background-color: blue ;
color: white;
padding: 0.5rem;
border-radius: 0.25rem;
margin: 0 ;
opacity: 1 !important;
transition: width 0.5s ease-out;
}
.search-bar {
border: none !important;
margin-right: 2px !important;
border-radius: 100px ;
min-width: 33px !important;
position: relative;
transition: width 0.5s ease-out;
}
.search-input {
background: transparent;
border: 0;
background-color: lightblue;
opacity: 0;
width:40px !important;
transition: width 0.5s ease-out;
&:focus {
outline: 0;
}
}
.search-btn {
cursor: pointer;
border: 2px solid blue;
border-radius: 0.25rem !important;
background: transparent;
margin: 0 !important;
position: absolute ;
top:0 ;
bottom:0;
right: 0;
}
.search-bar:focus-within{
width:50% !important;
border: 2px solid brown !important;
.search-input{
width: 100% !important ;
background-color: rgba(196, 158, 233, 0.205);
color: black !important;
cursor: initial ;
opacity: 1;
}
.add-btn{
display: none !important;
}
.multi-delete-btn{
display: none!important;
}
}
The issue:
When I click outside of the search icon, the "Add" button and "Delete-all" button should appear. But in my case they are just moving. (When I click the search icon, the add-button and delete-all button disappear– this is okay)
Here is an approach isolating only the search input and a search input container. The input is absolutely positioned inside a relative container so that I could grow the input from right to left. I did this by anchoring it to the right side of the parent container using right: 0.
I'm combining focus-within and placeholder-shown to grow/shrink the input. Using placeholder-shown is needed to prevent the input from shrinking if the user has typed something in the input. To hide the placeholder text and only show the search icon when the input is shrunk, I'm making the placeholder text color transparent.
.search-input-container {
position: relative;
}
.search-input {
--search-icon-width: 24px;
--search-max-width-expanded: 20rem;
--search-max-width-collapsed: 2.3rem;
--search-color: #ccc;
background-color: var(--search-color);
max-width: var(--search-max-width-collapsed);
padding: .5rem 0.75rem;
overflow: hidden;
transition: 0.3s max-width, 0.3s margin;
border: none;
background-image: url(https://image.flaticon.com/icons/png/512/93/93642.png);
background-size: 18px;
background-repeat: no-repeat;
background-position: calc(100% - 10px) 50%;
position: absolute;
right: 0;
font-size: 16px;
border-radius: 15px;
}
.search-input:focus-within,
.search-input:not(:placeholder-shown) {
max-width: var(--search-max-width-expanded);
padding-right: var(--search-icon-width);
}
.search-input:not(:focus-within)::placeholder {
color: transparent;
}
/* Ignore */
body {
display: grid;
place-items: center;
}
<div class="search-input-container">
<input placeholder="search" type="search" class="search-input">
</div>

Adding right: 50% to input tag doesn't do anything [duplicate]

This question already has answers here:
How can I center an absolutely positioned element in a div?
(37 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
Below, the left: 50% in the css does not move the input element in any way. Anyone know why? Is this just not possible? Do I have anything blocking it? I don't want to use margins because it will look different on different computers. If left: is not possible, how can this be centered either way?
(I put the left: 50% at the bottom of css for the searchbar)
#searchbar{
border-radius: 4px;
background-color: transparent;
cursor: pointer;
color: transparent;
background: #FFF;
padding: 1px 1px 1px 5px;
width: 30px;
height: 30px;
outline: none;
border: 1px solid #CCCCCC;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
text-align: center;
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
background-size: contain;
border:none;
right: 50%
}
<input id = "searchbar" name = "search" size = "1" autocomplete="off" maxlength="27">
Try,
<input id = "searchbar" name = "search" size = "1" autocomplete="off" maxlength="27">
CSS
#searchbar{
position:absolute;
left:50%;
border-radius: 4px;
background-color: transparent;
cursor: pointer;
color: transparent;
background: #FFF;
padding: 1px 1px 1px 5px;
width: 30px;
height: 30px;
outline: none;
border: 1px solid #CCCCCC;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
text-align: center;
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
background-size: contain;
border:none;
}
OR
<div id="searchbardiv">
<input id = "searchbar" name = "search" size = "1" autocomplete="off" maxlength="27">
</div>
CSS
#searchbardiv{
width:100%;
display:flex;
position relative
}
#searchbar{
position:absolute;
left:50%;
border-radius: 4px;
background-color: transparent;
cursor: pointer;
color: transparent;
background: #FFF;
padding: 1px 1px 1px 5px;
width: 30px;
height: 30px;
outline: none;
border: 1px solid #CCCCCC;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
text-align: center;
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
background-size: contain;
border:none;
}
You can use a flex box to center content.
.flex {
display: flex;
flex-direction: row;
justify-content: center;
}
#searchbar {
border-radius: 4px;
background-color: transparent;
cursor: pointer;
color: transparent;
background: #FFF;
padding: 1px 1px 1px 5px;
width: 30px;
height: 30px;
outline: none;
border: 1px solid #CCCCCC;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
text-align: center;
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Simpleicons_Interface_magnifier-1.svg/768px-Simpleicons_Interface_magnifier-1.svg.png) no-repeat scroll 0 0;
background-size: contain;
border: none;
}
<div class="flex">
<input id="searchbar" name="search" size="1" autocomplete="off" maxlength="27">
</div>

Redirect to URL on Placeholder Click

I have some code here that creates an animated search bar:
<html>
<head>
<style>
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 100%;
}
</style>
</head>
<body>
<form>
<input type="text" name="search" placeholder="Search..">
</form>
</body>
</html>
Whenever somebody clicks on it, the search bar expands and lets the user type the keyword(s). How would I make it so that when the user clicks on the search bar, it waits (e.g 1 secs) for the search bar to expand and then redirects to a URL. Do I change the input[type=text] to input[type=code] and do the same with input[type=text]:focus???? Please don't give too complicated answers and I am just a beginner. I looked up most directions to insert the code but my website does not support the site.com/?search=keywords. Please help!!
Thanks,
- Will
Maybe with JS :
<form>
<input type="text" name="search" placeholder="Search.." onClick="redirect()">
</form>
And :
<script>
function redirect() {
setTimeout(function(){
window.location.replace("http://stackoverflow.com");
}, 1000);
}
</script>
You can use the css propert for transition, "transition-delay" here is the code example. Here is a jsfiddle
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
-webkit-transition-delay: 2s; /* Safari */
transition-delay: 2s;
}
input[type=text]:focus {
width: 100%;
}

Animated Search Form expanding to the left

My box is expanding to the right, how can I make it expand to the left?
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 100%;
}
<form>
<input type="text" name="search" placeholder="Search..">
</form>
Just align it to the right of its container element. Either change the display attribute to block and set the right and left margins to 0 and auto, or keep it as an inline element and apply text-align: right to the container block.
input[type=text] {
display:block;
margin: 0 0 0 auto;
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 100%;
}
<form>
<input type="text" name="search" placeholder="Search..">
</form>

search box not getting positioned in css3

input[type=text] {
margin-left: 63%;
margin-top: -100%;
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: black;
background-image: url('searchicon.png');
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 150px;
}
<form>
<input type="text" name="search" placeholder="Search..">
</form>
even after puttin margin-top -100% the search box is not going up. it stopped going up after -10%. Any help would be highly appreciated. Thanks in advance. :)
Use "position:relative" and "top:-100px". It will move your search box to move up. You can increase the 'top' value as you need.
input[type=text] {
margin-left: 63%;
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: black;
background-image: url('searchicon.png');
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
position: relative;
top: -100px;
}
input[type=text]:focus {
width: 150px;
}