CSS Layout & Responsive Design
Educational Notes (Quick Links)
- HTML and CSS Basics
- Floating Elements
- Nested Elements
- Negative Margin & Float Layout
- ‘Float’ Over ‘Position’ Sometimes
- The ‘Position’ Property
- Adaptive Images
- Use Media Queries Sparingly
- CSS Inheritance
- Source Order, Specificity, And !important
- Display (Inline vs Block)
- Pseudo Elements
- Pseudo Classes
A Fluid Header
Heads up! this post was created when Microthemer was at version 4. The current version is 7. Some references to the interface may be out of date.
Create A Longer (Custom) Main Menu
By default, Twenty Ten’s main menu only includes two menu items. Many sites have more than this. Please create a custom menu:
By creating a custom menu with the name Header, you have ensured that your menu’s HTML code is exactly the same as the one used in this tutorial. This is a small but important point.
Now on to styling the header elements.
Drag the preview screen width down using the top horizontal ruler. You will notice that making the page wrapper fluid hasn’t helped much. The image and main menu both get cropped. This is because the content inside the fluid wrapper is still rigid. In this section I will show you how to make the various header components fluid. I will also flag the problems nested HTML elements create and demonstrate how to deal with them.
GUI Method
A Fluid Banner Image
At smaller screen sizes, the banner image gets cut off instead of scaling down. You can fix this by creating a selector that targets the banner image and then apply new styles to it.
Drag the preview screen width slider down and try not to be too disappointed. The image will still be stubbornly rigid. You have stumbled across the Nested elements pitfall. A common cause of CSS confusion.
The image doesn’t scale down because it is inside another fixed width element. The fixed width parent element doesn’t adjust to screen size. And so the image will be 100% of a fixed width, which is equivalent to having a fixed width itself. The first step to solving this problem is to identify which parent element has a fixed width value.
The second step is to make the fixed width #branding element fluid via Microthemer:
Drag the preview width slider down and rejoice. The image should scale down and finally the site is starting to look slightly responsive!
A Fluid Main Menu
You may notice that the main menu is still rigid. As before, the trick is to identify which elements have fixed width properties.
Menus always contain nested HTML elements. As was the case with the banner image, the nesting isn’t always obvious. Chrome’s inspector can give you the insights you need though.
Fluid Site Meta
The site title and site description, which are normally on the same line as each other, split onto two separate lines on small screens. This isn’t a catastrophe design wise, but it’s an accidental consequence. You can fix it by making the site title and description fluid too.
A Fluid Site Title
A Fluid Site Description
The site title and description will continue to stack side by side on smaller screens now. Some extra work will be needed for tidiness, but we can leave that for now.
Microthemer Note: Auto-Conversions
Microthemer auto-converts pixels to percentages when you enter an excel-style formula e.g. =%(220). Microthemer queries the width of the parent element to perform this calculation. If your selector targets more than one element, Microthemer uses the parent of the first element in the selection. You can override this by providing a number as a second parameter e.g. =%(220, 2) would tell Microthemer to perform the calculation for the second element in the selection.
You may prefer to set this auto-conversion behaviour by default. You can do this via the CSS Units tab in the preferences. Choose ‘px to %’ or ‘px to em’ from the available list of default CSS units and functions. Whenever you type a number Microthemer will convert it to an equivalent percentage or em. But please leave the default unit settings while you work through the rest of this tutorial.
Code Method
A Fluid Banner Image
Some points to note:
A Fluid Main Menu
Some points to note:
A Fluid Site Title
Some points to note:
A Fluid Site Description
Some points to note:
Nested Elements
As you’ve learned, web pages are made up of HTML elements. Each element can have other elements inside them which we call ‘child elements’. We say that child elements are ‘nested’ inside parent elements.
Chrome’s inspector reveals the nested nature of HTML pages. All web pages have a <html> element that contains a <head> and <body> element. It’s then up to the theme author to decide which elements should nest at ever increasing depths to complete the page. So far you’ve seen a #wrapper div, containing a #header div, containing a #branding div, containing a banner image. It is not uncommon for web pages to nest considerably deeper than four levels.
It’s not always obvious which element has been assigned the CSS value you want to change. The branding element could have caused hours of frustration when trying to make the banner image fluid. It was invisible to Microthemer and Chrome’s page highlighter because it contains floating elements. This situation can happen frequently. Twenty Ten’s main menu contains an invisible div.menu-header and ul#menu-main-menu element. They are not illuminated because they contain floating menu items.
A parent element can also be hard to spot when there is no padding, border, or margin separating it from its child. Parent and child occupy exactly the same space on the page. And so highlighting alone gives no clue about which element is being targeted. This happens with the menu items (<li>) and their child links (<a>) in Twenty Ten.
Look out for nested elements when your styles aren’t working as you expect.