Chapter 10

Tweaking WordPress Themes

In This Chapter

arrow Exploring basic CSS and defining CSS properties and values

arrow Setting a new background color, creating a header, and customizing navigation menus

arrow Changing fonts

arrow Knowing HTML essentials

Chapter 8 shows how you can use free WordPress themes in your website. Many people are quite happy to use these themes without making any adjustments to them at all. I can’t tell you, however, how many times people have asked me whether they can customize a theme that they’ve found. The answer to their question is always, “Of course you can make changes on your own.”

The practice of changing a few elements of an existing WordPress theme is known as tweaking. Thousands of WordPress site owners tweak their existing themes on a regular basis. This chapter provides information on some of the most common tweaks you can make to your theme, such as changing the header image, changing the color of the background or the text links, and changing font styles — and these changes are pretty easy to make, too! You’ll be tweaking your own theme in no time flat.

Using a theme exactly as a theme author released it is great. If a new version is released that fixes a browser compatibility issue or adds features offered by a new version of WordPress, a quick theme upgrade is very easy to do.

However, chances are good that you’ll want to tinker with the design, add new features, or modify the theme structure. If you modify the theme, you won’t be able to upgrade to a newly released version without modifying the theme again.

If only you could upgrade customized versions of themes with new features when they’re released. Fortunately, child themes give you this best-of-both-worlds theme solution. Chapter 11 explores what child themes are, how to create a parent theme that’s child-theme ready, and how to get the most out of using child themes.

remember Before you go too wild with tweaking templates, make a backup of your theme so that you have the original files from which to easily restore it if necessary. You can back up your theme files by connecting to your web server via FTP (see Chapter 3) and downloading your theme folder to your computer. When you have the original theme files safe and secure on your hard drive, feel free to tweak away, comfortable in the knowledge that you have a backup.

Styling with CSS: The Basics

A Cascading Style Sheet (CSS) is a cascading sheet of style markup that controls the appearance of content on a website. Every single WordPress theme you use in your blog uses CSS. The CSS provides style and design flair to the template tags in your templates. (See Chapter 9 for information about WordPress template tags.) The CSS for your WordPress theme is pulled in through the Header template (header.php) and is named style.css.

In the Header template (header.php) of most WordPress themes, you find the following line of code, which pulls the CSS (style.css) into the page to provide the formatting of the elements of your blog:

<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( ‘stylesheet_url’ ); ?>" />

warning Don’t tweak the line of code that pulls in the style.css file; otherwise, the CSS won’t work for your blog.

Chapter 9 covers the commonly used parameters for the bloginfo(); template tag used in WordPress themes.

CSS selectors

With CSS, you can provide style (such as size, color, and placement) to the display of elements on your blog (such as text links, header images, font size and colors, paragraph margins, and line spacing). CSS selectors contain names, properties, and values to define which HTML elements in the templates you will style with CSS. CSS selectors are used to declare (or select) which part of the markup the style applies to. Table 10-1 provides some examples of CSS selectors and their use.

Table 10-1 Basic CSS Selectors

CSS Selector

Description

HTML

CSS

body

Sets the style for the overall body of the site, such as background color and default fonts.

<body>

body {background-color: white}

The background color on all pages is white.

p

Defines how paragraphs are formatted.

<p>This is a paragraph</p>

p {color:black}

The color of the fonts used in all paragraphs is black.

h1, h2, h3, h4, h5, h6

Provides bold headers for different sections of your site.

<h1>This is a site title</h1>

h1 {font-weight: bold;}

A font surrounded by the <h1>..</h1> HTML tags is bold.

a

Defines how text links display in your site.

<a href= "http://wiley.com">Wiley Publishing</a>

a {color: red}

All text links appear in red.

Classes and IDs

You can find the stylesheet (style.css) for the default Twenty Fifteen theme on the Edit Themes page in your Dashboard (see Figure 10-1). Everything in it may look foreign to you right now, but I want to bring your attention to two items you see when you scroll down that template:

  • #content: One type of CSS selector. The hash mark (#) indicates that it’s a CSS ID.
  • .singular: Another type of CSS selector. The period (.) indicates that it’s a CSS class.
image

Figure 10-1: A WordPress theme stylesheet (style.css).

