Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Position vs CSS

Tags:

jquery

I am a server guy with limited but growing client side (javascript) abilities.

I have an app that requires a half dozen DIV tags with inner background images and text all positioned over larger image. Think transparent images with roll overs and text. All of this is somewhat dynamic depending on server side templates and configuration.

I am finding it much easier to position these programmatically using the core jQuery position() method than attempting a pure CSS and markup based approach. There are a number of techniques for vertially centering text within a DIV using CSS but position() just makes this easy.

My question:

How valid is an approach that makes extensive use of position() vs an approach that uses more of pure markup (traditional HTML and CSS)? I realize that in the end, most of the jQuery approach translates into styles and CSS.

like image 981
andleer Avatar asked Dec 17 '10 17:12

andleer


People also ask

What does position () method do in jQuery?

jQuery position() Method The position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

What is the difference between position and offset in jQuery?

Although both methods have some similarities, but the jQuery offset() method is different from the jQuery position() method. The offset() method retrieves the current position relative to the document, whereas the position() method retrieves the current position of an element relative to the parent element.

Is position inherited CSS?

As it can be observed in the above example, the position defined in the style is inherit, but the computed position value is relative. This is because inherit sets the value to that of its parent element. With that, we are done with the position property. Hope this helped you better understand, positioning in HTML/CSS.

What is the best position in CSS?

The absolute positioning is best if you need something the be placed at an exact coordinate. The relative positioning will take where the element already is, and add the coordinates to it. So for example if I have a <p> element inside of a <div> and I want to indent it, I would use relative with, say, left:15; .


2 Answers

At the heart of your question is the issue of Unobtrusive Javascript.

Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation

In particular Progressive Enhancement

Progressive enhancement uses web technologies in a layered fashion that allows everyone to access the basic content and functionality of a web page, using any browser or Internet connection, while also providing those with better bandwidth or more advanced browser software an enhanced version of the page.


What you have to ask yourself is how far will you stride to reach this ideal pinacle of js development. Sometimes its just plain stupid to code this way (yes, i said it). In a perfect world of unlimited deadlines and lightning quick fingers with an ever expanding mind you could and should code COMPLETELY unobtrusively in EVERY possible situation.

Again, let me stress that one should strive to achieve this goal whenever humanly possible. But here are some cases that I personally make exceptions.

  • Internal Apps: When I have complete control over the deployment platform I would be perfectly comfortable using the position property or any minor violations of the Unobtrusive Ideal. Especially if the budget for a project is low. The key here is to make sure you or whomever you are coding for knows the fallbacks here. (yes, you need JavaScript/Browser X to run this application).
  • Budget/time: If i'm in a major crunch and i'm faced with some petty CSS BS in ie6 sometimes it's just the right decision to make.

Yes, it's ok to use the position property but use it as sparingly as possible. The most important part is that your code degrades gracefully, or as gracefully as you deem fit. If the site/app is bearable w/o javascript (7% market share) then go for it.

jQuery/Javascript is not sufficient enough to replace CSS. You need to learn it if you will be coding in the front-end. There is no way around it.

like image 166
Derek Adair Avatar answered Oct 07 '22 22:10

Derek Adair


The worst consequence by doing that grammatically is users may see a flicker when the code gets executed and updates your styles.

Another consequence is using Javascript may be harder to understand to someone else who needs to update your code.

I would suggest to use CSS as much as you can, then use position for things that wouldn't be worth your time figuring out, like doing vertical center.

like image 23
wajiw Avatar answered Oct 07 '22 22:10

wajiw