How to style material-ui input with outlined variant using css

Currently, I am attempting to replicate the material-ui outlined input. The background color and input styles are different, so simply setting the label position to absolute and pushing it up is not working for me. Any suggestions on how to achieve this?

Here is my current CSS implementation: https://i.sstatic.net/fABFa.png

Material Design Example: https://i.sstatic.net/pUqQY.png


  inputStyle: {
    padding: 10,
    borderBottomWidth: 0,
  },
  labelStyle: {
    color: colors.black,
    marginBottom: 5,
    top: props.isFocused ? -6 : 12,
    opacity: props.isFocused ? 1 : 0.8,
    left: 6,
    zIndex: 100,
    position: 'absolute',
    display: 'inline-block',
    fontSize: props.isFocused ? 12 : 16,
    fontWeight: 400,
    backgroundColor: colors.white,
  },
  inputContainerStyle: {
    width: '100%',
    borderColor: handleInputBorder(props.error),
    borderRadius: 5,
    borderWidth: 1,
    backgroundColor: colors.white,
    marginBottom: 10,
  },
  containerStyle: {
    paddingLeft: 0,
    paddingRight: 0,
  },
  rightIcon: props.rightIcon,

Answer №1

After some consideration, I have made the choice to change the background color of the label to match the screen's background and move it slightly higher up on the page. It appears that this adjustment has resolved the problem I was experiencing.

Answer №2

While working with react.js, I have implemented a similar functionality.

https://i.sstatic.net/eLQL3.png https://i.sstatic.net/o8UdH.png https://i.sstatic.net/NJqGm.png

Here is my approach to the solution.

MaterialInput.js

import React, { useEffect, useState } from 'react';
import { allColors } from '../../../shared/styles/index';
import './MaterialInput.scss';
import Eye from '../../../shared/images/eye.svg';
import HiddePassword from '../../../shared/images/hiddePassword.svg';

export default function Login() {
    const [labelColor, setLabelColor] = useState('#9d9994');
    const [inputColor, setInputColor] = useState('#d6d6d6');
    const [showButton, setShowButton] = useState(false);
    const [showPassword, setShowPassword] = useState(false);
    const [inputValue, setInputValue] = useState('');

    useEffect(() => {
        if (inputValue.length && !showButton) return setShowButton(true);
        if (!inputValue.length) setShowButton(false);
    }, [inputValue]);

    const handleInputButton = () => {
        setShowPassword(!showPassword);
    }

    const handleOnFocus = () => {
        setLabelColor('#ff4f00');
        setInputColor('#ff4f00');
    }

    const handleOnBlur = () => {
        setLabelColor(inputValue ? '#696158' : '#9d9994');
        setInputColor('#d6d6d6');
    }

    const handleChange = (event) => {
        setInputValue(event.target.value);
    }

    return (
            <>
               <form style={{ paddingTop: '1em' }}>
                  <div style={{ borderColor: inputColor }} className="group">      
                     <input onChange={handleChange} value={inputValue} onFocus={handleOnFocus} onBlur={handleOnBlur} type={showPassword ? 'text' : 'password'} id="material-input" required/>
                     <label style={{ color: labelColor }} id="material-input-label">Password</label>
                     <button onClick={handleInputButton} style={{ width: '3.3em', outline: 'none',  position: 'relative' }} type="button">
                {showButton && <img src={showPassword ? HiddePassword : Eye} />}
                    </button>
               </div>
           </form>
        </>
        )
      };

MaterialInput.scss

.group { 
  border:2px solid;
  border-radius: 8px;
  height: 48px;
  position: relative;
}

input  {
  font-size:14px;
  padding:10px 10px 10px 5px;
  width:300px;
  border:none;
  height: 44px;
  color: #696158;
  padding-right: 15px;
  padding-left: 15px; 
  border-radius: 8px;
  &:focus {
    outline:none;
  }
}


label                {
  font-size:14px;
  font-weight:normal;
  position:absolute;
  pointer-events:none;
  left:15px;
  top:13px;
  transition:0.2s ease all; 
  -moz-transition:0.2s ease all; 
  -webkit-transition:0.2s ease all;
  background-color: white;
  padding-right: 0.3em;
  padding-left: 0.3em;
  display: flex;
  justify-content: center;
}

input:focus ~ label, input:valid ~ label  {
  top:-7px;
  font-size:12px;
  font-weight: 600;
}

::-webkit-credentials-auto-fill-button {
    visibility: hidden;
    position: absolute;
    right: 0;
}

input:required {
    box-shadow:none;
}

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

Different hover effects for Material-UI [v0.x] RaisedButton

Is there a way to customize the hover styling of a Material-UI RaisedButton? It seems that the design guidelines dictate what happens on hover, but is there a method to modify the button's color and background-color specifically for when it is being h ...

Exploring the unique scrolling attributes of Cypress: `get` method and relative positioning

Whenever Cypress' cy.get method is called, the page automatically scrolls so that the chosen element is displayed at the top of the webpage. This becomes problematic when there is a sticky toolbar with 'position: relative' on top of it, as ...

