navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

WordPress: Building Responsive Themes

WordPress: Building Responsive Themes

with Morten Rand-Hendriksen

 


The many ways visitors access web sites, via mobile devices, tablets, and desktops, now requires sites to incorporate responsive design elements that adapt to different screen sizes and browsers. In this course, Morten Rand-Hendriksen demonstrates design strategies, best practices, and actual code examples for creating a responsive web site. The course covers layout, navigation, responsive video embedding, and content sliders. The final chapter shows how to create an index page with jQuery Masonry, a jQuery plugin that helps you create dynamic grid layouts.
Topics include:
  • What is responsive design?
  • Installing the Anaximander example theme
  • Deciding what screen sizes to target
  • Designing menus
  • Adding responsive images and video
  • Using CSS media queries to apply different styles
  • Handling sidebars on mobile displays
  • Dealing with footer widgets
  • Installing jQuery Masonry

show more

author
Morten Rand-Hendriksen
subject
Web, CMS, User Experience, Blogs, Responsive Design
software
WordPress 3.x
level
Intermediate
duration
2h 55m
released
Aug 17, 2012

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi I'm Morten Rand-Hendriksen and this is WordPress: Building Responsive Themes.
00:10For this course I've created a special WordPress theme just for you that starts
00:15off as a static theme and at the end of the course ends up a fully responsive
00:19theme with lots of functionality.
00:21Throughout the course we will look at several different aspects of responsive
00:25design, including using media queries to create different layouts for
00:30different screen sizes,
00:32enabling images and videos to resize and respond to different screens, and
00:37adding advanced JavaScript functions, including a featured content slider which jQuery Masonry.
00:44More and more people used their cell phones or tablets to access the web, and as a
00:48result, we need to make web sites that look great and work properly across all
00:54these different devices.
00:56This course will show you how to do just that.
00:59So let's get cracking with WordPress: Building Responsive Themes.
Collapse this transcript
Using the exercise files
00:00All lynda.com members will have access to the key assets of this course, the
00:05starting state of the Anaximander theme, and a code snippets file.
00:10If you're a Premium member of the lynda.com online training library, you have
00:14access to all of the exercise files used throughout this course.
00:19In the Exercise Files folder you will find the Anaximander theme in the Static
00:25version when we start off, along with a code snippets file, and you'll also find a
00:30folder for each of the chapters in which we make changes to the code.
00:35The way this works is, for example, if you start on chapter 10, movie number
00:4002, you have a set of files that are the starting point where I'm
00:45starting in this movie.
00:46That means if you want to ensure that your code matches mine exactly, or if
00:51you're jumping in in the middle of the course, you can reset your theme to match mine.
00:56The way you do that is by opening your WordPress installation on your computer,
01:01or WordPress on your server, and then navigating to the theme, so here we have
01:06an Anaximander-Static, and then you grab all the content inside this exercise
01:12files folder and simply copy it in, overwriting what's in the current folder
01:17with what you're copying in.
01:23This will ensure that for each movie, you're always starting at the same
01:26place as me.
Collapse this transcript
What you should know before watching this course
00:00Before we get started with this course, there are a few things you should know.
00:04If this is the first time you're using WordPress, or if you have little
00:08experience using the application, I urge you to check out the WordPress
00:12Essential Training course right here in the lynda.com online training library.
00:17The Essential Training course will give you the fundamentals and the
00:20understanding of how WordPress works and ensure that you're able to follow the
00:24course all the way through.
00:26The WordPress Essential Training course covers how to publish your WordPress
00:30site on an external host on the web, but if you want to install WordPress
00:35locally on your computer, you can also check out the Installing, Apache, MySQL
00:40and PHP course right here in the lynda.com online training library.
00:46To be able to test your theme properly on a mobile device like a cell phone or a
00:50tablet, you need to either publish your site to the web, so you can access
00:54through the web, or you need to set up a local server on your local network so
00:59that you can access that network with your device.
01:02I suggest setting up a site on the web, simply because it's easier and less to manage.
01:08And finally, throughout this course I will be using demo assets in the site.
01:13That's images, text, and videos that we've created to showcase
01:17different features.
01:19As you follow along in this course, you should use your own assets, so your own
01:23text, your own images, and your own videos, so that when you're done you
01:28actually have real content on your site.
01:31Now, let's get started building responsive themes.
Collapse this transcript
1. Why Mobile Matters
What is responsive design?
00:00If I ask you how you access the web these days, I bet your answer will
00:04be something like "with my phone" or "with my tablet" before it is "on my desktop computer."
00:10With the introduction of smartphones and tablets, the way people visit and
00:14interact with the web has changed completely.
00:17In the past, the web was focused mainly on desktop computers with ever larger screens.
00:23Now, the web needs to focus on the exact opposite:
00:25mobile computers and smaller screens.
00:27You may have heard the term responsive design or possibly responsive layouts
00:33bandied about, and you may have a general idea of what it is.
00:37Regardless, let me give you a short primer.
00:40What you see here is the original Responsive Web Design article written by
00:44Ethan Marcotte and published on A List apart on May 25th, 2010.
00:50This article outlines what was to become the definition of responsive web design.
00:56The idea presented in the article is that any web site should conform and
01:02realign the content to the device that's accessing it so that if the screen is
01:07big, you have a lot of content; if the screen is small, you reorganize the
01:11content so that it fits and give the users a good user experience.
01:15This idea was quite revolutionary when the article was published, but now
01:19responsive design has become the new standard for the web, and it should be the
01:23new standard for the web, because we as web developers and designers need to
01:27ensure that when people visit web sites with different devices
01:31they should get a consistent experience through all these different devices so
01:36that they recognize the web site and they know what's going on.
01:39But that's all theory. Let's look at some examples.
01:42First, let's look at a web site that's not responsive.
01:44This is the web site I made for the 12x12 Vancouver Photo Marathon several years ago.
01:50It looks great on a desktop, and it functions quite well.
01:54However, if I reduce the size of my window, you will see that this site is not responsive.
01:59If I reduce it down past the standard width of the site, you will see that the
02:03margins of my window start covering content.
02:06And to access the content, I have to use a scrollbar,
02:09scroll left and right.
02:11And if you were to visit this site using a cell phone or a tablet, you'd
02:14notice you'd have to do pinch-and- zoom behavior to be able to see all the
02:18content and read it.
02:19That is not a good user experience, and that is not responsive design.
02:24If you want to employ responsive design on a WordPress site, you really
02:28don't have to go any further than the 2011 theme, which is the stock theme for WordPress.
02:33The 2011 theme is already responsive.
02:36So, if I reduce the size of the window here, you will see that the content
02:41reorganizes and reshapes to fit the size of the window.
02:44And you will see that content like the sidebar will appear when there's room
02:49for it and disappear, or rather, move down to the bottom of the site, when there's no room.
02:55This allows for a consistent look and design across multiple devices without
03:00forcing the users to have to do extra behaviors to get to all the content.
03:05Lastly, let's take a look at the project we are going to be working on in this course.
03:10For this course, I created a new WordPress theme that you can work with,
03:14you are going to customize, and that you can use for your own web site if you
03:17want to when you are done.
03:18This theme is fully responsive, and you can see it when we resize the window.
03:22The front page has a masonry layout
03:26that automatically reorganizes the content depending on the size of the screen.
03:30And you will notice that in addition, the menu changes from a standard list
03:34menu to a more advanced menu when the screen gets small enough so that it's
03:39easier to use on touch devices.
03:42If we go to single posts, you will see that on small screens the title and all
03:47the information is on top.
03:49But as we scale up the window, you will see that the title and other information
03:53slide to the left side, leaving the content on the right.
04:00The whole point of having a web site is for people to be able to access the
04:04information on that web site.
04:06That means as the technology landscape changes, so the web site has to change to
04:11accommodate this new reality.
04:12Responsive design is at the heart of this new paradigm.
Collapse this transcript
The different faces of WordPress on mobile
00:00With WordPress, you basically have three main routes you can choose to follow:
00:04use a responsive theme, use a theme switcher that serves up different themes
00:09depending on the device used, or use a plug-in that serves up a custom
00:14experience for mobile users.
00:16In this course will be looking at using a responsive theme, and through the
00:20course, I'll teach you how to build not responsive theme.
00:23If you ask me, using a responsive theme is the best solution to deal with the mobile issue.
00:29If you don't want to use a responsive theme, the second-best option is to use a mobile
00:34theme switcher plug-in and have a custom mobile theme that you can use instead.
00:39What you see here on the desktop screen is a site I built for a company called
00:43Better Outcomes Consulting in Vancouver.
00:46On the Desktop it looks like a phone, but if you look at it on a cell phone, you
00:51only see the middle part, so it looks like an application instead.
00:56What you're seeing here is the result of using a theme switcher plug-in.
00:59So when you on a desktop you get one theme, and when you're on a cell phone you
01:03get a different theme.
01:05The last option you have is to use a mobile plug-in.
01:08The most popular mobile plug-in is one called WPtouch.
01:12It gives you a customized mobile experience when you use mobile devices.
01:16The problem with this approach is that this mobile experience looks nothing
01:20like your main web site.
01:21You can customize it a bit to make it look more like your custom web site, but the
01:26mobile experience will always be a custom mobile experience.
01:30Deciding if you want a responsive theme, if you want a custom theme for mobile
01:34devices, or if you want to use a mobile plug-in is the first step on your path
01:39to creating a mobile-ready WordPress web site.
01:42In this course, we will focus on creating responsive themes.
01:46If you're looking for a plug-and-play solution, check out the WordPress mobile
01:50solution's course right here in the lynda.com online training library.
Collapse this transcript
Exploring the finished Anaximander theme
00:00Before we get started, let me quickly explain how this course is going to work.
00:05The idea of this course is to give you the tools and understanding necessary to
00:09build your own custom responsive themes or to take existing themes and make them responsive.
00:15To make this process easier, I've created a custom theme just for you that you
00:20can use as you follow along with this course.
00:23The theme start off as a regular static theme and as you progress, you'll
00:27incorporate responsive features that in the end will make it fully responsive.
00:32All the techniques taught in this course are universal, but you may have to
00:36change them a bit to fit different themes.
00:38The cool thing about this course is that when you're done, you'll have a fully
00:43responsive and quite advanced theme you can use for your own web site.
00:47I designed and built a theme with this in mind so that you didn't have to start
00:51over with your old theme when you're done, unless you want to, that is.
00:55Let's take a look at this new theme called Anaximander and see what it does once
01:00you're finished with the course.
01:01The theme comes with lots of built-in functionality.
01:04From the top, this themes has optional social icons, if you want to link your
01:09theme to Twitter or Facebook, along with a search box.
01:12Then we have a custom header with a custom menu that's hooked into the WordPress
01:17menu system that has nice dropdowns.
01:20The header is responsive, and you can also add an optional header image if you want to.
01:25In the course, I'll show you how to add a featured content slider, like the one you see here.
01:30We'll also incorporate what's called the Masonry-type layouts.
01:34It start to type of layout you see here on the front page, where all the content
01:38stacks next one another. And if we resize the window, you'll see the content
01:44reorganize to fit the screen.
01:48And if we go into a single post, you'll see we have a responsive layout again
01:53that responds to the size of the screen and fits all screens,
01:58and we also have advanced features like the display of related posts under each
02:02individual single post, and footer widgets.
02:06In developing this course, I wanted to give you a reward at the end, which is why
02:10I built the Anaximander theme.
02:12Once you've completed this course, you'll have an advanced responsive WordPress
02:16theme you can use for your own web site if you like, and this theme is only
02:21available right here on lynda.com.
Collapse this transcript
2. Starting with a Solid Theme
Getting and installing the Anaximander theme
00:00Throughout this course we will be working with and making changes to the
00:04Anaximander theme that I designed specifically for this course for you to use.
00:09So before we started, we have to install Anaximander into WordPress. As we move
00:13forward, I'll be using two installations of WordPress,
00:16one that's installed locally on my computer using BitNami--
00:20You can also do the same thing using WAMP or MAMP or WebMatrix or another
00:25application that lets you install WordPress on your computer--
00:28and in addition to the local install, I also have WordPress installed on a
00:32server on the web so I can access it with my phones and my tablets and check
00:36whether or not everything works.
00:38The static version of Anaximander ships in the exercise files, both the
00:43free ones and the full exercise files, and you'll find them under the
00:47folder Anaximander-Static.
00:49This is the folder that contains the themes.
00:51If you open it, you'll see the entire theme here.
00:54So what we need to do is simply grab that and dump it into WordPress.
00:59Themes in WordPress live under the wp-content folder, under themes.
01:03So simply grab Anaximander-Static and copy it into themes and it will
01:10appear inside WordPress.
01:12I'm also going to do this through my FTP application.
01:15So I'll navigate to my WordPress installation here.
01:17And here we have themes, and I'll grab it and dump it in.
01:26As this uploads, I can log in to WordPress on my computer, go to Themes, and here
01:36we will see we have Anaximander installed, and I can now activate an
01:41Anaximander on my local WordPress installation, go to the front page, and here we
01:48have the stock version of Anaximander running in my local installation.
Collapse this transcript
Configuring basic theme options
00:00When I built the Anaximander theme for this course I decided to build in some
00:05features that would make it more useful and easier to customize for you.
00:09Some of these features are standard in all themes and some are custom to this theme only.
00:14The features are also hooked into the new WordPress 3.4 theme customizer
00:18function for easier handling.
00:20Let's take a look at what options are available and how you can customize
00:24Anaximander to personalize your sites.
00:27By default, Anaximander ships like this.
00:29Off the top, you have a super header that has an RSS icon in it and a search
00:33box. You then have a blue main header that has the site title and site tag line,
00:39along with the main menu, and then you have the rest of the content.
00:43At the very bottom, you have a standard footer with information about the
00:49site and the theme.
00:52If you want to customize this, you can use the theme customizer function.
00:56You can access it either by going to the backend Dashboard, going to
01:01Appearance > Themes and then select Customize, or, if you are on the front end, you
01:08can go to the WordPress toolbar and select Customize.
01:12The theme customizer is quite built out for this theme, so it has additional
01:17features that you don't normally find in the theme customizer.
01:20Off the top, we start with the Super-header Icons.
01:22If you drop that down, you see here you can add your own Twitter handle, your
01:27Facebook page handle, and an alternate RSS feed if you want to use the
01:31regular WordPress RSS feed.
01:33If you enter information here--let's say I enter my Twitter handle--you see a
01:37little Twitter bird appears on the super-header and if you were to click on it,
01:41you'd go directly to my Twitter page.
01:43Likewise, if you enter the handle for your Facebook page--for instance, mine is
01:48pinkandyellowmedia for my company page--
01:54you get a Facebook icon, and again, if you click on it, you jump directly to the Facebook page.
01:59In the Alternate RSS Feed field, you can input another URL if you don't want to
02:04use the default WordPress Feed URL.
02:06If you don't have anything in these fields, the buttons will automatically
02:10disappear, and what you are left with is the RSS feed, which will always be there.
02:17The next option is Header and Link Color.
02:20If you look at the theme, you will see the header has this nice blue color and
02:24if we hover over any of these links down in the body, they also have the same blue color.
02:29You can change this color to any color you want using the Header and Link Color
02:33option. You drop it down and you can pick any color you want.
02:39That color will automatically preview both in header and on the links so that
02:44you can see whether or not that color works.
02:47If you are going to change the color, I suggest choosing a darker color, so that
02:51you get good contrast between the background color and the font.
02:54Next on the list is Site Title & Tagline, and this is a standard feature with all themes.
03:01Here you can change to Site Title and Tagline to anything you want, and you can
03:04also choose whether you want to display the header text or not.
03:08So if you want to change the Tagline, we can just type in something else and
03:15you see that takes place immediately in the preview.
03:18We can also change the Site Title and the same thing happens.
03:22So here you can experiment with different titles and different taglines and see
03:25how they appear in the theme.
03:29In this theme, the Display Header Text option is meant to coincide with the
03:33use of a header image.
03:34Let me show you what I mean.
03:36First, let me add a header image, so I will go to the Header Image option and
03:42upload a new header image. So I will go to my Computer and find a file I want
03:47to use and upload it.
03:49The header image is automatically added under the header, and this is done on purpose.
03:55I wanted to give you some different options here.
03:57In the Anaximander theme, you can choose whether you want to have just the
04:00standard header, if you want to have the header with the header image underneath
04:05it, or if you want to only have the header image and that will move the menu.
04:09If you want that last option, what you do is you add a header image first, then
04:13you go back to Site Title & Tagline and uncheck Display Header Text.
04:18When you do that, the header image is moved on top and the menu is slid in under
04:24the header image, as you see it down here.
04:26It's also restyled to fit more with this layout.
04:29To ensure that people have an easy time getting back to you homepage, the entire
04:33header image is now clickable, so you can click on it and go back to the
04:37front page at any time.
04:39I am going to turn the header back on so you can see all the content.
04:43While I was showing you this, I skipped over this Colors option.
04:46The Colors option allows you to change two things.
04:49You can change to Header Text Color, which is the text color for the title and
04:53tagline along with the main menu.
04:56You can change that to any color you want, so this allows you to experiment and
05:00see whether or not certain colors work in this environment, and you can find the
05:05exact right colors. And you can also change the background color.
05:10I have to zoom out my browser a little bit for you to see this.
05:13If I zoom out the browser, you will see we have a gray background here and I
05:17can drop this down and select any other color for the background and we can
05:21again experiment to see what would happen if we have different colors for the background.
05:28The last three options are standard options that come in WordPress.
05:31We have the Background Image option, which allows you to add a background image
05:35instead of just a solid background color.
05:38For this particular theme, I wouldn't recommend doing that because it looks
05:41strange unless you are very careful, but by all means, experiment and see what works.
05:46You also get to set to navigation.
05:48By default, WordPress will show the default menu, which is the Home button plus
05:53a list of all the pages in the site, but if you want to, you can create a
05:57custom menu inside the WordPress menu function and then assign out to the front page instead.
06:01If you do, and that menu has submenu items, the submenu items will appear like
06:07you see them here, in a nice dropdown.
06:12Finally, you have the Static Front Page option, which allows you to choose
06:15whether you wanted to have your latest post on the front page or a static
06:19page on the front page.
06:20Once you have customized your site and you like what you see, click Save &
06:24Publish to save and publish those customizations.
06:28If you don't like what you see and you want to go back to the way it was before,
06:31simply click Cancel and you are back to the default setup.
06:35And this customization option is something you can keep experimenting with
06:39throughout the course, and also after you have finished with the course, because
06:42nothing is ever final here. You can always add something else.
06:45I am going to cancel it out to go back to the default and then I want to quickly
06:50show you one more feature.
06:51Remember how I showed you the footer, where you see it's just a standard footer?
06:55Well, if you go to the backend, to Widgets, and add some footer Widgets--let's
07:02say we add Recent Comments, Recent Posts, Categories, and Archives, and we go
07:12back to the front page, and scroll down to the bottom--you will see that we now
07:19have an extra section with four different footer widgets, and those go on top
07:23of the main footer.
07:25So again, this is an option that you can choose to turn on or off by simply
07:29adding or subtracting widgets.
07:33Anaximander is a fully built-out theme with lots of custom options, so you can
07:38personalize it to fit your liking.
07:40Now that you know how it works, feel free to experiment with these different
07:43settings and see what you can come up with.
Collapse this transcript
3. Design First, Then Develop
Deciding what screen sizes to design for
00:00If you start looking at articles written about responsive design and development, you will
00:05notice that there are certain predefined screen sizes and scenarios everyone is targeting
00:09with their designs.
00:11As you start designing your own responsive themes, you have to ask yourself these questions:
00:16What screen sizes am I targeting, and what should the site look like on those screens?
00:21The answers to both these questions are, all screen sizes should be targeted and the site
00:26should look good on all of them.
00:28If you look at the standard screen sizes that we have to deal with right now, you have the
00:32classic PC monitor, which is roughly a 4 x 3 shape.
00:36It's usually around 1024 pixels tall and 1280 pixels wide. And then you have modern monitors
00:42like the HD monitors, which are 1080 pixels tall and 1920 pixels wide. That's really large.
00:50And then you have typical laptop monitors, which are quite short.
00:54I've seen them around 768 pixels tall and 1360 pixels wide.
00:59So this isn't a 16 x 9.
01:01This is an even weirder kind of size, and both laptop monitors and a lot of notebooks
01:06have these very short and wide type of screens.
01:10And then of course you have the mobile devices.
01:12A typical smartphone is 480 pixels on the long side and 320 pixels on the short side.
01:18Now, you may want to ask specifically about the iPhone with a Retina display.
01:23Well, the Retina iPhone will pretend to be 320 wide and 480 tall and then just provide
01:29you much better resolution, so you can still think of it as the same thing as a regular phone.
01:35When it comes to tablets, we have a lot more versions to deal with.
01:39We have the iPad 1 and iPad 2, which both were 1024x768, and now you also have the new iPad
01:49with a Retina display which is 2048 x 1536, and this operates the same way as the iPhone
01:56with a Retina display does.
01:58It pretends to have the same resolution as the original iPad, but the resolution is actually much greater.
02:03So you can think of it as the same as the old one, but you have to have better graphics to support it.
02:09But the iPad isn't the only type of tablet out there.
02:11You also have standard android tablets, and on average, they are around 1280 x 800, which is almost 16 x 9.
02:19But you now also have new HD Android tablets coming out in the market and you will often
02:24find that they are around 1920 x 1200. And that's just the tip of the iceberg.
02:30Consider this: let's say I am working on something like my blog and then I realize that I need
02:36to check some information on the lynda.com web site.
02:40I might do this and then put the two next to one another.
02:44This isn't a specific standard size, but it's a fairly common scenario, and I see it
02:47a lot where people will put a web site on one side and then a Word document on the other
02:52and move information back and forth. Or they may scale it down like this so they have
02:56one small web site on one side and a large web site on the other.
03:00And when Windows 8 comes out, Windows 8 has a feature that automatically makes a layout
03:04that looks very much like this, where you have a very narrow bar on one side and then a
03:09large bar on the other side.
03:10What I am trying to get at here is that you don't know the size of the viewport, but the
03:16person accessing your web site has.
03:19That means that you need to design your site in such a way that no matter what size this
03:23viewport or window has, your site will fit properly within it.
03:28That way, you create a properly responsive site that responds to the scenario the user
03:34is in, not the scenario that you're guessing the user will be in.
03:39When I design responsive web sites, I only focus on two sizes: the smallest size, which
03:44is the 320 pixel wide, and the largest size which would be a gigantic 1080p desktop monitor.
03:52Then I figure out how these two can be reconfigured so that no matter what screen size or window
03:58the site is displayed in, the content is right up front and easy to digest, and the font
04:03size and word count per line is within standard parameters.
04:07It's more work, but it pays off in space.
Collapse this transcript
Thinking responsively: Designing for many different screen sizes
00:00Designing responsive themes is surprisingly challenging the first time you try,
00:05but it does get easier once you get used to thinking in responsive terms.
00:09I think this has a lot to do with how we are used to design web sites.
00:14In the past, the only constraint we needed to think about was how wide the
00:17smallest computer screen was going to be,
00:20and generally, if you landed somewhere around the 960 mark, you were fine.
00:25So we were safe in designing sites that were always fixed width to 960 pixels,
00:30and they are also very easy to design.
00:32But now that we have all these different sizes of screens, we have to rejig how
00:37we think about the design approach to get to where we need to be.
00:41What you're seeing here is the original design concept for the Anaximander theme.
00:47This is what I made in Photoshop. And what I did was I started with the
00:51smallest possible screen, the cell phone screen, and then I decided to scale it up from there.
00:56There are many reasons why you want to go this route, starting with mobile first,
01:00but the primary one is that by starting with mobile first, you end up paring
01:05down the site to only what really matters.
01:07You'll notice that there are no sidebars here.
01:09There are no ads. There's no extra clutter. All you see is the main information.
01:14This layout is the front page and as you can see, it has an index where each of
01:20the stories are contained inside a box.
01:22So you see here is one box, you have a Read More button, and then you have the Next Box.
01:30And in each story you start off with an image and then the text.
01:34I did this because I know from experience that on index pages people really
01:38like to click on the images, so I want the image to be upfront.
01:42When you go to the single view of the story, you see that the layout is
01:47slightly different.
01:48There's a larger header text and the meta-content and the header text
01:52appears before the featured image, so the featured image is down here, along with the context.
02:04Once I had a clear idea of what the site was going to look like on a mobile
02:07device, I could scale my design up to fit larger screens.
02:12So what I did was I took this design and I simply changed the width of the total area.
02:17What I ended up with, after some work, was this.
02:21This, again, is the index page, and as you can see, one of the things I did was
02:25move the menu from under here, to up here on the right, simply to fill out the
02:30space, so it didn't look so empty.
02:32I also included a masonry layout for the index page so that each of the stories
02:37is stacking next to one another.
02:39That way I'd have room for more stories on a single page than you would if you
02:42stack them horizontally.
02:44And I would also solve that problem of the sidebar, because normally you would
02:48fill in the space with a sidebar because otherwise, your text lines will be too long.
02:54But I didn't want to have a sidebar, because then I'd have to hide it for
02:58the mobile side, so I ended up replacing the sidebar with the masonry layout instead.
03:03When you go to the single view, you see that I solved the same problem by
03:08moving the title and meta information to the left and then leaving the content
03:12itself on the right.
03:14This isn't the design I really came up with.
03:16I've seen this a lot in books that are printed on large pages.
03:20To make sure that the single lines of text aren't too long and have too many
03:24words in them, the layout person and the publisher has chosen to leave a very
03:28large gutter on either the left or the right side, and then they often put
03:32something like the title for the chapter in that gutter to take up the space.
03:38This allows me to provide both the title and all the meta information and the
03:42content on the same line and takes the way they need to scroll immediately after
03:47landing on the page.
03:49Once I have these two different layouts-- the small screen and the large screen--
03:55it was very easy to figure out what I was going to do with the middle cases,
03:59the in-between screens.
04:01Because now that I have a style guide to go with which says the title either
04:05goes on the left or on the top, I could simply say that once the screen gets too
04:09small to fit these side by side,
04:12I'll simply shift all these information on the top, and it will look just like the phone.
04:16And this is what it ended up looking like.
04:18This is the live version of Anaximander running on my blog.
04:22And as you can see, it looks almost identical to my mockup.
04:26That's not because the mockup is a screen grab of what you're seeing here, but
04:29because I was able to match the site almost perfectly to the mockups, by using
04:34the mockups as a blueprint.
04:36This is the front page with the index, and I can see here we have the masonry
04:40index with the Read More buttons on the bottom, and the stories line up next to
04:44one another so we get lots of stories on a very small page.
04:48And if I go to a single post, you see that here we have the title on the left and
04:54the content on the right.
04:56And as you can see, just when you land on the page, you can already start
04:59reading or ingesting what's here without having to scroll much.
05:03And if we scale this window down, you'll see that once we reach that point where
05:10the content is bigger than the window, instead of flexibly altering the size, I
05:16decided a new breakpoint, so it jumps into a new breakpoint.
05:20And then when we scale pass that breakpoint, the content is realigned and we
05:26now have the mobile layout, although it's much bigger.
05:29So we can continue scaling it down until we hit that mobile point.
05:39The result of this preparation where I first design for mobile, then for the
05:42full-size screen, and then figured out how to handle all the content is that I
05:47now have a site that's consistently attractive and easy to ingest.
05:51No matter what screen size people use or what window size they use or what
05:55device they use, they'll always get straight to the content and get what
05:59they're looking for.
06:00Designing responsive themes has more to do with how you think than how you design.
06:05Once you get into the habit of thinking content first and designing for the
06:09smaller screens first and taking away all the extra clutter you would normally
06:13put into your site, you end up with a much better layout that's easier to ingest
06:18for the people who visit your site, and that communicates the message better.
Collapse this transcript
Visualizing content realignment for better markup
00:00Once you have a clear idea of how your content will look on different screen sizes, you need
00:05to make sure your markup allows for this type of realignment.
00:09The one thing to keep in mind is that while the presentational aspects of your site may
00:14change as your responsive theme responds to different screen sizes and other scenarios,
00:20the markup, that is, the code behind the scenes, stays the same.
00:27That means your markup needs to accommodate all your different layouts simultaneously
00:29without becoming unnecessarily cluttered in the process.
00:33To make sure I know how I need to divide up my content, I like to draw lines in my designs
00:39so I can see what different sections I need to section off and how they relate to each
00:43other, and then I can match that between my full-size layout and my phone layout and see
00:49how these elements move in relation to each other.
00:52Let's, for example, take a look at the header.
00:55If I draw a box around the entire header, you'll see that we actually have three elements here.
01:01The reason why I know we have three elements is because if I look at the full-size version
01:06of the site versus the mobile-size version of the site I see the margins here are quite different.
01:12So, what I mean to do is make sure I'm able to address those margin issues on both the
01:17site title and the menu.
01:19So, I am going to box the title and menu into a separate box, and then I am going to separate
01:24each one of them.
01:25I'll separate this one and I'll separate this one out.
01:34Now that I've done this on my wide layout, I can match these boxes on my narrow layout.
01:40So, I'll go in here and find the same area, and since I know that these boxes span around
01:46both the title and the menu, I have to span the box across the whole thing, and then I
01:52have to separate out the content.
01:54So I'll separate out all the content first, and then I'll draw a box around the title
02:00and another box around my menu.
02:04So, based on this layout, I now know that I need to have a container and then another
02:11container within that container and then a container around my header and a container
02:16around my menu so that I can float them differently, because now I can see that the title needs
02:22to float to the left and the menu has to either float to the right on the full layout
02:29or float to the bottom on the phone layout.
02:33Likewise, I can take a look at my main content.
02:36I know that all this content is contained within one box, but then I have to separate these further.
02:45The header content is handled separately from the main content, meaning that I have to treat
02:52this entire section as one unit and then this entire section as another unit.
02:57So, I'll do the same thing in my mobile layout again.
03:00I'll draw a box around all the content, and then I can separate out the individual pieces.
03:11So, I have one section here for the header content and the meta element, and then I have
03:18another section here for the general body of the post.
03:24Looking at the two of them side by side, I now see that on the mobile side, everything
03:29is fine; I just have one set of content on top of the next.
03:33But in the wide layout, I now need to float the header content to the left and float the
03:40rest of the content to the right.
03:42But this isn't as easy as it looks, because if I float it using the general float behavior,
03:48I'll end up with this content spanning in underneath.
03:51So I have to use some sort of trick to shift everything into place the way I want it to,
03:55and we'll address that later in the course.
03:58But that just proves the point I'm trying to make.
04:01Before you start writing your markup, you need to know which pieces of content relate
04:05to each other and how they relate to the other pieces on the page.
04:10That way you can put containers around pieces that belong together and these pieces then
04:15become easier to handle, because you can place them next to one another or on top of one
04:20another or hide one and show the other one depending on what you want to do in your layout.
04:25By drawing out your main sections in your design and making it clear to yourself how
04:30they relate to one another, you'll be able to build markup that is ready to be responsive.
Collapse this transcript
Designing menus
00:00Menus have always been a hot button issue in web design and web
00:03development circles, and the heat has just increased for the introduction
00:07of responsive design.
00:09The challenge has always been how to create menus that look good, are accessible,
00:14and are functional all at the same time.
00:16You can usually only get two of those three.
00:19When it comes to responsive design, there's the added complication that we now
00:23have to create menus that work both for regular, old-fashioned mouse-based point-
00:27and-click behaviors and for new touch behaviors.
00:32This means making menus that have items that are easy to click on with clumsy
00:35thumbs and also dealing with the issue that in touch-based devices there is no
00:40such thing as a hover state, so regular dropdown menus don't work exactly as
00:45they're supposed to.
00:46In the Anaximander theme I've tried to address both the concerns of
00:50accessibility and also the concerns of mobile devices.
00:54What I've done is create menu styles that change depending on the size of the screen.
00:59So by default, on a large screen you get the standard menu, which has fairly
01:03small buttons that you have to touch exactly to get them to activate.
01:06But if you reduce the size of the screen down to a size that probably is on a
01:13mobile device, you get much larger buttons that are much easier to click on with
01:18your thumbs and have much larger active areas.
01:20At the same time, they are more distinct from the remainder of the design to
01:24make it more clear that these are menu items.
01:29In addition, as I alluded earlier, I made this menu accessible, even with a keyboard.
01:34So if you tab through this page, you'll see that as you get to the menu, you can
01:39actually go through each of the menu items by navigating with your keyboard.
01:44This is an important extra feature, because it means that the markup is really
01:48clear, both for search engines and for people who access this web site using some
01:53other device than a regular screen.
01:55This was achieved by adding a JavaScript library called Superfish.
01:59The Superfish Library allows you to create accessible menus that are easy to
02:04navigate with multiple different types of devices, including with a keyboard.
02:08Superfish was created by Joel Birch, and if you're ever going to create a theme
02:12that has a dropdown menu in it, I highly recommend including this JavaScript
02:16library so that you get properly accessible menus in your site.
02:21That's said, I'm going to give you my two cents on the matter.
02:25In my opinion, dropdown menus are a bad idea and should be avoided at all costs.
02:30There are many reasons for this, but the two main ones is A, dropdown menus are
02:35quite difficult to deal with on cell phones, primarily because the different
02:40types of cell phone browsers handle dropdown menus differently.
02:44So in many cases, even if you build the menu specifically to handle cell phones,
02:49it still will not work properly, and the result is you'll often see these
02:53custom-built menus to only appear on phones that add tons of extra code to your
02:58site and don't really do anything except make a different user experience for the phone users.
03:04That's not ideal.
03:06The other reason is, if you put up a dropdown menu, you are effectively hiding
03:11content on the page.
03:12What happens is you're making an assumption that everyone who visits your
03:16sites automatically will go and hover over all of your menu items just to see
03:20if there's something underneath.
03:21And yes, you could add an arrow next to the menu if you wanted to to indicate
03:25there is something underneath there,
03:27but you're still making a quite elaborate assumption about the person
03:30visiting your site.
03:32If you're putting links in a dropdown menu, you're basically hiding it from
03:35the person who visits.
03:37And I know what you are going to ask:
03:39So if you don't use a dropdown menu, what am I going to do with all these extra menu items?
03:44The answer is instead of making a dropdown menu, like the one you see here, go
03:48to that page that the first menu item links to and put the list of other items in the page.
03:55This is far more obvious, and it's also far more effective, because in a scenario
04:00where a person lands on this page first and doesn't use the menu, that person
04:05would have no idea that there were other pages related to this page, if those
04:10pages were only linked on the menu.
04:13Why on earth would they go and click on the menu when they're already on the page.
04:18So if instead you put the links inside the page or if you make a landing page
04:22for all these other pages that you are linking to, it becomes far easier for
04:27someone who visits to actually get to the content they are looking for.
04:31Unfortunately, I don't have a cure-all for the menu disease.
04:35What I can say though, is that by trying to reduce a number of menu items you
04:39use and focusing on information architecture to create navigation that is simple
04:44and intuitive, you should be able to use one of, or a combination of, the methods
04:48I've shown you to create responsive menus that work for all devices.
Collapse this transcript
Adding responsive images and videos
00:00It's relatively easy to create responsive text elements on a web page.
00:04Text, after all, is designed to reorganize and flow depending on the size of the
00:09box that contains it.
00:10Images and video are different matter entirely.
00:14Images by their very nature have a fixed height and width, and we have to deal
00:19with that in a nondestructive and functional manner.
00:21And when it comes to videos, it's a total gong show because there's so many
00:25standards out there and more often than not, we are pulling the videos from
00:29external sources like YouTube and Vimeo, and those sources put restrictions on us.
00:34But fret not, my friend. All can be solved with code.
00:38We just have to decide what the end result should look like first.
00:41Let's take a look at the images on this page.
00:45Before we dive into the code itself, I just want to give you an idea of what
00:48we're talking about when we are talking about responsive images and responsive video.
00:53If you look at this page, we have fixed widths for these images.
00:57If I change the size of my window, you will see that the image stays the same
01:02size no matter what I do.
01:04However, if I open this post in a single post and then again resize the window,
01:13you will see that the image will resize with the window.
01:18In certain sizes, you'll even see this image resize dynamically, like this.
01:24This is a built-in function in your browser where the browser automatically will
01:28realign the image and crunch the pixels to make everything work, but it's a
01:33function that you have to activate through the use of CSS.
01:36If you don't do it,
01:37you will end up with an image that stays the same size and as you resize your
01:41window, the image gets cropped closer and closer and closer and closer.
01:45I am sure you have seen this, both on your cell phone and also on web sites time
01:49and again, because it's fairly common.
01:51But in this course, we are going to look at how to work around it.
01:55There is one more important thing you need to remember.
01:57When you upload an image to WordPress, what WordPress does is it creates
02:01multiple different versions of that image in different sizes.
02:06As a result, the image we are looking at here is 620 x 375 pixels; however, if
02:14we go and look at the image on the front page, you see it's 298 x 180 pixels.
02:23This is important, because when you're loading the front page and you know that
02:26the image is going to be a fixed width and it's going to be small,
02:29you don't want to load up a huge image and then crunch it down to fit that small space.
02:34You want to load the image that's exactly the size you are for and in an ideal
02:39world, there would be a standard for this so that it would be easy to do and
02:45you could serve properly responsive images.
02:46Unfortunately, we're not at that point yet, because the browser manufacturers
02:50are still arguing over what is the best approach.
02:53So in the meantime, we have to handle those images ourselves and decide
02:57whether we want to take a large image and scale it down or if we just want to use small images.
03:02Like I said in the intro, compared to videos, images are easy.
03:06Videos are a whole different ball of wax, and in this course, you will learn how
03:11to make properly responsive videos, because when a video comes from YouTube or
03:16Vimeo or anywhere else, it's not responsive, which means if you resize the
03:21window, the video will stay the same size.
03:23It's a very frustrating problem, and it's not a problem that you can just fix with CSS.
03:28To fix that, we have to link to a JavaScript library designed specifically to
03:33address responsive videos, and we'll also do that in this course.
03:37Both images and videos can be made to be responsive, but it requires some
03:41decision making and some creative coding on our end.
03:44Fortunately for you, all of this can be solved with code, and all of it's
03:49covered in this course.
Collapse this transcript
4. Using Media Queries
What are media queries and how do they work?
00:00At the heart of responsive design lies a specific type of CSS, or Cascading Style
00:06Sheets code, known as media queries.
00:09Media queries are conditional statements that tell the browser to use specific
00:13sets of styles depending on the size of the screen and other factors.
00:18Let's take a look at how simple media queries work and how to build them.
00:22What I am going to do here is add a simple media query to the style sheet of this theme.
00:27So, I'll go to where I have my theme stored inside my WordPress installation.
00:32It's under the WordPress installation, under wp-content/themes/Anaximander.
00:39If I scroll down here, we have the style.css file.
00:42I am going to open this in my text editor, which is Notepad++.
00:46You can open it in any text editor;
00:48it doesn't really make any difference.
00:50What I am going to do here is add a simple media query all the way at the bottom
00:55of the document, and that media query is going to change the font-family on the
01:00entire site when the screen is smaller than a certain size.
01:03The first thing I need to do in a media query is to start a media query.
01:07It starts with an @ sign and then you say media.
01:11This is a media query.
01:12That's what it looks like.
01:14Then you set out the conditions that you want to apply.
01:17So, I'll say only screen because it should only apply to screens,
01:21so not print for instance, or other things.
01:24And then I say "and" because there are multiple conditions here. And then I
01:28will say (max-width: 900px).
01:34So, what happens here is if the screen is a screen and the max-width is 900
01:40pixels, meaning if it's anything smaller than or 900 pixels, then the following
01:46style would kick in.
01:48Then I create my style. And the style will be body, because this is going to
01:53be for the entire site, bracket, and then I'll put-in the actual style code, font-family:
02:01Georgia, Times, Times New Roman, serif.
02:11So, this is a standard font- family that will change the font to the
02:15regular serif-type font.
02:16I'll close my style and then close by media query.
02:22And before I save it, I just want to show you what the site looks like right now.
02:25So this is the site.
02:27It has this specific Google font that we are using.
02:31And even if I resize it, you see that the font stays the same.
02:34Now let's see what happens when I save this.
02:39So, I save this new style sheet with the media query in it and reload my page,
02:47and then resize it again.
02:51And you see, just when we hit the 900 pixel point, the fonts change.
02:57If we look at the code inspector, you can actually see this change happen.
03:01If I go to Computed Style here, you'll see this line here, font-family:
03:05Georgia, Times, Times New Roman.
03:07If I pull it out, it pops to Open Sans, Arial, Helvetica; if I pull it back in
03:16again, it switches to Georgia.
03:17So, you see the media query kicks in and changes a value.
03:21What it's actually doing is serving up a new style that overrides the old style.
03:26So, you can use these types of media queries for pretty much anything in the style sheet.
03:31That means you can change colors, you can change font, you can change
03:34layout, any element.
03:36What you are doing is saying, in the following conditions, when you are on a
03:40screen and the screen is a certain size, use these styles.
03:44In any other situation, use other styles.
03:46Now that you see the structure of a simple media query, you will realize that to
03:51create media queries on your own, all you really need to know is CSS.
03:55If you want to beef up on your CSS before you continue, you can go check out one
04:00of the other CSS courses right here in the lynda.com online training library.
04:05As you can see, by using media queries, you can create different styles for your
04:08pages depending on what size window or device the visitor is using.
04:13This allows you to design a site layout for any screen size and any scenario.
Collapse this transcript
Exploring CSS3, progressive enhancement, and graceful degradation
00:00There's one caveat I have to point out before we begin for real.
00:04Media queries and responsive design in general rely on modern browsers to work properly.
00:10By that I mean if the visitor is using an old and outdated browser or an old
00:14computer, they may not get the responsive experience.
00:18In fact, they may get a very different experience altogether.
00:21This is something you have to keep in mind.
00:24The great thing is you can find out who will not see this responsive behavior.
00:28If you go to a site like caniuse.com, they keep track of all the browsers that are
00:35available and what kind of capabilities those browsers have.
00:38If you scroll down here under CSS, you'll find Media Queries as one of the options.
00:44And media queries is the most important component of responsive design.
00:47Looking at this table here, you'll see that everything in green means that media
00:52queries work and you'll also notice that the only browsers right now who do not
00:57support media queries are Internet Explorer 7 and Internet Explorer 8, and of
01:02course, Internet Explorer 6 and 5 and so on.
01:05This is important because if you know that your clients are mainly using
01:10Internet Explorer 8 or 7 and you cannot convince them to upgrade to a more
01:15recent browser, like Internet Explorer 9 or 10, then you may want to reconsider
01:20using media queries.
01:22But it also means that for every other device and every other browser, media
01:26queries are safe to use.
01:28My general attitude to this is rather than dumb down the site just so that
01:33people who use old versions of Internet Explorer can access the site properly,
01:39create a site that has a nice layout in the full size and then let that be the
01:43size that people see if they're using these older browsers, and then the
01:47responsiveness will kick in for everyone else.
01:49And here's the great thing:
01:51in almost every case the only people who will actually benefit from the
01:56responsive design are people who are on phones and tablets, and those devices
02:00already support this type of behavior.
02:02The people who can't access these responsive features are people who use
02:06regular old computers, and chances are they won't expect the site to be
02:10responsive anyway, so it doesn't really matter.
02:13The reason I'm showing you this site and giving you this heads-up is that this
02:18doesn't just apply to media queries;
02:20it applies to all modern web code. So if you want to build further on this
02:25course, or if you encounter strange behavior in the Anaximander theme in older
02:29browsers, you should go to caniuse.com and check that specific behavior and see
02:35if it's supported in the browser you're seeing the problem in.
02:38Chances are it's an old browser, and that's why it's not supported.
02:41And my attitude is, if you're on an old browser--and that I'm talking about a
02:46browser that's like three or four or five years old--
02:48I'm not going to design it for you, because then I'll hold the Internet back
02:52just so that you can get the proper experience. Harsh?
02:56Yes, but that's the only way we'll move forward with web standards.
03:00When you build web sites, you're always at the mercy of the browser used to
03:04visit those web sites, and that's a factor you have no control over.
03:08The best you can do is providing graceful degradation for older browsers or
03:12progressive enhancements for newer ones.
03:14The good thing is, for the most part, the issues surrounding smaller screens only
03:18show up in newer devices with modern browsers, so it's not that big of an issue.
03:23Even so, it's worth taking note of this issue, which is why I bring it up.
Collapse this transcript
Understanding best practices for media queries
00:00Media queries can be placed anywhere in a CSS document, or even in a separate CSS document.
00:06The question is where to place them.
00:08I'll give you some general ideas of different approaches and then you can fit
00:12those approaches to your situation.
00:14The most obvious place to put a media query would be right next to the
00:18actual style itself.
00:20So for example, if we want to change the background color of the
00:23pre-header in a certain size of window and nothing else, we might place the
00:28media query directly after the style that creates this background to begin with.
00:33So, I could place it right here.
00:35Here is a media query that is placed directly after the pre-header, and then all
00:40I have to do is create a new style. So, I'll grab the name of the original style,
00:45and paste it in, and I'll close my bracket, and then I just have to put in the
00:50extra code I need to change the background. So I'll say background: red.
00:53When I save that and load my page and then resize the page,
01:01you will see that when we get to that point, I am changing the background color.
01:06However, if I am going to make a lot of media queries, it's going to be really
01:10confusing if I need to edit them later if I have to keep going up and down in my
01:14document and try to find them.
01:16So if you have a lot of media queries that apply to one specific size, it's a
01:21better idea to bundle them all together and place them either at the top or the
01:26bottom of your document.
01:28So let's say I want to change both the pre-header and some other elements.
01:32I can then place one set of media queries here and then just bundle the
01:36elements underneath.
01:38So let's say I want to change the font-family of the body again.
01:46I'll save it, reload my browser to reload the media query, and now you see we are
01:51changing both the super header and the font at the same time.
01:55This is a strategy I personally use.
01:58I place all the styles from each query in a separate section at the bottom of
02:03the document. And you are going to ask, why do you place it at the bottom of the document?
02:07Well, the way I do this when I design responsive sites is, I first try to figure
02:11out, what is the most common way people are going to access this site?
02:15If the most common way is cell phones and small devices, then I write my main
02:20styles to fit that scenario.
02:22If the most common way of accessing the site is going to be on a large screen,
02:26then I write my main styles to fit the large screen. And then I stack the media
02:31queries at the bottom, because this is a cascading style sheet, meaning the
02:35cascade goes from the top to the bottom.
02:38So I'm assuming that the person visiting the site will use the styles on top and
02:43only in the cases where they use a different type of device will they get to the
02:47bottom and read that style instead.
02:49You could choose to put the media queries on top of the page, but if the page
02:53loads slowly, you may experience weird situations where the media queries will
02:57make the content pop in and out as it gets loaded, and that looks really weird.
03:02So my general suggestion is to place the media queries at the very bottom of the document.
03:07There is always also the option of placing media queries in a separate document,
03:11and I have seen a lot of people do this.
03:13They create an entirely separate style sheet that is only called if certain
03:17media query conditions apply.
03:19I think this is a really bad idea, and the reason for that is in those cases, you
03:24will then first load a regular style sheet and then the browser will load an
03:28entirely different style sheet and apply it on top of the regular one.
03:31If you are going to do that kind of approach, it's better to just put the styles
03:35directly into the main style sheet, and that way the browser doesn't have to
03:38load several different style sheets.
03:40As you can see, there are many approaches to where to place media queries.
03:44I like to organize them at the bottom of the style sheet so that they are at the
03:47end of the cascade and organize them from the biggest screen to the smallest
03:51screen, but that's just one option.
03:53Play around and see what works best for your situation.
Collapse this transcript
Creating a responsive frame
00:00When you start building a responsive theme the first thing you need to do is make sure
00:04the frame--so the outer edges of this entire site--is responsive so than when you resize
00:11the browser the content inside starts flowing instead of staying the same.
00:15As you can see from this demo, you'll see that currently, it does not flow.
00:19So this is not responsive frame.
00:22This also gives you a preview of the more in-depth media query tweaking we'll do for
00:26the rest of the course, and I'll also get a chance to show you how I used the Web Developer
00:31tools in Chrome to figure out what styles I need to tweak.
00:36To make the frame responsive, we need to work with the style.css file that's found inside our theme.
00:42So I'll go to my WordPress installation on my computer, go to wp-content/themes, then
00:49Anaximander, and then find style.css down here at the bottom, and I'll open that in my code editor.
00:56If we go back to the browser, I'll do right- click on some of the content in my page and select
01:01Inspect element to open the Code Inspector. And from here I can highlight different sections
01:07of the page using the selector on the left to figure out what's going on in the styles.
01:12And you can see right away that this id= wrapper is the one that contains all this content.
01:18And on the right here you can see width is set 1040 pixels, and this is what I need to change.
01:24I can experiment with this by simply editing it right inside the browser.
01:28So I'll edit it to max-width instead and see what happens.
01:34Now that I've set it to max-width, you'll notice that as I resize the frame, the content starts moving around.
01:44That's because we're no longer working with a fixed width;
01:47we're working with a variable width that just has a max-width on it.
01:51So that's the first thing I need to do.
01:53I'll go in to my style sheet, find the wrapper style--
01:58it's right here--and change width to max-width.
02:01Save it. I'll reload my page, just to make sure this worked, and when I resize it now,
02:10you'll see that content reformats.
02:13That's exactly what I want.
02:14The next step is to create stepped stages.
02:17When I showed you the fully built-out version of Aximander, instead of having the whole site
02:22be flexible, we have predefined stepped regions that will pop in and become smaller.
02:28So what we need to do now is define those regions, and we're going to do that with media queries.
02:33So I'll go back into a stylesheet, scroll all the way to the bottom where I want to
02:37put the media queries, and then I'm going to put in some code.
02:40Now if you want to follow along, you can use the code_snippets found in exercise files.
02:44So I'll open that on the side here, and you'll see right here at the top we have Media Queries Aplenty.
02:52So I'll take that and paste it in.
02:54This is just a comment so that I know what's going on.
02:57Then I'm going to make my first media query.
02:59So I'll say @media only screen, and then I'll set it to and (min-width : 720px) and (max-width : 1039px).
03:20I'll just close my query and then you're going to ask, where do you get these values from?
03:25720 pixels and 1039, that seem kind of random, right?
03:29Well, if we look at the site and I select anywhere inside here and use the Inspect element
03:34function, you'll see that if we go up to wrapper, you'll see that this wrapper is exactly 1040 pixels wide.
03:45So the reason I've picked 1039 pixels is because I want this is to trigger when the window
03:51is smaller than 140.
03:53The reason why I set 720 is because the next step in the sequence will be exactly 720 pixels wide.
04:01So what I'm saying is if the screen is between 720 pixels and 1039 pixels then do the following queries.
04:08Now you'll remember we used the wrapper to control the size.
04:13So let's see what happens if I change this max-width from 1040 to 720px.
04:23Now, you see the width is much smaller, but the layout still okay, because we still have
04:27a sidebar on the right and the content on the left, and they have equal size.
04:33That means this is a size I want.
04:35So what I need to do now is create a new wrapper style inside my media query that has a width of 720 pixels.
04:43So I'll go ahead and say wrapper width 720 pixels, and I'll close that style.
04:54I'll save the style sheet, go back to my browser and reload it,
04:59and now at full width, we see that 1040 width, and if I scale it down, exactly when we hit
05:08the 1040 mark, the page pops in to 720.
Collapse this transcript
Customizing media queries with the Chrome Developer Tools
00:00In the previous movie we made a small change to the style sheet of the theme to make the
00:04frame responsive, and we then added a breakpoint so that as the window gets smaller than the
00:10predefined original size, the layout changes to a smaller size overall.
00:17Now we need to add additional media queries to deal with the content once the window gets
00:21smaller than this breakpoint.
00:24If I reduce the width of this window even more, you'll see that the margins, these large
00:30areas above and to the side of the content, are starting to take up a proportionally large
00:35amount of space in the layout. And if I go to my original designs, you'll see that whereas
00:41on screens we have a lot of space and the margins, on the small screens we've very little margins.
00:49And that's simply because I want to create as much space as possible for the content.
00:53So what we need to do now is figure out what styles we need to address to change these
00:58margins to become smaller so we have more space for our content.
01:03The first thing I'm going to do is create another media query, because the first media
01:06query we made addresses all the content that's between 720 pixels and 1039 pixels wide.
01:14So what I'm going to do is create another media query for screens that addresses any
01:19screen that is below 720 pixels wide.
01:24So I'll say max-width : 719 pixels.
01:33That way this query will kick in once we're below 720 pixels.
01:38I'll with an end bracket, and then we can add our new styles inside this query.
01:43Now we need to figure out what styles we're going to change.
01:46So I'll go back to my site and then use the Google Webmaster tools to select the elements
01:51I think we'll need to change.
01:53I'm looking at the pre-header, and I'm seeing that there's a lot of extra space here on the sides,
01:58so there's must be a margin set somewhere.
02:01So I'll select it, and I can see that there's a margin there. And when I go down here to
02:05my tool, I see that the I pre-header padding is set to zero pixels for top and bottom and
02:1250 pixels for left and right.
02:14So I need to change that.
02:16I'll go into media query, say #pre-header, and then I'll set it to #pre-header, and I'll
02:26set its padding to 0 pixels for top and bottom and 20 pixels for left and right.
02:33Close my new style, save the stylesheet, reload the page, and now you see we have a 20-pixel padding instead.
02:41If I hover over the element, you see the green areas on the left and on the right, but you
02:46also see that this search box still floating in.
02:49So that means there's something else going on with the search box.
02:52So I'll select the search box this time and then move up to the highest containing element
02:58and here I find that it's set to right 50 pixels.
03:02So again, I'm going to grab the style, header-search, and then I'll change the value--in this case right : 50px--
03:14so I'll change that to right : 20px, close my style, save the style sheet, reload the
03:22page, and now the search box also shifted in properly.
03:26Before I finish with the pre-header, I want to see what happens if I make the screen even smaller.
03:32If I make the screen smaller, you'll see at some point the search bar covers the social
03:37links and the search word actually disappears altogether.
03:40So what I need to do now, before moving on, is reduce the overall width of the search bar.
03:45So I'll select the search bar again, and here I see #header-search #searchform input# and
03:51then a width of 275 pixels.
03:54So if I go in here and experiment and change it to 175, I see that it now seems to work much better.
04:01So what I need to do is copy out the style, add it to by media query, save it, reload
04:10the page again, and now you see that as we scale up and down, the pre-header changes to
04:17fit the new layout.
04:21Using the same method, by looking out the elements with the Inspector and then figuring out what
04:26styles are causing these elements to shift in and out,
04:30you can now reformat the style code so that the entire page will fit with the design.
04:37Instead of forcing you to watch me as I identify each individual style and then make changes
04:41to them in my style sheet, I've created a code snippets document you can use to copy out the necessary
04:47styles for this media query.
04:49You'll find it in the exercise file.
04:52It's called code_snippets.
04:55If you scroll down here to 04.05, you'll find the entire media query.
04:59It starts with @media and goes down to END max -width : 719px. Simply copy it and replace
05:07this media query we just created, save it, and when you now reload the page again, you'll
05:16see that now all the content aligns with a 20-pixel margin instead of the previous 50 pixel margin.
05:21And as we scale the window up, the 50-pixel margin appears once the screen is big enough to handle it.
Collapse this transcript
Taking device width into account
00:00At this point, I can pretty much guess what's going to happen.
00:03You are going to take your new media queries in your new theme, upload them to your server,
00:09open the site on your phone, and notice that nothing happens.
00:13Well, that's because we haven't quite gotten to the point where our media queries interact
00:18with the phone yet.
00:19And that is because cell phones lie. That's the reality.
00:23They lie to the browser and to the web sites about what they are.
00:28A cell phone browser pretends to be a desktop browser, and unless you explicitly ask the
00:34cell phone browser, hey! How big is your screen?
00:38the web site will have no idea how big that screen is and it will assume that this is a desktop.
00:43As a result, you will see the full-width content.
00:46So what we need to do is make our web site ask the device how big the screen is. Fortunately.
00:53It's very easy to do that.
00:55All we need to do is go into our theme, open the header.php file, and add one line of code.
01:02So I'll go and add it right here on top of title.
01:06I'll put in a comment to see what it is for later, and then I'll put in a new meta tag. So I'll say
01:12<meta name="viewport".
01:18So this will now track the viewport, which is basically the screen, and then the content
01:24of this meta tag will be the device width, along with some other information.
01:29So we'll say content="width=device-width".
01:37I'll close the meta tag, and what happens now is this page will ask the device hey!
01:43What's your width?
01:44And then we will set the default width to that width, which means we now know how big
01:48the screen is, so that if the screen is smaller than 720 pixels, the correct media query will kick in.
01:55But there's one more thing we have to do.
01:57The problem with these cell phone browsers is, like I said, that they lie, and that means
02:03that we have to be extra careful about how they behave.
02:06You know how when you go to a web site, the cell phone browser has a tendency to try to
02:11zoom in on things?
02:12Well, you need to avoid that when you are using media queries because you've already
02:16accounted for the zooming in your media queries.
02:19So in addition to finding out the width of the device, we also want to tell it explicitly,
02:25do not scale my content. I know how to do it myself.
02:29So next to width=device-width, we are going to say comma, and then we are going to say
02:34initial-scale=1.0.
02:39That just means the initial scale of zooming is going to be a one-to-one ratio.
02:44So instead of seeing the site zoom all the way out, we are just going to see the site
02:49as it is supposed to be. And then we are also going to say maximum-scale=1.0.
02:57That way the phone will never try to scale anything up and finally, we are going to say
03:02user-scalable=no. Pretty self-explanatory.
03:07We are not letting the user use pinch and zoom to scale in and out.
03:12Now the meta tag is complete, and the cell phone can no longer lie to our web site about
03:17what it is, which means all our media queries will work.
03:21If you don't want to type this out, you can go to the code_snippets and you'll find that
03:26exact code right here.
03:27So you can just copy it from the code_ snippets file and paste it in if you want to.
03:32If you upload this new header.php file to your web server and then access your web site
03:38using your cell phone or tablet, you will see that now the media queries kick in the
03:43way they're supposed to and the site looks the way you want it to.
Collapse this transcript
5. Creating a Responsive Header
Resizing the site title and the description
00:00When creating a responsive theme, or any theme for that matter, I like to start from the
00:05top; that means starting with the header itself.
00:08The challenge with a header is that it usually has to contain multiple different components:
00:13the site title and tagline, usually a background image of some sort, and also the menu, like you see here.
00:21Your job is to make sure each element stacks up nicely, both to match your design and also
00:26to be the easy to understand and functional.
00:28Out of the box, Anaximander will have the site title and tagline, and then the menu should
00:33appear directly under it.
00:35In some cases, in some browsers, the menu will appear shifted to the right here, and
00:40that's fine, because we're going to be working on realigning the header right now to make
00:45it match the design.
00:47If we look at the design, you see that the title and tagline is shifted to the left and
00:52the menu is shifted all the way to the right, and that's what we want to achieve.
00:57If we were working with a static site, this would be very simple.
01:00We'd simply float the title and tagline to the left, and then we'd float the menu to the right.
01:06However, because this is a responsive site, we have to be a little careful about what
01:11we were doing here.
01:13First of all, we have to use percentages to decide the area that the title and tagline
01:18is going to fit and the area that's going to be occupied by the menu so that as the
01:23window decreases in width, the proportion of area occupied by the title and tagline
01:30in the menu remains the same even though the pixel widths change.
01:34But there's an added challenge here.
01:36Like I said, the logical thing to do here would be to simply float one component to
01:41the left and the other to the right.
01:43What you want to end up with is an area occupied by the title and tagline and another area
01:48occupied by the menu.
01:50However, if you float this content, what will happen is the browser will assign vertical
01:55space for each of these items, and it will say, as long as the item on the right has
02:01smaller or the same vertical space as the item on the left, they should float left and right.
02:08However, if the item on the right occupies more vertical space than the item on the left,
02:15then the item on the right is allowed to float in under the item on the left, and you end up with this.
02:22As a result, while a short menu will look fine because it floats properly, a long menu
02:29will float in underneath the header, and that's not what we want to do.
02:34We want to ensure that even in the case where the menu is very long, it will stay on its
02:39side of the layout.
02:42To do that, we have to be creative.
02:44What we need to do is float the entire container that contains both, the title, the tagline
02:50and the menu to this right space.
02:55Then we're going to use a negative margin to float the title and tagline outside of
03:00that space and place it to the left.
03:04By doing that, we're ensuring that no matter what happens, the menu will continue to contain
03:09the same horizontal space and title and tagline will always stay on the left.
03:16To do this, we need to find out what percentage of area the title and tagline is covering
03:21and what percentage of area the menu is covering.
03:23There is a math formula for this.
03:26What you do is you find the width that you want, so that's the short width.
03:31You multiply that by 100 and then you divide the result by the total width of the entire area.
03:37This will give you the percentage of the shorter width in relation to the full width.
03:42If we look at our design and look at the guides, we'll see that the total width for the entire
03:46area is 940 pixels, and the area we want to be occupied by the title and tagline is 320 pixels.
03:56So the first thing we'll do is take the container that contains all the elements and shift it
04:01to the right by giving it a margin to the left.
04:05So first, we'll find out what container that is.
04:07I'll use the Web Developer tools to identify the container.
04:11If I just scroll up here, you'll see. Here it is, header-container, and then I have to
04:15find the percentage.
04:16I will go to my style sheet and find the header-container. Here it is.
04:25Then I want to set margin-left and now I need the percentage.
04:31So I'm going to open my calculator and then I want 320 pixels, which is the short width.
04:36I'm going to multiply it by 100, and then I'm going to divide that by the total width, which is 940 pixels.
04:43We get 34.04, et cetera, et cetera.
04:46So I'm going to copy that entire value, paste it in, percentage, and close the style.
04:53When I save that and reload my page, you will see that all the content is now shifted over to the right.
05:01The next thing I need to do is move this title and tagline back over to the left.
05:06However, now we're working with a smaller total width.
05:10So what I need to do now is find out the percentage width that is equivalent to 320 pixels out
05:16of this new area.
05:19This new area, if you look at it, is 620 pixels.
05:24So I'll go and do the math again, this time 320*100/620.
05:31This gives me 51.61, et cetera.
05:34I'll copy that, and then I need to apply that to just this header section.
05:39So I'll go in, use my Web Developer tools to find out what the section is.
05:44It's called id="header-title".
05:45Go back into my Style Sheet, find #header- title, and say margin-left again. And this time I
05:53want to do a negative margin because I want to shift it outside the box,
05:56so I'll say minus and paste in the percentage.
06:00I'll save it, reload the page, and you see now my title is on the left and my menu is
06:07on the right; however, the title is knocking the menu down.
06:12To avoid this, I'm going to float the title to the left.
06:16So I'll go float: left.
06:18Save it, reload it, and now the title and tagline are on the left and aligning with the menu.
06:27But there is one more thing I need to do.
06:29When we resize this window, you'll see that when we get down to a certain point, it looks
06:34like that effect I was talking about where the content is floating underneath happens,
06:39even though it's not supposed to.
06:41But that's not the case. What we're seeing here is the title doesn't have a percentage
06:47width yet, so therefore it's pushing the content in the menu to the right.
06:51The final thing we need to do is to give this menu area the correct percentage width.
06:57That percentage width is the percentage width of this area in comparison to the total area.
07:06In other words, the menu takes up 51.61, et cetera, percentage and we want the other half
07:14of that equation.
07:15So I'll simply take that number,
07:17I'll go to my calculator and say 100-, and I'll paste in that number, and I get 48.387, et cetera.
07:25I'll copy out that value and set the header-title to have a width of that percentage.
07:33I'll save that, reload the page one last time, and now when we resize the window again, you'll
07:41see that the title and tagline align to keep within its space, while the menu aligns to
07:49keep within its space.
07:52As you can see, working with responsive layouts is challenging because you now have to work
07:57with percentages, and you also have to have an intricate understanding of how CSS works
08:02and the different quirks CSS has to offer.
08:05The good thing is, once you know the tricks, like this using a negative margin to assign
08:10space for certain content, you're able to create layouts the way you want them to, without
08:15too much hassle.
08:17We'll be using this same technique further into the course to handle the general content.
Collapse this transcript
Adding media queries to the header
00:00We have now created a header that's responsive in that it resizes and realigns as we reduce
00:06the width of our window.
00:08However, if we make the window small enough, it starts looking really strange.
00:13That's because we've yet to add media queries to make sure that as we hit certain points
00:18in our layout, the menu will also respond to that point and change so that the layout is more useful.
00:25What we want to do here is create a situation where when the width of the window is small
00:30enough, the menu ceases to stay on the right, and instead moves in underneath the title.
00:36That will make it more accessible, and we'll avoid this squishing effect that you see here.
00:44To do that, we need to add media queries.
00:46So let's figure out what we want to do first.
00:49In the previous movie, we used percentages to move the entire header container to the
00:54right and then shift the title and tagline to the left.
00:58So logic dictates that if we reset these values, we will end up with a situation where the
01:03title and tag line is on the top and the menu's on the bottom.
01:08If you look at our style sheet, you'll see that here we have the header-container and
01:12the header-title styled inside the main style sheet.
01:15So if we simply copy these elements out and go all the way down to our media queries,
01:21we can add these styles in to the media query we want to.
01:25In this case we want the breakpoint to happen at 719 pixels.
01:28If you remember, that's the second breakpoint where the layout goes from being fixed, like
01:34this, to fluid, like this.
01:39To reset the menu layout, I'm going to go in here to my media query and find a logical
01:44place to place it, so I'll place these styles directly after header_margin.
01:49I'll paste in the original styles first and just do some cleanup so that it looks nice.
01:58And then what I want to do is reset these values to default, or just 0.
02:02So I'll reset margin-left to 0 and this margin to 0 and I'll say float: none, and reset the width to 100%.
02:16I'll save this and when I reload my page now, you'll see that the menu is underneath the
02:24title and tag line, and if I scale the window up, the menu appears on the right-hand side.
02:30So a simple media query was all that was needed to reset the layout back to what it was originally,
02:37which is what we wanted.
Collapse this transcript
6. Creating a Responsive Menu
Making the menu responsive
00:00Now the menu is responsive and it lays out nicely for all screen sizes using media queries,
00:06but it's still not quite the same as the design.
00:10In particular, the menu isn't aligning quite the way I originally intended.
00:15If you look at the menu here, you see that although it stays on the right-hand side of
00:19the screen, it's still shifted to the left of the container, because container ends right
00:24here and that's where the menu is shifted.
00:27In the design, I wanted the menu to shift all the way to the right.
00:31To figure out how to fix this, I'm going to use the Developer tools in Chrome to identify
00:36the styles that are affecting the menu.
00:38And you'll see here there are quite a few containers that handle the menu. The top container
00:44is this one, id- access, and if I select it, you'll see that it says display: block and margin-top 10px.
00:51So what happens if I float the menu to the right?
00:54I'll simply add that here, float: right.
00:57And you see that now the menu floats to the right, almost, because there's still space
01:03here, which means there's something else that's creating a space.
01:07And just looking at how the menu behaves, I can tell that there's probably a margin
01:10here somewhere. So, I'll right-click on one of the elements, and you see the margin is
01:15not in the anchor element, so it must be in the list element. And here it is.
01:20You see it says, float: left, which is what floats menu so that it become a horizontal
01:25menu rather than a vertical menu.
01:27You can see if I turn it off, it becomes a vertical menu.
01:31And then it says margin: top 0, right 2em, bottom 0, left 0.
01:39So what I want to do is take that 2em and place it out at the end so that it becomes
01:48top 0, right 0, bottom 0, left 2ems.
01:53And now you see the menu stacks the way it's supposed to, which means I can implement those
01:58two styles in my style sheet.
02:00So I go to my style sheet, I'll find access, then here I will first float access to the
02:10right. And then I have to find access li and change it so that it says 0, 0, 0, 2em. Save
02:21that, reload the page, and now we have the menu layout we want--almost, because, if I
02:29resize this window now, you'll see that when the media query kicks in, the menu is still
02:38flowing to the right, and that looks weird.
02:40It should be floating to the left at this point.
02:43So all we need to do is reset the menu styling in the media query.
02:47Now, remember what I changed here. I set access to float right, and in access li, I changed the margin.
02:55So logically what I need to do is go down to my media queries and here, I'll set access
03:04to float: none, and then I'll set access li margin: 0 2em 0 0.
03:18So I'm basically setting everything back to the way it was before.
03:22Now I save this and reload the page.
03:26You'll see the menu aligns to the left when the media query has kicked in and when we
03:32scroll out, it aligns again to the right.
Collapse this transcript
Creating a different menu design for small screens
00:00When dealing with responsive menus, especially for smaller devices like phones and tablets,
00:06you may need to think of providing alternate styles rather than just resizing or reorganizing of layouts.
00:12Even though a menu might be easy to click on with a mouse, it might be harder to click
00:16on when you use your thumb or your finger on a touchscreen device because the menus
00:21generally are really small when you have them on a regular screen.
00:25If you look at the original design, you will see that I intended the menus on smaller screens
00:29to be these large buttons that are below the main header so that they're separated clearly
00:36menu items and they are easier to click on.
00:39So what I want to do is create new styles in the media queries that completely restyle
00:44the menu in the page.
00:46I will show you how I do this by using the Chrome Developer Tools.
00:51By using the Chrome Developer Tools, I'm able to experiment with the live code on the page
00:56and then I can change only those elements that need to be changed and nothing else, to
01:00make my style code as short as possible.
01:03The first thing I am going to do is reduce the width of the screen until I hit that point
01:07where I want things to change.
01:10Then I'll use the Developer Tools to identify the area I want to change.
01:14The first thing I want to do here is change the background color to white, because I know
01:19that I want all this area to be white.
01:22So I will go and identify the entire area, which is the ID access down here, and then
01:27I see the media query has already kicked in here, so that's where I want to add my changes.
01:32The first thing I want to do is add a new background color,
01:35so I will say background:fff, which is white. And now I see the white kicks in;
01:40however, the menu is floating in the space sort of.
01:45I want the menu to span all the way across and all the way to the bottom.
01:49The reason why it's floating in space is because the menu is further contained by this divider
01:57that's called header_margin, and header_margin adds 20 pixel padding around the whole header.
02:05So what I need to do is override what header margin is doing.
02:09So I will go back to access and I'll add some extra style code.
02:12I will say margin and then I first want to add some extra space at the top,
02:18so I will say 20 pixels. And then I'll set the right bottom and left margins to -20 pixels
02:26to override the styling from header margin.
02:28So I will say -20 pixels for the right, -20 pixels for the bottom, and -20 pixels for left.
02:36And now you see the menu appears to hover underneath the main header, even though it
02:41technically is still part of the main header. And of course, you can't see the buttons right
02:45now, because they are white, and we will get to that in a bit,
02:48but first I want to save what I just did in my actual style sheets.
02:52I will highlight it here.
02:54I will go to my style sheet and find that style. It's all the way here at the bottom, inside
03:00the media query for 719. And here we have access.
03:05So I will simply replace access with the new code I created, clean it up a bit, and then I
03:12will also make a comment for myself so I know what's going on here.
03:17Save it, reload the page, and now the menu is at the bottom.
03:23The next thing I need to do is make these menu items visible again,
03:27so I will select one of the menu items and then look at the styling that affects it, and
03:31I see here, under access a, that the color is set to white.
03:36So if I just go and change that to #333, for example, I get this nice gray color instead,
03:42so that's probably what I need to do.
03:45In addition, if we look at the design, you will see that these menu items have padding
03:50on the top and on the sides.
03:53So I will go and add that padding.
03:54So I will change the padding here from what it is, to 10 pixels on the top and bottom and
04:0125 pixels on the left and right.
04:04So now you see my menu items are much larger.
04:07So before I do anything else, I am just going to save this.
04:09I am going to create a new style called access a, with a new padding and a new color.
04:13So I go down here again, add access a, padding: 10 pixel, 25 pixel, color: #333333, and close the style.
04:32I will save that, reload the page to make sure it works, and now we have those elements
04:38in the right color.
04:39However, you see that the hover state is white, and we don't want that. So, I am going to go
04:44back and add that hover state as well.
04:47So I will go access li:hover > a, and access li:hover > focus, then set the color to #000, which is black.
05:09Now you may wonder why I am using this code, access li:hover, to a, instead of saying access li: hover.
05:19That's because in this theme you have a dropdown menu, and you want the style to also take effect
05:24if the dropdown menu is activated.
05:26So I will save this, reload the page again, and now you see, if I hover over the menu,
05:35you get a subtle difference in the color of each of the items.
05:38Going back to the design reference again, we see that we also have a border around each item.
05:44So I will go back, and I will find the list item, and I see that in addition to adding
05:47the border, I also have to get rid of that extra margin.
05:51So first I will set the margin to 0.
05:55That way each of the menu items stacks closer together.
05:58And then I will set the border-right to 1px solid, and the color #e5e5e5, and I'll copy that style out and
06:12set border-bottom to the same thing.
06:18Now you see we have a nice clean menu that appears where it is supposed to and that also
06:23reorganizes as we scale the screen up and down.
06:26So I will copy out the style, find access li:, and replace it, clean up a bit, save it,
06:39reload the page, and here we have a fully functional responsive menu that only pops
06:47in when the screen gets small enough.
06:50Now, there are a couple of additional things you need to do to make this menu responsive.
06:54In particular, you also need to address the dropdown elements.
06:57So what I've done is, in the exercise files for this course, in the code_snippets, you
07:02will find the complete code snippet for this movie so that you can add all the additional styles.
07:08It's not that many, but there is a couple.
07:10So if you go down to movie 6.2 and you simply add in all the style code here, jump menu
07:19down, all the way down to access ul ul, and replace what you just did, save it, and reload
07:29the page, you will get the same result and now the menu will work perfectly in all scenarios.
07:35By providing a separate style set for smaller screens, you can create a whole new look and
07:40functionality for the menu without having to touch the underlying markup in the process.
07:44Pretty nifty, right?
Collapse this transcript
7. Creating a Responsive Main Area
Making a responsive single-post layout
00:00Now that we have a responsive header, it's time to move on to creating responsive layouts
00:05for our content, and we'll start with the single post.
00:08If I go to a single post, you see that we have the layout that's based on the small screens.
00:15We have the title and meta information on top and then we have the content underneath.
00:19But on this large screen, that doesn't look very good, because we have way too much space
00:23for these lines of text.
00:25And when you get text that is above a certain number of words per line, it becomes very hard to read.
00:31That's the reason why, in the original design, I created this layout with the header and
00:36meta content on the left and the main content on the right.
00:39So, what we are going to do now is use that same technique we used to create that responsive
00:44header to change the layout of this main content area to make it flow the way it's supposed to.
00:51So we'll use that same math formula I showed earlier to find the correct percentages so
00:56that we can lay this out.
00:58First, we need to identify the elements we are going to move.
01:02You'll remember what we want to do here is first take all the content on the page and
01:07shift it to the right so that it only occupies this section of the page.
01:13So, the first thing we need to do is find that container.
01:16I'll go to my browser and use the Developer Tools to find what container contains everything.
01:23And you see here we have three main elements for the single post: we have a header, we
01:29have the entry-content, and we have a footer.
01:32And all of those three elements are contained within this article with a class post-container.
01:37So this is the element we want to shift to the right first.
01:40I'll go to my style sheet and find the main content section, and here, I'll add in a new
01:45style for a post container.
01:52This style is going to have a margin left, and now I just need to find out what that value is.
01:58So, I'll go back to my design.
02:00I'll find the total width, which is 940 pixels, and then I'll find the width of the margin
02:07we are going to add, and that margin is 320 pixels.
02:11So, in my calculator, I go 320 times 100 divided by 940.
02:18That gives me 34.04, et cetera. I'll copy that and paste it in as a percentage.
02:25Now, you'll notice I am not rounding this out because computers are incredibly good
02:30at math, so there's no point in rounding it out.
02:32Ideally, you'd even have a longer percentage than this, but that's what the calculator
02:36gave me, so that's what I am using.
02:38I'll save this, and when I reload my browser, you'll see the content shift to the right
02:44like we want it to.
02:45Now, I want to shift the header-content back to the left.
02:49So first, I'll identify the header-content container, and you see here, it's called header
02:54class entry-header.
02:56So I'll create that style.
02:59And I know that in this case I want to have a margin-left
03:04that is negative, and I just have to find that value.
03:08Going back to the design, I know that I want to shift it the equivalent of 320 pixels to the left,
03:15but now that percentage has to be relative to the total width of just this area, which is 620 pixels.
03:22So, I'll go back to my calculator and go 320 times 100 divided by 620.
03:29This gives me 51.61, et cetera.
03:32I'll copy that, I'll paste it in, make it a percentage, and then I also want the entry
03:38header to float left,
03:39so I'll say float: left. And when I save this and reload my browser, the header is now floating to the left.
03:49However, you also notice that it's shifting everything down, and not only that, but it's
03:53spanning into an area it's not supposed to be in.
03:56That's because we haven't yet defined a width for the header, so it's taking up 100% of the space.
04:02The final thing we need to do is go back to our design and look at what the total width
04:06of this header is supposed to be.
04:08You'll see here that we only have this much space for the header, because I left a 20-
04:13pixel gutter between the header and the rest of the content, so that it doesn't clash into each other.
04:18So, what we want to do is find the percentage equivalent of this width, which is 300 pixels,
04:25in relation to the total width, which we've defined here as 620 pixels.
04:31Back to the calculator. We go 300 pixels times 100 divided by 620.
04:37We get 48.38, et cetera.
04:40I'll copy that and go width 48 point, et cetera, percentage, save, reload it in the browser,
04:53and now it lines up properly.
04:56More importantly, it will also scale down.
04:58So when I change the size of this window and we ignore the image for now, you'll see that
05:03these two elements size proportionately when the scale changes.
05:08The final thing we need to do is add the media queries so that when we get below that breakpoint,
05:13everything realigns, so we get the title and everything on top and the content below it.
05:18So I'll go in here and copy out the styles I just created, go down to my media queries,
05:24and find the media queries for the breakpoint, which is here, at 719 pixels.
05:31We'll scroll down and find a logical place to place it, which would be right here.
05:37And I'll paste in my current styles, do some cleanup, and then I'll just reset everything.
05:47So, the margin-left set to zero, margin-left set to zero, float set to none, and finally,
05:56the width set to 100%.
06:00Save that, reload the browser again, and everything snaps into place.
06:06Small screens, larger screens.
Collapse this transcript
Making images responsive
00:00Once you've made the main content responsive, you have to make sure that the images also
00:05respond to the size of the screen or window; otherwise, you get a weird effect where the
00:09image starts spilling out over the edges.
00:12You can see it here, when I start resizing this window, that the image literally spills
00:17out over the edge, and if I go down further, you'll see that we start cropping the image
00:23with the edge of the window.
00:24That is not an ideal situation, and we need to fix it.
00:28But there's an added challenge.
00:29In WordPress, the author can insert images in many different ways.
00:33From the top here, you see the first image is inserted with a style called None, which
00:38means it just flows with the rest of the content.
00:41Below that, we have an image aligned to the left, we have one aligned to the center, and
00:47we have aligned to the right, and finally, we have an image that has a caption on it,
00:51and we need to account for all of these when we create image styles.
00:57Fortunately, there's a relatively easy way of making images responsive.
01:03What you have to do is tell the browser to set the width of the image to 100% of the containing box.
01:11So in this case the containing box for the image is the container that contains all the content here.
01:16We can see it here; it would be the entry-content. So the ideal situation would be that as the
01:22window is resized, the image is scaled to fit within that container.
01:28To do that, we are going to use a style code called max-width.
01:32So I'll go into my style sheet, and I'll find the Image section, and first, I am simply going
01:37to apply a style that applies all images.
01:39So I'll just say image and I'll set max-width to 100%.
01:46That means the image, at the biggest, will be 100% of the containing container. And then
01:51I'll also set height to auto.
01:56By setting height to auto, I am telling the browser to automatically resize the height
02:00of the image to match in proportion to the width, so that the image doesn't get stretched
02:05and start looking weird.
02:07When I save this and reload my browser, you'll see, at full resolution it makes no difference,
02:13but as we scale the window down, you'll notice that the images now scale along with the
02:18rest of the content.
02:25That is, with one exception.
02:26If we scroll down to the bottom, you'll see that my caption image still stays the same
02:32size, and we're still covering it.
02:34And that's what I was talking about.
02:36In WordPress we have to account for many different scenarios.
02:40So what we need to do here is add in some specific styles that capture all of these
02:44different scenarios.
02:46If you go to the code snippets file for this course, you'll find that entire stack of code
02:51here, at the very top, under 07.02.
02:53If you copy that out and paste it in and place it with this simple image call and save it, you'll
02:59see that now everything sizes, including the captioned image.
03:02If we go back and look at the code, you see it's still the same code, max-width: 100%,
03:06height: auto, and it even has an explanation here of what's going on.
03:11And what we're doing is simply applying the style to specific types of images.
03:16Size-auto, size-full, size-large, size-medium, these are all the standard sizes in WordPress.
03:22We also assign it to the attachment images, which is a different page that you can look at images.
03:27We assign it to images in sidebars and we assign it to images with captions.
03:32Before we finish up completely with images, I also want to account for certain specific
03:37scenarios where images are handled differently inside WordPress.
03:41So I've created another set of style code here that I can copy out from the code_snippets
03:45file and paste in, and this applies specifically to images that are aligned to the center and
03:52also to captioned images that are aligned to the center.
03:56What this style does is it sets the margin to auto on left and right so that the image
04:02truly aligns in the center, and also sets a bottom margin to create some extra space.
04:07Again, we're setting the max width to 100% and height to auto, so this is just a custom
04:12style specifically for these two scenarios.
04:16Save that, open it in the browser, and you now have a set of fully responsive images in your site.
04:26When building WordPress themes, it's important to cover all eventualities.
04:30By targeting each image insertion scenario, you end up with responsive images no matter
04:35where and how they are inserted.
Collapse this transcript
Making videos responsive by including FitVids
00:00Now that we've made the layout, and also the images, responsive, it's time to take a closer
00:05look at how to make responsive videos.
00:07Unlike images, you can't simply use CSS to make videos responsive, at least not the videos
00:13that you bring in from other sources, like YouTube and Vimeo and other video sources.
00:18If you look at the video we inserted here-- this is a simple YouTube video--you'll see
00:23that it looks fine in the full layout, but if I reduce the screen size, you'll see that
00:28the video is not responsive at all, which produces a very bad user experience.
00:33Just imagine visiting this on your cell phone.
00:36You can't even see the play button.
00:40But before we start talking about how to make the video responsive, let me just show you
00:44the easy way of adding videos to WordPress.
00:47If I have this video, Farm to Table, that's on Vimeo, or I have YouTube, all I have to do
00:53is create a new pst, give it a title, write some text, and then, making sure I'm in HTML
01:01mode, I'll all go to the video, copy out the URL--and you can do this in YouTube too--
01:08paste in the URL, give it a category--I'll set this under Student Work--and publish the story.
01:15What happens now is when we view the post, WordPress cuts a hole in our page, called an
01:24iFrame, and then goes to Vimeo and grabs the necessary content and then embeds it automatically.
01:30So, whereas in the past, we have to go and grab all the embed code and paste it in and work with it,
01:36now we can simply get for WordPress to do this automatically.
01:39However, just like with YouTube, this video, not responsive.
01:48The problem of videos not being responsive is not new, and fortunately, someone has already
01:53solved that for us.
01:55But to make that solution work, we have to include a JavaScript library specifically for that purpose.
02:01That JavaScript library is called FitVids, and you can find it under fitvids.js.com.
02:10To include FitVids into WordPress refers me to download the FitVids library, so I'll jump to GitHub.
02:17I'll find the file I want, this jquery.fitvids.js file.
02:22I'll open it and click on the Raw, so that we see just the file itself.
02:27And then, in my browser, I'll right-click on the page, go Save as, and then save the file
02:33in my theme. So I go to wp-content/themes/ Anaximander, and I find my js folder and I save it here.
02:42Now that we've saved the file inside our theme, we need to create a function that will call
02:47up the JavaScript library whenever the theme is used.
02:50That function will live in the functions.php file, inside our theme.
02:55So I'll open the functions.php file and I'll scroll all the way to the bottom, because
03:00that's where to want to add it.
03:01And then I'm going to add the new function here.
03:04Because this is fairly complex, I've a created a code snippet for you that you can copy and
03:08paste in, so we'll do that and then I'll explain what happens afterwards.
03:13First, copy from this top line here, down past add_action('init','anaximander_fitvids').
03:19I'll copy that piece and paste it in.
03:23If we look at this function, it's not as complicated as it looks.
03:26First, we're creating a function with the name anaximander_fitvids, and then down here, we're
03:31telling WordPress, add the anaximander_fitvids function on initiation.
03:38Inside the function, we have a conditional call here that says, this only happens if
03:43the current page is not an admin page, because we don't want to call fitvids when we're in the Admin panel.
03:49That's just a waste of resources.
03:51Then we get to the actual function.
03:53It consists of three elements.
03:55First, we register the script itself.
03:58We register it by giving it a name. So I called it fidvids; you can call it whenever you want.
04:03Then we tell WordPress where the script is.
04:06It's in the current template directory, and then it's under the folder js, and the file
04:12is call jquery.fitvids.js.
04:15We're also telling WordPress that this file is related to the jQuery library that WordPress ships with,
04:22so that now WordPress knows I'm calling this file and I also have to kick in jQuery itself.
04:30Once we've registered a script, we have to enqueue the script. Enqueing the script
04:35means we're telling WordPress, I want to actually see the script in my page.
04:40So here you see we say, wp_enqueue_script, and then we call the name we just gave the script up here.
04:46If we took out the rest of this function right now and ran the script, you'd see that WordPress
04:51would call up jQuery and the FitVids library in the header of our page, but nothing would happen.
04:58We need to do two more things for this to work. First, we need to add a script that
05:02assigns the FitVids function to a specific class inside our document.
05:08This is the only way FitVids can know which content is a video and where it should be applied.
05:13So here we have a simple JavaScript function that does just that.
05:18Then we create a function around that function that is called, so that we can actually inject
05:23this function into our theme.
05:26We call that function add_fitthem, and then up here we've create another action that's calling
05:31add_fitthem, so this function, in the head of the page.
05:36To see all this in action, save the functions.php file and reload your page.
05:42If you're in Chrome, you can now hit Ctrl+U to see the code behind the page, and from here
05:49you can go Ctrl+F and search for FitVids and you'll see that we've two calls here.
05:54First, we have the actual library itself, this call right here.
05:58You see jquery.fidvids.js is being called. And then in addition, you'll see up here,
06:04we have that script that appends FitVids to the video class.
06:10However, if we resize this window, you will see that the video is still not responsive.
06:16But if you were paying close attention, you will know why.
06:19Remember how I said we're appending FidVids library to the video class?
06:24Well, right now that videos doesn't yet have a video class attached to it.
06:28To make that happen, we have to go and edit the post and wrap the string in a div with a class video.
06:39I'll update the page, view the post again, and if everything worked right, we should
06:47now have a responsive video. Look at that. Cool, eh?
06:55But I'm going to give you one more piece of code that will make this process even easier.
07:00As you saw, WordPress doesn't automatically append a video class to wrap around videos,
07:05so you have to do that manually.
07:07So what I've done is I've created a function that does just that for you, so you don't
07:11have to do it every time you add a video.
07:13If you copy this function here that's directly under the other function and paste it in directly
07:19under the other function, what this function does, as it says, if there is a video that's
07:25been inserted using that old embed method, I want you to wrap that video in a div that
07:32has the class video automatically.
07:33If we save this and go back to a post that already has a video--so I'll go back to Posts
07:42and find a post that has a video--and I simply update that post, because we have to now tell
07:48WordPress to reinitialize that video so that the new wrapper is wrapped around the video,
07:54and we go view this post, you'll see that this posts now also has a responsive video in it.
08:05Now that we've added FitVids into our theme and we've added that function that automatically
08:10wraps new videos in the div that has the class video, you'll have responsive videos throughout your site.
08:18Just keep in mind that this only applies to new videos you've added.
08:21If you've old videos on your site, you need to go back in to the Admin panel and just
08:25resave those posts that have oembed videos in them, so that WordPress wraps them in that
08:31necessary div or the class video.
Collapse this transcript
8. Creating a Responsive Sidebar
Making the sidebar responsive
00:00Most WordPress themes come with sidebars.
00:03I've started designing WordPress themes without sidebars, but every now and again you need them.
00:08For that reason, the Anaximander theme has a sidebar on the default index page.
00:13The sidebar poses some new challenges when it comes to responsive layouts.
00:17Do you shrink the size of the sidebar by percentages or do you leave it a fixed width?
00:23And if so, what happens when the sidebar gets bigger than the main content?
00:28My philosophy here is to leave the sidebar a fixed width until its width is equal to
00:33that of the main content.
00:35At that point, I either hide the sidebar or I shift it down below the rest of the content.
00:41If you look at how I laid out this page, you'll find something you may be surprised at.
00:47Instead of simply floating the content left and right, which is the common thing to do,
00:51I've decided to use a backwards version of the technique we used to create this responsive
00:56header and also the responsive single posts.
00:59If you look at this regular-index section, you'll see that here we have a margin on the
01:03right that's -320 pixels, and in regular- content, we have another margin of 320 pixels.
01:12So basically, what's happening is we are shifting the content to the right and then shifting
01:16it back to the left, leaving a large space in which the sidebar can slide in.
01:21So you see that space in this orange area here.
01:25That means if we want the content to span across the page and the sidebar to shift below
01:29the page, we need to reset these two styles.
01:33But first we need to decide where we want that breakpoint to occur.
01:36If I resize my browser here, you'll see that at the second breakpoint, we pretty much have
01:41that situation I was talking about, where the content and the sidebar are of equal width.
01:47That means I want to use that breakpoint as the breakpoint for moving the sidebar below the content.
01:53Knowing that, I'm going to go into my media queries and apply these specific styles.
01:58So I'll go to my style sheet, scroll all the way to the bottom, where I know my media query is.
02:03It's right here.
02:04It's the one for max-width : 719. Let's scroll to the bottom and here I'll create my new styles.
02:11So the first one was regular-index, and here I want to remove the float, so I'll float.
02:23And then I also want to reset the margin to 0.
02:26Then I have to apply the same approach to the regular content container,
02:29so again, I'll say margin: 0.
02:33So just to reiterate, what we're doing here is finding regular-index, and here you see
02:37we're currently floating regular-index to the left and the margin on the right is -320,
02:43so we are resetting regular-index to float: none and margin: 0, and then we are also resetting
02:51regular-content's margin to 0.
02:54I'll save this, reload this page in the browser, and you see the sidebar disappeared, and more
03:00importantly, when the window size increases, the sidebar reappears.
03:05But it didn't really disappear; the sidebar moved to the bottom of the page.
03:09So if I again make it disappear and scroll down, you'll see the sidebar is now here on the bottom.
03:14But you'll also see that the sidebar looks odd, because it's shifted to the right.
03:19What I have to do now is create new styles that affect the sidebar only in this situation
03:25so the sidebar looks nice when it's at the bottom of the page.
03:29If you go to the code_snippets for this course, you'll find all that style code.
03:33It's not all that much, so you just copy it, go back to your media query, and paste it in.
03:40When you save this and reload the page, you'll see we have a new nice sidebar area down here at the bottom
03:48that's formatted to fit better with a layout for being on the bottom.
03:52And if we increase the size of the window, we're back to the regular sidebar.
03:59Before I wrap up, let's just take a quick look at what we just pasted in.
04:02If I place my browser here on this side and we go down and take a look at the sidebar
04:07widgets, you'll see that the new sidebar widgets have a solid gray background, which you can
04:12find here, sidebar .widget, and the background is right here.
04:16They're also spanning across the entire width rather than floating to the right, and that's
04:21done here in the sidebar style.
04:23We set float to none, overflow to inherit, and then the width to 100%.
04:28You'll also notice that inside sidebar widget we now have width at inherit, which means
04:33this style is inheriting the width of the containing style, which is sidebar.
04:39In other words, your sidebar widgets are now 100% wide instead of having a fixed width,
04:44as they did before.
04:46The rest of the styling is made to match the footer widget styling that you see on the
04:50bottom of the page here.
04:52Sidebars require special consideration when designing responsive themes.
04:55I personally don't like flexible sidebars, so I showed you how to leave them with a fixed width.
05:01You can use the same technique, but with percentages to make the sidebars flexible if you like.
05:07There's no right or wrong here, so it's completely up to you.
Collapse this transcript
Hiding sidebars on mobile
00:00One common solution to dealing with sidebars on mobile devices is to simply hide them if
00:05the screen is too small.
00:07I tend to agree with the strategy, as stacking the sidebar underneath the main content can
00:12produce an awfully long page on a mobile phone, and you have to scroll a lot to get
00:16to where you want to go.
00:18But hiding content with media queries has a couple of unintended drawbacks.
00:22First, let's take a look at how you would hide the sidebar.
00:27In the previous movie, we used media queries to move the sidebar down to the bottom of
00:31the page when the window is too small to fit it,
00:34and we also added a bunch of extra styles to make the sidebar styling fit with the rest
00:39of the page when the sidebar is at the bottom. You can see it
00:43if you resize the window here. You will see the sidebar disappeared.
00:46When I scroll down, the sidebar now appears at the bottom.
00:51But if you don't want to show the sidebar at all, and you want to hide it using media
00:55queries, instead of adding all the extra styling, all you have to do is find the container--in
01:00this case id sidebar--and simply say display: none.
01:07When we reload this page now, you'll see that the sidebar is gone.
01:11It's not here on the bottom, and if we resize the window, the sidebar reappears.
01:18That also means all this extra style code that we added earlier is now just a waste
01:23of code, so you can remove it if you want to.
01:26But I said there were some unintended drawbacks of hiding the sidebar.
01:29You see, what happens when we use media query is is we're just changing the style code
01:34on the page; we're not changing the content itself.
01:38That means even if the sidebar is hidden, like what we see here, the sidebar is still
01:44there in the code; we're literally just not showing it.
01:48This may be a problem in some cases because sidebars often have large content in them.
01:54That large content may be ads from Google or another ad source, or images or videos or other things.
02:01And even though we're hiding it using CSS, all that content is still loaded into the phone
02:06or loaded into the tablet, and the browser is working hard to render all that information
02:11only to not display it.
02:14If you ask me, hiding a sidebar on mobile devices is a preferred option to avoid too much clutter.
02:20If you want to go this route, you just have to keep in mind that you're still downloading
02:24all the content that's in that sidebar.
02:27So don't put too much heavy material in the sidebar so that you're using up extra data
02:32to download something that you are not going to show.
Collapse this transcript
9. Creating a Responsive Footer
Dealing with footer widgets
00:00The footer of a web site is often left as unused real state.
00:04I find the footer to be quite useful for adding further information about the site and links
00:09to relevant content.
00:11To make it easy to configure the footer area, I usually add a set of optional widgets in the footer.
00:17You can see them down here at the bottom.
00:19I added four different widgets here--Archives, Categories, Recent Comments, and Recent Posts--
00:24but now that I've added the footer widgets, I need to make them responsive.
00:28If we look at this theme and resize the window, you'll see that as we resize it, the footer
00:34widgets automatically resize with it.
00:37The problem of course is that at some point the footer widgets become almost unreadable
00:42because they're squished so tight together.
00:43What we need to do is apply media queries to handle the footer widgets once the screen gets too narrow.
00:50And what I want to do here is simply box each widget in its on 100% width box and then stack
00:56them one after the other.
00:58If we look at the footer widgets for the Developer Tools, you'll see why they do what they do.
01:07Each of the widgets is contained inside a list item, and that list item has a percentage width,
01:13is floating to the left, and has a percentage margin to the right.
01:17As a result it'll rescale to a percentage of the total width of the screen.
01:22This also tells us the solution to our problem.
01:25To make the footer widgets stack one on top of the other, all we have to do is reset these three values.
01:31So I'll copy out the style, go into my style sheet, and down to my media queries at the bottom. I'll
01:36find the media query that affects all the content on the small screens,
01:41so this one at 719, and I'll go all the way to the bottom of that media query and paste
01:46in the style I just copied out from Chrome.
01:51Now all I have to do is reset the width.
01:54So I will set the width to 100%, because I want it to spend across the whole page.
01:58I'll set float to none, because I don't want to content to float, and I'll also set margin-right to 0.
02:06Save this, reload the page, scale it down, and you'll see right at the breakpoint the
02:18footer widgets will now stack one on top of the other.
02:21But I need to add one more piece of style, because as you can see, the footer widgets
02:25don't have any space between them,
02:27so it's hard to see where one ends and the next begins.
02:30So I'll go back in to my styles and margin bottom 2em, save it, reload it, and now there's
02:39a nice little space between each of the widgets.
02:41Of course, you can choose to change that value if you want to, but I think that the 2em value works fine.
02:47But there's one other piece of footer content in the site that you haven't seen yet.
02:55In addition to the footer widgets, there's some content that only kicks in at certain times.
03:00In this post, you'll see it. It's right here at the bottom.
03:04We see three related posts.
03:06This is a custom function that's built into the theme that looks for three related posts
03:12to the current article based on when they were posted and based on the categories that
03:16are in this article.
03:17If there are not enough related posts then you'd simply don't see this box.
03:21And just like with the footer widgets, these boxes are responsive, so they resize with the
03:26window, but we've to give them a breakpoint too where we want to stack them one on top of the other.
03:34That means we first need to find out how they work,
03:36so we use the Developer Tools to look at them, and we'll see, just like the footer widgets,
03:42each of these boxes is contained within a list item in an unordered list, and each has
03:47a percentage width floating to the left and a percentage margin to the right.
03:52So again, we copy out the style, paste it in, clean it up, and then we set the width to 100%,
04:08float to none, and the margin-right to 0, and finally, we should give it a margin on the bottom too.
04:14So if I set the margin-bottom to 2em.
04:20Reload the page and now each of items stacks one on top of the other.
04:27Now if you want to be really clever, you'll notice that these two styles had the exact
04:33same style code, which means we don't have to repeat them like this.
04:37We can simply take the name, insert a comma in front of the first one, paste in the second
04:43one, and then we can delete the style, and now the footer sidebar list items and the
04:50related list items are handled by the same style.
04:53Shorter code does the exact same thing.
04:56Making repeated side-by-side list items responsive is no more complicated than any other component on the site.
05:03You just have to consider how they're put together, so you can make the appropriate changes
05:08at the appropriate breakpoints throughout the site.
Collapse this transcript
Adding navigation links that return to the top of the page
00:00This may seem trivial, but adding a link to the very bottom of your theme pointing the
00:05visitor back to the top can be extremely useful.
00:09This is especially true if you write long articles or have a lot of content on the page,
00:13because navigating to the top of a page with swipe behaviors on a phone can be a real pain.
00:19Adding a navigation link back to the top is easy.
00:22You just have to target a specific ID within the page markup and add it to the URL.
00:27I like to add a specific line of text to this link, just to make a kind of humorous.
00:32So the first thing we are going to do is identify the container we want to target with our link.
00:37I'll click somewhere in the main content towards the top to activate my Developer Tools
00:42and then find in an id I want to target.
00:45Let's say want to move people up to the content ID, found right here.
00:49It's pretty much at the top, and it's where the content starts.
00:53Then I'm going to add the link to my footer file.
00:55So I'll go to my theme, find footer.php, and scroll down to the bottom, and here I'll add
01:03a new paragraph right at the end, so it's absolutely at the bottom of the footer.
01:10And inside the paragraph I'm going to add an anchor with the a href of #content, which
01:18is that id I just found.
01:20I'll close the anchor and add the closing anchor tag, and then I'll put in some link
01:25text so it's clear what's going on when you click this link.
01:28"You have reached the bottom.
01:32Click here to get back on top."
01:36The final thing I need to do is close my paragraph.
01:39I will now save the footer file, reload my page, scroll all the way to the bottom, and here
01:48we now have a link that says, "You've reached the bottom, click here to get back on top."
01:52When you click it, we jump all the way back to the top the page.
01:56Like I said, looks trivial, extremely useful.
01:59Responsive design is more than content reorganization.
02:03It's also adding in-functionality that makes the site easier to use for different users.
02:08The return-to-the-top navigation link has little of value for desktop computer users,
02:13but is very useful for visitors using smartphones and tablets.
Collapse this transcript
10. Adding a Responsive Featured Content Slider
Using FlexSlider to create a responsive slider
00:00Featured content sliders are a great way of showcasing specific content on a site.
00:05That said, they can also be a great hindrance when trying to create a responsive site.
00:11There are ways around this problem, but they usually involve a lot of code wrangling and
00:15quite a bit of JavaScript and CSS sorcery.
00:19Fortunately, much of this has already been solved by others,
00:23and in a solution called FlexSlider, you will find a featured content slider that is both
00:27responsive, functional, and even includes swipe behaviors for smartphones and tablets
00:33and lots of other advanced features.
00:35Let's take a look at how you can include FlexSlider into the Anaximander theme, or any other theme for that matter.
00:42The first thing we need to do is get FlexSlider itself, and we get it here at woothemes.com/flexslider.
00:49This, by the way, is also where you'll find all the documentation and examples and other information.
00:55And the amount of stuff you can do here is quite extensive, so it's worth taking a close
00:59look at the site.
01:00So, I am going to download FlexSlider here, and get it onto my computer, and then I'll
01:06open this folder.
01:09And from here, I only need three components. I want flexslider-min.
01:13This is the JavaScript library.
01:15I want the flexslider.css CSS file, and I want the images folder.
01:20I'll open my theme next to it, and I am going to put the flexslider.css file, along with
01:26the images folder, into the CSS folder.
01:33Now that I have the files I need, I need to tell WordPress to use FlexSlider, and I am
01:37going to use the same enqueue method I used for FitVids earlier.
01:41So, we'll go to functions.php, scroll all the way to the bottom, make some space, and
01:50here I want to add the functions to call FlexSlider in.
01:53Just like before, this requires quite a bit of code, so I've compiled it all for you, and
01:58you just have to copy it and paste it in.
01:59So you see here under 10.01, you'll find the FlexSlider code. Just go down and copy all
02:06of it and paste it in to functions.php.
02:11If we take a quick look at this function, it's exactly the same type of function that
02:14we saw previously with FitVids.
02:17We have the function itself, aximander_flexslider.
02:21It's called down here add_action. On initiation, we call anaximander_flexslider.
02:27And here we do that register_script function again, where we give the script a name, we call
02:32it from the template_directory, and it's called then jquery.flexslider_minified.
02:37We enqueue that script, and then we also enqueue the style sheet. So we go wp_register_style,
02:46give the style a name, point to the style, template_directory css/flexslider.css, and
02:54then we enqueue the style here.
02:56Finally, we have to add the script function, and this is where, if you want to customize
03:02FlexSlider further, you would add your extra script, because if you go into the examples
03:08of what is possible with FlexSlider, you'll see there's tons of extra functionality you
03:12can hook into if you want to and you can make lot of customizations with this tool.
03:17If you do that, this is where you would place it.
03:20But for now, we're just going to save it and then we have to add some content to our page
03:25that can be inside the FlexSlider.
03:27To do that, I am going to go and modify index.php.
03:31That's the template file that controls the front page.
03:34So, I'll go back to my theme, find index.php, edit it with Notepad, and then directly under
03:43div content main here, I am going to add some new lines, and then here I'm going to paste
03:48in some dummy content.
03:50Back in my code_snippets file, I've already created that dummy content for you.
03:53So, you can just copy out from the beginning comment till the end comment here and paste
03:59it in, and you'll see all we're doing here is creating a divider with the class FlexSlider
04:05that has an unordered list with a class slides, and each of the list items within this unordered
04:11list will be one of the slides.
04:13You can also see that each of the list item has an image and then a caption.
04:18When I save this and reload my page in my browser, you see FlexSlider appears at the top.
04:26And right now, FlexSlider is pulling images directly from my Flickr account. And we can
04:30even navigate between the images using either of these navigation buttons or by using the
04:36buttons down here on the bottom.
04:39But the coolest thing is, as I said before, FlexSlider, as the name suggests, is responsive
04:47and flexible, which means simply by adding FlexSlider into the site, like we just did,
04:53you automatically have a fully responsive slider.
04:56Before we go any further, I want to say two things.
04:59First of all, you should go check out the live demo of FlexSlider, because you'll see
05:03that you have a lot of different options when it comes to how you want to use FlexSlider.
05:08You have the standard version, which is what we just included, and you can also add things
05:12like thumbnail navigation or even add a carousel that cycles through multiple pieces of content.
05:19All the documentation for these types of behaviors, along with code examples like the one you
05:24see right here, can be found on the FlexSlider web site.
05:28And there is one more thing I want to point out.
05:32FlexSlider is a perfect example of an open source application in constant development.
05:38That means there are always developers working on improving FlexSlider, so chances are, if
05:43you implement it, it may not work exactly the same way that I'm showing you.
05:48But that also means it probably has even more features and is even more advanced.
05:52So, if you want to use FlexSlider, be sure to go to the web site, download the latest
05:57version, read the documentation, and make sure you're using it to its fullest.
Collapse this transcript
Creating a loop to show sticky posts in a featured slider
00:00In the previous movie, we used the FlexSlider JavaScript library to add an advanced responsive
00:06featured-content letter on our front page.
00:09But right it's still running our dummy content.
00:12The next and obvious step is to add featured content from our own site to the slider.
00:18The way we are going to do that is by creating a custom template file that has a separate
00:23loop that will loop through only the sticky posts inside WordPress and then display them
00:29in the FlexSlider.
00:31So before we do that, we need to make some of our posts sticky.
00:34I'll go up to the backend, go to Posts > All Posts, and then just select some random posts here.
00:43I'll select these four, go to Bulk Actions and Edit and from here, I can set the Posts to Sticky.
00:56Sticky posts are supposed to always appear at the top of the index page, and you can also
01:01use them for things like a featured-content slider.
01:05Now that I have set some sticky posts--and you can see that they are sticky because
01:08they say so right here on the index page--we can create a function that hooks into the
01:13sticky posts and then puts those posts into the featured slider.
01:18So first, I am going to create a new file.
01:20I'll save it inside my theme, and I'll call it featureflexslider.php. And I'll make sure
01:31that this is a PHP file. And then I am going to go to my code_snippets and copy out this
01:40custom code that I have created for you.
01:47Paste it into the document, save it, and then I'll quickly explain what's going on.
01:51At the top here, we have a comment. It's put inside php bracket so that it won't appear
01:56inside the site itself, and then we are creating this < div class="flexslider"> and the <ul class="slides">.
02:03You will notice this matches exactly what we had in our dummy content here, <div class="flexslider">,
02:09<ul class="slides">. Then we create a loop.
02:13First, we tell WordPress to track all the sticky posts and put them into a variable we call sticky.
02:19Then we say we want six posts maximum and we only want the posts that appear inside
02:26that variable, sticky, that we got previously.
02:30Then we go to WordPress and say we now want to have a new query, and a query is basically
02:35when you go to the database and say give me all your posts and I'll do some stuff with them.
02:39Then we say, for each of the results that come out of this query, do the following.
02:44If it has a post_thumbnail--so that would be the featured image--create a list item,
02:49and within that list item create a link that goes to the post for that thumbnail, then
02:55show us the thumbnail and put in the caption with the title of that post. Close the link,
03:02close the list item, close for each, and then the loop goes back to the top.
03:07It says, are there more posts? Okay, and it goes through the process again.
03:11The reason why we have this if ( has_post_thumbnail () is because this being a featured content slider
03:16requires an image; if we don't have an image, it will look really strange.
03:21Then the function ends with a closing of the unordered list and a closing of the div.
03:26But this wasn't placed inside a file that we are using,
03:28so now we have to call this function inside our index file.
03:32So I'll go back to my index file and remove this demo content, because we don't need
03:36it anymore, and in place of the demo content,
03:39we are going to call in this featureflexslider.php file.
03:44So I'll use a function that comes with WordPress.
03:46It is called get_template_part, and it'll automatically look through the entire folder for the current
03:56template or theme for a file with whatever name we put in here. So this file is called
04:02featureflexslider, and I'll close the php delimiters.
04:09Now when I save this and reload the front page, you will see that we now have the featured
04:17images up here at the top.
04:21However, you'll also see they look really strange.
04:26That's because we are currently calling an image size that wasn't there when we originally
04:30added the content.
04:32This is a common problem you see a lot in WordPress where you switch to a new theme
04:36and then all of a sudden all your images have different weird shapes.
04:40So before we go any further, we need to use a plug-in to resize these images to fit with
04:45the current theme.
04:47So I'll go to my Dashboard, go to Plugins > Add New Plugins, I'll search for Regenerate Thumbnails,
04:57click Install Now, to install the plugin, activate the plugin, and once it's installed,
05:04I'll go up to Tools > Regenerate Thumbnails and run the plugin.
05:09The plugin will now go through all the images on my site and resize them to fit with all
05:14the predefined sizes we have inside the theme.
05:17One of those predefined sizes I've already set to the size we need for the FlexSlider,
05:23which means all those images will now be resized to fit in the FlexSlider, and we'll see a
05:27much better display once the process is done.
05:30The process is complete, and I go back to my front page and you now see that the FlexSlider
05:36looks much better because it has the properly sized images.
05:42And if you click on any of these, you jump directly to that post. But there's one more
05:53thing I want to do.
05:54You will notice that if we go down to the bottom here and we navigate to page 2, we
06:01again get the FlexSlider, and I only want the feature slider to appear on the front page.
06:06So if you go back to the code_snippets file, you will see that, here, instead of just having
06:11the call to get_template_part, I have created a function that you can use instead.
06:15So if you copy out that function and paste it into your index.php file, what this function
06:23does is it goes and checks, is this the front page, and if it is the front page, is it not
06:29paged, meaning is it not page 2 or 3 or 5 or 10?
06:34It has to be page 1.
06:36Only if both these conditions are accurate will it show the template part.
06:40If we save that, reload page 2, we no longer see the FlexSlider, but go back to page 1
06:49and here we have the featured-content slider.
Collapse this transcript
11. Making a Responsive Index Page That Dynamically Reorganizes with Masonry
What is jQuery Masonry?
00:00If you've spent any amount of time on the Internet in the past few years, you've likely come
00:05across something very cool, called Masonry, though you probably had no idea that's what it was.
00:11Masonry is a jQuery plug-in that let's you create dynamically reorganized content on your
00:16site in a very nifty way.
00:18The best way to understand what Masonry does is to look at these two examples on the Masonry web site.
00:24It shows the content before and after applying Masonry.
00:28What's happening here is we have a page that has a bunch of items, so a regular index page,
00:34but we don't want to list them vertically like you would normally do; we want to list
00:37them horizontally.
00:39The problem is some of the items are bigger than the others and as a result, they take up more space.
00:45If we use CSS to float these items left to right, what happens is the tallest item in
00:52each row defines the vertical height of that row and nothing can flow in to fill up that space.
00:58So what we end up with is a layout with a lot of dead space in between content.
01:03What Masonry does is it figures out where this content is and then finds elements that
01:09can fit into those places.
01:11As a result, we get a more condensed layout, and it's also easier to cram more information
01:16in to a tighter space.
01:18The reason why you want to use Masonry is exactly that; you get to fit more content
01:23into a small space, and you also get to use more of the space on the page.
01:28As an added benefit, Masonry is responsive.
01:32So if we change the size of this window, you'll see that the content dynamically reorganizes
01:38to fit the new sized window.
01:44This means simply by including Masonry in our index page, we automatically get a responsive
01:49index page that showcases as much content as possible in the currently available space.
01:55Masonry is being used both on responsive pages and on static pages, because of the way it handles layouts.
02:02What you see on the Masonry page is a fairly standard approach, but people use it for a
02:06lot of cool stuff.
02:08This is an example. It's an artist that uses Masonry of the front page to create this artistic
02:14representation of his own art.
02:17As you can see, the items are still reorganizing in the same way, but because of what the items
02:21look like, it looks really neat.
02:27What we're going to do is take this regular static index page where each item is listed
02:33one below the other,
02:35and change it into dynamic Masonry page, like the one I've here on DESIGN IS PHILOSOPHY.
02:41All we're going to do is add the Masonry plug-in and hook it into our index page and then also
02:48activate some featured images on the index page.
02:52When we do that, we get a response of layout that fits with the different screens, and because
02:57I made some very careful measurements, we will end up in a situation where the smallest
03:02screen--so the mobile phone--will have a beautifully laid out vertical stack of stories, just like
03:08what you see here.
03:10As a bonus, the front page will also have some front page widgets.
03:18So for example, this lynda ad that you see here at the top is actually a widget, and there's
03:22also two widgetized areas inside this layout that will activate as we move forward.
03:31jQuery Masonry provides a whole new way of organizing index pages and allows us to put
03:36more content on index pages without sacrificing real estate in the process.
Collapse this transcript
Installing jQuery Masonry
00:00Now that you've seen what Masonry can do, let's install it into the Anaximander theme
00:05so we can use it to create a cool, functional front page.
00:09To download Masonry, go to the Masonry web site and right-click on the Download the script
00:14jquery.masonry.min.js.
00:16So I'll go click Save link as, and I'll navigate to my WordPress installation or where I have
00:25my theme, and I'll go into my theme, go into the js folder, and simply save jquery.masonry.min.
00:36Now that I have jquery.masonry added to my theme folder, I want to enqueue it into the site.
00:42So again, I'll go to functions.php and I'll scroll all the way to the bottom, make a few
00:50new lines, and this is where I want to enqueue jquery.masonry.
00:55If you go to the code_snippets file for this course, you'll find that full function for
01:00enqueueing masonry.
01:01So I'll simply copy it out and paste it in, and then I'll explain what happens.
01:07Just like the two other times we included JavaScript into WordPress.
01:11what we are doing here is creating a function that registers and enqueues a script properly
01:17so that WordPress can handle it in the right way.
01:20Off the top, we create a function, give it a name.
01:24I called it anaximander_masonry. And then after the function we create an action that on initiation
01:31of the theme calls this function.
01:34Inside the function we check whether or not the current page is an admin page, and if
01:39it is, then we don't do this.
01:41So you see here it says exclamation point, which is not, so we're saying if this is not
01:47an admin page, then run the following.
01:50So if it is an admin page, none of this happens.
01:53Inside the function we first register the script by giving it a name, then pointing
02:00to where it is. It's in the template_directory under the folder js and the script is called
02:05jquery.masonry.min.js.
02:07This script is related to the jQuery library that ships with WordPress, so therefore we're
02:14telling WordPress to also load the jQuery library.
02:20Once we register a script, we enqueue the script,
02:24and by enqueueing the script, I mean we add the script to the very top of the page.
02:29So all we are doing here is saying wp_enqueue_script and then we just call that name that we just gave the script.
02:36This adds the jquery.masonry script itself to WordPress.
02:39The next thing we need to do is create the function that actually calls up a jquery_masonry script.
02:45That function is down here and what it does is it says find the ID masonry-index and assign masonry to it.
02:55And then inside that ID we have an itemSelector called .index-post-container and each of these
03:01should be floated in the way that masonry floats content.
03:06This, by the way, is also where you want to add additional functions,
03:10so for example if you want to add the animation function that animates this content as you move it.
03:18So what I mean is when this content slides around in the screen, instead of it just popping
03:23into place, you want to add that in here.
03:27So what you would say is isAnimated: true, and there are countless other options you can also add.
03:34You'll find all of these in the documentation on the masonry web site.
03:38Once you've completed a function to add in Masonry, save functions.php, go back to your
03:44site, reload it, and though nothing happens quite yet, to make sure Masonry is being enqueued
03:52properly, go Ctrl+U in Chrome to open the code itself and then Ctrl+F to open the Finder
03:59and type in masonry.
04:01And here you can see masonry being enqueued and you'll also see that function we created
04:08being listed in the footer, meaning masonry will kick in.
04:12Now note that I said it's listed in the footer.
04:14If we go back to the function here, you'll see that the add_action function that adds the
04:18anaximander_add_masonry, which is this custom code here, is being added in wp_footer, meaning
04:25it's added at the very end of the page.
04:27That just means the function doesn't run until all the content is filled in on the page,
04:33and we're doing that because we don't want masonry to try to reorganize content before
04:37all the content is available.
04:39With Masonry installed and up and running, we're now ready to hook it into our index page.
04:45
Collapse this transcript
Configuring the index page with Masonry
00:00Now that we have jQuery Masonry hooked into our theme, it's time to apply it to the index
00:06page to create an index page with a Masonry layout.
00:10But before we do that, let me explain exactly what we are going to do.
00:14When I designed the Anaximander theme, I had a plan for two different index page layouts,
00:19so it's clear to the visitor where they are.
00:22On the front page, so the regular index or the blog index, I want to use Masonry to lay
00:27out the content in three boxes, one next to the other.
00:31But on all the archive pages--so Author archive, Regular archive, Category archive and Tag
00:39archive--I want to retain this regular type of index where one post stacks on top of the
00:44other and we have a regular sidebar. That means a)
00:48we're only applying Masonry to the index.php file and b)
00:54we have to retain the styling for this regular index.
00:58Looking at the index page, we can see what we need to do here.
01:01First, we need to get rid of the sidebar. We then need to reorganize the content itself
01:06to match the Masonry layout. And finally, we have to make sure that on the bottom here,
01:12our pagination stays on the bottom and works properly.
01:19But just before we dive into the code, let me show you something that I have hidden inside this theme.
01:24If we select a single post here, you will see that there's this figure class "the-thumbnail"
01:31that's inside the code, and if you look at the code itself, you will see there's an image
01:36here, but it's hidden using the style over here, regular-index .the-thumbnail display set to none.
01:42If I turn that style off, you will see that each of these posts actually has a thumbnail attached to it.
01:49The reason why I hid it is because right now it looks kind of weird and I only want to
01:54use that thumbnail image in the Masonry index.
01:58That's also why it's sized the way it is.
02:00But if you want to have these featured images on each of the posts also in regular indexes,
02:06all you have to do is find out style in the style sheet and take it away.
02:14Now let's get down to business.
02:15The first thing we need to do is get rid of the sidebar.
02:18I'll go to my theme, find index.php, open it in my editor and then if I scroll to the
02:26bottom, we find a call to the sidebar right here.
02:30Now, because I'm not 100% sure I want to get rid of it yet instead of deleting the function
02:35altogether, I am simply going to comment it out, and by commenting it out inside the
02:41php delimiter, it won't get rendered in the page.
02:44If we were to place HTML comments on the outside of the php delimiter, this code would be rendered
02:49and then hidden in the HTML.
02:51But I want to just hide it all together.
02:53So I'll save that, reload my page, and the images disappear of course, because I still
02:59have the style, but more importantly, you see that my sidebar disappears, and that's what I want.
03:05Now I need to hook Masonry into the content on my page so that Masonry can realign it.
03:12The way that works is I tell Masonry what content it should work with and then Masonry
03:17applies the necessary styling and the necessary code to that content.
03:22To find out what that content is, I'll use my Developer Tools to identify the containers
03:28that contain all of those content. And if we look here, you will see that on my index
03:33page, I have a section with an id called regular- index and I also have a divider with an id called
03:40regular-content and clicking on them, you can see that these are the two containers
03:45that create the space for the sidebar.
03:47Now I said previously I want to retain this layout for the other index pages, meaning
03:53rather than going in and changing the styling for regular-index and regular-content, what
03:58I need to do is create new names for these ids so that I can apply new styling to them.
04:04But if I look closely, I see that the regular index id in the styling is the one that creates
04:10that extra space we need for the sidebar and regular-content just responds to that style.
04:16So I should be able to remove regular-content altogether without affecting our Masonry layout.
04:22I simply don't need it.
04:24So I'll go into index.php and find regular-content right here and I'll comment this out and see what happens.
04:32And of course, when I comment out the beginning of a div, I also have to comment out at the end.
04:37It's down here.
04:38so I'll simply move this comment over so that it's gone, save index.php, reload it
04:45in the browser, and now my content spans all the way from left to right, which is exactly what I want.
04:53The last thing I need to do is rename regular- index to something else so that we can create custom
04:58styles for it, and here we have a bit of a chicken-and-egg situation, because this is
05:03the id that we'll be targeting using Masonry, and if you look at the function you created
05:08previously in the functions.php, you will see that that name is already defined right here,
05:14because what we are doing is we are creating a small function that tells Masonry to handle
05:19the content inside the masonry-index ID. And what Masonry will do is it will find each
05:25item inside the masonry-index ID, and then the items with the index_post_container classes
05:32are the ones that are going to be reorganized by Masonry.
05:35So what we need to do is change the name of the ID from regular-index to masonry-index.
05:41So I'll go in here, change it to masonry-index, and then I'll also change the comment that
05:49closes it, so I see where it is.
05:53I'll save it again, reload my page. The images will appear again because the styling that
05:59hid the images were applied to regular-index, but more importantly, if we look at one of
06:04these single posts again, you will see that each post now has this index_post_container
06:11class attached to it, and it's also set as a masonry-brick, meaning that now Masonry
06:16is handling each of these items.
06:19If you paid very close attention, you will also notice one other thing.
06:24The pagination is all of a sudden up here.
06:26That's because the pagination is floating based on the overall content.
06:31So the last thing we need to do here is move the pagination out of this container altogether
06:36so it doesn't appear here, but rather, down at the bottom of the page.
06:40So to close-up, we'll go and find our pagination, which is right here, cut it out, and just place
06:47it outside the masonry-index section. Save it, reload the page, pagination is no longer
06:54here, and if we scroll down to the bottom, it appears at the bottom where it's supposed to be.
Collapse this transcript
Using CSS to finalize the Masonry layout
00:00So far, we've hooked jQuery Masonry into our WordPress theme; we've made it interact with
00:06the front page; but even so, this front page still doesn't look like that nice tiling effect that we want.
00:14That's because the last step in the process of adding masonry to your WordPress theme
00:18is to apply styling so that all the boxes are contained the way we want them to.
00:24If we look at the original design, and this is where drawing out your design in advance
00:28has a huge benefit.
00:30If you draw your design in advance, you know exactly where you want to go with your styling,
00:35so it's just a matter of applying that styling to your style sheet.
00:39Looking at the original design, you can see exactly what we want to do.
00:42Each of the items should be contained in a box that's 298 pixels wide.
00:51That box should have a one-pixel border of gray color, and there should also be a margin
00:56between each of the boxes of 20 pixels.
01:00Now that we know what it's going to look like, we can go into our markup and identify the
01:05elements that we want to apply styles to.
01:07So I'll use my Developer Tools to find out which element is containing each of my stories,
01:14and I can see it's this one, the article, and it has the class index-post-container.
01:20So that's what I want to apply my styles to.
01:23Now I can go into my style sheet, open it in the code editor, and then I'll create a
01:30new section for my new styles.
01:33I'll place it right after the regular index section that we already have.
01:38So I'll copy this comment out and then scroll down until I find the end of that section.
01:44Here it is. And I'll say Index- Masonry and then I can create a new style.
01:53The first style should apply to only masonry-index and inside that .index-post-container so
02:07that this style will apply to each of the items.
02:10Then it's just a matter of defining exactly what it's going to look like.
02:14As I showed you in Photoshop, the width of each item should be 298 pixels. Each item
02:20should have a border.
02:22It's 1 pixel wide, solid, and has a gray color, so I'll set it to e3e3e3. And we also want
02:32each item to float to the left, so I'll say float: left.
02:37Save the new style sheet, reload the page, and as if by magic, everything now stacks nicely,
02:46side by side and one on top of another.
02:49But it doesn't look quite right yet.
02:51There's a couple of things we still need to do.
02:54First, we need to make some space in between each item.
02:58So what I need to do is, for each item, add a margin to the right and to the bottom, and
03:03I know that that margin should be 20 pixels.
03:06So I'll go back, say margin: 0 for top, 20 for right, 20 for the bottom, and 0 for left.
03:16Save it again, reload the page, and now they stack on top of one another with the correct spacing in between.
03:25But there are two issues here.
03:26First, we now only have two items next to one another instead of three, and second,
03:32you see that the title here is shifted all the way to the left, which looks kind of odd.
03:38So let's deal with the title first.
03:41I'll take a look at the title with the Developer Tools and I see that it's affected by this
03:46index-title class.
03:48So now I can create a new style that affects only that.
03:52So I'll say masonry- index and then index-title.
03:59That way this style will only affect the index- title in the masonry-index and not any other place
04:04the same index-title appears, like in the regular indexes.
04:09And then I'll set margin: to 20 pixels all around.
04:13Save it, reload the page, and now you can see the titles align with the rest of the content.
04:23But what are we going to do about the content not spanning all the way across here?
04:27And more importantly, why is it happening?
04:30Well, if we do some math here, you'll realize that the overall container here is 940 pixels
04:36wide, and each of these articles is 300 pixels wide once you add the 1-pixel border on either side.
04:44You remember we set them to 298s and then added a border.
04:49The problem is, if you take 300 plus 20, plus 300 plus 20, plus 300 plus 20, you don't end
04:59up with 940; you end up with 960.
05:04The containing box is simply not big enough to contain all our content.
05:08There are several ways you can deal with this. The absolute easiest way of dealing with
05:12it is to simply make that outermost containing box too big.
05:17So what we'll do is, go in, create a new style, say masonry-index--because that's the containing
05:25box--and I'll set the width to 105%.
05:32That extra 5% is just enough to allow us that extra space so that the content slides up
05:39next to it without disturbing the layout of the whole page.
05:43So now you see we have the correct layout for the masonry index, all the way down to
05:48the bottom, and if we navigate to the next page, we get more stories.
05:57And just as importantly, if I go to an archive page, like Category archive page, I still
06:03have the same layout I had before, so Classic layout on archive pages, Masonry layout on
06:11index pages, and the Masonry layout is responsive.
Collapse this transcript
Adding media queries to the Masonry index
00:00jQuery Masonry is now hooked into the front page, working properly, and we have this nice
00:06Masonry front page on our site.
00:09When all this is done, we need to make sure this layout looks good on all the devices
00:13and all screen sizes, and that includes the very smalles screen size, that vertical phone.
00:20If you remember back to when we made the media queries, when we scaled this window down to
00:25a smaller size, so equivalent to that small phone, we have a 20-pixel padding on the left
00:31and it 20-pixel padding on the right.
00:33The problem is the box here in the Masonry layouts is 300 pixels wide, meaning the total
00:40width here is 340 pixels, whereas the phone's screen size is 320 pixels.
00:47What we need to do is reduce this padding for just this smaller size so that when people
00:53use their phones and hold them vertically, they'll get 320 pixels across exactly.
00:59What we need is a new media query targeting just this situation.
01:05To add the new media query, we'll go to our style.css file, scroll all the way to the
01:10bottom, and if you want to, you can go and grab the media query from earlier, for instance this one.
01:17Copy it out, and we paste it in here, and then we make the conditional statement max-width: 3 20px.
01:26Then it'll only affect smart phones held vertically or really narrow windows on a computer.
01:34Now all we've to do is find those values that we want to affect and then paste them in here.
01:38Now if you look at our original media queries, you'll see that there's a bunch of CSS selectors
01:44when we set the left or right values to 20 pixels.
01:47You see it here and here and here and here and so on.
01:54So what we're going to do is take the same selectors, copy them into a new media query,
01:58and then reduce that width the 10 pixels.
02:01Just to make it easier for you, I've made a code_snippet that just does that. So you
02:06just have go in to the code_snippet file and find that media query, copy it out, scroll
02:12to the bottom of style.css, and paste it in.
02:17When you save this new media query and go back to your browser and reload the page
02:23and then reduce the page down to that same width again,
02:26you'll see that right when we hit that 320 mark, the new media query kicks in, and we
02:32get this nice 10-pixel padding on the left and right.
02:36And it also realigns the header in the menu and the footer and so on.
02:40You can see here, it keeps popping in and out between 10 and 20.
02:47So what we're doing here is using media queries one last time to refine the look for just
02:52one particular usage scenario.
Collapse this transcript
Next steps
Exploring hidden features of the Anaximander theme
00:00If you've followed this course from top to bottom, beginning to end, you will now have
00:05a fully responsive WordPress theme with a featured-content slider at the top, a Masonry
00:11layout index page, and responsive content throughout the site.
00:16Now, I want to show you a couple of hidden features that are built into the Anaximander
00:21theme that will make it easier for you to use and also better for the people who visit the site.
00:27The first feature is the front-page widgets.
00:31The front-page widgets allow you to add regular widgetized items, like lists of recent posts
00:37or recent comments or archive lists or whatever, in three different places on the front page.
00:44To activate them, go to either the WordPress toolbar and select Widgets or go to the
00:50Dashboard and go under Appearance and Widgets.
00:53And here you see we have Front page widget 1, Front page widget 2, and Front page widget 3.
00:59So, let's place a Categories list in Front page widget 1, Recent Comments in widget 2,
01:11and the Archives in widget 3.
01:14If I now open the front page in a separate window, you will see that right here in my
01:20masonry-index, we now have a Categories list.
01:23If we scroll down, you will see the Recent Comments appear, and all the way at the bottom,
01:32you see Archives.
01:34And if I add more widgets to one of these lists--let's say I want to add a list of pages
01:39or even a custom menu to the first widget area--
01:44I can reload the page, and you will see that that widget appears on top.
01:51And just like with everything else on this page, these widgets are placed inside the
01:55masonry-index, meaning as I resize the window, they will move around and reorganize to fit.
02:07The second feature is related to the featured images.
02:11If you write a post, you can use the featured image in an advanced way in this theme.
02:17What I've done here in this post is I have created a post, assigned a featured image,
02:21but I didn't add the featured image to the top of the post.
02:27If I want to add the featured image to the top of the post automatically, instead of
02:31putting it into the post itself, I can simply go into Edit Post and change the post Format
02:38from Standard to Image.
02:41If I set the post Format to Image, it will automatically include the featured image at
02:46the very top of the post every time.
02:48As you can see, we have a featured image of the car down here, and when I update this
02:54post and click View Post, the featured image appears at the top of the post.
03:01This is very useful because now you don't need to add the image twice.
03:05If you want the image to appear at the top and you also want it to be a featured image,
03:09simply add it as a featured image, set the post Format to Image, and it will automatically
03:15appear inside your post.
03:19The last feature is the one I think is the coolest.
03:23If you add a video from either Vimeo or YouTube and you do it using the oembed technique, like
03:28I did here in this Farm to Table post,
03:32you can see the video appear inside the post, but you don't see it on the front page.
03:36What I did in this theme was add a feature that would automatically include the video
03:43on the front page for you.
03:44So, if you go to Edit Post, you'll see that the video is here included as an oembed, meaning
03:50I just placed URL to the video inside the HTML view, and I switched the post Format
03:57to Video, update the post, view the post, and then reload my front page,
04:08you'll see that now that video appears in playable format right on your front page.
04:15(video playing)
04:21This automatically happens for YouTube videos and for Vimeo videos.
04:26If you don't want the video to appear on the front page--you only want to show an image from the video--
04:31all you have to do is go to the post and edit it, set the post Format back to Standard,
04:39and then upload a featured image, and that featured image will appear instead.
04:44So here you have a very advanced option.
04:47That means when it comes to video posts on the front page, you have three options.
04:52If you simply add the video into the post along with text, it will appear as the title
04:57and then some text, and no video will be shown.
05:01If you add a featured image to your post, that featured image will show on the front page.
05:07If you go and change the post Format to Video, instead of the featured image, on the front
05:13page, you'll see a playable version of the video.
05:17In designing the Anaximander theme, I wanted to give you a theme with a lot of advanced
05:21functionality that you can actually use for your own web site now that you have completed it.
05:27That means take the theme, customize it in any way you want, change the colors, add a
05:32header image, change the layout, and use it on your web site.
05:36The whole point of this theme is to give you the tools necessary to figure out how to build
05:41your own theme using all these advanced options, and have that theme be responsive, so it looks
05:47great on desktop computers, tablets, and on phones.
Collapse this transcript
Where to go from here
00:00And that, my friend, brings us to the end of this course.
00:03Before I let you go, I just have a few more things I'd like to say.
00:07All the techniques that I've shown you in this course, whether it be design techniques,
00:11development techniques for responsive design, or how to use CSS in different ways to create
00:17different types of layouts, are all universal and can be applied to any theme.
00:23Of course, how you apply it will change, but the general concepts will stay the same.
00:29Media queries are media queries no matter where you go, and you just apply them in the
00:33same way I did and use the same techniques and you'll have great results.
00:38The techniques I've showed you on how to include JavaScript tools like FitVid, FlexSlider,
00:43and jQuery Masonry can also be applied to any theme.
00:47Again, how exactly and where exactly you place these things may change, but the overall technique
00:54on how to include them by enqueuing the JavaScripts in the function.php file and then applying
00:59them to your general templates stays same across all themes.
01:04Now that you have a fully responsive and fully functional version of an Anaximander, I urge
01:08you to pick the theme apart and configure it in a way that you like, so you can use
01:13it on your own site.
01:15You're free to use it the way it is.
01:17You can customize it in any way you like, to make it look more like what you want it
01:21to. You can also build child theme from it, or you can use components that you find inside
01:26an Anaximander theme and include them in another theme.
01:29You're free to do whatever you want with this code.
01:32If you want to learn more about responsive web design, I urge you to start by reading
01:37the original article, Responsible Web Design by Ethan Marcotte
01:41that's published on A List Apart.
01:44This is where responsive design started, and understanding the history of this technique
01:49makes it easier for you to understand what you can do with it.
01:52If you want inspiration, you should go and check out this web site, mediaqueri.es.
01:59This is an ongoing library of thousands upon thousands of web sites that use responsive design, and
02:05it showcases what these web sites look like in different widths, so you can see what people are doing.
02:12It's a great place to see what's going on in responsive design, and you can also submit your own site.
02:18Just be advised, it takes a very long time from you submit it till it actually appears
02:23on the site, because there are so many sites that are being done this way.
02:27But if you're looking for inspiration, this is a great place to start.
02:31If you want kind of a rundown of what's hot in responsible design right now, you can also
02:35sign up for this newsletter by Brad Frost.
02:39It gives you exactly that: a rundown of what's hot in responsible web design right now.
02:44And you get it right in your inbox, so you can read the great articles and tutorials
02:48that are being published on it.
02:50But what is most important about responsive web design, and what I hope I was able to impart
02:55to you with this course, is that you can take responsive web design and make it your own
03:01and make any site work really well on all different screen sizes and all devices.
03:07By using responsive web design, you're creating better user experiences for anyone visiting
03:12the sites you build.
03:14And in the end, that's really all that matters.
03:16So go forth and build responsive WordPress themes.
Collapse this transcript


Suggested courses to watch next:

CSS: Core Concepts (8h 49m)
James Williamson

WordPress Essential Training (6h 43m)
Morten Rand-Hendriksen


CSS: Page Layouts (8h 57m)
James Williamson

WordPress Mobile Solutions (44m 26s)
Morten Rand-Hendriksen


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked