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

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

Related

RWD, using ONLY CSS + "attribute content" is possible write TAGS? [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 1 year ago.
Improve this question
it is totally different to Line break (like <br>) using only css
I need:
/* in window FULL */
.contentbreak{
content:'';
}
/* in window PHONE */
.contentbreak{
content:'<br />';
}
but maybe I also need usedOTHERS tags.
is possible without use JavaScript ?
You can simply use
<br class="mobile">
with according css styles:
#media screen and (min-width: XYZpx) {
/*XYZ: your breakpoint; every value bigger than the one here would hide line break*/
.mobile {
display: none;
}
}
Another possibility (pure CSS):
#media screen and (min-width: XYZpx) {
#content::after{
/* #content is the line before line break */
content: "\a";
white-space: pre;
}

CSS variables – :root not working in media query [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I can't figure out why this media query is not working. The media query is always referring to the first instance of the :root variables instead of the :root variables inside the media query.
// Outside media query
:root {
--default: 10px;
}
DIV {
width: var(--default);
}
//Inside media query
#media screen and (max-width: 55rem) {
:root {
--default: 500px;
}
DIV {
width: var(--default);
}
}
This always results in the h1 tag being 10px wide instead of 500px wide, even if the media query. (Testing in Chrome and iOS Safari.)
Any tips?
I'm following this guide: https://www.freecodecamp.org/news/how-to-make-responsiveness-super-simple-with-css-variables-8c90ebf80d7f/
DEMO
const log = () => console.log(getComputedStyle(document.documentElement).getPropertyValue('--default'));
log();
window.addEventListener('resize', log);
:root {
--default: 10px;
}
h1 {
width: var(--default);
}
#media screen and (max-width: 55rem) {
:root {
--default: 500px;
}
h1 {
width: var(--default);
}
}
<h1>HEADING</h1>

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.

Shorter declaration for inputs in 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
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);

Resources