IDs and classes define styling properties for different sections of your WordPress theme. Table 10-2 shows examples of IDs and classes from the header.php template in the Twenty Fifteen WordPress theme. Armed with this information, you’ll know where to look in the stylesheet when you want to change the styling for a particular area of your theme.

Table 10-2 Connecting HTML with CSS Selectors

HTML

CSS Selector

Description

<div id="page">

#page

Styles the elements for the page ID in your template(s).

<div class=".site-header">

.site-header

Styles the elements for the site-header class in your template(s).

<h1 class= "site-title">

.site-title

Styles the elements for your site-title class in your template(s), but also follows rules for the h1 values set in the CSS.

<div class= "search-toggle">

.search-toggle

Styles the elements for your search-toggle class in your template(s).

<div id= "content">

#content

Styles the elements for the content ID in your template(s).

remember If you find an element in the template code that says id (such as div id= or p id=), look for the hash symbol in the stylesheet. If you find an element in the template code that says class (such as div class= or p class=), look for the period in the stylesheet followed by the selector name.

CSS properties and values

CSS properties are assigned to the CSS selector name. You also need to provide values for the CSS properties to define the style elements for the particular CSS selector you’re working with.

In the default Twenty Fifteen WordPress theme, the markup on line 30 in the Header template (header.php) is <div class="site-branding">. This class, with the name site-header, provides styling for the site header.

In the default Twenty Fifteen WordPress theme stylesheet, the CSS defined for the site-header class is as follows:

.site-branding {

min-height: 2em;

padding-right: 60px;

position: relative;

}

remember Every CSS property needs to be followed by a colon (:), and each CSS value needs to be followed by a semicolon (;).

The CSS selector is .site-branding, which has three properties:

  • The first CSS property is min-height, which has the value of 2em;.
  • The second CSS property is padding-right, which has the value 60px;.
  • The third CSS property is position, which has the value of relative;.

Table 10-3 provides some examples of commonly used CSS properties and values.

Table 10-3 Common CSS Properties and Values

CSS Property

CSS Value

Examples

background-color

Defines the color of the background (such as red, black, or white).

Markup: <div id="page">

CSS: #page {background-color: white}

background

Defines a background image.

Markup: <header id="banner">

CSS: header#banner {background: url(images/header.jpg) no-repeat;}

font-family *

Defines the fonts used for the selector.

Markup: <body>

CSS: body { font-family: ‘Lucida Grande’, Verdana, Arial, Sans-Serif;}

color

Defines the color of the text.

Markup: <h1>Website Title</h1>

CSS: h1 {color: blue}

font-size **

Defines the size of the font used for the text.

Markup: <h1>Website Title</h1>

CSS: h1 {font-size: 18px;}

text-align

Defines the alignment of the text (left, center, right, or justified).

Markup: <div id="wrapper">

CSS: #wrapper {text-align: left;}

* W3Schools has a good resource on the font-family property here: www.w3schools.com/cssref/pr_font_font-family.asp

** W3Schools has a good resource on the font-size property here: www.w3schools.com/cssref/pr_font_font-size.asp

Changing the Background Color

In Chapter 9, I discuss the Header template (header.php) in detail. In this section, I show you how to tweak the background color in the default Twenty Fifteen theme. If you’re not using the default Twenty Fifteen theme, you can also use the <body> tag in a Header template to change the background color of your website.

Creating a custom background

