Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to use javascript to create a responsive layout?

Tags:

javascript

css

I made the download of several free responsive layout (you can find them simply through google if you're curious) and I see that the layout has at least one or two javascript files.

The question is: javascript is essential to create a responsive layout?

Then, in the reply to this question is "no" and you have also the possibility to link a free responsive layout made only with html and css, well, you will receive a wonderfull BIG thank you.

like image 705
marco Avatar asked Apr 06 '14 14:04

marco


2 Answers

The whole point of responsive layouts is that it can (and should) be done with CSS3 media queries only.

However, this can often require some clever HTML design, especially if you want to have a slide-in menu (hint: :active can be very powerful when combined with tabindex to make an otherwise "inert" element respond to click events like a link) and many developers just can't be bothered with that, especially when jQuery is so readily available.

So basically, yes, you can make a responsive layout with CSS only. And if you succeed, congratulations! JavaScript can be used to make things easier, but in general if you think you need it, you probably just need to rethink how you're doing things.

Unfortunately, I have no links to JavaScript-less responsive layouts for you, that's because I'm very DIM - Doin' It Meself!


Edit back While I appreciate Martijn's demonstration of a use of JavaScript in making images essentially have variable resolution depending on screen size, images can be made responsive simply by using SVG if possible. If this is not an option, consider using a container with a background-image - only the image that matches the media query will be loaded :)

like image 90
Niet the Dark Absol Avatar answered Oct 21 '22 23:10

Niet the Dark Absol


Sometimes yes, sometimes no.

First understand what is response layout: Responsive Layout is the one that dynamically changes itself. Depending on the Browser's screen size. So that it fits perfectly on every screen type, size, resolution etc. So that the website's layout doesn't break.

You can just use CSS3 media query to change the layout, or else you can use jQuery or some other JavaScript to make this happen.

But remember, JavaScript is not required to make the document Responsive.

Sometimes Yes!

Sometimes the developer is better in writing the code using JavaScript, such as jQuery API. So he would find it easy to write the code in jQuery to dynamically handle all the events in the Browser window to make a website Responsive.

I myself would find it pretty easy to write the code in jQuery as compared to CSS. So for that purpose, I would have to add the jQuery source file to the document to render it that way. Otherwise I won't be able to create the responsiveness in the Website or would have to stick to the pure JavaScript

Example would be:

if($(window).width() > '1300') {
  $('body').css({
    'height': '100%' /* etc */
  });
}

Sometimes No!

Some developers are good at CSS (CSS3, and its media Queries too). So they try using CSS3 to render the document and make it responsive.

CSS3 is really much easy than jQuery and it would be helpfull to use it. It would also won't require any of the Script file to be included. You can easily write the code, in the default CSS file. And the changes would be made accordingly.

@media only screen and (max-width: 1300px) {
  body {
    height: 100%;
  }
}

But Remember

If you use plain CSS and then use CSS3 Media Queries to change the layout of the website, you will be able to just detect the screen size and other elements. You won't be able to check for the Browser's properties or the content on the screen etc.

like image 44
Afzaal Ahmad Zeeshan Avatar answered Oct 21 '22 22:10

Afzaal Ahmad Zeeshan