confusing css selector syntax ".tablediv h3 {}" - css-selectors

I'm learning a web design course on Coursera and this snippet of code confuses me:
.tablediv h3 {
text-align: center;
}
I understand simple selector h3 {} or class selector .tablediv {} but what's the syntax of .tablediv h3 here? Thanks~

Related

How to apply custom styling on react-datepicker with css modules?

I am trying to apply custom styling on react-datepicker datepicker input. Using modules:
.custom-input {
border-radius: 10px !important;
padding: 5px 10px 5px 30px !important;
border: 2px solid #cccccc !important;
font-size: 16px !important;
line-height: 26px !important;
}
And then I add the className to the datepicker:
<DatePicker
className={s["custom-input"]}/>
I created codesandbox here:
https://codesandbox.io/s/clnd-datepicker-forked-h9ju59
Any idea why its not working, even with !important on?
[EDIT]
Codesandbox is fixed.
Hello Alex : I took the code to my local vs code and updated the code a little bit which worked:
The only difference I code see here is instead of using s[""] you can use like :
import styles or <any name you would like to call it> from "./Datepicker.module.css"
and then call the css class name in the code like:
className ={styles.<cssClassName>}.
See my code ss:
PS: I also found one strange thing about codesandbox is that it keeps complaining about .module.css file in typescript code file. hence my codesandbox that I sent earlier wasn't working well.

Configure how comment hotkey works in vs code with style jsx

I'm having issues when I try to ctrl + / to comment my style jsx code inside VS Code editor.
I found this question that relates, but never had a good answer. How to comment styles inside <style jsx> tag in NextJS
The issue:
<style jsx>{`
.myDashboard {
font-size: 13px;
line-height: 28px;
}
`}</style>
In VS Code, when i use ctrl + / it tries to do a React JSX style comment
<style jsx>{`
.myDashboard {
{/* font-size: 13px; */}
line-height: 28px;
}
`}</style>
Resulting in this error
Is there an extension or setting I need to make VS Code play nice in my style jsx?
Thanks for your help!

Question about mixing styled-components with material-ui