Is the `visibility: hidden` property not functioning as expected?

I am trying to conceal the script for the photoset until it is fully loaded, but unfortunately the code below does not seem to be effective: JavaScript $('.photoset-grid').photosetGrid({ rel: $('.photoset-grid').attr("data-id"), gutte ...

Change the class of an element only within the div that is directly below it

Trying to achieve: Visit this link In my navigation menu, I have two folders with subpages under them. The CSS for displaying the page titles within the folder is as follows: .main-nav .subnav ul {display:none; transition: all 100ms ease-in-out; } .mai ...

The img-fluid class is not properly adjusting the height of the image

I'm currently working with Bootstrap 4 and facing a challenge with creating a grid of 4 images - 2 on top and 2 at the bottom. I've applied the img-fluid class, but the image resizing is based solely on width, leading to the height being too larg ...

Customizing Material UI: Grouping Tab components within a div container

How can I wrap the children of a Material UI Tabs component in divs? <Tabs value={value} indicatorColor="primary" textColor="primary" onChange={handleChange} > <div> <Tab label="x" /> ...

Using Stack and Drawer Navigations Together in React Native Navigation(v6)

I am looking to merge Stack and Drawer navigations. I have multiple screens and wish to display select screen labels in the drawer tab. <RootNavigatorStack.Navigator> <RootNavigatorStack.Screen name="DrawerTab" component={DrawerNavig ...

I'm having trouble anchoring my images to a specific spot on the webpage in HTML

After creating my divs in css and linking them to my index file, I encountered an issue when zooming out of the page. The images would shift to the left while the background remained centered. I needed help figuring out how to keep an image fixed on the pa ...

I'm trying to understand why this code isn't running properly. Can someone explain why my CSS is not being executed?

For some reason, using button .order{} to select elements inside the button is not applying the CSS codes as expected. The text color remains unchanged. However, when using just `.order` to select the elements, the CSS code works perfectly fine. Can someon ...

The element's height appears to be fluctuating unexpectedly when I attempt to adjust it using percentage values within a narrow range

I'm utilizing React and Bootstrap in this project. Here's an overview of my code: I have an element with height set to 0, in rem. My goal is to make the height of this element increase as I scroll down the page, creating the illusion that it is ...

Tips on adjusting the SVG view box for optimal visibility in a web browser

Recently, I've encountered an issue with the following code snippet: d3.xml("https://crossorigin.me/https://svgshare.com/i/5w9.svg").mimeType("image/svg+xml").get(function(error, xml) { if (error) throw error; document.body.appendChild(xml.docume ...

Adjusting the tab size and space size to their default settings within VSCode

I'm currently facing an issue with my VSCode setup. I am trying to configure the space size to be equal to 1 and tab size to be equal to 2 as the default for every project. However, despite my efforts, it keeps reverting back to spaces: 4 for each new ...

Ways to center items in a row-oriented collection

When dealing with a horizontal list, the issue arises when the first element is larger than the others. This can lead to a layout that looks like this. Is there a way to vertically align the other elements without changing their size? I am aware of manual ...

What are some ways to implement webkit-line-clamp on a website?

I'm trying to shorten my paragraph using the following CSS code: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; However, the issue is that it condenses everything into one line. I actually want the text to display on 3 lines before t ...

Ensuring Smooth Alignment of CSS Divs

I am facing challenges with the compatibility of my CSS code for HTML checkboxes. In certain versions of Internet Explorer and when I insert the live HTML into my PowerPoint presentation, the center div overlaps with the left div causing the entire page to ...

Relocating to the middle of the webpage (Automatically resize for mobile compatibility if feasible)

Apologies for my poor English. Are there any suggestions on how to center this without using margins or other methods? Perhaps there are alternative solutions. https://i.sstatic.net/dXfwL.png Also, is it feasible for the image and video to automatically a ...

Tips for displaying multiple rows horizontallyALLOWING multiple rows to appear horizontally can be achieved by

This is a demonstration of my desired outcome https://i.sstatic.net/UErvL.png However, I am currently only able to show one product per row. I have attempted the following approach, but it is not functioning as expected. <ol> <?php $no = 1; ...

How to align items at the center in material-ui styling

I have a row of cards inside a container that I want to align in the center with equal spacing around them. I am using the material-ui UI library for the layout. Despite adding the justifyContent: center property, the cards are not evenly spaced. This is ...

how do I address the issue of Property 'navigation' not being found in type 'Readonly<{}> &'?

Here are two snippets of code that I am having trouble with: CustomHeader.tsx import { View, StyleSheet, Button } from 'react-native'; import { NavigationScreenProps } from 'react-navigation'; import Icon from 'react-native-vecto ...

Ensure that the navigation menu is consistently displayed properly, without any of its contents extending beyond the

I am experiencing an issue with the navigation menu on my website. Everything looks great in normal view, but when I resize the browser window to a smaller width, the menu ends up overflowing below the main content. I want the navigation menu to always dis ...