1 August 2011
Additional required products
Intermediate
You have several options to ensure that your WordPress site works properly in all major devices like phones, tablets, and desktops. In this tutorial you'll learn how to set up CSS3 media queries using a custom child theme, as well as preview and edit your styling within standard screen resolutions using Dreamweaver CS5.5. The theme will be optimized for use with smart phones, tablets, and desktop systems. If you are new to WordPress/Custom WordPress theming, you may want to step through Brian Wood's four-part series on Editing a WordPress theme with Dreamweaver CS5 first.
In order to follow along with this tutorial, you must have WordPress installed locally.
NOTE: There is one step that you need to take before you start that is crucial: When you install WordPress 3.2.1, it has a default theme of "Twenty Eleven." This theme already has media queries built into the main style sheet (a different method from what you will learn in this tutorial). Since most of you are going to install another template, you need to start with a theme that has no media query conditions in it. You will need to switch to the "Twenty Ten" theme that still ships with WordPress 3.2.1.
http://localhost/yourWPfolder/wp-admin .
Note: The sample download files that accompany this tutorial do not include the database file and are provided only as a reference.
A child theme in WordPress is the simplest theme possible. It typically only includes a style sheet and any necessary images and inherits the template files from a parent theme. Actually, the only required file in a child theme is a style sheet named style.css.
A child theme resides in its own directory in wp-content/themes and is created, most of the time, to make modifications to the parent theme. Next, you are going to create a child theme so that you can create styles that override the parent theme styles for different devices.
For this exercise, create a new directory named multiscreen within the wp-content/themes folder on your hard drive (see Figure 2).
In the multiscreen folder you will create a new style sheet using Dreamweaver. In this style sheet, you will insert the information header by which WordPress recognizes the child theme. As with any WordPress theme, the information header must be at the top of the file; the only difference is that in a child theme the Template: line is required, so that WordPress knows which theme is the parent of the child theme.
Here is an example information header of a child theme's style.css:
/*
Theme Name: Twenty Ten Child
Theme URI: http: //example.com/
Description: Child theme for the Twenty Ten theme
Author: first last
Author URI: http://example.com/about/
Template: twentyten
Version:0.1.0
*/
To create the required CSS file, follow these steps:
/*
Theme Name: Multiscreen
Description: Multiscreen Theme using CSS Media Queries.
Author: Your Name Here
Template: twentyten
*/
@import url("../twentyten/style.css");
#site-title a
{
color: #009900;
}
The @import rule brings in the parent's style sheet.
The #site-title a rule defines a color (green) for the site's name, overriding the corresponding rule in the parent style sheet.
Note: Make sure that no other styles appear above the @import rule in the style.css file! This prevents the child theme from importing the parent template style sheet.
This should show you that the style you added to the child theme overrides the same style in the parent theme. Next, you will start to set up styling that conditionally applies to the page depending on device screen resolution, using media queries. But first, let's take a look at what media queries are.
CSS3 has brought us so many new features like rounded corners and @font-face, to name a few. It also introduced media queries, which extend the functionality of old school CSS media types. A media query lets you change the styling for a page depending on the resolution or other features of the device it's being viewed in, without changing the actual page content.
Media queries can be applied to a WordPress theme in many ways. If you look at the example below, there are three links to style sheets in the same document. Each uses its own media type to define under what conditions it should be applied to the page content. Those media types are written generically as: media="some media type and (certain features)".
Looking at the first link tag with phone.css in it, it means that a certain style sheet (phone.css) should apply to a device of a certain media type (" screen") with a certain feature (maximum or minimum screenwidth). As mentioned earlier, you need only one version of the HTML file and the CSS file will switch whenever the conditions of an expression is met.
Here is an example of writing media queries in the HTML link tag:
<!-- Phone -->
<link href="phone.css" rel="stylesheet" type="text/css" media="only screen and (max-width:320px)" />
<!-- Tablet -->
<link href="tablet.css" rel="stylesheet" type="text/css" media="only screen and (min-width:321px) and (max-width:768px)" />
<!-- Desktop -->
<link href="desktop.css" rel="stylesheet" type="text/css" media="only screen and (min-width:769px)" />
Note: For more details on media queries, see David Powers's Introduction to media queries.
As you work with media queries, you will need a way to test them. In Dreamweaver CS5 and CS5.5, there is a feature called the Multiscreen Preview panel. This panel provides a preview of the page that you are currently editing as it would appear on devices supporting different screen resolutions (see Figure 5), and is something you will be using throughout this article.
In your WordPress site, you will see how to apply media queries using your child theme. Using this method, you don't change the style sheet of the parent theme, you just create styles to override them. Adding media queries to your child theme style sheet
Next, you specify the media queries within the child theme style sheet (style.css) you created earlier.
#site-title a { color: #009900; }.@import url("../twentyten/style.css");:@media screen and (max-width:320px)
{
#site-title a {
color: #009900;
}
}
@media screen and (min-width:321px) and (max-width:768px)
{
#site-title a {
color:#3CF;
}
}
@media screen and (min-width:769px)
{
#site-title a {
color: #F00;
}
}
The next step will be to add styles to each media query that override specific parent templates styles. For instance, the "Twenty Ten" template has a style for menu items that look like this:
#access .menu-header li, div.menu li {
float: left;
position: relative;
}
In order to have the menu items stack rather than float on phones, you can create the same style in your " max-width:320 px " media query, with a float property difference, like this:
#access .menu-header li, div.menu li {
float: none;
position: relative;
}
We can't go through all of the styles you might create for phone viewing, because each template is different; what follows are some basic phone media query rules for the "Twenty Ten" theme.
@media screen and (max-width:320px) {
#site-title a {
color: #009900;
}
}
with:
@media screen and (max-width:320px) {
#site-title a {
color: #009900;
}
#access .menu-header, div.menu, #colophon, #branding, #main, #wrapper, #access, #primary, #secondary, #container, #content {
width: auto;
float:none;
padding:0;
margin:0;
}
#site-title, #site-description, #content, #primary, #secondary, #footer {
margin: 0 7%;
}
#content, #site-info, #site-generator {
margin-top:2em;
float:none;
}
#site-description {
float:none;
margin-bottom:1.6em;
}
#site-title {
float:none;
width:90%;
}
#site-info, #footer {
float: none;
width: 90%;
}
#footer {
padding-bottom:1.8em;
}
#branding img {
height:auto;
width:100%;
float:none;
}
#access .menu-header, div.menu {
margin:0;
}
.menu ul {
width:100%;
margin:0;
padding:0;
}
.menu ul li {
margin:0;
padding:0;
display:block;
background-color:#666;
color:#990000;
float:none;
}
#access a {
padding-left:20px;
}
}
Next, you will preview the site in Dreamweaver so you can see the phone styling you added applied to the page.
At this point, you could close the Multiscreen Preview panel and continue to edit the styling for the phone media query, then create styles for the media query for tablets (@media screen and (min-width:321px) and (max-width:768px ) as well, testing in the Multiscreen Preview panel as you go.
Next, we'll discuss an altrenative method for adding media queries and styling differences by editing the existing theme files.
Another method for setting up media queries is to apply them within the theme itself, by editing the theme files.
Remember that a simple WordPress web page is made up of three basic sections: a header, the content, and a footer (see Brian Wood's tutorial on Editing WordPress themes using Dreamweaver CS5). The header section, which lives in the header.php file, contains links to style sheets and JavaScript files, and can be found in your theme's folder.
You will need to add your media query links to the header section. For example:
<!-- Phone -->
<link href="phone.css" rel="stylesheet" type="text/css" media="only screen and (max-width:320px)" />
<!-- Tablet -->
<link href="tablet.css" rel="stylesheet" type="text/css" media="only screen and (min-width:321px) and (max-width:768px)" />
<!-- Desktop -->
<link href="desktop.css" rel="stylesheet" type="text/css" media="only screen and (min-width:769px)" />
Make a copy of style.css
For this example, you will activate the "Twenty Ten" theme again. You will then create copies of your default style sheet—your goal is to have three style sheets, one for each target: phone, tablet, and desktop.
The default style.css will be for desktop.
bloginfo('stylesheet_url') outputs to: http://localhost/yourfoldername/wp-content/themes/twentyten/style.css.
bloginfo and press Ctrl+spacebar; note that bloginfo is defined in the file general-template.php, which is located in wp-includes/general-template.php (see Figure 9). You can use site-specific code hinting to help us code, but also to help see where things like the bloginfo() function are defined (in what files within our WordPress install).
stylesheet_url is defined using get_stylesheet_uri(). Insert the cursor after get_stylesheet_uri and press Ctrl+Spacebar again. You can see that get_stylesheet_uri is defined in theme.php (see Figure 10).
get_stylesheet_uri() function twice (it's found around line 80 or so in the Twenty Ten theme file).get_stylesheet_uri() function to get_stylesheet_uri_phone() and change the URI from '/style.css' to '/phone.css.'get_stylesheet_uri() function to get_stylesheet_uri_tablet() and and change the URI from '/style.css' to '/tablet.css.' (see Figure 11).
case 'stylesheet_url':
$output = get_stylesheet_uri();
break;
Next, you need to add the links for the phone and tablet CSS to the header.php file.
<link rel="stylesheet" type="text/css" media="only screen and (min-width:769px)" href="<?php bloginfo( 'stylesheet_url' ); ?>"/>
<link rel="stylesheet" type="text/css" media="only screen and (max-width:320px)" href="<?php bloginfo('stylesheet_url_phone' ); ?>"/>
<link rel="stylesheet" type="text/css" media="only screen and (min-width:321px) and (max-width:768px)" href="<?php bloginfo('stylesheet_url_tablet' ); ?>"/>
With the new style sheets, changes to the theme files, and new links in the header.php, you could test your site. Unfortunately, there are no differences between the style sheets yet, right? That's what you'll do next in Dreamweaver.
In Dreamweaver CS5.5, you now have the ability to change the viewable area in the Document window in Live and Design View to match certain device resolutions. This makes it easy for you to not only check the style sheets for each applicable media query, like you can in the Multiscreen Preview panel, but also make edits. Using this new feature, Code Navigator, and the CSS Styles panel, you'll edit several styles for the phone style sheet to get you started.
Tip: You can also choose View > Window Size to change the view size.
#branding img and change the styling in phone.css to look like this:#branding img {
border-top: 4px solid #000;
border-bottom: 1px solid #000;
float: none;
height:auto;
width:100%;
}
You can also use Inspect mode to pinpoint styles to edit, then use the CSS Styles panel to modify the phone.css styling. That's what you'll do next.
#wrapper selected in the code (see Figure 18). Click. This freezes Inspect mode so that you can focus on editing the #wrapper style.
Next you will change the #site-title properties.
<h1> in the Live Code. You should see the #site-title style selected in the CSS Styles panel.
There are lots of other styles you will need to edit in order to make the page fit properly within smart phone resolution (see Figure 20). Here is just a sampling:
#access .menu-header, div.menu style by either removing the width or adding a % based width.#access .menu-header li, div.menu li style by removing the float. You could also add some border radius to that same style using the new CSS3 border-radius pop up in the CSS Styles panel. #primary, #secondary style and the #container style).
#content style.
Continue editing the phone styling, then change the viewable width in Dreamweaver to match tablet resolution (768 x 1024) and begin editing the styling for the tablet.css using the methods you just learned.
Using these different techniques, you can easily apply media queries to your WordPress theme and create a foundation for customizing the look and feel of your WordPress site for various devices.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.
Tutorials and samples |
| 04/23/2012 | Resolution/Compatibility/liquid layout |
|---|---|
| 04/20/2012 | using local/testing server with cs5 inserting images look fine in the split screen but do not show |
| 04/18/2012 | Ap Div help |
| 04/23/2012 | Updating |