Alter the appearance of another div when focused on

Is there a way to change the fill color of the SVG when a user focuses on the input field? I have included the HTML and SCSS for reference, but I'm not sure how to write the CSS code to achieve this effect.

<div class="search">
    <svg width="20px" height="20px" viewBox="0 0 88 88" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <g id="Symbols" stroke="none" stroke-width="1" fill-rule="evenodd">
            <g id="Icon-Search" fill="#b3b3b3" fill-rule="nonzero">
                <path d="M86.829,81.172 L64.128,58.471 C69.055,52.312 72,44.5 72,36 C72,16.118 55.883,0 36,0 C16.118,0 0,16.118 0,36 C0,55.882 16.118,72 36,72 C44.5,72 52.312,69.054 58.471,64.127 L81.172,86.828 C81.953,87.609 82.977,88 84,88 C85.024,88 86.048,87.609 86.829,86.828 C88.391,85.267 88.391,82.733 86.829,81.172 Z M36,64 C20.536,64 8,51.464 8,36 C8,20.536 20.536,8 36,8 C51.465,8 64,20.536 64,36 C64,51.464 51.465,64 36,64 Z" id="Shape"></path>
            </g>
         </g>
      </svg>

      <input class="input inputSearchField" type="text" placeholder="Search...">
  </div> 



.input{
    width: 250px;
    height: 35px;
    padding-left: 16px;
    font-weight: 700;
    background-color: $white-color;
    border-radius: 4px;
    border: 1px solid $lighter-gray;
    font-size: 16px;
    outline:none;
    transition: all 0.30s ease-in-out;
    &::placeholder {
        color: $light-gray;
    }
    &:focus {
        box-shadow: 0 0 5px #64e469;
        border: 1px solid #64e469;
    }
}
.search { 
    position: relative;
    margin: 0 auto;
    text-align: center;
    width: 270px;
    margin-bottom: 30px;
  }
  .search svg { 
    position: absolute;
    top: 7px;
    right: 22px;
    fill: $lighter-gray;
  }

Answer №1

Don't forget to take a look at this awesome CodePen Demo. Make sure to position your input element before your SVG, unless you're ready to use some JavaScript for the task. CSS lacks a "look behind" selector, making it a bit tricky to achieve. Even the General Sibling Combinator works in the opposite direction.

If you decide to rearrange the elements, simply use:

.input:focus + svg #Icon-Search {
  fill: #64e469;
}

Alternatively, if maintaining the current structure is crucial, then you would need JavaScript similar to what's demonstrated in this Demo

let searchField = document.getElementsByClassName('inputSearchField');
let svgIcon = document.getElementById('Icon-Search');

searchField[0].onfocus = function(){ svgIcon.style.fill = '#64e469'; }
searchField[0].onblur  = function(){ svgIcon.style.fill = '#b3b3b3'; }

The JavaScript approach can be more intricate!

Answer №2

I'm pondering if you have an HTML structure similar to this:

<div class="search">
  <input class="input" />
  <svg />
</div>

Your CSS code could look something like this:

.input:focus + svg { ... }

Alternatively, if the svg element precedes the input, then you can use the tilde (~) selector instead of the plus (+) selector.

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 best way to access Sass variables in a different Sass file?

I'm currently working on a WordPress theme and running into an issue with utilizing SASS from another file using the @use method. For those of you who may not know, the @import rule method will soon be deprecated which is why I am trying to implement ...

Does anyone know of a tool that allows you to save HTML as a standalone page?

Looking for a solution to easily send standalone .html files with no external dependencies to clients on a regular basis. The original pages are built using node.js and express, and feature libraries like High Charts. Up until now, I have been manually pre ...

To ensure a rectangular image is displayed as a square, adjust its side length to match the width of the parent div dynamically

How can I make the images inside my centered flexbox parent div, #con, be a square with side length equal to the width of the parent div? The image-containing div (.block) is positioned between two text divs (#info and #links). I want the images to be squa ...

Update the content of an HTML element without having direct access to the HTML code

I am currently in the process of creating a website using a website builder, and I am interested in customizing the default message that is displayed when a required field in the website form is left empty. The form in question is an integral part of the w ...

When `focus` is bound to a jQuery event handler, the behavior of the select element becomes erratic on

