Shorter declaration for inputs in Sass [closed] - css

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Inputs and buttons have a lot of predefined styles, that had been reseted in the first place, for example borders and backrounds. If borders and backgrounds are styled later differenty, is there a way to make the declaration shorter?
.mybutton1,
[role="button"].mybutton1,
input[type="submit"].mybutton1,
input[type="rest"].mybutton1,
input[type="button"].mybutton1,
button.mybutton1 { {
font-size: 10px;
background-color: red;
border: solid;
}
.mybutton2,
[role="button"].mybutton2,
input[type="submit"].mybutton2,
input[type="rest"].mybutton2,
input[type="button"].mybutton2,
button.mybutton2 { {
font-size: 10px;
background-color: blue;
}

Use common style in to input styles and differentiate with your class, check below codes.
[role="button"],
input[type="submit"],
input[type="rest"],
input[type="button"],
button {
font-size: 8px;
}
.mybutton1 {
font-size: 10px;
}
.mybutton2 {
font-size: 14px;
}
Fiddle: https://jsfiddle.net/nikhilvkd/s12643k1/
[Updated]
OR
You can do with sass like below codes
#mixin buttonStyle($class, $fontSize) {
[role="button"].#{$class},
input[type="submit"].#{$class},
input[type="rest"].#{$class},
input[type="button"].#{$class},
button.#{$class} {
font-size: $fontSize;
}
}
#include buttonStyle(mybutton1, 20px);
#include buttonStyle(mybutton2, 40px);
#include buttonStyle(mybutton3, 60px);
Fiddle demo: https://jsfiddle.net/nikhilvkd/s12643k1/1/
Note: First method is more good for less css codes

You can use mixins for that I guess:
#mixin generate_size($class_num, $size) {
.mybutton{$class_num},
[role="button"].mybutton{$class_num},
input[type="submit"].mybutton{$class_num},
input[type="rest"].mybutton{$class_num},
input[type="button"].mybutton{$class_num},
button.mybutton{$class_num}
{ font-size: $size; }
}
#include generate_size("1", 10px);
#include generate_size("2", 15px);

Related

Is it better to organize css by property or by selector [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this question
In process of developing a site, I have stumbled on two ways on how to organize my css (it's actully scss). The first one is by groupinng my selectors/combinators around a property, in this way (as in the example bellow) I belive I am avoiding duplication of properties. (More after the example)
.footer {
background-color: $primary;
& a,
& i,
& h2,
& p {
color: #ffffff;
}
& a,
& p {
#include font(20, 300);
display: block;
margin-bottom: 10px;
}
& h2 {
#include font(24, 700);
margin-bottom: 15px;
text-transform: uppercase;
}
}
In the second way, I'd group my properties inside elements that have the same style (as in the example bellow).
I believe this way makes more sense, since you're referring to each block of properties as styles to elements that look alike. But the first case seems to offer a bit more visual clarity. Are there arguments on which way is better and which one provides better organization and less mind effort in understanding the code?
.footer {
background-color: $primary;
& a,
& p {
#include font(20, 300);
color: #ffffff;
}
& h2 {
#include font(24, 700);
color: #ffffff;
text-transform: uppercase;
}
& i {
color: #ffffff;
}
}
I try to do as minimum as possible to directly set rules directly to html balise as I find it confusing (just my opinion though).
Maybe for some resets of rules to apply on the whole site some times.
So I would go with the first approch, but using classes instead (with meaningfull names):
.footer {
background-color: $primary;
& .text-white {
color: #ffffff;
}
& .text-medium {
#include font(20, 300);
}
& .title {
#include font(24, 700);
text-transform: uppercase;
}
}
This also reduce the CSS code needed as a same balise can have multiple classes.
Maybe some persons would say it force you to edit your HTML to add all the classes needed but I find it easier to maintain that way.

Wrap media query in class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I was wondering if there as a reason why I couldn't wrap a media query I have for the home page with the .home{} class so that it only fires there.
So instead of
#media (max-width:416px){
.home .et_pb_code_0 {
border-right:0 !important;
}
.home .et_pb_code_1 {
border-right:0 !important;
border-left:0 !important;
}
.home .et_pb_code_2 {
border-left:0 !important;
}
}
I would use:
.home {
#media (max-width:416px){
.et_pb_code_0 {
border-right:0 !important;
}
.et_pb_code_1 {
border-right:0 !important;
border-left:0 !important;
}
.et_pb_code_2 {
border-left:0 !important;
}
}
}
Thanks!
You can't do that, it's not the right syntax, may be you could use that with preprocesors like SASS and LESS, but not in pure CSS.

