How to make WordPress Themes Responsive

Responsive WordPress Theme XO by Semiomantics

25 Jan How to make WordPress Themes Responsive

Responsive Themes for WordPress

Responsive Websites become a must in 2012. This tutorial demonstrates how to make your WordPress Theme Responsive.

What is Responsive Web Design ?

In very simple terms : Responsive Web Design aims to display content adapted to the visitors screen size. This means namely that the layout width must be fluid, i.e. expressed as a percentage of the width of the screen, typefaces expressed in em and images must be scalable.

Practically speaking: a responsive designs can be viewed on a smart-phone or on a large PC screen, the display will always be the same and adapted to the size of the screen or browser window. n stead of using multiple style-sheets for different screen sizes, designers use Media Queries which allow to create multiple layouts using the same content. To achieve scalability, CSS media queries, such as min-width or orientation are used.

Make your WordPress Theme responsive using a Child Theme

A quick and easy way consists in creating a child theme; this allows you to upgrade your theme as the case may be without altering your customizations.

How to create a responsive child theme?

1. Create a new folder in your wp-content/themes folder and name it anything you like; for the purpose of this exercise I call it “responsive”

2. Create a style sheet in your new Child Theme folder (call it styles.css).

3. Suppose your main theme is called “wptheme”; paste the following into style.css

 /*
 Theme Name: wptheme Child
 Theme URI: http://www.yourdomain.com/
 Description: Child theme for wptheme
 Author: Your Name
 Author URI: http://www.yourdomain.com
 Template: wptheme
 Version:1.0
 */


The only line you really need to respect is Template:wptheme; this line refers to the parent theme folder.

4. Import the parent themes style-sheet by adding now the following line to your new style.css:

@import url(“../wptheme/style.css”);

5. Define the screen sizes you design for. (Smart-phones, tablets, PC) To test the result we just create different h1 color values for the different screen sizes:

@media screen and (max-width:320px)
{
h1 {
color: #ff0000;
}
}
@media screen and (min-width:321px) and (max-width:768px)
{
h1 {
color:#00FF00;
}
}
@media screen and (min-width:769px)
{
h1 {
color: #0000FF;
}
}

Having set these basic parameters, you can now apply styles and layout values to taste, such as vertical navigation display on small screens.

How to make Images responsive

To make images responsive we will need to take some WordPress specifics into account and there fore we will intervene with CSS3 and Media Queries and modify styles.css and functions.php of our theme.

Add the following to your style-sheet to make images scalable:

img{max-width: 100%;}
img{ -ms-interpolation-mode: bicubic; }

Now the images will be re-sized to fit horizontally, while the vertical scaling is still defined by WordPress. We need to remove WordPress image height and width values to scale our images proportionally.

In WP, image classes come with a with and height properties; we delete these properties like so:

change in your WP editor:

<img class=”imgclass” src=”../images/imagethumb.jpg” alt=”” width=”100″ height=”100″ />

to this:

< img class=”imgclass” src=”../images/imagethumb.jpg” alt=”” />

This will work for your post and page images, however not for the images created by WP dynamically, such as post thumbnails. We need to delete with and height properties dynamically with a function.

Add to functions.php:

function remove_wp_width_height($string){
return preg_replace(‘/\/i’, ”,$string);
}

Now you need to make a final modification in your template and replace:

the_post_thumbnail();

with the following:

echo remove_wp_width_height(get_the_post_thumbnail(get_the_ID(),’large’));

That’s all!

Hopefully the above will help you to do additional customization wok and as the case may be, to adapt your great themes to the requirements of 2012 Web Design .

8 Comments
  • ราคาอุปกรณ์ไฟฟ้า
    Posted at 00:26h, 18 April Reply

    Thanks for sharing, I’m learning how to create my theme. It’s useful.

  • Trisha Cupra
    Posted at 13:21h, 25 March Reply

    Instead of the function, I found this helpful tip on the WP Support Forum (and it worked for me):

    The answer probably lies in the CSS for the TwentyEleven theme, which is responsive:

    /* Images */
    .entry-content img,
    .comment-content img,
    .widget img {
    max-width: 97.5%; /* Fluid images for posts, comments, and widgets */
    }
    img[class*=”align”],
    img[class*=”wp-image-“] {
    height: auto; /* Make sure images with WordPress-added height and width attributes are scaled correctly */
    }
    img.size-full {
    max-width: 97.5%;
    width: auto; /* Prevent stretching of full-size images with height and width attributes in IE8 */
    }

    Looks to me like images with an explicit width and height declared on them require a resetting of the width and/or height to ‘auto’ in order to take advantage of responsive resizing.

    Good luck!

  • David Radovanovic
    Posted at 19:50h, 13 March Reply

    Hi. Thanks for the tutorial. You may want to cleanup the function code.

  • Pritesh Taral
    Posted at 14:22h, 27 February Reply

    Its nice tutorial.. :) It really help me . I will start making my own responsive theme.. By the way I wanted to know that – Can we convert existing theme into responsive theme .

  • Z Nicholas
    Posted at 20:37h, 09 February Reply

    Looking forward to the implementation stage :)

  • Bianca Gubalke
    Posted at 02:48h, 26 January Reply

    Lots of work to come – but amazing evolution!

  • Z Nicholas
    Posted at 17:10h, 25 January Reply

    Very informative post, thank you!

    • Yorgo Nestoridis
      Posted at 01:15h, 26 January Reply

      Thanks; wait till we implement it on some sites :-).

Post A Comment