Tips for changing the text color to stand out from the color of the input field

My input field undergoes color changes depending on whether it's in dark mode or light mode. However, I'm struggling to maintain a distinct color for the input field border and text. The client prefers a very light border for the input field but wants the input text to have more contrast while typing. Despite my efforts to modify the code, both the text and border continue to stay the same color.

function App() {
  const [bg, setBg] = React.useState('light');
  const toggleMode = () => {
    setBg((prev) => {
      if (prev === 'light') return 'dark';
      return 'light';
    });
  };
  return (
    <div>
      <button onClick={toggleMode}>Toggle Mode</button>
      <p>Mode is {bg}</p>
      <div className="form-input">
        <div
          className="form-input-title"
          style={{
            fontFamily: "'Lato', sans-serif",
            color: bg === 'light' ? 'black' : '#CBCBCB',
          }}
        >
          Email
        </div>
        <div className="form-input-input">
          <input
            type="text"
            name="name"
            style={{
              color: bg === 'light' ? '#DCDCE4' : '#48484D',
            }}
          />
        </div>
      </div>
    </div>
  );
}

const rootElement = document.getElementById('root');
const root = ReactDOM.createRoot(rootElement);

root.render(<App />);
.form-input {
  text-align: left;
  width: 100%;
  max-width: 400px;
  padding: 7px 0px;
  font-weight: 700;
  letter-spacing: 1px;
}

.form-input-title {
  color: #CBCBCB;
  font-size: 14px;


}

.form-input-input {

  width: 100%;
  text-align: left;
  font-family: "Fira Sans", sans-serif;
  border-radius: 5px;
  border: 2px;
  position: relative;
  display: inline-block;
}


