Is CSS the only way for styling HTML documents? - javascript

I am a beginner web-designer. I know the basics of HTML, CSS and Javascript. I know that CSS used to style HTML docs, but is that the only way ? Can I use JavaScript to style HTML, somehow ? Or Is there any other way rather than CSS ?

JavaScript is a scripting language, not a styling language. While you can use JavaScript to apply CSS, it's still CSS, not JavaScript, that's defining the styles.
There exist HTML presentational attributes from a long time ago, but those have been superseded by CSS. You can still use them, not that you would want to, considering they'll most likely get translated to CSS anyway.
There also exist CSS preprocessors with custom languages, such as Sass/SCSS, LESS and Stylus, but they all compile to CSS as well, because that's what's actually implemented by browsers. So even if you use these preprocessors, at the end of the day you're still "writing" CSS.

I know that CSS used to style HTML docs, but is that the only way ?
It is the only sensible way
Can I use JavaScript to style HTML, somehow ?
You can generate CSS with JavaScript. (Not advisable for general styling).
You can use JSSS if you're only targeting Netscape 4.x.

You're also able to style HTML with HTML-attributes itself, but this way is pretty impractical and old-fashioned. Most of these techniques are deprecated and the functionality is VERY limited.

I suppose the correct answer to your question is no, because html includes some basic style elements itself. But CSS is the best way to style your documents and you absolutely should learn and use it. Once you're comfortable using CSS, try using a framework such as Twitter bootstrap to make your life easier.

It's not the only way but the recommended way.
You can include styling in your html tags but it becomes messy.
That's why CSS was invented so use it !

As mentioned previously, CSS is not the only way to stylize HTML but definitely the standard. If you're looking for a more programmatic way utilizing JavaScript you can look into less which (taken directly from the webpage) ...extends CSS with dynamic behavior such as variables, mixins, operations and functions and functions map one-to-one with JavaScript code, allowing you to manipulate values however you want.

Related

Dynamic: CSS vs JavaScript

JavaScript is more dynamic than CSS. But how is JavaScript more dynamic than CSS? How does JavaScript coding work in a better way than CSS?
It's two different things. CSS is a stylesheet language while JavaScript is a programming language. It's really like comparing apples and oranges. (Actually, the distinction isn't all that obvious on a technical level, as apparently CSS is even Turing complete, but as most people practically use the term "programming language", they're different.)
But I guess you can argue that JavaScript is more dynamic, in that it is a programming language that can dynamically change both the content and the presentation of your page. You can actually add and remove CSS rules dynamically, in run-time, with JavaScript. You can also alter the attributes of the DOM elements in your page, like classes, IDs etc.
Yeah, CSS is not as dynamic as JavaScript, and the new dynamic selectors do not work in older browers. Then again people won't upgrade if nothing breaks, so go with a pure CSS solution =)
Toggleable menus done with pure CSS usually uses the :selected dynamic selector and styles it with some visible menu element to have it only visible when a hidden checkbox is checked.
While JavaScript can be more flexible because it's a programming language, CSS can perform better because it's built in to the browser. JavaScript can emulate many of the same CSS properties, but CSS will have better performance. It can do things like animation smoother for example.
Many times people will write JavaScript code that sets CSS properties to let CSS do the rendering for that reason.
JavaScript not only stops at making cool design, it does a lot of other things directly to HTML and CSS will only help you with page design things and CSS can't replace your website's action maker, Javascript. I mean... imagine a CSS drop-down menu, haha.
Good question though.
There's significant overlap between their capacities.
Pure CSS3 can be used to build an attractive and responsive menu bar with drop-downs. And you can do the same in pure Javascript. But you may find that HTML+CSS is more expressive in creating layout+style.
Javascript will come in handy when you have to dynamically generate content on the client side (e.g. editor and simulators that run in-browser). You then may find it easier to do (practically) everything in Javascript.

Behavior/Styling Separation - How to use IDs, classes and custom attributes for CSS and JavaScript?

