Expanding the CSS Search Box

Visit the link

I've been working on coding a CSS text box. When you hover over the button in the provided link, you'll notice that the 'Type to search' functionality is working smoothly as it moves to the right.

My current focus is on syncing the background of the button with the text box itself.

The main objective here is to expand the box only towards the right side.

<html>
  <head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="styles.css">
  <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js">
  </script>`enter code here`
  </head>
  <head>
    <title>First Experience with CCS</title>
  </head>  
  <head>
  <style>
    h1 {

    color: orange;
    text-align: left;

    }

    </style>
    </head>
      <body>
    <h1>
      <span style="font-family:Arial">Welcome to Search Box</span>
    </h1>
      </body>  

<head>
<body>
<div class="search-box">

  <input class="search-txt" type="text" name="" placeholder="Type to search">
  <a class="search-btn" href="#">
  <i class="fas fa-search"></i>
  </a>
</div>  
</head>

    </body>
</html>
body {
    margin: 0;
    padding: 0;
    background: red;
   }

.search-box{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #2f3640;
    height: 40px;
    border-radius: 40px;
    padding: 10px;
   }

   .search-box:hover > .search-txt{

       width: 240px;
       padding: 0 6px;
   }

   .search-box:hover > .search-btn{
    background: white;

}

   .search-btn{
    float: left;
    color: red;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: none;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: 0.4s;   
   }

   .search-txt{
    position: absolute;
    float: right;
    border: none;
    background: none;
    outline: none;
    padding: 0;
    color: white;
    font-size: 16px;
    transition: 0.4s;
    line-height: 40px;
    width: 0px;
   }

Answer №1

Is this what you had in mind?

https://codepen.io/anon/pen/eovbjW

Remember, a webpage should only have one HEAD and one BODY tag.

Your layout got messed up because of the combination of float and display:flex. I personally try to avoid using float nowadays.

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">

    <link rel="stylesheet" href="styles.css">

    <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script>

    <title>Trying Out CSS for the First Time</title>
  </head>

  <body>
    <h1>Welcome! Let's Explore a Search Box</h1>

    <div class="search-box">
      <div class="fas fa-search"></div>

      <input class="search-txt" type="text" placeholder="Type your search here" />
    </div>  

  </body>
</html>

CSS
[edit] If you're new to CSS variables, don't worry. I used them creatively in .search-text as an example of how they work. I even utilized calc() to combine two CSS variables in the left attribute within .search-text.

:root {
  --background-black: #2f3640;
  --background-red: red;

  --search-box-padding: 10px;
  --search-box-size: 40px;
}

html, body {
  margin: 0;
  padding: 0;
  background-color: var(--background-red);
}

h1 {
  color: orange;
  font-family: Arial;
  margin-left: 1rem;
}

.search-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%);
  height: var(--search-box-size);
  width: var(--search-box-size);
  background-color: var(--background-black);
  border-radius: 50%;
  padding: var(--search-box-padding);
  cursor: pointer;
}

.search-box > .fa-search {
  padding: var(--search-box-padding);
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border-radius: 50%;
  color: var(--background-red);
}

.search-box:hover > .fa-search {
  background: white;  
}

.search-txt {
  position: absolute;
  top: var(--search-box-padding);
  left: calc(var(--search-box-size) + var(--search-box-padding));
  border: none;
  outline: none;
  padding: 0;
  height: var(--search-box-size);
  width: 0px;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;

  background-color: var(--background-black);
  color: white;
  font-size: 16px;
  transition: 0.4s width, 0.4s padding;
}

