Why use two different rules for the same selector? [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 1 year ago.
Improve this question
I was looking around at some examples and noticed that there were two CSS rules with the same selector, doing two different things. Is there a reason they couldn't have been merged?
Example:
.example {
padding: 0.5em;
text-align: center;
}
.example {
background: #ffffff;
}
What's the difference from this?
.example {
padding: 0.5em;
text-align: center;
background: #ffffff;
}

There is no reason at all. It is personal preference to keep them on seperate or keep them together. You can choose to either seperate them or keep them together. No matter which you choose, the end result remains the same.

The reason is a mistake.
If the class has been defined earlier, it should contain all properties here. Duplicating a class is not good practice. It seems to be even a mistake.

Related

How to assign width to flexbox items in SCSS [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 months ago.
Improve this question
.service-acc {
display: flex;
gap: 10px;
width: 100%;
&-activity {
width: 70%;
}
&-password {
width: 30%;
}
}
In the above SCSS code I am trying to assign the width for flex items by dividing them 70% and 30%, because I want the first item is bigger than the second.
The above SCSS works fine but I want to know whether this approach is correct or some other solution(or CSS properties) to do better than this. Thanks.
This is not really a great solution. How are you going to do this on mobile?
Here is some documentation to get you along:
Controlling Ratios of Flex Items Along the Main Axis

CSS everything selector problem with multiple :not() [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 2 years ago.
Improve this question
there is a problem that I have had since I started using CSS everything selector [*] with multiple [:not()].
Examples below does not work as I tried:
.post-body *:not(.has-color):not(.has-bg){
color: red
}
.post-body *:not(.has-color)*:not(.has-bg){
color: red
}
.post-body *:not(.has-color .has-bg){
color: red
}
.post-body *:not(.has-color , .has-bg){
color: red
}
Imagine something like WordPress post content; I can not change the content whole structure but I do need to set a primary color for texts which do not have a specific background or text color. So I am trying to set Red Color to any element except elements that contain ".has-color" or ".has-bg" that is it there is no relation between them.
Has somebody solved this issue or even seemed to something like this?
Your first example should work, as shown in this CodePen, but as Louys notes, it’s hard to tell without any markup.
.post-body *:not(.has-color):not(.has-bg) {
color: red;
}

Dealing with "vw" units in CSS [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 have a margin variable in SASS like:
$space-xxl: 8vw;
I'm using it with elements like
.element {
margin-bottom: $space-xxl;
}
This works fine because the margin depends on the screen width. I need a fallback for older browsers, is this the best way to do it?
$space-xxl: 8em;
$space-xxl: 8vw;
No, I think the fallback needs to be in the element itself, because SASS variables will not be read by the browser after you compile them.
.element {
margin-bottom: 8em;
margin-bottom: $space-xxl;
}

Way of Thinking CSS classes [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 8 years ago.
Improve this question
I'm thinking about css refacto in my job, and i'm wondering if it's a good idea (considering best practices) to create css class with only one property.
A simple example, is it usefull to create many classes this way
.center-text {
text-align: center;
}
What's the best between doing this or using small libs like Knacss (if you know it) for example.
BIGGEST PROBLEM WITH CSS CLASSES: THEIR LOCATION INSIDE YOUR FILE / CODE MATTERS!!
lets assume we have this html element:
<div class="test altr">some text</div>
this css file:
.test
{
color: red;
}
.altr
{
color: blue;
}
will result in a blue text (the div has those 2 classes). BUT this file will result with a red color:
.altr
{
color: blue;
}
.test
{
color: red;
}
the order of command in css is determine by the css file (and not the order inside the html class attribute)
not to mention that the physical order between and tags inside your html alo affects the order of commands in css (last command override all previous commands)
so.. whatever you do - please be careful with that
One minor drawback I see is the amount of text in your HTML will increase slightly due to pile up of classes. Not best SEO practices, but it's minor.

CSS class naming rules [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 9 years ago.
Improve this question
I have 2 controls which must have 100% width. So I have a question what is better:
1) declare CSS style name for each control (CSS style will contain only width: 100% declaration)
2) declare only one CSS style name which will be applied to all widgets requiring 100% width?
I mean:
.myFirstWidget {
width: 100%;
}
.mySecondWidget {
width: 100%;
}
vs
.maxWidthWidget { width: 100%; }
Use .maxWidthWidget it will be easier to update a site if you stick to these naming conventions. Only separate if they ever need to be different.
Maybe like this?
.myFirstWidget,.mySecondWidget {
width: 100%;
}
I'd go for the one-class fits all. It's more descriptive as a name, and it's easier to maintain, and uses the principle of reusability. :-)
A better solution is:
.myFirstWidget, .mySecondWidget {
width: 100%;
/* Other shared properties... */
}
.myFirstWidget {
/* Properties specific to this widget... */
}
.mySecondWidget {
/* Properties specific to this widget... */
}

Resources