I want to clarify the good practices in terms of Designer/Developer workflow.
Here is my observation, let me know if I am wrong : there is no fundamental reason why CSS and JS should use the same attributes. Events associated to an html tag are not related to the styles that are applied to it and separating them would help the maintenance for both the developers and the designers.
And my question : why isn't there a strict separation between the two? I understand that we are free to use custom attributes for JavaScript and keep IDs, classes and HTML tag names for CSS however everything in jQuery seems to be made to use them. For example :
$("#myid").html("Test");
$("#myid").addClass("clickable");
$("#myid").removeClass("clickable");
if($("#myid").hasClass("clickable")) var clickable = true;
etc. Why is so?
Side-question : is there some performance issues about using only custom attributes for JavaScript?
$('[event="clickable"]').html("Test");
This article from Roy Tomeij http://roytomeij.com/2012/dont-use-class-names-to-find-HTML-elements-with-JS.html mentionned in stackoverflow : Separate ID and Class for JS and CSS seems to advice not to use classes in Javascript but that's all I have found so far and I want to expand the discussion.
It is valuable for both CSS and JavaScript to have the two items : unique IDs and multiple classes but when I think about it we could have div such as
<div css_id="mydiv" css_class="green" js_id="unique_element" js_class="other_event">
and hypothetical CSS properties and jQuery functions such as
div#mydiv{margin_top:20px;} //reads from css_id attributes
div.green{color:green;} //reads from css_class attributes
$("#unique_element").click(function(){alert("Clicked!")}) //reads from js_id attributes
$(".other_event").click(function(){alert("Congratulations!")}) //reads from js_class attributes
and in the same way
$("#unique_element").addClass("clickable2"); //add a value to the js_class attribute
Thank you all! I do not intend to reinvent everything just to explore ways to improve teams' workflows.
I think you can answer your own question. What possible benefit does having markup that looks like this provide?
<div css_id="mydiv" css_class="green" js_id="unique_element" js_class="other_event">
To me it looks messy and harder to deal with. In theory, content is king, and in the beginning you have a beautifully marked-up semantic HTML document. Thoughtfully, you add semantic class names and IDs based on the content within them. Using these IDs and classes, you are able to staple design and functionality onto your document using CSS and JavaScript respectively.
To answer your other question, IDs are faster for JavaScript. If you're using JQuery, the performance is negligible if you cache the DOM references as variables (which you should always do if you reference an element more than onec). For example, var $myclass = $('.myclass'); You can read more about selector performance here.
For CSS, many people recommend avoiding IDs for styling as much as possible. You can read the spec on specificity, but basically using lots of ids will lead to some of the same (though less extreme) problems as using !important.
The best I've seen is:
<div class="button button-primary js-modal-popup">
Essentially - add a class with js- prepended to it so you can distinguish from your stylistic classes and your jQuery hooks. It allows you to to create as many ID's or classes as you'd like without inhibiting your styles to be tied to actions as well.
there is no fundamental reason why CSS and JS should use the same attributes.
This is true. They're two separate languages...so, yes. There is no 'fundamental reason' why they should use the same attributes.
however everything in jQuery seems to be made to use them.
I don't think this is true, though. JQuery has specific functions for class and so on because, well, they're existing CSS attributes. But it also has the same exact functions for custom attributes, so there's no asymmetry here. I don't think this could be used as evidence that Jquery was made to use CSS attributes anymore than custom attributes.
As to your questions of performance or which to use, I think that bookcasey adequately answered them.

is positioning with javascript a good practice

I've just learned javascript and jquery a few weeks ago, since then I always use it to position all my divs and stuff in the screen by knowing the size of the screen and the window which I find extremely useful, but now I don't know if is this a good practice, cause it makes my web-pages fully dependant on jquery which I don't know if it may cause some troubles with different browsers.
I would really like to find answers like "Sure is a good practice, use all the scripts you want" cause I'm really loving this javascript stuff but well just tell what you think.
Use JavaScript for behaviors and CSS for styling.
Styling with JavaScript is slower and difficult for another developer/designer to figure out what you did.
Also if you use Firebug or Chrome Web Inspector you can not see where your styling is coming from.
Optionally set classes from JavaScript and not specific styling. You can transition between classes to create a nice effect. But at least your colleague can see where the styles are defined and change it.
I'm sorry, but I'm going to burst your bubble, somewhat.
It's somewhat OK to do it - as long as the page looks OK if you disable Javascript, as well. I would say it should look even better than OK. I would also say that you should only do that if the functionality of your site really demands Javascript, as well.
Keep in mind that a certain percentage of users will have Javascript disabled. I've seen sites that look horrible this way (I use NoScript on Firefox, and selectively enable Javascript as I browser), and a couple where nothing at all appears without JS enabled.
Bad.
As Darin notes, you can (and should!) use CSS for positioning and styling. That is what it was made for! Feel free to enhance the page with Javascript, but keep in mind what I say above!
You could use CSS for positioning and styling of elements. That's what it was designed for.
It's okay to use it for positioning in some cases, but using CSS is the better practice whenever applicable.
Well, In my opinion you should avoid it as often as possible. But I know sometime you don't have the choice.
And yea you can make great web apps using scripts.
It depends what you're positioning.
CSS should be your first choice for positioning standard, run-of-the-mill sections and elements of a webpage. To opt for JavaScript in these cases suggests a lack of understanding of CSS.
Likewise if you find yourself using JS to position things for different devices. Again, CSS has evolved; media queries came along for that. Rule: always exhaust CSS possibilities first.
That said, it would be oversimplification to suggest that JavaScript never be used for positioning. All of us, rightly or wrongly, have resorted (and it is just that, resorting) to JS in order to get things to look right cross-browser, particularly where support for older IEs is concerned.
But by far the biggest use case for JS positioning is for modern web aps and games. If you're building a game with random asteroids dotted around, JS is absolutely the choice for that, since the positions are based on calculation and computation.
Finally, bear in mind that when you position in JS, you are of course still using CSS. JS doesn't have its own, concurrent styling/positioning system - it simply sets CSS properties. It is simply able to do so more dynamically and conditionally than flat CSS.
It is almost certainly bad practise. Use CSS for styling - JavaScript to do this is slower, more work, and more prone to breaking.
If you're positioning everything absolutely (fixed coordinates) it won't look good on screens of different resolutions. There's no real answer to this question.. scripts have their place, and you can use all the scripts you want... Positioning all of the elements of your layout, however, is not a job for JS. Use CSS for that.
I'd start here: Starting with HTML + CSS
There is not one method for all situations. Each web application needs to employ the right tools and practices to achieve its goals. This varies so much between applications that there is not a "correct" answer to your question.