.search-box:hover > .search-txt{
  width: 240px;
  padding: 0 0.75rem;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the functionality of {all: initial} and how does it address issues with default values?

Why do paragraphs inherit properties like line-height and text-align from the div element? If values for these properties are: normal -> for line-height [i.e. font-size = 16px (initial / default value) * normal (value is usually around 1.2 -> 19.2 ...

Updating the style of different input elements using Angular's dynamic CSS properties

I am seeking guidance on the proper method for achieving a specific functionality. I have a set of buttons, and I would like the opacity of a button to increase when it is pressed. Here is the approach I have taken so far, but I have doubts about its eff ...

Arranging form elements and a map in a side-by-side layout using HTML5

How can I align the contents of a map and a form side by side with responsive design? I've attempted using tables and lists, but the form keeps appearing on top of the map. Ideally, I want the contents to be displayed side by side on wider screens and ...

What method does nosotroshq.com use to achieve the navigation effect when hovering over a menu item?

Seeking advice from skilled individuals familiar with JQUERY: Can anyone explain the technique used by nosotroshq.com in their top navigation bar? Specifically, how do they create the slow animation when hovering over "ABOUT US"? Any insights would be ap ...

The descendant selector in CSS fails to apply changes to the element

I am struggling to modify the border of a specific descendant element and so far I have not been successful. Despite using the correct descendant selector in the HTML below, the style change is not taking effect. If anyone has any insights on what could be ...

Elevation of the specific area in my design

Can someone help me with a height issue I am experiencing? You can find the page in question here: The problem arises when zooming all the way out - the background of the #article div does not extend to the footer. How can I adjust the height of the #art ...

Keeping everything aligned, my aim is to position the search bar and the logo side by side on the left-hand side and stretch the search bar all the way to the end

Looking to adjust the placement of the logo and search bar on the left side while extending the search bar further to the right. Struggling to get the desired layout? No worries, we've got you covered! Also need a join and log in button aligned to the ...

When utilizing *NgIf, the button will be shown without the accompanying text being displayed

When trying to display either a confirm or cancel button based on a boolean set in my component.ts, I implemented the following code in my HTML: <mat-dialog-actions class="dialog-actions"> <button class="cancel-btn" ...

HTML and CSS: Centering elements inside div containers

Is it possible to align elements within a </div> using the align attribute? For example, as shown in the image below: e1:align="top" e2:align="right" e3:align="bottom" e4:align="left" EDIT: This query is purely for self-learning purposes in HTML ...

The parent is relatively positioned and its child is absolutely positioned and floated to the left

I'm currently working on a design where I have a group of parent containers with the float left property applied. Inside each parent container, there is a child div with position absolute defined within them. Here's an example: <div class="wr ...

Smoothly reveal a border at the moment of transition

I currently have images set up as checkboxes that display a yellow border upon clicking with a transition effect. However, the transition of the border causes the images to shake and shutter slightly due to margin adjustments. To resolve this issue, I tri ...

Tips for displaying a pop-up when pressing a link:

I've spent a considerable amount of time on this project without making much progress. However, I came across a tutorial with a beautiful menu design that I decided to replicate. Here is the code: HTML: <!DOCTYPE html> <html> <head> ...

Utilizing graphics within the Atom text editor

Hey, I have a bit of a silly question. I can't seem to figure out why these images aren't showing up on my Atom editor. Can anyone help me with this? <body> <div class="top-container"> <img src="images/clou ...

Maintain the proportional scale of images set to a percentage with window resizing

Currently working on developing a web application. The images in the app are sized using percentages, with three overlapping another image that functions as a toolbar. Everything looks great when viewed in fullscreen mode, but the problem arises when the s ...

How can we stop a form from being submitted when the input field is

Similar Question: How to prevent submitting the HTML form’s input field value if it empty <?php include '../core/init.php'; if(isset($_POST['nt']) && isset($_POST['ntb'])){ mysql_query("INSERT INTO `pos ...

Does li behave like a separate element from p when it comes to pseudo-class assignments?

While experimenting with multiple pseudo-classes assignments involving p and li content, I made an interesting observation. It seems that adding an extra line of text can sometimes disrupt the pseudo-class assignments. For example, in the code provided be ...

Email Embedder - Incorporating HTML source code into email communications

I've encountered an issue where the CDNs (Bootstrap and FontAwesome) do not work when I embed my HTML source code into an email. Despite using inline styling throughout, the receiver still faces this problem. Any insights on why the CDNs are failing t ...

Align the text characters vertically within a word or sentence

I currently have a logo that consists of lines and text. I would like to create a similar version using CSS for my website. My goal is to have the letters "T - S o N" appear in one line with the letter "o" positioned vertically centered between the "S" an ...

When dynamically adding input fields in Bootstrap, there is a smaller gap between inline inputs

When adding a new list item dynamically in a modal using jQuery append, the spacing in the first li element seems to have a larger gap between the input fields compared to the rest that are added later. Even after checking the developer tools and confirmin ...

Tips for building a modal box with HTML, CSS, and jquery!

Looking to create a modal box using HTML, CSS, and jQuery? <button id="myBtn">Click here to open the Modal</button> This code will help you achieve that: <!-- This is the Modal content --> <div class="modal-content"> <sp ...