The Twenty Fifteen WordPress theme is packaged with the option to change the background to a different color or use an image for your background. To use the nifty, built-in custom background feature to change the Twenty Fifteen background for your blog, follow these steps:

  1. Click the Background link under the Appearance menu.

    The Custom Background page loads in the Dashboard.

  2. Click the Select Color to change the background color.

    You can enter a hex color code in the text box provided shown in Figure 10-2.

    If you don’t know what hex color code you want to use, click the Select Color link and click a color within the provided color picker (see Figure 10-2). The color selected in Figure 10-2 is #f1f1f1, which is gray.

    tip A hexadecimal (or hex) code represents a certain color. Hex codes always start with a hash symbol (#) and have six letters and/or numbers to represent a particular color; for example, the code #f1f1f1 represents the color gray in hexadecimal code. I talk more about hexadecimal values in the following section, “Changing the background using CSS.”

  3. Click Background Image to use an image file for the background or upload an image from your computer.

    Click the Browse button under Background Image and select a file from your the existing WordPress Media Library, or click Upload Files to upload an image from your computer. Then click the Choose Image.

  4. Change the display options for your new background image.
    • Background Repeat: Select No Repeat, Tile, Tile Horizontally, or Tile Vertically in the drop-down menu to set the image repeat behavior of the background image on your website.
    • Background Position: Select Left, Center, or Right to set the screen position of the background image on your website.
    • Background Attachment: Select Scroll to set the background image to scroll down the page, or select Fixed to set the background image in a static position (so that it doesn’t scroll down the page).
image

Figure 10-2: The color picker on the Custom Background page.

If you do not see the Background link under the Appearances menu in the Dashboard, that means that the theme you are using does not currently support custom backgrounds (note that the default theme, Twenty Fifteen, does). You can add support for the Custom Background feature to any theme with just a few lines of code.

Just follow these steps:

  1. Browse to the Edit Themes page and click the Theme Functions (functions.php ) template.

    The Theme Functions template opens in the text editor on the left side of the page.

  2. Add the following line of code to the Theme Functions template somewhere after the line that says <?php :

    add_theme_support( ‘custom-background' );

    This line of code tells WordPress that your theme has added the Custom Background feature.

  3. Click the Update File button.

    The Theme Functions template is saved, along with your changes. The Background link now displays below the Appearance menu.

  4. View your website page to see your changes.

Changing the background using CSS

The <body> tag is simple HTML markup. Every theme has this tag, which defines the overall default content for each page of your website — the site’s body. In the Twenty Fifteen stylesheet (style.css), the background for the body is defined like this:

body {

background-color: #fff;

}

The background for the <body> tag uses a hexadecimal color code of #fff that gives the background a white color. You can use a color or an image (or both) to style the background of your website:

  • Color: The W3Schools website has a great resource on hex codes and color names at www.w3schools.com/HTML/html_colornames.asp.
  • Image: You can easily use an image as a background for your site by uploading the image to the images folder in your theme directory. That value looks like background: url (images/yourimage.jpg). ( Note: The url portion of this code automatically pulls in the URL of your website, so you just need to leave the url text as is.)

You can also use a combination of colors and images in your backgrounds.

tip In the case of some basic colors, you don’t have to use the hex code. For colors such as white, black, red, blue, and silver, you can just use their names — background-color: white, for example.

If you want to change the background color of your theme, follow these steps:

  1. In the WordPress Dashboard, click the Editor link on the Appearance menu.

    The Edit Themes page opens.

  2. From the Select Theme to Edit drop-down menu, choose the theme you want to change.
  3. Click the Stylesheet template link.

    The style.css template opens in the text editor on the left side of the Edit Themes page (refer to Figure 10-1).

  4. Scroll down in the text editor until you find the CSS selector body.

    If you’re tweaking the default theme, this section is what you’re looking for:

    body:before {

    background-color: #fff;

    }

    If you’re tweaking a different template, the CSS selector body will look similar.

  5. Edit the background property’s values.

    For example, in the default template, if you want to change the background color to black, you can use one of the following:

    background-color: #000000;

    or

    background-color: black;

  6. Click the Update File button in the bottom-left corner of the page.

    Your changes are saved and applied to your theme.

  7. Visit your site in your web browser.

    The background color of your theme has changed.

Using Your Own Header Image

Most themes have a header image that appears at the top of the page. This image is generated by a graphic defined either in the CSS value for the property that represents the header area, or through the use of a feature in WordPress called a custom header. In the WordPress default Twenty Fifteen theme, all the hard work’s been done for you. Including a custom header image on a blog that uses the Twenty Fifteen theme is pretty darn easy.

To upload a new header image, click the Header link in the Appearances menu in your Dashboard, and follow these steps:

  1. Click the Add New Image button under the Header Image title.

    Select the image from the WordPress Media Library, or upload an image from your computer and then click Open.

  2. Click Select and Crop.

    The Crop Image page appears, where you can crop the image and adjust which portion of the header image you would like displayed (see Figure 10-3).

  3. Click the Crop Image button.

    The Header Image page appears, and your new header image is now displayed.

  4. View your website.

    Your new header image appears at the top of your website.

image

Figure 10-3: Adjust the dotted lines to choose the area of the image to display.

In themes that do not have the custom header image feature, you can easily define a background image for the header image using CSS.

For purposes of this example, the HTML markup for the header in the template is this:

<header id="site-header"></div>

In the CSS (style.css) file, you can use a background image by defining it in the CSS properties for #site-header. Use this code:

#site-header {

background: url(/images/header-image.jpg) no-repeat;

max-width: 100%;

height: auto;

}

The background value indicates an image called header-image.jpg. For it to display on your site, you need to create the image and upload it to your web server under /wp-content/theme-name/images/.

Creating Custom Navigation Menus

A navigation menu is a listing of links displayed on your site. These links can be to pages, posts, or categories within your site, or they can be links to other sites. Either way, you can define navigation menus on your site through the built-in Custom Menu feature in WordPress.

It’s to your advantage to provide at least one navigation menu on your site so that readers can see everything your site has to offer. Providing visitors with a link — or several — to click keeps the point-and-click spirit of the web.

Adding the Custom Menu feature to your theme

The Custom Menu feature is already built in to the default Twenty Fifteen WordPress theme, so you don’t have to worry about preparing your theme for it. However, if you’re using a different theme, adding support for the Menu feature to your theme is easy:

  1. Click the Editor link under the Appearance menu. Then click the Theme Functions (functions.php) template.

    The Theme Functions template opens in the text editor on the left side of the Edit Themes page.

  2. Type the following function on a new line in the Theme Functions template file in your theme:

    add_theme_support( ‘nav-menus’ );

  3. Click the Update File button to save the changes to the template.

    This template tag tells WordPress that your theme can use the Custom Menu feature, and a Menus link now appears under the Appearance menu in the Dashboard.

  4. Open the Header template (header.php).

    Click the Header link on the Edit Themes page to open the Header template in the text editor on the left side of the Edit Themes page.

  5. Add the following template tag by typing it on a new line in the Header template (header.php) somewhere after the <body> tag:

    <?php wp_nav_menu(); ?>

    This template tag is needed so that the menu you build using the Custom Menu feature will display at the top of your website. Table 10-4 details the different parameters you can use with the wp_nav_menu(); template tag to further customize the display to suit your needs.

  6. Save the changes you’ve made to the Header template.

    Click the Update File button at the bottom of the page.

Table 10-4 Common Tag Parameters for wp_nav_menu();

Parameter

Information

Default

Tag

id

The unique ID of the menu (because you can create several menus, each has a unique ID number).

Blank

wp_nav_menu( array( ‘id’ => ‘1’ ) );

slug

The menu name in slug form (for example, nav-menu).

Blank

wp_nav_menu( array( ‘slug’ => ‘nav-menu’ ) );

menu

The menu name.

Blank

wp_nav_menu( array( ‘menu’ => ‘Nav Menu’ ) );

menu_class

The CSS class used to style the menu list.

Menu

wp_nav_menu( array( ‘menu_class’ => ‘mymenu’ ) );

format

The HTML markup used to style the list — either an unordered list (ul/li) or div class.

div

wp_nav_menu( array( ‘format’ => ‘ul’ ) );

fallback_cb

The parameter that creates a fallback if a custom menu doesn’t exist.

wp_page_menu (the default list of page links)

wp_nav_menu( array( ‘ fallback_cb’ => ‘wp_page_menu’) );

before

The text that displays before the link text.

None

wp_nav_menu( array( ‘before’ => ‘Click Here’ ) );

after

The text that displays after the link text.

None

wp_nav_menu( array( ‘after’ => ‘&raquo;’ ) );

Building custom navigation menus

After you add the menu feature to your theme (or if you’re already using a theme that has the menu feature), building menus is easy — just follow these steps:

  1. Click the Menus link in the Appearance menu in your Dashboard.

    The Menus page opens in your WordPress Dashboard.

  2. Type a name in the Menu Name box and click the Create Menu button.

    The Menus page is reloaded with a message that tells you your new menu has been created.

  3. Add links to your newly created menu.

    WordPress gives you three ways to add links to the new menu you just created (the items in this list are shown in Figure 10-4):

    • Pages: Click the View All link to display a list of all the page(s) you have published on your site. Select the box next to the page names you want to add to your menu. Then click the Add to Menu button.
    • Custom Links: In the URL field, type the URL of the website that you want to add (https://www.google.com). Next, type the name of the link that you want displayed in your menu in the Label text field (Google). Then click the Add to Menu button.
    • Categories: Click the View All link to display a list of all the categories you’ve created on your site. Select the box next to the category names you want to add to the menu. Then click the Add to Menu button.
  4. Review your menu choices on the right side of the page.

    When you add new menu items, the column on the right side of the Menus page populates with your menu choices.

  5. Edit your menu choices, if needed.

    Click the Edit link to the right of the menu link name to edit the information of each individual link in your new menu.

  6. Save your menu before leaving the Menus page.

    Be sure to click the Save Menu button under Menu Settings on the right side at the top of the Menus page. A message appears, confirming that the new menu has been saved.

image

Figure 10-4: The Custom Menus options on the Menus page in the Dashboard.

tip You can create as many menus as you need to for your website. Just follow the parameters for the menu template tag to make sure you’re pulling in the correct menu in the correct spot on your theme. Pay attention to either the menu ID or menu name in the template tag. You find more options for your navigation menus by clicking the Screen Options tab at the top-right corner of your Dashboard. From there, you can add things like Posts and Custom Post Types to your menu options, as well as add descriptions for menu items.

The HTML markup for the menu is generated as an unordered list, by default, and looks like this in the sites source code:

<ul id="menu-main" class="menu">

<li id="menu-item-1" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1"><a href="/">Home</a></li>

<li id="menu-item-2" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2"><a href="http://localhost/wpdemo/blog/">Blog</a></li>

<li id="menu-item-3" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-3"><a href="http://localhost/wpdemo/about/">About</a></li>

</ul>

Notice in the HTML markup that the first line defines CSS ID and class: <ul id="menu-main" class="menu">. The ID in that line reflects the name that you gave your menu. Because I gave my menu the name of “Main” when I created it in the Dashboard, the CSS ID is menu-main. If I had named it “Foo,” the ID would instead be menu-foo. This assignment of menu names in the CSS and HTML markup is why WordPress allows you to use CSS to create different styles and formats for your different menus.

When developing themes for yourself or others to use, you want to make sure that the CSS you define for the menus can do things like account for subpages by creating drop-down menu effects. You can do this several different ways, and Listing 10-1 gives you just one example of a block of CSS that you can use to create a nice style for your menu (this CSS example assumes that you have a menu named “Main”; therefore, the HTML and CSS markup indicate ‘menu-main’).

Listing 10-1: Sample CSS for Drop-Down Menu Navigation

#menu-main {

            width: 960px;

            font-family: Georgia, Times New Roman, Trebuchet MS;

            font-size: 16px;

            color: #FFFFFF;

            margin: 0 auto 0;

            clear: both;

            overflow: hidden;

            }

#menu-main ul {

            width: 100%;

            float: left;

            list-style: none;

            margin: 0;

            padding: 0;

            }

