Thursday, January 25, 2024

How to Create an Expanding Search Input with HTML, CSS, and JavaScript

How to Create an Expanding Search Input with HTML, CSS, and JavaScript

As a designer, you can enhance the user experience of a website or mobile application by adding subtle animations to its components. One such component is a search input box. This tutorial will cover how to create an expanding search input; one which expands when a user clicks on a search icon.

You’ll find this sort of expanding search input effect if ever you use Youtube in the browser:

screenshot of the youtube expanding search inputscreenshot of the youtube expanding search inputscreenshot of the youtube expanding search input

Search Input Demo

We will use every front end developer’s favorite three technologies:

  • HTML
  • CSS
  • JavaScript

We will mainly use CSS to make it functional and some HTML and JavaScript. Check out this demo to see the final result.

HTML Structure

The HTML Structure will be just a simple div containing an input element and a button.

1
<div class="search-wrapper">
2
  <input type="search" placeholder="Search" />
3
  <button class="search-btn">
4
    <i id="search-icon" class="fa-solid fa-magnifying-glass"></i>
5
  </button>
6
</div>

You’ll notice this is an input with a type of search, not text. There’s no real difference between the two, but search is semantically correct in our case.

<input> elements of type search are text fields designed for the user to enter search queries into. These are functionally identical to text inputs, but may be styled differently by the user agent.” — mdn

Styling With CSS

The first thing we want to do is to center everything using Flexbox. Apply the following styles to the body.

1
body {
2
    display: flex;
3
    align-items: center;
4
    justify-content: center;
5
    background-color: gray;
6
    min-height: 100vh;
7
}

Next, we will style the full search input and later we will work on the expanding feature. For the wrapper element, add an height, width, border, border-radius and a background .

1
.search-wrapper {
2
  width: 50px;
3
  height: 50px;
4
  display: flex;
5
  border-radius: 4rem;
6
  background: #fff;
7
  align-items: center;
8
  justify-content: center;
9
  outline: none;
10
  border: 2px solid #888;
11
  
12
}

We have also added the style display: flex to the wrapper div element so that the child elements .i.e. input and button will also be centered. For the input element, set border:none, and outline:none. We also want it to have a full width and height. Set a margin-left:20px to the placeholder text and some padding.

1
.search-wrapper input {
2
  width: 100%;
3
  height: 100%;
4
  padding: 10px;
5
  margin-left: 20px;
6
  outline: none;
7
  border: none;
8
  font-size: 1.5rem;
9
10
}

To make the icon visible, set a background color to the button. Round the button with border-radius:50%, and position it to the right of the wrapper element by setting margin-left: auto . The styles will look like this:

1
.search-btn {
2
  font-size: 2rem;
3
  border-radius: 50%;
4
  padding: 10px;
5
  border: 2px solid white;
6
  width: 60px;
7
  cursor: pointer;
8
  height: 60px;
9
  background: #e66610;
10
  margin-left: auto;
11
}

Expanding Feature

Now that the entire search input is complete, we can work on the expand feature. The first thing we want to do is reduce the width of the wrapper element.  

1
.search-wrapper {
2
  width: 60px;
3
  height: 60px;
4
  display: flex;
5
  border-radius: 4rem;
6
  background: white;
7
  align-items: center;
8
  justify-content: center;
9
  border-radius: 100px;
10
  outline: none;
11
 
12
13
}

Next, Set position:relative to the wrapper div element and position:absolute to the input element; This will ensure that the input's position is relative to the parent element rather than the body.

1
.search-wrapper {
2
  /* the rest of the CSS */
3
  position: relative;
4
}
5
6
7
.search-wrapper input {
8
  position: absolute;
9
  top: 0;
10
  left: 0;
11
  /* the rest of the CSS */
12
}

Now we have something like this.

round input textround input textround input text

As you can see, we still need additional CSS.

Let’s make the button float on top of the input by setting z-index:1 to the button. 

1
.search-btn {
2
  font-size: 2rem;
3
  border-radius: 50%;
4
  padding: 10px;
5
  border: 1px solid #be0671;
6
  width: 60px;
7
  cursor: pointer;
8
  height: 60px;
9
  background: #be0671;
10
  margin-left: auto;
11
  z-index: 1;  
12
13
}

The final style is to hide any overflow by setting overflow: hidden on the wrapper div element.

1
.search-wrapper {
2
  /* rest of the CSS */
3
  overflow: hidden;
4
 
5
}

JavaScript Functionality

