How can I imitate the look of the outline and label from Material-UI's outlined textfield?2019 Community Moderator ElectionMy dropdown menu doesn't take in horizontal and there is a space between the dropdown buttons. Any one?How get data from material-ui TextField, DropDownMenu components?Material-ui textfield with labelReactJS - How can I set a value for textfield from material-ui?Mobile Responsive DivsHow to resize material-ui's tabsReactJS: How to put <input/> inside a Material UI's <TextField/>?Material UI - TextField outlined doesn't render outlineHow can I change the label size of a material ui TextField?Creating a label alongside an input using material-ui's TextField and getInputProps
School performs periodic password audits. Is my password compromised?
Why restrict private health insurance?
Making a kiddush for a girl that has hard time finding shidduch
What do *foreign films* mean for an American?
Is a piano played in the same way as a harmonium?
Possible to detect presence of nuclear bomb?
Why do we say ‘pairwise disjoint’, rather than ‘disjoint’?
Is it possible to find 2014 distinct positive integers whose sum is divisible by each of them?
When a wind turbine does not produce enough electricity how does the power company compensate for the loss?
Street obstacles in New Zealand
How do spaceships determine each other's mass in space?
What is this diamond of every day?
Power Strip for Europe
Why does Solve lock up when trying to solve the quadratic equation with large integers?
Getting the || sign while using Kurier
Is it possible that a question has only two answers?
What will happen if my luggage gets delayed?
Giving a career talk in my old university, how prominently should I tell students my salary?
Recommendation letter by significant other if you worked with them professionally?
What do you call someone who likes to pick fights?
How do we create new idioms and use them in a novel?
What is Tony Stark injecting into himself in Iron Man 3?
Trig Subsitution When There's No Square Root
Proving a statement about real numbers
How can I imitate the look of the outline and label from Material-UI's outlined textfield?
2019 Community Moderator ElectionMy dropdown menu doesn't take in horizontal and there is a space between the dropdown buttons. Any one?How get data from material-ui TextField, DropDownMenu components?Material-ui textfield with labelReactJS - How can I set a value for textfield from material-ui?Mobile Responsive DivsHow to resize material-ui's tabsReactJS: How to put <input/> inside a Material UI's <TextField/>?Material UI - TextField outlined doesn't render outlineHow can I change the label size of a material ui TextField?Creating a label alongside an input using material-ui's TextField and getInputProps
I'm trying to imitate the outlined textfield from Material-UI but I don't know how to hide the border behind the title text.
In the below image, notice how the "Due Date/Time" is taken from the Material-UI library and the title hides the border behind it but when I tried to imitate it with a custom component I just couldn't hide the border.
Alternatively, Is there a better way to use this outline design instead of just implementing it with CSS?
My current component looks liks this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
css reactjs material-ui
add a comment |
I'm trying to imitate the outlined textfield from Material-UI but I don't know how to hide the border behind the title text.
In the below image, notice how the "Due Date/Time" is taken from the Material-UI library and the title hides the border behind it but when I tried to imitate it with a custom component I just couldn't hide the border.
Alternatively, Is there a better way to use this outline design instead of just implementing it with CSS?
My current component looks liks this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
css reactjs material-ui
Set background-color
– evgeni fotia
Mar 6 at 22:19
Have you triedbackground-color: inherit
on the<div>Color</div>
?
– Buzinas
Mar 6 at 22:21
add a comment |
I'm trying to imitate the outlined textfield from Material-UI but I don't know how to hide the border behind the title text.
In the below image, notice how the "Due Date/Time" is taken from the Material-UI library and the title hides the border behind it but when I tried to imitate it with a custom component I just couldn't hide the border.
Alternatively, Is there a better way to use this outline design instead of just implementing it with CSS?
My current component looks liks this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
css reactjs material-ui
I'm trying to imitate the outlined textfield from Material-UI but I don't know how to hide the border behind the title text.
In the below image, notice how the "Due Date/Time" is taken from the Material-UI library and the title hides the border behind it but when I tried to imitate it with a custom component I just couldn't hide the border.
Alternatively, Is there a better way to use this outline design instead of just implementing it with CSS?
My current component looks liks this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
css reactjs material-ui
css reactjs material-ui
edited Mar 7 at 22:04
Ryan Cogswell
5,1111624
5,1111624
asked Mar 6 at 22:09
Slava ZorefSlava Zoref
63114
63114
Set background-color
– evgeni fotia
Mar 6 at 22:19
Have you triedbackground-color: inherit
on the<div>Color</div>
?
– Buzinas
Mar 6 at 22:21
add a comment |
Set background-color
– evgeni fotia
Mar 6 at 22:19
Have you triedbackground-color: inherit
on the<div>Color</div>
?
– Buzinas
Mar 6 at 22:21
Set background-color
– evgeni fotia
Mar 6 at 22:19
Set background-color
– evgeni fotia
Mar 6 at 22:19
Have you tried
background-color: inherit
on the <div>Color</div>
?– Buzinas
Mar 6 at 22:21
Have you tried
background-color: inherit
on the <div>Color</div>
?– Buzinas
Mar 6 at 22:21
add a comment |
3 Answers
3
active
oldest
votes
Just apply the same background color on the color div as the parent's background color, you can do it by background-color: inherit like this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
background-color:'inherit' **(just add this line)**
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
add a comment |
There is a great deal of flexibility in what you can do with TextField
. TextField
supports plugging in different types of inputs (e.g. Select
, input
, custom pickers) via the inputComponent
property. You could leverage this to put anything inside its labelled outline by creating a custom component like this OutlinedDiv
:
import React from "react";
import TextField from "@material-ui/core/TextField";
const OutlinedDiv = ( children, label ) =>
return (
<TextField
variant="outlined"
label=label
InputLabelProps= shrink: true
InputProps=
inputComponent: ( className ) => (
<div className=className>children</div>
)
/>
);
;
export default OutlinedDiv;
The className
passed to the inputComponent
takes care of the CSS that makes this all work. You can then use this like in the following:
import React from "react";
import ReactDOM from "react-dom";
import OutlinedDiv from "./OutlinedDiv";
import Avatar from "@material-ui/core/Avatar";
import deepOrange from "@material-ui/core/colors/deepOrange";
import deepPurple from "@material-ui/core/colors/deepPurple";
import red from "@material-ui/core/colors/red";
import green from "@material-ui/core/colors/green";
import blue from "@material-ui/core/colors/blue";
import Grid from "@material-ui/core/Grid";
function App()
return (
<div className="App">
<OutlinedDiv label="Color Picker">
<Grid container justify="center" alignItems="center">
<Avatar style= backgroundColor: deepOrange[500] />
<Avatar style= backgroundColor: deepPurple[500] />
<Avatar style= backgroundColor: red[500] />
<Avatar style= backgroundColor: green[500] />
<Avatar style= backgroundColor: blue[500] />
</Grid>
</OutlinedDiv>
<br />
<br />
<OutlinedDiv label="Custom Outlined Thing">
You can put whatever you want in here.
</OutlinedDiv>
</div>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
add a comment |
The outlined textfield was really tricky to implement. When pushing that feature, we had to consider several options, each with their own drawbacks
- SVG Element
Easy to build and animate, but tough to scale with the surrounding elements. If we had gone this route, we would have needed to listen for some type of resize event, which would mean either using a window resize event, which is not robust, or using a newer and less supported feature such as a ResizeObserver/MutationObserver. There are polyfills, but that would have increased the bundle size about 2K for a relatively small feature.
The SVG route is likely what will be used in the future. It is also worth noting that this is how Google's Material Components Web solves the problem.
- Plain old border with a background on the label
This is by far the simplest approach, but it is also somewhat inflexible. You can see an example of this in Google's new sign-in flow. There they actually set the background color to white. This is probably a fine approach for plenty of users, but of course won't work if your background is a gradient or some similar edge case. That said, there's no need to worry about resize because it's just a border.
- Fieldset and legend
This is what we ended up going with, largely because of its flexibility for end users. Fieldset and its legend component are both built-in ways of attaining pretty much this exact functionality. The big drawbacks to this are that styling across browsers is tough, and the properties we'd be animating on are not performant, such as legend width. Additionally, it's always best to use semantic HTML of course, which this is not, which means we need to use aria-hidden
to instruct screen readers to ignore the element.
Depending on your goals, any one of these solutions may work best for you. Beware that getting the perfect solution that solves all of these problems may be very tricky!
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032966%2fhow-can-i-imitate-the-look-of-the-outline-and-label-from-material-uis-outlined%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just apply the same background color on the color div as the parent's background color, you can do it by background-color: inherit like this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
background-color:'inherit' **(just add this line)**
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
add a comment |
Just apply the same background color on the color div as the parent's background color, you can do it by background-color: inherit like this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
background-color:'inherit' **(just add this line)**
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
add a comment |
Just apply the same background color on the color div as the parent's background color, you can do it by background-color: inherit like this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
background-color:'inherit' **(just add this line)**
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
Just apply the same background color on the color div as the parent's background color, you can do it by background-color: inherit like this:
<div style=inputContainerStyle>
<div style=
...titleStyle,
transform: 'translate(-43px, -11px) scale(0.75)',
fontSize: '17px',
color: 'rgba(0, 0, 0, 0.54)',
position: 'absolute',
background-color:'inherit' **(just add this line)**
>
Color
</div>
<div
className="flex-row"
style=
border: '1px solid rgba(0, 0, 0, 0.23)',
padding: '18.5px 14px',
borderRadius: '4px',
>
availableColors.map(color => <div style=colorCircleStyle(color) />)
</div>
</div>
answered Mar 7 at 5:12
Awab IjazAwab Ijaz
113
113
add a comment |
add a comment |
There is a great deal of flexibility in what you can do with TextField
. TextField
supports plugging in different types of inputs (e.g. Select
, input
, custom pickers) via the inputComponent
property. You could leverage this to put anything inside its labelled outline by creating a custom component like this OutlinedDiv
:
import React from "react";
import TextField from "@material-ui/core/TextField";
const OutlinedDiv = ( children, label ) =>
return (
<TextField
variant="outlined"
label=label
InputLabelProps= shrink: true
InputProps=
inputComponent: ( className ) => (
<div className=className>children</div>
)
/>
);
;
export default OutlinedDiv;
The className
passed to the inputComponent
takes care of the CSS that makes this all work. You can then use this like in the following:
import React from "react";
import ReactDOM from "react-dom";
import OutlinedDiv from "./OutlinedDiv";
import Avatar from "@material-ui/core/Avatar";
import deepOrange from "@material-ui/core/colors/deepOrange";
import deepPurple from "@material-ui/core/colors/deepPurple";
import red from "@material-ui/core/colors/red";
import green from "@material-ui/core/colors/green";
import blue from "@material-ui/core/colors/blue";
import Grid from "@material-ui/core/Grid";
function App()
return (
<div className="App">
<OutlinedDiv label="Color Picker">
<Grid container justify="center" alignItems="center">
<Avatar style= backgroundColor: deepOrange[500] />
<Avatar style= backgroundColor: deepPurple[500] />
<Avatar style= backgroundColor: red[500] />
<Avatar style= backgroundColor: green[500] />
<Avatar style= backgroundColor: blue[500] />
</Grid>
</OutlinedDiv>
<br />
<br />
<OutlinedDiv label="Custom Outlined Thing">
You can put whatever you want in here.
</OutlinedDiv>
</div>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
add a comment |
There is a great deal of flexibility in what you can do with TextField
. TextField
supports plugging in different types of inputs (e.g. Select
, input
, custom pickers) via the inputComponent
property. You could leverage this to put anything inside its labelled outline by creating a custom component like this OutlinedDiv
:
import React from "react";
import TextField from "@material-ui/core/TextField";
const OutlinedDiv = ( children, label ) =>
return (
<TextField
variant="outlined"
label=label
InputLabelProps= shrink: true
InputProps=
inputComponent: ( className ) => (
<div className=className>children</div>
)
/>
);
;
export default OutlinedDiv;
The className
passed to the inputComponent
takes care of the CSS that makes this all work. You can then use this like in the following:
import React from "react";
import ReactDOM from "react-dom";
import OutlinedDiv from "./OutlinedDiv";
import Avatar from "@material-ui/core/Avatar";
import deepOrange from "@material-ui/core/colors/deepOrange";
import deepPurple from "@material-ui/core/colors/deepPurple";
import red from "@material-ui/core/colors/red";
import green from "@material-ui/core/colors/green";
import blue from "@material-ui/core/colors/blue";
import Grid from "@material-ui/core/Grid";
function App()
return (
<div className="App">
<OutlinedDiv label="Color Picker">
<Grid container justify="center" alignItems="center">
<Avatar style= backgroundColor: deepOrange[500] />
<Avatar style= backgroundColor: deepPurple[500] />
<Avatar style= backgroundColor: red[500] />
<Avatar style= backgroundColor: green[500] />
<Avatar style= backgroundColor: blue[500] />
</Grid>
</OutlinedDiv>
<br />
<br />
<OutlinedDiv label="Custom Outlined Thing">
You can put whatever you want in here.
</OutlinedDiv>
</div>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
add a comment |
There is a great deal of flexibility in what you can do with TextField
. TextField
supports plugging in different types of inputs (e.g. Select
, input
, custom pickers) via the inputComponent
property. You could leverage this to put anything inside its labelled outline by creating a custom component like this OutlinedDiv
:
import React from "react";
import TextField from "@material-ui/core/TextField";
const OutlinedDiv = ( children, label ) =>
return (
<TextField
variant="outlined"
label=label
InputLabelProps= shrink: true
InputProps=
inputComponent: ( className ) => (
<div className=className>children</div>
)
/>
);
;
export default OutlinedDiv;
The className
passed to the inputComponent
takes care of the CSS that makes this all work. You can then use this like in the following:
import React from "react";
import ReactDOM from "react-dom";
import OutlinedDiv from "./OutlinedDiv";
import Avatar from "@material-ui/core/Avatar";
import deepOrange from "@material-ui/core/colors/deepOrange";
import deepPurple from "@material-ui/core/colors/deepPurple";
import red from "@material-ui/core/colors/red";
import green from "@material-ui/core/colors/green";
import blue from "@material-ui/core/colors/blue";
import Grid from "@material-ui/core/Grid";
function App()
return (
<div className="App">
<OutlinedDiv label="Color Picker">
<Grid container justify="center" alignItems="center">
<Avatar style= backgroundColor: deepOrange[500] />
<Avatar style= backgroundColor: deepPurple[500] />
<Avatar style= backgroundColor: red[500] />
<Avatar style= backgroundColor: green[500] />
<Avatar style= backgroundColor: blue[500] />
</Grid>
</OutlinedDiv>
<br />
<br />
<OutlinedDiv label="Custom Outlined Thing">
You can put whatever you want in here.
</OutlinedDiv>
</div>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
There is a great deal of flexibility in what you can do with TextField
. TextField
supports plugging in different types of inputs (e.g. Select
, input
, custom pickers) via the inputComponent
property. You could leverage this to put anything inside its labelled outline by creating a custom component like this OutlinedDiv
:
import React from "react";
import TextField from "@material-ui/core/TextField";
const OutlinedDiv = ( children, label ) =>
return (
<TextField
variant="outlined"
label=label
InputLabelProps= shrink: true
InputProps=
inputComponent: ( className ) => (
<div className=className>children</div>
)
/>
);
;
export default OutlinedDiv;
The className
passed to the inputComponent
takes care of the CSS that makes this all work. You can then use this like in the following:
import React from "react";
import ReactDOM from "react-dom";
import OutlinedDiv from "./OutlinedDiv";
import Avatar from "@material-ui/core/Avatar";
import deepOrange from "@material-ui/core/colors/deepOrange";
import deepPurple from "@material-ui/core/colors/deepPurple";
import red from "@material-ui/core/colors/red";
import green from "@material-ui/core/colors/green";
import blue from "@material-ui/core/colors/blue";
import Grid from "@material-ui/core/Grid";
function App()
return (
<div className="App">
<OutlinedDiv label="Color Picker">
<Grid container justify="center" alignItems="center">
<Avatar style= backgroundColor: deepOrange[500] />
<Avatar style= backgroundColor: deepPurple[500] />
<Avatar style= backgroundColor: red[500] />
<Avatar style= backgroundColor: green[500] />
<Avatar style= backgroundColor: blue[500] />
</Grid>
</OutlinedDiv>
<br />
<br />
<OutlinedDiv label="Custom Outlined Thing">
You can put whatever you want in here.
</OutlinedDiv>
</div>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
edited Mar 7 at 12:04
answered Mar 7 at 4:46
Ryan CogswellRyan Cogswell
5,1111624
5,1111624
add a comment |
add a comment |
The outlined textfield was really tricky to implement. When pushing that feature, we had to consider several options, each with their own drawbacks
- SVG Element
Easy to build and animate, but tough to scale with the surrounding elements. If we had gone this route, we would have needed to listen for some type of resize event, which would mean either using a window resize event, which is not robust, or using a newer and less supported feature such as a ResizeObserver/MutationObserver. There are polyfills, but that would have increased the bundle size about 2K for a relatively small feature.
The SVG route is likely what will be used in the future. It is also worth noting that this is how Google's Material Components Web solves the problem.
- Plain old border with a background on the label
This is by far the simplest approach, but it is also somewhat inflexible. You can see an example of this in Google's new sign-in flow. There they actually set the background color to white. This is probably a fine approach for plenty of users, but of course won't work if your background is a gradient or some similar edge case. That said, there's no need to worry about resize because it's just a border.
- Fieldset and legend
This is what we ended up going with, largely because of its flexibility for end users. Fieldset and its legend component are both built-in ways of attaining pretty much this exact functionality. The big drawbacks to this are that styling across browsers is tough, and the properties we'd be animating on are not performant, such as legend width. Additionally, it's always best to use semantic HTML of course, which this is not, which means we need to use aria-hidden
to instruct screen readers to ignore the element.
Depending on your goals, any one of these solutions may work best for you. Beware that getting the perfect solution that solves all of these problems may be very tricky!
New contributor
add a comment |
The outlined textfield was really tricky to implement. When pushing that feature, we had to consider several options, each with their own drawbacks
- SVG Element
Easy to build and animate, but tough to scale with the surrounding elements. If we had gone this route, we would have needed to listen for some type of resize event, which would mean either using a window resize event, which is not robust, or using a newer and less supported feature such as a ResizeObserver/MutationObserver. There are polyfills, but that would have increased the bundle size about 2K for a relatively small feature.
The SVG route is likely what will be used in the future. It is also worth noting that this is how Google's Material Components Web solves the problem.
- Plain old border with a background on the label
This is by far the simplest approach, but it is also somewhat inflexible. You can see an example of this in Google's new sign-in flow. There they actually set the background color to white. This is probably a fine approach for plenty of users, but of course won't work if your background is a gradient or some similar edge case. That said, there's no need to worry about resize because it's just a border.
- Fieldset and legend
This is what we ended up going with, largely because of its flexibility for end users. Fieldset and its legend component are both built-in ways of attaining pretty much this exact functionality. The big drawbacks to this are that styling across browsers is tough, and the properties we'd be animating on are not performant, such as legend width. Additionally, it's always best to use semantic HTML of course, which this is not, which means we need to use aria-hidden
to instruct screen readers to ignore the element.
Depending on your goals, any one of these solutions may work best for you. Beware that getting the perfect solution that solves all of these problems may be very tricky!
New contributor
add a comment |
The outlined textfield was really tricky to implement. When pushing that feature, we had to consider several options, each with their own drawbacks
- SVG Element
Easy to build and animate, but tough to scale with the surrounding elements. If we had gone this route, we would have needed to listen for some type of resize event, which would mean either using a window resize event, which is not robust, or using a newer and less supported feature such as a ResizeObserver/MutationObserver. There are polyfills, but that would have increased the bundle size about 2K for a relatively small feature.
The SVG route is likely what will be used in the future. It is also worth noting that this is how Google's Material Components Web solves the problem.
- Plain old border with a background on the label
This is by far the simplest approach, but it is also somewhat inflexible. You can see an example of this in Google's new sign-in flow. There they actually set the background color to white. This is probably a fine approach for plenty of users, but of course won't work if your background is a gradient or some similar edge case. That said, there's no need to worry about resize because it's just a border.
- Fieldset and legend
This is what we ended up going with, largely because of its flexibility for end users. Fieldset and its legend component are both built-in ways of attaining pretty much this exact functionality. The big drawbacks to this are that styling across browsers is tough, and the properties we'd be animating on are not performant, such as legend width. Additionally, it's always best to use semantic HTML of course, which this is not, which means we need to use aria-hidden
to instruct screen readers to ignore the element.
Depending on your goals, any one of these solutions may work best for you. Beware that getting the perfect solution that solves all of these problems may be very tricky!
New contributor
The outlined textfield was really tricky to implement. When pushing that feature, we had to consider several options, each with their own drawbacks
- SVG Element
Easy to build and animate, but tough to scale with the surrounding elements. If we had gone this route, we would have needed to listen for some type of resize event, which would mean either using a window resize event, which is not robust, or using a newer and less supported feature such as a ResizeObserver/MutationObserver. There are polyfills, but that would have increased the bundle size about 2K for a relatively small feature.
The SVG route is likely what will be used in the future. It is also worth noting that this is how Google's Material Components Web solves the problem.
- Plain old border with a background on the label
This is by far the simplest approach, but it is also somewhat inflexible. You can see an example of this in Google's new sign-in flow. There they actually set the background color to white. This is probably a fine approach for plenty of users, but of course won't work if your background is a gradient or some similar edge case. That said, there's no need to worry about resize because it's just a border.
- Fieldset and legend
This is what we ended up going with, largely because of its flexibility for end users. Fieldset and its legend component are both built-in ways of attaining pretty much this exact functionality. The big drawbacks to this are that styling across browsers is tough, and the properties we'd be animating on are not performant, such as legend width. Additionally, it's always best to use semantic HTML of course, which this is not, which means we need to use aria-hidden
to instruct screen readers to ignore the element.
Depending on your goals, any one of these solutions may work best for you. Beware that getting the perfect solution that solves all of these problems may be very tricky!
New contributor
edited 2 days ago
New contributor
answered 2 days ago
Eric NagyEric Nagy
11
11
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032966%2fhow-can-i-imitate-the-look-of-the-outline-and-label-from-material-uis-outlined%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Set background-color
– evgeni fotia
Mar 6 at 22:19
Have you tried
background-color: inherit
on the<div>Color</div>
?– Buzinas
Mar 6 at 22:21