#menu-main li {

            float: left;

            list-style: none;

            }

#menu-main li a {

            color: #FFFFFF;

            display: block;

            font-size: 16px;

            margin: 0;

            padding: 12px 15px 12px 15px;

            text-decoration: none;

            position: relative;

            }

#menu-main li a:hover,

#menu-main li a:active,

#menu-main .current_page_item a,

#menu-main .current-cat a,

#menu-main .current-menu-item {

            color: #CCCCCC;

            }

#menu-main li li a,

#menu-main li li a:link,

#menu-main li li a:visited {

            background: #555555;

            color: #FFFFFF;

            width: 138px;

            font-size: 12px;

            margin: 0;

            padding: 5px 10px 5px 10px;

            border-left: 1px solid #FFFFFF;

            border-right: 1px solid #FFFFFF;

            border-bottom: 1px solid #FFFFFF;

            position: relative;

            }

#menu-main li li a:hover,

#menu-main li li a:active {

            background: #333333;

            color: #FFFFFF;

            }

#menu-main li ul {

            z-index: 9999;

            position: absolute;

            left: -999em;

            height: auto;

            width: 160px;

            }

#menu-main li ul a {

            width: 140px;

            }

#menu-main li ul ul {

            margin: -31px 0 0 159px;

            }

#menu-main li:hover ul ul,

