How to Change The Style of: -Webkit-Autofill

How do I change the style of an element through another element selector with withStyle from materialUI

The short answer is that this isn't possible in CSS (see Is there a CSS parent selector?).

The way to achieve the desired result is to change the parent (e.g. add/remove a "focused" class) via JavaScript in response to focus/blur events. This is the approach Material-UI uses internally to change the styles of input containers (see https://github.com/mui-org/material-ui/blob/v4.9.14/packages/material-ui/src/InputBase/InputBase.js#L414).

Here is a modified version of your sandbox using this approach:

import React from "react";
import ReactDOM from "react-dom";
import { withStyles } from "@material-ui/core/styles";
import classnames from "classnames";

const InputComponent = ({ value, classes }) => {
const [focused, setFocused] = React.useState(false);
return (
<div
className={classnames(classes.inputContainer, {
[classes.focused]: focused
})}
>
<input
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
value={value}
className={classes.input}
/>
</div>
);
};

const styles = {
input: {
fontSize: 14,
padding: 15,
"&:focus": {
backgroundColor: "#F8F8F8"
}
},
inputContainer: {
border: "1px solid black",
backgroundColor: "white",
"&$focused": {
border: "1px solid #006CFF"
}
},
focused: {}
};

const InputWithStyles = withStyles(styles)(InputComponent);

export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<InputWithStyles />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit Change input container based on input focused state

I was expecting to see your example using one of the Material-UI input components. If you are actually using one of those and removed it for the sake of simplicity in your example, then let me know. The way to handle this for Material-UI inputs would be a bit different -- Material-UI is already managing knowledge of the focused state, so it generally wouldn't be necessary to repeat that work. Instead, you would just leverage the class that Material-UI is already adding to the input container.

How to change a style in CSS such that it affects only code in a `section` or a `div`

The div had class .content not the img, so it's .content img

<!DOCTYPE html>
<html>

<head>
<title>Page Title</title>
<style>
img {
max-height: 20px;
}

.content img {
max-height: 40px;
}
</style>
</head>

<body>

<h1>My First Heading</h1>
<img src="https://i.ibb.co/hZj77BZ/lena01.jpg" alt="Lena">
<p>My first paragraph.</p>
<div class=content>
<img src="https://i.ibb.co/hZj77BZ/lena01.jpg" alt="Lena">
<!-- end -->
</div>

</body>

</html>

How to change the style with props

One way to do it is to change the style object to function instead. That function will take a parameter that will be used to define the text color:

import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants'

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

const MyText = ({isLive}) => {
return (
<Text style={styles.text(isLive)}>
Change code in the editor and watch it change on your phone! Save to get a shareable url.
</Text>
)
}
export default function App() {
return (
<View style={styles.container}>
<MyText isLive />
<MyText />
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
text: (isLive) => ({
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
color: isLive ? 'green' : 'black'
}),
});

How to change style properties of an element in JavaScript?

The problem is that the script runs before the DOM is fully constructed and the element that you want to style actually exists.

You need to move your <script> tag to the end of the <body> element:

 <body>
<div id="background" class="content">
<!-- <img src="Temp1.jpg" alt="Greeting card image"> -->
<h1 id="Title" class="text">Sample title text</h1>
<h2 id="Name" class="text">Sample name text</h2>
<h3 id="Message" class="text">Sample message text</h3>
</div>
<script src="script.js"></script>
</body>

How to change style of element in click if its in a for loop in Javascript + Vue?

You can use index or id if you have it in review object:

new Vue({
el: '#demo',
data() {
return {
reviews: [{text: 'aaa'}, {text: 'bb'}, {text: 'ccc'}],
selected: null
}
},
methods: {
onClickDescription(i) {
this.selected = i
}
}
})
.description {
color: green;
}
.active {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<div v-for="(review, i) in reviews" :key="i">
<p class="description" :class="i === selected && 'active'" @click="onClickDescription(i)">{{ review.text }}</p>
</div>
</div>


Related Topics



Leave a reply



Submit