CSS class naming rules [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 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... */
}

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 best practice regarding classes and ids [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
New here. I am currently building a personal website with HTML & CSS (following MDN course).
CSS styling can make sheets quite lengthy.
Is it a good practice to style "reusable components" (using classes) and then using "id" to slightly alter such components?
Below code example + visual.
Thanks for the help!
.block { /* use of class for the block component */
background-color: var(--darkest-green);
border: 1px solid black;
margin: 10px 0px;
}
#block_webpages { /* use of ID to adjust needed properties */
background-color: var(--darkest-green);
}
#block_articles { /* use of ID to adjust needed properties */
background-color: var(--light-green)
}
#block_about { /* use of ID to adjust needed properties */
background-color: var(--green);
}
<div class = "block" id = "block_webpages">Introduction of the different web pages</div>
<div class = "block" id = "block_articles">About the website's author</div>
<div class = "block" id = "block_about">Shows the latest published article</div>
rendering on the webpage - very basic element for demo purposes
id's are more powerful selectors than class. It is advisable to use class not id for better programming or coder. (there are many other reasons also)
You can do the same thing with the class itself without giving id.
Good that you are using CSS variable naming property👍🏼

Why use two different rules for the same 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 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.

Why does my CSS code not work on the headings on my site? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want the headings on my site to be responsive. It's not something the theme provides unfortunately,so i'm going to have to use CSS.
I've used the following:
#media (max-width: 365px) {
porto-u-heading {
font-size: 16px;
line-height: 0.5;
margin-bottom: 1em;
}}
#media (max-width: 365px)
{ porto-u-sub-heading {
font-size: 30px;
}
}
Unfortunately, it's not working on my site: https://wordpress-588666-1906106.cloudwaysapps.com/
Can anybody help me figure out what's wrong?
In CSS, use a dot in front of class names for the selectors: .porto-u-heading { ... }

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.

Resources