Do I always use .css files?

Are .css files always needed? Or can I have a .css "main" file and define other style elements inside the HTML page?

Is there always padding , borders , etc. should always be defined in a .css file that is stored separately or can I embed it on an HTML page?

+4
source share
13 answers

It is technically possible to use the built-in CSS formatting and not have an external stylesheet. You can also embed a stylesheet in an HTML document. The best practice in web design is to separate CSS into a separate stylesheet. The reason for this is that a CSS stylesheet exists to determine the presentation style of a document. An HTML file exists to define the structure and contents of a document. And perhaps you may have JavaScript files that exist to add additional behavior to the document.

Saving layout markup, layout, and behavior creates a cleaner design.

From a practical point of view, if you have one external CSS stylesheet, the browser can cache it. If several pages on your site have the same appearance, they can use the same external stylesheet, which only needs to be loaded once by a web browser. This will reduce your network bandwidth charges and also create faster end-user experience.

+16
source

You can include CSS inside the HTML page. It is located in the <style> , which is located in the <head> :

 <head> <style type="text/css"> body{ background-color: blue; } </style> </head> 

Please note, however, that it is better to use .css files.

+6
source

Entering rules on an HTML page gives them greater "specificity" and, therefore, priority over external rules. If multiple CSS rules conflict, the ID wins the class, and inline styles win the identifier.

  <head>
   <style type = "text / css">
     span.reminder {color: blue;}
     span # themostimportant {color: red;}    
   </style>
 </head>
 <body>
   <span class = "reminder" id = "themostimportant">
     This text will be red.
   </span>
   <span class = "reminder" id = "themostimportant" style = "color: green;">
     This text will be green.
   </span>
 </body>
+2
source

You can define CSS at three levels, externally nested within the document (inside the <style> tag) or inline element.

Depending on your needs, you can use all three, as a rule, external sheets are good for general styles, since you can apply them all over the world. If you have specific cases that you must deal with, you can use other levels.

+1
source

You can do it. However, by moving your CSS to a separate file, it can be cached. This reduces the amount of data that needs to be transmitted for each page, reducing bandwidth costs and increasing speed.

+1
source

You do not have to save CSS in an external file, no. What you are asking is "inline" css: including style directives directly inside the page itself through <style> blocks.

There are times when this may make sense, in moderation, but in general it is not the way you want to go . Saving CSS in an external style sheet makes it easy to work with both your HTML and your style, especially since the project scales and changes hands.

+1
source

One of the great advantages of using CSS in an external file is that one rule can apply to many pages . Here is a contrast of the three CSS approaches:

Inline Styles - to change the color to blue, you need to find every place where the red style exists, possibly on many pages.

  <span style = "color: red;"> This is a warning. </span>

Page styles - this allows you to mark something - in this case a warning, not what it looks like . You can change all the β€œwarnings” on the page to instead have a yellow background by changing one line of code at the top of the page.

  <head>
   <style type = "text / css">
     .warning {color: red;}
   </style>
 <body>
 <span class = "warning"> This is a warning. </ span *>

The external file is the same code as above, but the fact that the style information is in a separate file means that you can use the warning class on many pages.

+1
source

You can use it anywhere, css files are not a requirement. however, it is recommended to use css files, as this simplifies the work of the site and changes it in the future.

0
source

Necessary? No. You can do it, but you prefer.

This is usually the best type to save your CSS from your html whenever possible, though.

0
source

What I usually do.

At least in the beginning. As the page design draws to a close, I move most of the stuff into "main" style.css

0
source

I prefer to keep the style in CSS, because it separates the presentation from the presentation, which allows me to easily switch between presentations. In addition, it stores all the information in one place instead of being split between two places.

0
source

Css can improve performance because they are cached from the browser and pages are smaller!

0
source

Use an external file for all styles used by sitewide, a document stylesheet for styles that are used only on this page, and use inline styles when a style affects only this single element.

External style sheets do not always have low bandwidth. If you put each style for each page of your site into one giant css file, your users take on a large boot load, even if they only visit your home page once.

Thoughtfully dividing your styles into main.css with the most common styles, and then into additional stylesheets as users go deeper, can help make the download smaller for some paths through the site.

0
source

Source: https://habr.com/ru/post/1276953/


All Articles