Is there any problem using JQuery/JavaScript to apply lots of CSS styles?

I want to apply ALL the CSS styles 100% with JQuery/JavaScript
But is is bad idea to do this?
Why would you create that overhead in javascript when straight HTML/CSS is fully capable of handling it? (in fact intended to)
Also, you're adding an unnecessary requirement to the styling of your site: having javascript enabled.
Main reason IMHO: Performance! Native CSS is much faster than JS.
Also worth mentioning:
Doesn't work for users with NoScript etc.
Overhead. Not such a big deal i think. There are more important things to optimize.
If you lazy load stuff, e.g. by using jQuery.load(), you have to reapply your styles to these elements. Not nice :/
Conclusion: CSS is made for styling, JS isn't.
If your visitor has javascript disabled, they'll see an ugly white page without any styling.
I don't see why you would do this, to be honest. It's not user-friendly.
Use a stylesheet.
A lot of people say to apply classes not individual styles.
You will see varying levels of performance degradation across different browsers. It's faster and cleaner to let the application render the styled HTML page because that's what it's optimized to do.
If you're dynamically loading content and already have fade effects/transitions so your content is only revealed after it's styled, you might be ok.
if you are going to do this, only apply styles with jQuery (rather than using javascript) to maintain cross-browser compatibility.
I would recommend against your approach because of the time it will take jQuery to loop through all of your elements and apply styles.
something like $('span').css(...); has to traverse the entire DOM and loop through a collection of elements. $('#title').css(...); does not have to loop through the entire DOM because it immediately knows where to look.
Also, browsers are designed to process stylesheets and render your styled elements very quickly.

jQuery equivalent of YUI StyleSheet Utility?

Does jQuery - or one of it's plugins - have equivalent functionality to the YUI StyleSheet Utility?
"The StyleSheet Utility is capable of creating new stylesheets from scratch as well as modifying the existing stylesheets held as properties of elements sourced from the same domain or any inline elements."
This (I'm fairly sure) is creating and modifying CSS stylesheets themselves not looping through the DOM and changing element's style property (as the jQuery.css() method does).
I think this technique has the potential to significantly change the way a lot user interface related Javascript is written so would be interested to hear about any other libraries where it's been implemented too.
Found a couple that look like they do similar things. I haven't tested them. jQuery.Rule looks to be pretty good though
jQuery.Rule by Ariel Flesler
This plugin allows quick creation/manipulation of CSS Rules, in a "jQuery-way". It includes features like chaining, iteration using each, selectors with context.
GlobalStylesheet by Jeremy Lea
Enables CSS modification that uses a 'global' stylesheet, rather than inline CSS. This is particularly handy for modifying CSS styles that you want to remain persistent until a page is refreshed again.
The short answer is no. YUI is the avant garde in this matter.
However I fully expect there to be something similar being made in the coming weeks(months), as the methodology that YUI is using does not seem to be unreplicable and considering how useful and important this feature is.
Maybe study YUI's method and make a plugin yourself?
Edit: Looks like I'm wrong. This is why you never post negatives like this. :)

Categories

Resources