#menu-main li:hover ul ul {

            left: -999em;

            }

#menu-main li:hover ul,

#menu-main li li:hover ul,

#menu-main li li li:hover ul, {

            left: auto;

            }

#menu-main li:hover {

            position: static;

            }

remember The CSS you use to customize the display of your menus will differ; the example that I provide in the previous section is just that: an example. After you get the hang of using CSS, you can try different methods, colors, and styling to create a custom look of your own. (Find additional information about basic CSS later in this chapter.)

Displaying Custom Menus using widgets

You don’t have to use the wp_nav_menu(); template tag to display the menus on your site, because WordPress also provides you with Custom Menu widgets that you can add to your theme. You can therefore use widgets instead of template tags to display the navigation menus on your site. This feature is especially helpful if you have created multiple menus in and around your site in various different places.

Your first step is to register a special widget area for your theme to handle the Custom Menu widget display. To register this widget, open your theme’s functions.php file and add the following lines of code on a new line:

// ADD MENU WIDGET

if ( function_exists('register_sidebars') )

               register_sidebar(array(‘name’=>‘Menu Widget’,));

These few lines of code create a new widget area called Menu on the Widgets page in your Dashboard. At this point, you can drag the Custom Menu widget into the Menu Widget area to indicate that you want to display a Custom Menu in that area. The Available Widgets area with the Menu Widget displayed is in Figure 10-5.