#media queries before or after class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I wanted to know which would be best practice for media queries.
If you were to target a screen size I would generally do something like:
section#about {
background-color: yellow;
color: black;
padding: 5px 20px;
#media (max-width: 600px) {
padding: 0;
}
}
.button-small {
margin-bottom: 12px;
#media (max-width: 600px) {
margin-bottom: 6px;
}
}
Would the following be better:
section#about {
background-color: yellow;
color: black;
padding: 5px 20px;
}
#media (max-width: 600px) {
section#about {
padding: 0;
}
}
.button-small {
margin-bottom: 12px;
}
#media (max-width: 600px) {
.button-small {
margin-bottom: 6px;
}
}
Instead of nesting #media queries inside classes, you would create a standalone #media query and add the class you would need changed?
NB: Sorry all, I'm using a preprocessor (SASS). I'm thinking of ways to organize code legibility.
Media queries can't be nested like this in pure CSS. Only CSS preprocessors (like LESS and Stylus) allow you to do that. The CSS preprocessor itself will take the code you've given in example 1 (which is invalid CSS, but valid in a CSS preprocessor) and convert it into something similar to example 2 (valid CSS).
If you're using a CSS preprocessor then example 1 is probably the best approach if you have a long chain of nested elements, but if you're not using a CSS preprocessor then example 2 is the only one which will give you any results.
Using SASS I prefer the first approach tbh. Also, if I were to write that code I'd most definitely have one file for _buttons.scss and one for the _about-section.scss. Meaning both wouldn't be able to share a media query anyway. I would however recommend defining your media queries as variables you can re-use:
$bp-medium: (min-width: 600px);
And then later:
#media #{$bp-medium} {
// Code here...
}
One reason I prefer the media query inside selector approach is because when you're nested inside SASS you still want to keep your media queries in the same area as the original styling. For example:
.button {
background: blue;
padding: 1rem 2rem;
// Large Buttons
&--large {
padding: 1.5rem 3rem;
#media #{$bp-medium} {
padding: 2rem 4rem;
}
}
}
It would be annoying to have to "leave the scope" just so that the media query isn't inside the class.

Parent styles in a nested class but only using one class name on element - SASS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So, I'm having a look at some naming conventions. I'd like to try and have some class names similar to: .block(red).
So my HTML would look like:
<div class="block(red)"></div>
my compiled css should look like:
.block(red) {
padding: 100px;
height: 100px;
width: 100px;
background: red;
}
So i would like my SASS to look something like:
.block {
padding: 100px;
height: 100px;
width: 100px;
&\(red\) {
background: red;
}
}
Obviously as the (red) part of the class name is only extending the parent class name it won't bring in the styles from .block, I could #extend .block within the (red) class but that will be a little messy over time.
So, is there any way you guys can think of, that allows me to write SASS like my above example while only having to write one class on my element?
First of all, parentheses aren't allowed in CSS class names, they're reserved for certain selectors.
Update: escaping the parentheses using \(\) does seem to be allowed and works.
If you want to do something like this, you could use OOCSS, BEM, SMACSS, ACSS or something similar.
Update:
If you really only want to use one CSS class, you could use a SCSS #mixin like this:
Codepen: http://codepen.io/grrtbrtr/pen/KVeOja
SCSS
#mixin modulify($selection-modifier) {
$self: &;
&\(#{$selection-modifier}\) {
#content;
#extend #{$self};
}
}
.block {
padding: 100px;
height: 100px;
width: 100px;
#include modulify('red') {
background: red;
}
#include modulify('blue') {
background: blue;
}
}
HTML
<div class="block(red)"></div>
<div class="block(blue)"></div>
For me, it keeps the SCSS code clean, and allows you to use only 1 CSS class in your HTML.

Best Practise write CSS with SCSS for website responsive [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
When I want to create a responsive website, say there are two devices with different screen.
What should I create #media screen {} for different file, like code below:
#media screen and (min-width: 676px) {//Some code here}
or write #media screen {} directly for same file?. This example my code:
.home {
.container {
padding: 10px 0 20px 0;
#media screen and (max-width: 480px) {
padding: 2px 0 10px 0
}
}
}
And please include examples as my reference material.
Thanks.
You can try one of the below approaches:
Use one media query for all elements that will be added it the
bottom of your scss file.
Add each media query with it's element, like you did. I don't prefer
that approach because it will make it harder to find/edit your code
when working on large projects.
Example 1
.section--about {
text-align: center;
p {
color: #727272;
}
}
#include mobile {
.section--about {
p {
font-size: 1.5em;
}
}
}
Exmple 2
.section--about {
text-align: center;
p {
color: #727272;
}
#include mobile {
p {
font-size: 1.5em;
}
}
}
Further reading:
Approaches to Media Queries in Sass
Sass Guidelines
Write Better Media Queries with Sass
Media Queries mixins for Sass

Resources