The JavaScript functionality will expand the wrapper element to full width when you hover with a mouse and reduce the width if you remove the mouse. Let’s first define the CSS class for increasing the width.

1
.active {
2
  width: 350px;
3
}

Select the wrapper div element 

1
 const search = document.querySelector(".search-wrapper");

Next, add two event listeners, mouseover and mouseout. On mouseover, we will add the active CSS style; on mouseout, we will remove the CSS style.

1
search.addEventListener("mouseover", () => {
2
  if (!search.classList.contains("active")) {
3
    search.classList.add("active");
4
  }
5
});
6
7
search.addEventListener("mouseout", () => {
8
  if (search.classList.contains("active")) {
9
    search.classList.remove("active");
10
  }
11
});

Animation

The final step is to add a transition effect to make the transition effect smooth. 

1
.search-wrapper {
2
  /* the rest of the CSS */
3
  transition: 400ms ease-in-out;
4
5
}

You can play around with the transition values to achieve the desired feel. That’s it for this tutorial; check out the CodePen below. 

Conclusion

This tutorial has covered creating an expanding search bar that includes animations to improve interactivity. Incorporating transitions in your design is essential to make the user interface more engaging and enjoyable.


Saturday, January 13, 2024

4 Quick Photoshop Keyboard Shortcuts(+Bonus Tip!)

4 Quick Photoshop Keyboard Shortcuts(+Bonus Tip!)

Today, I've got a list of four Photoshop keyboard shortcuts that every Adobe Photoshop user should be using. Trust me, they're super simple, but you'll be amazed at how much time they can save you. Oh, and guess what? I'll also show you how to create your own keyboard shortcuts. Exciting, right? So let's dive right in!

Here's a video from the Envato Tuts+ YouTube channel to help you follow along:

What You'll Learn in This Tutorial 

  • How to crop an image in Photoshop using shortcuts
  • How to add a layer in Photoshop using shortcuts
  • How to change the background color in Photoshop using a shortcut key
  • How to change the brush size in Photoshop using a shortcut
  • How to add a shortcut in Photoshop

How to Crop an Image in Photoshop Using Shortcuts

Just hit C on your keyboard to Crop an image to the perfect size or expand the canvas to make it larger. This simple shortcut makes it easy to adjust your images with precision.

crop an image using the C keycrop an image using the C keycrop an image using the C key
Close-up portrait of a young man

How to Add a Layer in Photoshop Using Shortcuts

To create a New Layer in Photoshop, press Control-Alt-Shift-N on Windows or Command-Option-Shift-N on Mac. This handy shortcut is one of the most frequently used actions in Photoshop, allowing you to create layers without interrupting your editing flow. 

create a new layer using Control+Alt+Shift+N on Windows or Command+Option+Shift+N on Mac.create a new layer using Control+Alt+Shift+N on Windows or Command+Option+Shift+N on Mac.create a new layer using Control+Alt+Shift+N on Windows or Command+Option+Shift+N on Mac.
Girl with unusual hairstyle

How to Change the Brush Size in Photoshop Using Shortcuts

One of my personal favorites is to use the [ and ] keys to effortlessly adjust the Size of any brush-based tool in Photoshop. Stop manually adjusting the size of brushes—use the bracket keys instead, and see just how much time you save! 

Hold Shift-[ or ] to change the Softness of your brush. 

How to Change the Background Color in Photoshop Using a Shortcut Key

Press Alt/Option-Backspace to create an instant fill color on the selected layer. 

Don't forget to set your Foreground Color to the hue you want to fill the layer background with.
Press Alt/Option+Backspace to create an instant fill color Press Alt/Option+Backspace to create an instant fill color Press Alt/Option+Backspace to create an instant fill color
Sushi Logo

Bonus Tip: How to Add a Shortcut in Photoshop

If you find these shortcuts a bit complicated, no worries. You can create your own shortcuts by going to Edit > Keyboard Shortcuts.

That means you can customize any key on your keyboard, even the function keys, to effortlessly perform your favorite tools and actions. So go ahead and make your shortcuts fit your workflow perfectly.  

create your own shortcuts by going to Edit > Keyboard Shortcuts.create your own shortcuts by going to Edit > Keyboard Shortcuts.create your own shortcuts by going to Edit > Keyboard Shortcuts.

That's It!

To sum it up, mastering these Photoshop shortcuts can level up your workflow and productivity game. Don't hesitate to dive in and start using these shortcuts like a pro. In no time, you'll be navigating through the software with ease. Happy designing!