image

Figure 10-5: Widget page displaying the Menu Widget under Available Widgets.

To add the widget area to your theme, head over to the Theme Editor (Appearance image Editor) and open the header.php file; then add these lines of code in the area you want the Menu widget displayed; for example, near the site name:

<ul>

<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘Menu Widget’) ) : ?>

<?php endif; ?>

</ul>

These lines of code tell WordPress that you want information contained in the Menu widget area displayed on your site.

Changing Font Family, Color, and Size

You can change the fonts in your theme for style or readability. I’ve seen typographic (or font) design experts use simple font variations to achieve amazing design results. You can use fonts to separate headlines from body text (or widget headlines and text from the main content) to be less distracting. Table 10-5 lists some examples of often-used font properties.

Table 10-5 Fonts

Font Properties

Common Values

CSS Examples

font-family

Georgia, Times, serif

body {font-family: Georgia; serif;}

font-size

px, %, em

body {font-size: 14px;}

font-style

Italic, underline

body {font-style: italic;}

font-weight

Bold, bolder, normal

body {font-weight: normal}

The web is actually kind of picky about how it displays fonts, as well as what sort of fonts you can use in the font-family property. Not all fonts appear correctly on the web. To be safe, consider sticking to some commonly used font families that appear correctly in most browsers:

  • Serif fonts: Times New Roman, Georgia, Garamond, Bookman Old Style
  • Sans-serif fonts: Verdana, Arial, Tahoma, Trebuchet MS

Changing font color

With more than 16 million different HTML color combinations available, you can find just the right color value for your project. After some time, you’ll memorize your favorite color codes. I find that knowing codes for different shades of gray helps me quickly add an extra design touch. For example, I often use the shades of gray listed in Table 10-6 for backgrounds, borders on design elements, and widget headers.

Table 10-6 My Favorite CSS Colors

Color

Value

White

#FFFFFF

Black

#000000

Grays

#CCCCCC

#DDDDDD

#333333

#E0E0E0

You can easily change the color of your font by changing the color property of the CSS selector you want to tweak. You can use hex codes to define the colors.

You can define the overall font color in your site by defining it in the body CSS selector like this:

body {

color: #333333;

}

Changing font size

To tweak the size of your font, change the font-size property of the CSS selector you want to change. Font sizes are generally determined by units of measurement, as in these examples:

  • px: Pixel measurement. Increasing or decreasing the number of pixels increases or decreases the font size (12px is larger than 10px).
  • pt: Point measurement. As with pixels, increasing or decreasing the number of points affects the font size accordingly (12pt is larger than 10pt).
  • em: A scalable unit of measurement that is equal to the current font size. For example, if the font size of the body of the site is defined as 12px, then 1em is equal to 12px; likewise, 2em is equal to 24px.
  • %: Percentage measurement. Increasing or decreasing the percentage number affects the font size accordingly. (If the body of the website uses 14px as the default, 50% is the equivalent to 7 pixels; 100% is the equivalent to 14 pixels.)

