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
Fancy Form Styling
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.
You’ve gained practical experience working with CSS properties and concepts pertaining to layout. Though essential to your progress, they’ may not give you that ‘Oohh, Aahh’ feeling. In this section I’m going to introduce some more colorful styles alongside some final CSS layout tips. This will hopefully give you a flavour of what else of possible with Microthemer.
Restyling The Twenty Ten Comment Form
Navigate to the Hello world! post using one of the Hello world! links you added to your custom main menu. The home page shows the Hello world! post by default, but you need to go to the dedicated single post page in order for the comment form to appear.
To see what the comment form looks like with some content, please paste the following text into the textarea (but don’t hit the submit button):
The following instructions will show you how to convert Twenty Ten’s comment form from this:
To this:
GUI Method
Comment Wrapper: Create A Selector
Comment Wrapper: Apply New Styles
Comment Triangle: Create A Selector
Comment Triangle: Apply Styles
Comment Label: Create A Selector
Comment Label: Apply Styles
Comment Textarea: Create A Selector
Comment Textarea: Apply Styles
Comment Button: Create A Selector
Comment Button: Apply Styles
Comment Button (Hover State): Create A Selector
Comment Button (Hover State): Apply Styles
Code Method
Import Google Fonts
One of the advantages of the UI over writing code is that Microthemer automatically building a Google Fonts URL when it detects the use of Google fonts. You will need to do this manually when using the code method. Paste the following import statement at the very top of the code editor:
Some points to note:
Comment Wrapper
Start a new line in the code editor just above the media query code and type the following code:
Some points to note:
Comment Triangle
Start a new line in the code editor and type the following code:
Some points to note:
Comment Label
Start a new line in the code editor and type the following code:
Some points to note:
Comment Textarea
Start a new line in the code editor and type the following code:
Some points to note:
Comment Button
Start a new line in the code editor and type the following code:
Some points to note:
Comment Button (Hover State)
Start a new line in the code editor and type the following code:
Some points to note:
The Display Property (Inline vs Block)
The display property has evolved and now has many possible values. I’m going to talk about inline, block, and inline-block. These values determine if width, height, margin, or vertical-align work as expected or not.
Block elements fill the width of their parent element (unless they are floated) and start on a new line. Some elements that are block by default include <p>, <div>, <ul>, <li>, and all headings. Block elements ignore the vertical-align property.
Note: the content inside block elements can be aligned by setting text-align: center. But if you wanted to centrally align a smaller block element inside a bigger element, you must set margin-left and margin-right to auto.
Inline elements flow with the text without starting a new line. Some elements that are inline by default include <a>, <span>, <b>, <em>, and <code>. Inline elements ignore the width, height, and margin-top/bottom properties (but do respond to margin-left/right). Setting a height of 1000px for instance will do nothing to change an inline element’s height. Inline elements do not fill the full width of their parent. If floated left or right, inline elements behave like block elements (they do respond to width, height, and margin-top/bottom properties).
Note: inline elements can be aligned by setting e.g. text-align: center, but this must be set on a parent block element, not the inline element itself.
Inline-block elements behave as though they were block elements. Apart from one detail. They flow with the text like inline elements rather than filling a whole line like block elements.
Note: setting display to block on input elements doesn’t set the input to the full width of its parent element, unlike most elements that have display:block.
Understanding Pseudo Elements
Pseudo elements are elements that you create with CSS rather than HTML tags. They’re called pseudo elements because they don’t exist as part of the HTML source code. You can apply CSS to them as if they were HTML elements though.
The most common pseudo elements are created with ::before and ::after. You may see :before or :after with only a single colon used sometimes. This is deprecated (no longer regarded as valid by the CSS specifications) but still works.
When you want to style an element in a way that would normally involve adding extra HTML, ::before and ::after are useful. Styling content to look like a speech bubble is a good example. Designers often create a pseudo element with ::after and use it for the pointy part. Another common application for pseudo elements is inserting SVG icons e.g. Font Awesome.
Sometimes pseudo elements cause confusion for beginners. It can be hard to trace styling back to a selector if it applies to a pseudo element. Microthemer cannot detect pseudo elements unfortunately. Browser inspectors come to the rescue though. Chrome inserts ::before and ::after in the HTML pane on the left, almost as if they were real HTML elements. Look out for these, you can click them to see which styles apply in the CSS pane on the right.
Note: pseudo elements can only be applied to HTML elements that can be containers for other HTML elements. The textarea elements cannot have HTML child elements. That’s why the comment triangle pseudo element was applied to the textarea wrapper element in the previous demonstration, instead of the textarea.
::before and ::after aren’t the only pseudo elements.
Understanding Pseudo Classes
Pseudo classes target elements that have a particular property or are in a particular state. This can be quite diverse. The most common pseudo class is probably :hover, which applies to elements in the hover state. Other pseudo classes include :first-child, which applies to elements that are the first child of their parent, and :required, which applies to form elements that have the required attribute defined. There are many more.