Hello I am a react newbie and new to styling as well :)
I am trying to style the material-ui Button component with styled components
I am doing this by overriding global class names, I know material-ui introduced global class names like MuiButton-root etc
I am not clear on the use of "&" in parent selector, for example:
const StyledButton = styled(Button)`
&.MuiButton-root {
width: 500px;
}
.MuiButton-label {
color: ${props => props.labelColor};
justify-content: center;
}
`;
The above code works and can achieve the following:
The Button has a width of 500px
The label has a color of red (labelColor is passed in as a prop)
Please see sandbox below for full code
Question:
Why do I need "&" selector for the MuiButton-root, whereas for the MuiButton-label I don't?
Also is this the best way to style material-ui with styled components?
Please see the following sandbox: https://codesandbox.io/embed/practical-field-sfxcu
The '&' selector is used to target the classes and neighbouring elements/classes. Take a look at cssinjs. Its the underlying system behind MUI's styling.
But in short to answer your question;
const StyledButton = styled(Button)`
&.MuiButton-root { //this targets any class that is added to the main component [1]
width: 500px;
}
.MuiButton-label { //in css-in-js this is effectively '& .MuiButton-label [2]
color: ${props => props.labelColor};
justify-content: center;
}
[1] Targets main classes on component
<StyledButton className='test bob'> << this element is targeted
</StyledButton>
[2] Targets child elements either through class or element type. Note the space between & and the actual class.
<StyledButton className='test bob'>
<span className='MuiButton-label'>test</span> << this element is targeted instead
</StyledButton>
You can also use the element tag directly
span {
color: ${props => props.labelColor};
justify-content: center;
}

Wagatil multi styled paragraph

What is the preferred way to achieve a multi style text paragraph (without a fixed structure)?
I was thinking about extending the RichTextField with a b and c classes?
p {
font-size: 20px;
}
.a {
font-size: 200%;
color: red;
font-wight: bold;
}
.b {
font-size: 80%;
color: blue;
}
.c {
font-size: 140%;
color: green
}
<p>
<span class="a">Multi</span>style
<span class="b">Site</span>
<span class="c">claim</span>
are terrible, but necessary.
</p>
I believe you are looking for a StructBlock. However, I would make sure to ask: what are you using these different colored text fields for? Wagtail might already have a predefined field for your use case, such as a BlockQuoteBlock. If not, you can make your own block.
In your app model, you would add a block class that looks something like this:
class ParagraphBlock(blocks.StructBlock):
red_paragraph = blocks.TextBlock(required=False)
blue_paragraph = blocks.TextBlock(required=False)
green_paragraph = blocks.TextBlock(required=False)
class Meta:
icon = 'text'
# if desired, you can add a block template
# template = 'paragraph_block.html'
If you need more customization, you can also create a custom template for the block.
See also the answer to this question, which is similar to what you are looking for.

How to use material-ui (alpha) with styeld-components properly?

I've been trying to use styled-components with the alpha version of material-ui
According to the documentation, this should work out of the box.
This code:
const StyledButton = styled(Button)`
color: red;
text-transform: uppercase;
`;
return <StyledButton>Button</StyledButton>;
will generate something like this:
<button tabindex="0" class="MuiButtonBase-root-3177716317 sc-bdVaJa sxRGN" type="button" role="button">
...
</button>
It looks good.
However, the only problem I have is the order of the injected CSS styles (pic). Styles from styled-components are injected before MUI's styles which make their priority lower.
Is there any way to solve this without using !important?
In the current release (i.e. non-alpha) version, what you've asked would indeed require !important basis:
"Note that CSS properties defined inline are given priority over those defined in a CSS class. You need to use !important to take precedence over the inline style."
Ref: http://www.material-ui.com/#/customization/styles
Perhaps the alpha hasn't quite moved away from this inline requirement yet or it is still a work-in-progress.
What I've done to overcome this sort of thing myself is to (unfortunately) recreate the entire CSS on a standard <button> element when I need such a solution. Here's an example of how I'm doing that with a react-photonkit "theme"
// #flow
import styled from 'styled-components';
const PhotonStyledButton = styled.button`
font-family: Arial, Roboto, Helvetica, sans-serif;
height: 30px;
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 12px !important;
line-height: 1.4;
text-align: center;
white-space: nowrap;
vertical-align: middle;
cursor: default;
background-image: none;
border: 1px solid transparent;
border-radius: $default-border-radius;
box-shadow: 0 1px 1px rgba(0,0,0,.06);
-webkit-app-region: no-drag;
&:focus {
outline: none;
box-shadow: none;
}
color: #333;
border-top-color: #c2c0c2;
border-right-color: #c2c0c2;
border-bottom-color: #a19fa1;
border-left-color: #c2c0c2;
background-color: #fcfcfc;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#fcfcfc), color-stop(100%,#f1f1f1));
background-image: -webkit-linear-gradient(top, #fcfcfc 0%, #f1f1f1 100%);
background-image: linear-gradient(to bottom, #fcfcfc 0%, #f1f1f1 100%);
&:active {
background-color: #ddd;
background-image: none;
}
`;
export default PhotonStyledButton;
styled-components in general is compatible with any component library. When you write styled(AnotherComponent) we take that component and inject an automatically generated class name. This means essentially it's the same thing as writing <AnotherComponent className="sc-asdf123" />!
The current version of material-ui specifically is a bit difficult to custom style because it uses inline styles. From the MaterialUI documentation:
Note that CSS properties defined inline are given priority over those defined in a CSS class. You need to use !important to take precedence over the inline style.
This means simply using styled(MaterialButton) won't work as the passed-in styles will mostly just be ignored. You need to bump the specificity of your styles to override the inline styles that material-ui ships with. (this article is a great primer on specificity if you're not familiar with the details)
Answer for the alpha version of material-ui
The current alpha version of material-ui has switched to using JSS under the hood. (which is CSS in JS not inline styles, like styled-components) This means the issue is likely to be that the styled-components styles are injected after the default material-ui styles. (which are injected by JSS)
JSS supports custom injection points so you might be able to add a <!-- jss --> comment to the HEAD of your HTML to make sure JSS injects its CSS before the styled-components injected CSS?
Answer for the current version of material-ui
There are two ways to bump the specificity of the styled-components injected styles, one more tedious and one a bit more "hacky". The first one is adding !important at the end of all of your styles:
const Button = styled(MaterialButton)`
color: blue!important;
`
While this works in most cases it gets tedious very fast when you have lots of custom styling in a component. The better way is to use the class name hack:
const Button = styled(MaterialButton)`
&&& {
color: blue;
}
`
These ampersands get replaced with the automatically generated class name meaning the outputted CSS looks something like this:
.sc-asdf123.sc-asdf123.sc-asdf123 {
color: blue;
}
This bumps specificity by a big margin, thusly overriding the defined inline styles, and is less annoying than having to put !important after each rule.
Now we can use <!-- material-ui --> to make sure the styles are injected after it.
By default, Material-UI will look for a html comment named to inject styles after. By adjusting the placement of this comment within your HTML body you can control the order that CSS rules are applied to your components. (ref)

Resources