In the default template CSS, the font size is defined in the <body> tag in pixels, like this:

font-size: 12px;

When you put all three elements (font family, color, and font size) together in the <body> tag, they style the font for the overall body of your site. Here’s how they work together in the <body> tag of the default template CSS:

body {

font-size: 12px;

font-family: Georgia, "Bitstream Charter", serif;

color: #666666;

}

remember Serif fonts have little tails, or curlicues, at the edges of letters. (This book’s text is in a serif font.) Sans-serif fonts have straight edges and are devoid of any fancy styling. (The heading in Table 10-6 uses a sans-serif font … no tails!)

When you want to change a font family in your CSS, open the stylesheet (style.css), search for property: font-family, change the values for that property, and save your changes.

In the default template CSS, the font is defined in the <body> tag, like this:

font-family: Georgia, "Bitstream Charter", serif;

tip Font families, or fonts with multiple names, should appear in quotes in your stylesheet.

Adding borders

Using CSS borders can add an interesting and unique flair to elements of your theme design. Table 10-7 illustrates common properties and CSS examples for borders in your theme design.

Table 10-7 Common Border Properties

Border Properties

Common Values

CSS Examples

border-size

px, em

body {border-size: 1px;}

border-style

solid, dotted, dashed

body {border-style: solid}

border-color

Hexadecimal values