What causes the odd behavior where users need to click twice on a select-option for it to drop down/up after binding an eventhandler to the focus event using jQuery? $('input, select, textarea').focus(function() { $(this).addClass('input_ ...

Ways to showcase the chosen choices from earlier stages in the intelligent guide

Currently, I am developing a user interface for my latest project and have incorporated the Smart Wizard from . In particular, I am utilizing the smartwizard-modal.html found in the examples section of smartwizard. This specific smart wizard modal consis ...

What could be causing my CSS div class to cover up the image in the background?

Within my HTML code, I have a <div> that contains a whole partial view: <div class="bkgImg"> /* content goes here */ </div> Inside this main <div>, there are two additional <div> elements - one without a class and one wi ...

CSS: Strategies for eliminating empty cells within a grid layout by filtering out rows that contain partial or incomplete content

I am in need of creating a grid layout where each div is a specific width, and the number of rows depends on how many items there are. The twist is that I am unsure of the width of the outer container. My initial solution was to use CSS grid: #container ...

Angular and Bootstrap 5 combine to create a dynamic multi-item carousel featuring animated slide transitions and embedded YouTube videos

I'm trying to create a multi-item carousel using YouTube videos, and although I have managed to get it working with Bootstrap 5 carousel and cards, the animation when the carousel slides is not as smooth as I would like. The issue seems to be that the ...

What could be causing my div to not appear when using jquery's show function?

I'm dealing with HTML code that looks like this: <div id="answerTypeSection" style="visibility: hidden"> <div class="row"> <div class="col-md-2">adfadfafasdfasdfas</div> </div> <label class="control-label"&g ...

The accordion won't function properly if it is added using append() after the document is ready

I have implemented a button to add an accordion, but unfortunately it doesn't seem to be working properly. I am unsure of how to troubleshoot and resolve this issue. If the accordion HTML is appended after the document.ready() function, then the accor ...

svg-to-json.js | The mysterious disappearing act of my file

After completing the installation process for svg-to-json.js as detailed in my previous post, I ran the command: node svg-to-json.js filename.txt The expectation was that a .json file would be generated, but I couldn't locate it. Is it supposed to ...

The start screen fails to display when clicking the restart button

I am struggling to get the restart button to display the start screen again. Even though I have called the restart function within the clearColors function, which should show the start screen, it hasn't been effective so far. Initially, in the fadeOut ...

CSS confusion: What's causing the header to not align horizontally and the footer to not justify evenly spaced?

I am new to coding and experimenting with CSS flexbox to design a responsive layout. I am struggling with aligning the header elements horizontally in the widest viewport and adjusting the footer to space out evenly. For the header, I attempted using disp ...

Implementing event handling with .On() in Jquery following .Off()

I need assistance with my responsive navigation bar. I am having trouble with the jQuery code to disable hover events if the width is less than or equal to 768px and enable it for desktop screens. $(window).on('load resize', function (e) { v ...

The <a> tag does not lead to a different webpage and cannot be clicked on

I have developed a web component that includes a method to generate a copyright string: '<p>Copyright © 2020 John Doe<a href="https://www.example.com">. Terms of Use</a></p>' After creating the string, I conver ...

Preventing special characters, numbers, and spaces from being used as the first character in a word in HTML with regular expressions

Is there a way to restrict users from inputting special characters, numbers, and spaces only in the first place of a word using regex in HTML? <label><span>Current Carrier</span></label> <input name='Current Carrier' t ...

Looking to extract specific information from a webpage using VBA

I'm trying to extract the text 'Catalog Manager/ Sales' & 'EOL (product/component)' from the view source tab using VBA. Here is an excerpt of the view source code: <tr> <td nowrap="true" valign="top" width="165px" cl ...

Site with Three Steady Columns

While I know that this question has been asked in the past, I'm eager to see if there have been any recent changes. I am currently searching for a fixed 3 column layout using html/css, with the main content (middle) area being located first in the DO ...

The HTML and body elements do not possess a width of 100% within the document

When viewing my portfolio site on smaller screens such as 425px and below, there is a white gap on the right side. This issue arises because the HTML and body elements do not inherit full width by default. Do you know why this happens and how I can resol ...