.form-input input {
  width: 100%;
  padding: 12px 10px;
  font-size: 14px;
  height: 41.77px;
  border-radius: 5px;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Answer №1

To ensure consistency in the color of the input text, border, and outline when focused, you need to specify the border-color, color, and outline CSS properties:

const bg = false;

export default function App() {
  return (
    <input
      style={{
        color: bg ? "red" : "skyblue",
        outline: bg ? "red" : "skyblue",
        borderColor: bg ? "red" : "skyblue"
      }}
    />
  );
}

However, it is strongly advised to use a different color for the outline for accessibility reasons. Opting for a slightly darker or lighter shade than the default text/border color would be ideal.

You can also employ the border-style: solid to achieve a consistent solid color for your input field.


An alternative DRY method:

  const selectedColor = bg ? "red" : "skyblue";

  return (
    <input
      style={{
        color: selectedColor,
        outline: selectedColor,
        borderColor: selectedColor
      }}
    />
  );

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 ensure that TwentyTwenty.js images are always positioned in the

Looking to utilize the TwentyTwenty.js code for image comparison, but facing an issue where the images are aligned to the left side. Is there a way to keep them centered without explicitly setting the width on the container? <div id="beforeafter" class ...

Build Folder not being generated in Next Js version 13.5.6

I'm running into an issue where I am unable to generate the build folder in Next.js 13.5 using npm run build. Additionally, I need to upload this dynamic Next.js project to GoDaddy cpanel. Can anyone help? Thanks! ...

What is the most effective method for incorporating personalized React components in the midst of strings or paragraph tags

Summary: Exploring the frontend world for the first time. Attempting to integrate custom components within p-tags for a website, but facing challenges in making them dynamically changeable based on user interaction. Greetings all! As a newbie in front-end ...

Place the span element above the div container

I recently created some social media buttons with share counters, but I'm facing an issue where the counter data appears inside the buttons rather than on top of them. Has anyone encountered this problem before? Is there a way to display the numbe ...

How to dynamically change the content of the <header> in Nextjs depending on the loaded component

I recently finished creating a app/components/Header.js component. export function Header() { return <> <header className="w-full h-14 grid grid-cols-12 bg-gray-50 text-black dark:bg-gray-900 dark:text-white"> <div ...

The React component using createPortal and having its state managed by its parent will remain static and not refresh upon state modifications

I am facing a problem that can be seen in this live example: https://stackblitz.com/edit/stackblitz-starters-qcvjsz?file=src%2FApp.tsx The issue arises when I pass a list of options to a radio button list, along with the state and setter to a child compon ...

Using TypeScript 4.1, React, and Material-UI, the className attribute does not support the CSSProperties type

Just starting out with Material-UI and we're utilizing the withStyles feature to style our components. Following the guidelines laid out here, I successfully created a classes object with the appropriate types. const classes = createStyles({ main ...

What are the steps to create a downpour in every location on Earth

Is there a way to create a continuous raining effect for my weather app using CSS only? I have achieved a satisfactory look, but the rain effects seem to only cover random chunks of the screen rather than the entire screen. How can I make it cover the enti ...

Issue with remounting in Nextjs 13

import { useRouter, useSearchParams, usePathname } from 'next/navigation'; export function useQueryParams() { const pathname = usePathname(); const router = useRouter(); const searchParams = useSearchParams()!; const updateQu ...

Ensure the width of an HTML table by using only the <td witdth> tag

How can I set a fixed width for each column in my HTML table without using the width property in CSS? The current code is not displaying the table properly, with it only rendering at 100% width of the screen. Here's a snippet of the relevant code: ...

Generate a visually dynamic representation of a live website page

I'm curious if it's possible to create a login page similar to the one shown in this image, using HTML, CSS, and Javascript. Instead of a traditional background image, I want the background to display the actual layout of another website, such a ...

Is it possible to emphasize a duration of 25 minutes on an analog clock by utilizing CSS gradients?

I am in the process of incorporating a countdown timer but with an analog clock rather than a digital one. My objective is to emphasize the next 25 minutes as a circle sector that commences from the minute hand on the clock. Here is what I have developed ...

What could be the reason for scrapy not returning any URLs?

Recently, I attempted to develop a tool to simplify my apartment search and access relevant information quickly (the website is not very user-friendly). However, I have encountered an issue and I may be overlooking something obvious...or perhaps I'm j ...

What is the best way to incorporate the chosen value into an array?

I'm currently working on implementing an Autocomplete feature and need some help with it: var arr=[]; const changeHandler = (value) => { // arr.push(value); // console.log("arr",arr) setItem(arr) } ...

Moxios consistently providing empty responses in tandem with thunk and axios interactions

Having trouble with mocking my axios function, it keeps returning empty data: LoginAction: export function loginUser(email, password) { return function(dispatch) { return axios .post(`${API_URL}/users/authenticate`, { email, password }, { wit ...

Leveraging ajax for showcasing page content in a floating div container

I am in need of creating a button on my page that, when clicked, will display a floating div above the page and load another internal page. I believe this can be achieved using Ajax, but I have no experience with it and don't know where to start. Her ...

Why can I only click on the text of my button?

Looking for a quick fix here! I've been trying to make the entire button clickable, but so far, only the text is hyperlinked. Any suggestions on how to enclose the whole button with the link? As you can see, I'm working on making it mobile-friend ...

"Tricky HTML table layout challenge: Margins, paddings, and borders causing

Working with HTML <table cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <img src="..."> </td> </tr> </tbody> </table> Applying CSS rules * { border: none; ...

Ways to identify when a person has scrolled a specific distance

Is it possible to modify the appearance of my top navigation bar once the page has been scrolled a specific number of pixels? I assume this requires some sort of listener to track the amount of scrolling. I came across element.scrollTop, but it doesn' ...

Font malfunctioning on Microsoft Edge and Internet Explorer

Apologies if this seems like a silly question, but I recently imported a font from my computer into a CSS file. While it displays correctly in Chrome, it's not working in Microsoft Edge or Internet Explorer. Unfortunately, I don't have access to ...