body {border-color: #CCCCCC}

Finding additional CSS resources

The time may come when you want to explore customizing your theme further. Here are some recommended resources:

Understanding Basic HTML Techniques

HTML can help you customize and organize your theme. To understand how HTML and CSS work together, think of it this way: If a website were a building, HTML would be the structure (the studs and foundation) and CSS would be the paint.

HTML contains the elements that CSS provides the styles for. All you have to do to apply a CSS style is use the right HTML element. For this example, I break down a basic block of HTML:

<body>

<div id="content">

<header>

<h1>Headline Goes Here</h1>

</header>

<section>

<article>

<p>This is a sample sentence of body text. <blockquote>The journey of a thousand miles

starts with the first step.</blockquote> I'm going to continue on this sentence and end it

here. </p>

<p>Click <a href="http://lisasabin-wilson.com">here</a> to visit my website.</p>

</article>

</section>

</div>

</body>

All HTML elements must have opening and closing tags. Opening tags are contained in less-than (<) and greater-than (>) symbols. Closing tags are the same, except that they are preceded by a forward-slash ( / ).

For example:

<h1>Headline Goes Here</h1>

Note that the HTML elements must be properly nested. In line eight of the preceding example, a paragraph tag is opened (<p>). Later in that line, a block quote is opened (<blockquote>) and is nesting inside the paragraph tag. When editing this line, you could not end the paragraph (</p>) before you end the block quote (</blockquote>). Nested elements must close before the elements they are nested within close.

Finally, proper tabbing, or indenting, is important when writing HTML, mainly for readability so that you can quickly scan through code to find what you’re looking for. A good rule to follow is that if you didn’t close a tag in the line above, indent one tab over. This practice allows you to see where each element begins and ends. It can also be very helpful when diagnosing problems.

You will use several very basic HTML markup practices over and over in web design and putting together websites. Earlier in this chapter, I discuss how to combine CSS styling with HTML markup to create different display styles (borders, fonts, and so on).

The following sections provide you with commonly used HTML markup samples that you will find helpful as a reference for using HTML in your website code.

Inserting images

You will probably want to insert an image into your website, whether it is within the body of a post or page, in the sidebar by using a widget, or within the template code itself. The HTML markup to insert an image looks like this:

<img src="/path/to/image-file.jpg" alt="Image File Name" />

I break down this code for you in easy snippets to help you understand what’s at work here:

  • <img src=: This is the HTML markup that tells the browser that the website is looking for an image file.
  • "/path/to/image-file.jpg": This is the actual directory path through which the web browser will find the physical image file. For example, if you uploaded an image to your web server in the /wp-content/uploads directory, the physical path for that image file would be /wp-content/uploads/image-file.jpg.
  • alt="Image File Name": The alt tag is part of the HTML markup and provides a description for the image that search engines will pick up and recognize as keywords. The alt tag description will also display as text on a browser that cannot, for some reason, load the image file; for example, if the server load time is slow, or if the user is using a screen reader with images turned off, the text description will load to at least provide visitors with a description of what the image is.
  • />: This HTML markup tag closes the initial <img src=" tag, telling the web browser when the call to the image file is complete.

Inserting hyperlinks

You will probably want to insert a link within the body of a website — commonly referred to as a hyperlink, which is a line of text that is anchored to a web address (URL) so that clicking the text takes a visitor to another website or page that appears in the browser window.

The HTML markup to insert a hyperlink looks like this:

<a href="http://wiley.com">John Wiley & Sons, Inc. </a>

To break down that markup, here is a simple explanation:

  • <a href=: This is the HTML markup that tells the browser that the text within this tag should be hyperlinked to the web address provided in the next point.
  • "http://wiley.com": This is the web address, or URL, that you intend the text to be anchored to. It needs to be surrounded by quotation marks, which define it as the intended anchor, or address.
  • >: This markup closes the previously opened <a href= HTML tag.
  • John Wiley & Sons, Inc.: In this example, this is the text that is linked, or anchored, by the web address, or URL. This text displays on your website and is clickable by your visitors.
  • </a>: This HTML markup tag tells the web browser that the hyperlink is closed. Anything that exists between <a href=".."> and </a> will be hyperlinked, or clickable, through to the intended anchor, or web address.

Commonly, designers use URLs, or web addresses, to link words to other websites or pages; however, you can also provide hyperlinks to files such as .pdf (Adobe Acrobat), .doc (Microsoft Word), or any other file type.

Inserting lists

Say you need to provide a clean-looking format for lists of information that you publish on your website. With HTML markup, you can easily provide lists that are formatted differently, depending on your needs.

Ordered lists are numbered sequentially. An example is a step-by-step list of things to do, like this:

  1. Write my book chapters.
  2. Submit my book chapters to my publisher.
  3. Panic a little when book is released to the public.
  4. Breathe sigh of relief when public reviews are overwhelmingly positive!

Ordered lists are easy to create in a program such as Microsoft Word, or even in the WordPress post editor because you can use the WYSIWYG editor to format the list for you. However, if you want to code an ordered list using HTML, the experience is a little different. My previous step list sample looks like this when using HTML markup:

<ol>

<li>Write my book chapters.</li>

<li>Submit my book chapters to my publisher.</li>

<li>Panic a little when book is released to the public.</li>

<li>Breathe sigh of relief when public reviews are overwhelmingly positive!</li>

</ol>

The beginning <ol> tells your web browser to display this list as an ordered list, meaning that it will be ordered with numbers starting with the number 1. The entire list ends with the </ol> HTML tag, which tells your web browser that the ordered list is now complete.

Between the <ol> and </ol> are list items designated as such by the HTML markup <li>. Each list item starts with <li> and ends with </li>, which tells the web browser to display the line of text as one list item.

warning If you don’t close an open HTML markup tag — for example, if you start an ordered list with <ol> but don’t include the closing </ol> at the end — it messes up the display on your website because the web browser considers anything beneath the initial <ol> to be part of the ordered list until it recognizes the closing tag: </ol>.

Unordered lists are very similar to ordered lists, except that instead of using numbers, they use bullet points to display the list, like this:

  • Write my book chapters.
  • Submit my book chapters to my publisher.
  • Panic a little when book is released to the public.
  • Breathe sigh of relief when public reviews are overwhelmingly positive!

The HTML markup for an unordered list is just like the ordered list, except that instead of using the <ol> tag, it uses the <ul> tag (UL = unordered list):

<ul>

<li>Write my book chapters.</li>

<li>Submit my book chapters to my publisher.</li>

<li>Panic a little when book is released to the public.</li>

<li>Breathe sigh of relief when public reviews are overwhelmingly positive!</li>

</ul>

Note that both the ordered and unordered lists use the list item tags, <li> and </li>, and the only difference is in the first opening and last closing tags:

  • Ordered lists: Use <ol> and </ol>
  • Unordered lists: Use <ul> and </ul>
  • List items: Use <li> and </li>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.136.170