1622879640
The goal isn't to just write CSS that works. Code CSS that is efficient and easy to maintain
Here are some common mistakes that most web developers make, and how identifying and avoiding them can help you write better and more efficient CSS!
1. Using Color Names Instead of Hexadecimal
When you say color: blue; you're essentially telling the computer to display whatever shade of color it thinks blue is. By doing this, you’re giving the browser control over how your web page should be displayed, and as a developer, this is something you should never do. By vaguely naming the color as blue, it can easily differ from the shade of blue that you had in mind, and worse it can also vary from browser to browser.
Using hexadecimal values eg. color: #4169E1; hence becomes something that all developers should adopt. It ensures specificity, is supported by all browsers, and gives back to you the control to decide exactly how you want your web page to be displayed.
Note: An efficient way for finding and using hexadecimal values is by first putting in the name of the closest color to the desired shade, and then inspecting the element to find the hex value using the color dropper.
2. Hard Coding px Instead of Relative Units
While it sometimes becomes imperative to use absolute px values, you should always use relative measurements such as em, % (percent), rem (root-Em), and others whenever possible.
This ensures that the website scales proportionally according to the user’s choice of zoom level and screen/browser size.
So, instead of declaring the sizes in absolutes,
p {
font-size: 16px;
line-height: 20px;
margin-bottom: 8px;
}
do this instead:
p {
font-size: 1rem;
line-height: 1.25em;
margin-bottom: 0.5rem;
}
3. Not Using Font Fallbacks
No matter how beautiful a particular font makes your page look, or how much it catches the eye, you always have to consider that not all font types are supported by all computers. If you’re using a font that some browsers do not support, it means that your web page might not be as beautiful and eye-catching as you’re designing it to be for all users.
So, after you use your favorite font, say Helvetica, always make sure to list fallback fonts that the browser can use in case it isn't supported.
Instead of writing this,
#selector {
font-family: Helvetica;
}
expand the code by font fallbacks such as:
#selector {
font-family: Helvetica, Arial, sans-serif;
}
Now, even if the browser doesn't support Helvetica, it would fall back to the second most preferred option Arial before going to the browser default.
4. Not Using CSS Shorthands
Take a look at the CSS below:
font-family: Helvetica;
font-size: 14px;
font-weight: bold;
line-height: 1.5;
This can easily be condensed into a single line using the CSS shorthand for font:
font: bold 14px/1.5 Helvetica;
Similarly, this list of properties for a background image:
background-image: url(background.png);
background-repeat: repeat-y;
background-position: center top;
can be written as:
background: url(background.png) repeat-y center top;
Why are we doing this? The reason is simple. It not only reduces your CSS file size but also makes the stylesheet easier to read and more organized.
Here’s a list of CSS shorthands to help you write cleaner code. It’s all about ingraining it into your coding habits, but once you do it, there's no going back!
5. Over Qualifying Selectors
Just like every other thing in excess, too much specificity is a bad thing. And more often than not, it is not even necessary. Take a look at the CSS below:
header #nav ul li a {...}
First of all, the header specification is absolutely unnecessary since an ID, having the highest specificity, has already been used (IDs are unique and only associated with one element). Moreover, an unordered list (ul) always has list items (li) within. So having to mention that becomes pointless. The same selector can now be written as:
#nav ul a {...}
With CSS there are only two levels of specificity — specific and not specific enough. Including all those extra elements might make it look ‘safe’ but are actually unnecessary and are only adding to the length of your stylesheet.
As a general rule, your selectors should be as short as possible. Be just as specific as is necessary for it to work.
6. Using ID’s instead of Classes
The most cogent argument against using ID’s is that it has a much higher specificity than classes, which is why it becomes hard to overwrite and extend your styles. A class on its own can’t overwrite styles belonging to an ID. To “beat” the ID, you would need either more IDs or to use !important, which can begin specificity wars in your stylesheets.
Class selectors can also be used for several HTML elements on the same page, unlike IDs which are unique to each element. Being able to reuse styles is one of the advantages of CSS.
To maintain a consistent convention, use only the class attributes to define styles and IDs while integrating interactivity with Javascript instead.
7. Not Using CSS Reset
If you have ever displayed an HTML page with no CSS styling, you know that the web browser itself “styles” the page using some default values as a fallback. The text has a particular font size and style, the margin and padding are set to certain values.
While this is a good thing for someone who does not use CSS, it is important to first reset these values when you put your own styles into the page. These values vary from browser to browser, hence a CSS Reset is the only way to ensure that all your styles are uniform and effective.
This entails resetting all the styles of all the HTML elements to a predictable baseline value. Once you do this, you can style all the elements on your page as if they were the same to start with. A blank slate.
An easier but incomplete way to do this is by resetting the margin and padding using a universal selector:
* {margin:0; padding:0;}
For a complete reset, however, you can use the Eric Meyer reset (modifying it as per your choice), to reset borders, underlines, and colors of elements like list items, links, and tables so that you don’t run into unexpected inconsistencies between web browsers.
8. Repetitive Code (Redundant Selectors and Properties)
In general, repeating yourself while coding is not considered a good practice. CSS is no different. Take a look at the code below:
#selector-1 {
font-style: italic;
color: #e7e7e7;
margin: 5px;
padding: 20px
}
.selector-2 {
font-style: italic;
color: #e7e7e7;
margin: 5px;
padding: 20px
}
A better way to write this is by combining them, with the selectors separated by a comma (,):
#selector-1, .selector-2 {
font-style: italic;
color: #e7e7e7;
margin: 5px;
padding: 20px
}
This is not just more efficient, but also reduces maintenance time and page-load speed.
9. Not Separating Design from Layout
The job of CSS is to provide styling, and the job of HTML is to provide structure. Generally, HTML should be written in a way that captures the information hierarchy of the page, ignoring any design concerns. Afterward, CSS can be added to make things ‘look nice.’
However, while HTML provides structure, it cannot always position elements on the exact spot of a page you want it to appear, which is where we use CSS to scaffold the layout of the page. Once an element is put into the right place on the page, it’s easy to style it without worrying about the display and position. This is why Layout CSS should be separated from Design CSS.
Instead of putting the layout as well as design properties together,
.article {
display: inline-block;
width: 50%;
margin-bottom: 1em;
font-family: sans-serif;
border-radius: 1rem;
box-shadow: 12px 12px 2px 1px rgba(0, 0, 0, .2);
}
.sidebar {
width: 25%;
margin-left: 5px;
}
<div class="article"></div>
<div class="article sidebar"></div>
Separate the design and layout of elements:
/* layout */
.article, .sidebar {
display: inline-block;
}
.article {
width: 50%;
margin-bottom: 1em;
}
.sidebar {
width: 25%;
margin-left: 5px;
}
/* display */
.card {
font-family: sans-serif;
border-radius: 1rem;
box-shadow: 12px 12px 2px 1px rgba(0, 0, 0, .2);
}
<div class="article card"></div>
<div class="sidebar card"></div>
This ensures separation of concerns, which is a common software engineering principle that helps keep our code maintainable and easy to understand.
10. Writing Unorganized CSS
Instead of writing your styles just as you think of them, do yourself a favor and organize your code neatly. This will ensure that next time you come to make a change to your file, you’re still able to navigate it.
#css #programming #developer
1625635172
AppClues Infotech is a well known web application development company. We help you to redefine your technological environment in this constantly changing digital age and competitive scenario. We specialize in developing customer centric web applications and provide you with enhanced web app development services as well as modern app marketing services which are focused to drive effectiveness and efficiency to your business.
We have team of creative website developers, designers, testers and quality engineers help businesses to operate their day-to-day activities actively. We serve superb app design & development services with great ease and awareness. With our responsive website design services, we provide user-friendly experience across all platforms (desktop, tablet, mobile).
Want A Beautiful Web App? We build 100% Responsive, Mobile Friendly, Professionally designed websites Loved By Clients & Google.
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#top responsive web app development company in usa #top web development companies in united states #best web development company usa #web application development company usa & india #web app development company in new york #top web app development company
1621833831
AppClues Infotech is the best company for Web app development services in USA. We offer end-to-end web development solutions by deliberately consolidating most recent web technologies with mature project development methodologies & robust project management tools. We persistently include our clients to ensure the final product will surpass goals from both a business and user experience point of view.
If you are interested in any of these professional web development services for your new or existing business, request a free web development quote or contact our web development company for additional information on these services.
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#top app developers in united states #custom web application development company usa #web application development company in usa #top web app development company in usa #web application development services in usa #professional web development services
1596530868
Want to develop a website or re-design using CSS Development?
We build a website and we implemented CSS successfully if you are planning to Hire CSS Developer from HourlyDeveloper.io, We can fill your Page with creative colors and attractive Designs. We provide services in Web Designing, Website Redesigning and etc.
For more details…!!
Consult with our experts:- https://bit.ly/3hUdppS
#hire css developer #css development company #css development services #css development #css developer #css
1606198822
An extensively researched list of top Web developers in Australia with ratings & reviews to help find the best Web development companies across Australia.
#web development service providers in australia #top web development companies in australia #best website developers in australia #australian web developers to hire #top web developers in australia
1617354704
AppClues Infotech is one of the leading progressive web app development company in USA. We offer the best progressive web app development & design solution to create high-performance & secure PWA.
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#top progressive web app development company in usa #hire progressive web app developers in usa #best pwa development company in usa #custom progressive web app development company #progressive web apps development #progressive web app development services