Tips on controlling the color of an input field in a form using React

Within my React app, there is a text input field situated inside a form that displays in its default gray color before any interaction:

https://i.stack.imgur.com/FdNos.png

Once the input field is clicked on, it changes to white as shown here: https://i.stack.imgur.com/SaWto.png

My goal is to maintain the initial grayish color of the text box even after it has been clicked. How can I accomplish this? Below is the code snippet I have so far:

html,
body {
  height: 100%;
  margin: 0;
  background: rgb(105, 103, 116);
}

.main-search {
  position: relative;
  margin-bottom: 10px;
  border-radius: 5px;
}

.main-search:hover {
  box-shadow: 0px 4px 20px 0px rgba(0, 0, 0, 0.05);
}
<form onSubmit={this.spinSubmit}>
  <div className="input-field container main-search">
    <input className="main-search-input-field" id="search" type="search" placeholder="Search..." value={this.state.searchQuery} onChange={this.handleChange} />
    <button id="btn-submit-search"><i className="material-icons">search</i></button>
  </div>
</form>

All the testing has been carried out using Chrome browser, if that makes any difference.

Answer №1

All you have to do is include

.main-search-input-field {
    background-color: silver;
}

In your stylesheet.

Answer №2

implement this.

input[type="search"], textarea {    
  background-color :rgb(105, 103, 116);
  border:0px;    
}

result:

https://i.stack.imgur.com/ZTUs1.png

Another possibility is to utilize this. however, update "className" to "class"

.main-search-input-field, textarea {
  background-color :rgb(105, 103, 116);
  border:0px;
}

 <input class="main-search-input-field" id="search" type="search" placeholder="Search..." value="search" onChange="" />

Answer №3

To view the demonstration, check out this fiddle - Visit the fiddle here

All you have to do is add CSS styling to your input element.

input[type="search"] {    
 background-color :rgb(105, 103, 116);
 border:0px;
 border-bottom:1px solid #ccc;
}

Answer №4

When it comes to styling with CSS:

#search{
background-color: rgb(105, 103, 116);
}

Answer №5

To resolve the issue, I adjusted the type attribute within my input tag to "text" rather than "search":

 <input className="main-search-input-field" id="search" type="text" placeholder="Search..." value={this.state.searchQuery} onChange={this.handleChange} />

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

Unable to align Flexbox item to flex-end position

Can someone help me troubleshoot this codepen with the .inner-wrp div (dark red) fitting under the lightblue .top-right div? Check it out here! .purple { background: purple; } .green { background: green; } .lightblue { background: lightblue; } ...

NextJS believes that when extracting data from "FormData", it is considered as having a value of "null"

I'm currently working on a NextJS application and encountering an issue where the form data seems to be considered null by NextJS even before I pass it. Below is my action.ts file: 'use server'; import { date, z } from 'zod'; impo ...

Web Essentials is experiencing a problem with minified CSS

Today, while using Web Essentials 2013 for Update 3 with Visual Studio Express 2013 for Web, I encountered an issue with the minified CSS file. The website I am currently working on is built on a Twitter Bootstrap platform and utilizes two separate CSS fil ...

Steps to access a Windows folder after clicking on a link within an HTML page using Asp.net

I attempted to use the following code : <a href="file:///C:\Programs\sort.mw">Link 1</a> <a href="file:///C:\Videos\lecture.mp4">Link 2</a> Unfortunately, this code does not work in Google Chrome or Firefox bro ...

Tips for eliminating the gap between a heading and column in Bootstrap 4

.rowHeight{ height: 100px; } .padding-0 { padding-right: 0px; paddig-left: 0px; margin-left: 0px; margin-right: 0px; } .cart{ ...

The WP archive.php template is malfunctioning

Having an issue with my website Whenever I try to view the monthly archive () the design appears flipped upside down. I initially assumed that if the archive.php file was missing, WordPress would default to using index.php for the archive page. So why is ...

How to resize and center an image within a div element as they both scale proportionally

Can someone help me figure out how to center a resized image within its container, which should also be centered? I've been struggling with this for 7 hours and can't seem to get it right. Any assistance would be greatly appreciated :-) Here is ...

Preventing users from copying and pasting information from my form by implementing javascript restrictions

I'm looking for a solution to prevent users from copying and pasting in my form using JavaScript. I want to restrict the ability to paste or copy any content into the form. Any assistance would be greatly appreciated! ...

Display an image with only a portion visible, no canvas, no frame, and no need

My dilemma involves an img with a surrounding box div. http://jsfiddle.net/6d4yC/7/ 1) I am seeking to display only a portion of the image (250x150) without generating a white overlay when it is in its large size. Placing a #box1 div around the image has ...

Creating a dynamic search feature that displays results from an SQL database with a dropdown box

Is there a way to create a search bar similar to those seen on popular websites like YouTube, where the search results overlay the rest of the page without displacing any content? I've searched extensively on Google and YouTube for tutorials on databa ...

What are the risks of using react + bootstrap without importing bootstrap.js?

When bootstrap library is used along with React, potential unpredictable behavior and difficult bugs may arise due to DOM modifications. Is it feasible to use bootstrap without including bootstrap.js? What are the drawbacks? While I am aware of the ' ...

Looping through two arrays simultaneously in Angular JS

Seeking help to display two arrays in a conversational manner using ng-repeat Let's say we have two arrays defined as follows: messages_send = ["hi","good, How about you?","cool","LOL","NFW"] messages_received = ["hey, How are you?","good, Thanks ...

Determine if a specific identifier is already present using Javascript or Jquery

Is there a way to determine if an html tag with its specific id is present more than once in the code? This is my pseudocode for checking: if($('#myId').length > 1) { // It exists twice } ...

Use CSS to create a fullscreen animation for an element

Is there a way to animate a div element so that it expands to fit the screen when clicked without specifying an initial position? I need the div to be able to adjust dynamically within the screen. .box { width: 80px; height: 80px; background: red; ...

Retrieving checkbox values from a MySQL database in HTML/PHP

My database contains data stored in varchar format, such as S,M,L,XL. On my PHP page, I have checkboxes corresponding to these options. Is it feasible to fetch this data from the database and display the pre-checked checkboxes in HTML using PHP? (For exa ...

How can I use jQuery to reset a select dropdown to its initially selected option upon page load?

Can a select element be reset to its original loaded option using jQuery without the need for creating a custom cache of the initial value? ...

Get rid of the underlined anchors in your email signature

I'm currently in the process of creating personalized email signatures for one of my clients. At this point, I am testing them on various platforms such as GMail, Hotmail, Brinkster, and more. One issue I am facing is trying to remove the underline t ...

Issue with React Toastify display not showing

I'm having trouble getting a pop-up alert to appear when a user interacts with a radio button. I've included the relevant code snippet below. Even though I see the console message when I select a radio button, the pop-up doesn't show up. Can ...

Troubleshooting a WordPress Blog Homepage CSS Dilemma

My goal is to keep the featured image size consistent on the main blog page. I made adjustments to this code: HTML: <div class="featured-image-excerpt"> <img class="attachment-post-thumbnail size-post-thumbnail wp-post-image" src="http://mrod ...

The background image in CSS fails to display when a relative path is utilized

Currently, I am working on a JavaFX project which has the following file structure (CEP serves as the root directory): CEP +img menu.jpg +src +css_files MainMenu.css The main objective is to set the background image from the img dir ...