Dreamweaver is a WYSIWYG (What You See Is What You Get) application. Given Dreamweaver’s design strengths, why would you ever bother looking at code, much less editing it?
Believe it or not, there are people who purchase Dreamweaver who then spend most of their time in Code view. There are a number of reasons why; some of the more common we hear are:
• “It’s perfect for control freaks like me who just want to change that one character or move things one pixel.”
• “I learned tags before good WYSIWYG editors existed, so it’s faster for me to make changes this way.”
• “There are things I can do this way that can’t be done any other way in Dreamweaver.”
These are all perfectly valid reasons to want to use Code view (and there are plenty of other reasons). If you haven’t before, check it out. You might find that Code view works best for you, or that it’s useful when you simply want to tweak one small thing on your page.
Yes, it’s another toolbar in Dreamweaver, but the Coding toolbar is a little different: it only shows up when you’re in Code view, and it only acts on those things that you would deal with when you’re in Code view.
1. If you’re in Design view, click the Code button in the Document toolbar. This switches you into Code view, so you’ll now see the tags (instead of the WYSIWYG appearance of your page) and the Coding toolbar. For instance, shows a page in Design view, whereas shows the same page in Code view.
2. On the left side of the document window, you’ll see the Coding toolbar . Let’s review its buttons, from top to bottom:
• Open Documents: The name of this button might make you think that it’s a way to open documents, but that’s incorrect; it actually offers a pop-up menu of all the items that are currently open.
• Show Code Navigator: Displays the Code Navigator window. Code Navigator is covered in Chapter 7.
• Collapse Full Tag: Collapsing code (and why you’d want to do it) is covered later in this chapter. For now, it’s enough to know that this button will do it. Holding the Alt (Option) key while clicking this button collapses everything outside the current tag.
• Collapse Selection: This button collapses the current selection of code, and holding down the Alt (Option) key while clicking this button collapses everything outside the selection.
• Expand All: If any code on the page is collapsed, clicking this button expands it again.
• Select Parent Tag: This button causes the selection area to change to include the parent tag, based on the current cursor position.
• Balance Braces: If your current cursor position is inside a <script>
or <style>
container, clicking this button causes the innermost set of braces ( )
, { }
, or [ ]
to be selected. This also works inside external JavaScript and CSS files.
• Line Numbers: Toggles the display of line numbers in the current document. If they’re currently displayed, they’ll be hidden, and if they’re currently hidden, they’ll display.
• Highlight Invalid Code: If you enter tags that Dreamweaver doesn’t like and you’ve chosen this option, your mistake will be highlighted in yellow. If you click in the highlighted area, the Property Inspector will tell you more about the error .
• Word Wrap: Toggles between wrapping code to the window (on) and allowing long lines to scroll horizontally off the screen (off).
• Syntax Error Alerts in Info Bar: Turns on or off an information bar that appears at the top of the page if Dreamweaver detects a code syntax error. The program also highlights the line of code containing the error.
• Apply Comment: The pop-up menu on this button gives several choices, each of which wraps comments around the selected text:
• Apply HTML Comment: Inserts <!--
and --!>
before and after the selection (respectively).
• Apply /* */
Comment: Wraps this type of comment around the selected CSS or JavaScript.
• Apply //
Comment: Inserts this type of comment at the beginning of each selected line of CSS and/or JavaScript.
• Apply '
Comment: Inserts this type of comment at the beginning of each selected line of VBScript.
• Apply Server Comment: If you’re working on a server-side file such as PHP, JSP, or ColdFusion, Dreamweaver automatically detects the file type and inserts the correct type of comment.
• Remove Comment: Removes comment tags from the selected code.
• Wrap Tag: Opens the Quick Tag Editor (described later in this chapter) allowing you to wrap a tag around the selection.
• Recent Snippets: Allows you to insert a recently used snippet. Snippets are covered in Chapter 16, “Making Life Easier: Using Templates, Libraries, and Snippets.”
• Move or Convert CSS: Here you can either convert inline CSS to a rule or move CSS rules. CSS is covered in Chapters 7–9.
• Indent Code: Takes the selection and moves it to the right, based on your chosen indentation preference in the Code Format settings (covered in the sidebar “Text Editing Tips,” later in this chapter).
• Outdent Code: Takes the selection and moves it to the left.
• Format Source Code: From this pop-up menu, you can choose whether to apply formatting to the file as a whole or to just the current selection. Whichever you choose will be formatted as set in the Code Format settings, which can also be accessed via this pop-up menu.
Some of these options can also be set from View > Code View Options . There are a few extra ones there, also: Hidden Characters, Syntax Coloring, and Auto Indent. In earlier versions of Dreamweaver you could get to this from the View Options button in the Document toolbar, but that’s gone away in CS5.
If you appear to be missing a button or two, don’t fret—your document window may just be smaller than the Coding toolbar. If that’s the case, you’ll see a couple of downward-facing arrows at the bottom of the toolbar. Click on those arrows and the missing buttons appear off to the side .
If for some reason you don’t like this toolbar and just want it to go away, you can do that just as you would with other Dreamweaver toolbars: choose View > Toolbars > Coding. And if you change your mind, that same command brings it back.
Dreamweaver opens files in Design view by default. If you want all your HTML files to open in Code view instead, open Preferences and choose the File Types/Editors category. In the “Open in Code View” text box, add a space followed by .html
. You can add any other file types where you want to use Code view by default by adding those file extensions, too.
Does losing your WYSIWYG view freak you out a little? Wish you had a way to see both Code and Design views at once? Well, that’s exactly what the Split view is about. It splits the document window into two separate panes, so you can see both code and WYSIWYG simultaneously.
Dreamweaver CS4 added two enhancements:
• A split code view where both panes show code. This is very useful when you want to copy and paste code from one section of your page to another part of the same page.
• An option to split the pane vertically (versus the long-time ability to split it horizontally). In both horizontal and vertical split modes, you can choose which panes contain the Code and Design views.
1. Whether you’re in Design view or Code view, click the Split button in the Document toolbar. Your document window splits into two: one part for Design view and one for Code view. By default the split is vertical .
2. If you want to reverse the two (you prefer Code on the right instead of Design or vice versa), use the Layout button pop-up menu on the Application Bar . When you’re in Split view, you can choose whether Design view should go on the top (when split horizontally) or left (when split vertically).
3. (Optional) If you want to use the horizontal Split view, toggle Split Vertically from the View menu or the Layout button. The display changes from the vertical split to reflect the horizontal choice .
4. (Optional) If you want the Code view in both panes, so that you can look at different parts of the same page in each pane, choose Split Code from the View menu or the Layout button. Your display changes to reflect your choice.
If you update your page in Design view, Code view automatically updates in response. But if you update in Code view, the Property Inspector displays the message, “You have made changes to the code. To edit selection properties, click Refresh or press F5.”
When you’re in vertical Split view, the Coding toolbar only extends down the left side of the code pane, not down the full document window.
If you select anything in one view, the document automatically scrolls to show and highlight the same selection in the other view.
If you’ve never looked at HTML tags before, Split view can be a great way to familiarize yourself with which tags go with which elements of your design.
You can change the size of the panes by pointing at the border between them. When the cursor changes into a two-headed arrow, you can click and drag the border to resize the panes.
Live Code view is a version of Split view that complements Live View (see Chapter 1 for more about using Live View). Normally, when you’re in Split view, you see the Design view in one pane and the Code view in the other. If you then click the Live View button, Dreamweaver changes the rendering in the Design view pane to show how the contents of that pane would look in a Web browser. The underlying code in the Code view pane remains the same. But what if your Web page uses JavaScript for visual effects such as rollovers? As the script executes, it’s actually changing the HTML on the page, rewriting it under script control. Live Code view allows you to see how the HTML in the Code view pane changes as you interact with the page in the Design view pane. This is especially useful if you’re trying to debug a script because it is not producing the effect that you want.
1. In any view, click the Live View button on the Document toolbar.
The page rendering changes to show what the Web page would look like in a Web browser .
2. Click the Live Code button on the Document toolbar.
The document changes to Split view, with the code in the Code view pane highlighted to indicate that it is live . As you interact with the page in the Design view pane, the code in the Code view pane is updated.
There are useful options for Live Code, found in the Live View Options menu in the Browser Navigation toolbar. We discuss these options in more detail in Chapter 14, “Building Pages with Dynamic Elements.”
If you prefer Design view (as do most people who’ve bought a WYSIWYG editor, after all) but wish you could type a little bit of HTML now and then without switching back and forth between modes, there’s an answer: the Quick Tag Editor.
1. With your page in Design view, put the cursor in the place where you want to add a tag, or select the tag you want to change, or select the element you want to wrap a tag around.
2. Press Ctrl-T (Cmd-T).
or
Click the Quick Tag Editor button at the right edge of the Property Inspector.
3. The Quick Tag Editor appears, in one of its three modes: Insert, Edit, or Wrap. In any of these modes, start typing, and the possible options appear in the code hints menu below.
• Insert HTML: This mode allows you to place a new tag on the page .
• Edit tag: This allows you to modify an existing tag, either to change it or to add attributes to it .
• Wrap tag: In this mode, you can wrap a new tag around an existing element .
4. To accept your changes and leave the Quick Tag Editor, press Enter. To leave the Quick Tag Editor without making any changes, press Escape.
Dreamweaver tries to guess which Quick Tag mode you want to be in. If it guesses incorrectly, you can cycle through the three modes by pressing Ctrl-T (Cmd-T) again. Be careful about what’s actually chosen when you do that, though—sometimes, the selected area can expand. For instance, if you’ve selected the text of a link with the goal of modifying that link, but you’ve also (accidentally) selected something outside the <a>
(link) tag, you can find that you’re modifying the <p>
(paragraph) around the text instead of the link itself.
If the Quick Tag Editor appears but it’s covering an area you want to see, you can move it. Just click and drag in the area that displays the mode name to put it in the location you want.
You’ve seen the Quick Tag Editor; now here’s the Tag Editor itself. It’s not really slower than the Quick version, and it’s handy for when you want more control than you can get in, say, the Property Inspector, but you don’t want to deal with actual HTML tags.
1. With your page in Design view, select an element where you want to edit an existing tag. For instance, you can select an image or the text of a link.
2. Right-click, and choose Edit Tag.
or
Press Shift-F5.
or
From Dreamweaver’s menu, choose Modify > Edit Tag.
3. The Tag Editor dialog for that particular tag appears. Every tag has a custom dialog of its own with a variety of categories in the left column. shows the Tag Editor dialog for an <img>
tag, and shows the Tag Editor dialog for an <a>
tag. Make any desired changes.
4. Click OK when complete.
Near the bottom right of the Tag Editor dialog is the Tag info disclosure arrow. Click it, and the dialog size increases to include a description of the tag you’re modifying. For more information about this description, see “Using the Tag Chooser” and “Using the Code Reference,” later in this chapter.
You can also access the Tag Editor from Code view by right-clicking on a tag .
If you’re beginning to think that there are a lot of different ways to insert elements into a Dreamweaver page, you’re right—except that you haven’t even seen many of them yet. Here’s a tool you might not have run across yet—Dreamweaver’s Tag Chooser.
1. With your page in Code view, put the cursor into the section of the document that you want to add to or edit.
2. Right-click and a menu of the available options appears. Choose Insert Tag (see figure in “Using the Tag Editor”). The Tag Chooser dialog appears .
3. Click any of the icons or tag categories on the left side, and the matching tags will appear on the right.
4. Select one of the tags on the right, and click Insert. If the tag you chose contains angle brackets (<
and >
), it will be inserted into the page. Otherwise, a Tag Editor dialog appears, allowing you to enter the required information. For more about the Tag Editor dialog, see “Using the Tag Editor.”
5. When you are done with the Tag Chooser dialog, click Close.
1. With the Tag Chooser open , select a tag on the right side.
2. Click the Tag Info button that’s just above the Help button on the bottom left. The Tag Info reference area appears, along with useful information about the tag you selected .
or
Click the <?>
button directly to the right of the Tag Info button. The same information appears, but in the Code Reference panel. For more information about the Code Reference panel, see “Using the Code Reference,” later in this chapter.
You can also open the Tag Chooser by clicking the bottommost icon in the Insert panel when you’re in the Common category. If you do this in Design view, Dreamweaver automatically switches to Split view.
If you’ve spent much time working with tags and code, you’ll often find yourself in a situation where you wish you could hide just some of it. Thankfully, that’s something you can do with Dreamweaver.
1. In Code view, select the code that you want to collapse . The simplest way to do this is to click somewhere inside the area, and then click the Select Parent Tag button on the Coding toolbar (see figure in “Using the Coding Toolbar”).
2. Just to the left of the code, you’ll see a vertical line that goes from the beginning of the selected area to the end. Windows users will see a white box with a minus sign on each end, and Mac users will see two gray triangles, the top one facing down and the bottom one facing up. Click one of these endpoints, and the selected code collapses . You’ll now see the first part of the first line inside a highlighted box, plus a white box with a plus sign (Windows) or a right-facing gray arrow (Mac).
or
Click anywhere inside the tag you want to collapse, and click the Collapse Full Tag button on the Coding toolbar.
or
Select the code you want to collapse, and click the Collapse Selection button on the Coding toolbar.
or
Click in a particular location on the page, and press the Alt (Option) key while clicking the Collapse Full Tag button on the Coding toolbar. This collapses everything but the full tag around your particular cursor position.
or
Select a segment of code, and press the Alt (Option) key while clicking the Collapse Selection button on the Coding toolbar. This collapses everything but the current selection.
or
Right-click inside a tag on the page, and choose Selection . From there you can choose to Collapse Selection, Collapse Outside Selection, Collapse Full Tag, or Collapse Outside Full Tag.
• Put your cursor over the highlighted box and a tip appears displaying all of the collapsed code .
• Collapse a section of code if you need to move it from one area of the page to another. You’ll know that you’re moving exactly what you want to move and no more.
• If you collapse a section of code and then save and close the page, the next time you open the page that section will still show as collapsed.
• Click the collapsed code symbol (the white box with a plus sign (Windows) or the right-facing gray arrow (Mac)) to the left of the code you want to expand.
or
Double-click the highlighted box.
or
Click the Expand All button on the Coding toolbar, which expands all the collapsed code on the page.
or
Right-click on the page, and choose Selection . From there you can choose to Expand Selection or Expand All.
When it comes to working with HTML, not all text editors are created equally—and Dreamweaver’s code hints are one reason why text editor fans will use its Code view rather than someone else’s garden variety notepad-like application.
1. In Code view, click anywhere on the page that you want to add a tag. Start a new tag by typing <
, and the code hints appear in a pop-up menu .
2. Type in the first character or two of the tag, and the code hints menu should change to reflect your typing . When the hinted value is the tag you want, press Tab.
3. To add an attribute to your new tag, press the spacebar and continue to type. The code hints pop-up menu will appear again, this time displaying just the valid attributes for that tag . Again, choose the one you want and press Tab to get the full attribute added to your tag.
4. If you select an attribute that needs to link to a file, the code hint changes to a Browse button . Click the button to browse to the file on your disk.
5. If you want to add an attribute to an existing tag, click immediately after the tag name (for instance, right after the g
in <img
), press the spacebar, and the attributes available will display .
6. To end a tag, type </
, and the most recently opened tag will automatically be closed.
If the code hints pop-up menu doesn’t appear, press Ctrl-spacebar (Cmd-spacebar) to make it display.
You can use the arrow keys on your keyboard to move up and down through code hints to choose the tag or attribute you want.
If you’re using XHTML and you end a tag with >
rather than />
, code hints will automatically change it to the proper format.
You can’t modify an attribute with code hints; instead, delete the attribute and re-create it.
If you want a special character but can’t remember the exact name of it (such as
for non-breaking space), just type in the first ampersand &
and you’ll get a code hint that lists all the possible characters you can enter.
If there’s anything you don’t like about how code hints work, you can change them to suit your taste by modifying the values in the Code Hints category of Dreamweaver’s Preferences .
Newly added in CS5 is improved code hinting for PHP, including custom class hinting, integrated documentation, and site-specific code hinting for CMS and application frameworks. To control the site-specific code hinting, choose Site > Site-Specific Code Hints. The resulting dialog has presets for the three CMS platforms that Dreamweaver CS5 supports, allowing you to control how code hints are provided for WordPress, Joomla! and Drupal sites.
If you prefer to stay in Design view but every so often you just want to tweak something, the Tag Inspector panel is one of the simplest ways to do so. Along with the Tag Editor and Quick Tag Editor (covered earlier), it’s a great way to modify existing tags, and it’s an easy way to get used to the attributes that exist for each tag.
1. If the Tag Inspector currently isn’t displaying, choose Window > Tag Inspector.
2. If you’re in Design view, click on an element on the page, or choose a tag from the tag selector. If you’re in Code view, click anywhere inside a tag. The tag and its attributes appear in the Tag Inspector . Be sure the Attributes tab is selected.
3. There are two ways to view the attributes in the Tag Inspector: by category or as a list.
• To view by category, click the Show Category view button. Categories can be open or closed; to open a closed category, click the +
button (Windows) or the right-facing triangle (Mac). To close an open category, click the -
button (Windows) or the downward-facing triangle (Mac). When open, the list of attributes that fall into that category are displayed.
• To view as a list, click the Show List view button. This choice displays all the tag’s attributes in an alphabetical list .
4. You can now modify, add, or delete attributes. There are different ways to edit different types of attributes:
• If you’re adding or modifying an attribute whose value is a URL , you’ll see the Point to File icon and a browse icon to the right of the entry field. Alternatively, you can type into the entry box, which is also the case for most attributes.
• If your attribute has particular defined values, clicking in the value column displays a pop-up menu containing the valid values .
• If the attribute contains a color, clicking on the small box in the value column brings up the standard color picker .
5. There’s no OK or Accept changes button on the panel; simply clicking away from the attributes or off the panel implements your changes on your page.
As you might remember from Chapter 1, it’s also possible to change tag attributes in the Property Inspector. Because the Property Inspector only shows the most commonly used attributes, you’ll want to use the Tag Inspector those times when you need to get at the others.
Best practices for Web pages call for content to be separated from structure and behavior, so you should have separate HTML, CSS, and JavaScript documents. Separating the functions like this makes it easier to make changes in any of the documents without accidentally breaking your site. And since CSS and JavaScript documents can apply to many HTML pages on your site, changes you make in them are automatically applied to all the pages that reference those files.
Many older pages, and even pages that are created in Dreamweaver, can have JavaScript that is embedded in the HTML page. This can be within the document’s <head>
tag or can be in the <body>
. The JavaScript extractor automates moving embedded JavaScript code into an external .js
file.
1. Open a page containing JavaScript code.
2. Choose Commands > Externalize JavaScript.
Dreamweaver will analyze the page, and the Externalize JavaScript dialog will appear .
3. In the dialog, choose “Externalize JavaScript and attach unobtrusively.”
This option will move the JavaScript from the HTML page to a new external JavaScript file and add a link to the external file to the head
section of the HTML page.
4. Select or deselect any of the proposed JavaScript edits in the dialog.
5. Click OK.
Dreamweaver displays a dialog that gives you a rundown of what the Externalize JavaScript command did .
Once you externalize Dreamweaver behaviors, you can’t reedit them in the Behaviors panel, because Dreamweaver doesn’t have a way to read in JavaScript code from external files.
It’s often very handy to have a book close by when you’re first starting to work with code (we recommend HTML, XHTML, and CSS, Sixth Edition, Visual QuickStart Guide by Elizabeth Castro [Peachpit, 2006] by the way). If you don’t happen to have a book nearby, don’t fret—Dreamweaver has several built-in reference books.
• Right-click the element on the page that you have questions about, and select Reference . The Reference tab of the Results panel appears .
or
Follow the directions in “Using the Tag Chooser,” earlier in this chapter.
or
From Dreamweaver’s menu, choose Window > Reference.
1. In the Reference tab, find the code fragment you want, click it once, and the entire fragment will be selected .
2. Right-click the selected text and choose Copy.
3. Paste the copied code into your document wherever you want it.
If you don’t like the size of the text in the Code Reference tab, you can change it. Right-click anywhere inside the panel and you’ll have your choice of small, medium, or large text.
The HTML, CSS, and JavaScript reference material included in Dreamweaver is excerpted from Danny Goodman’s book, Dynamic HTML, The Definitive Reference (O’Reilly Media, 1998). It’s a darn good book, but a little out of date, so for learning HTML, we still recommend the Visual QuickStart Guide. Want to learn CSS or JavaScript? We’re required to recommend Styling Web Pages with CSS: Visual QuickProject Guide (2009) and JavaScript & Ajax for the Web: Visual QuickStart Guide (2009), both from Peachpit Press by, well, us (so you can guess who required it).
Besides the reference material, the Code Reference tab also includes information on ColdFusion, ASP, JSP, PHP, SQL, XML, and XSLT (and probably a few other three-letter acronyms, too). There’s also the UsableNet Accessibility Reference, a great resource for learning how to make your sites more accessible.
No matter how much you love Dreamweaver, there may be times when you want to use a different text editor. There are many possible editors you might use, and which one you pick is just a matter of personal preference.
1. In Dreamweaver’s Preferences, go to the File Types/Editors category. You’ll see a dialog that looks like either (Windows) or (Mac).
2. If you’re on a Mac and you want to use BBEdit as your external editor, click the Enable BBEdit Integration button, and you’re done. Otherwise (on either platform), click the Browse button next to the External Code Editor text field, navigate to your application of choice, and click Open. Next, decide when you want to reload modified files and when you want to save on launch. The choices for both are Always, Never, or Prompt.
3. Click OK to accept your changes.
4. To switch to your chosen editor, select Edit > Edit with application name .
If the application was closed, it will now launch, and the current page will open in the text editor and come to the front .
5. If you’re in BBEdit and you want to switch back to Dreamweaver, choose Markup > Misc > Dreamweaver.
The best part of using an external editor is being able to open your Dreamweaver window in Design view while you have the text visible in the external editor. You can then edit with either application, and the other application’s window updates to match. In some ways, this is more powerful than Dreamweaver’s Split view, where any time you make a change to the code, you have to manually refresh Design view. Yes, this does mean that you’ll need either a very large monitor or multiple monitors, but take our word for it—there’s no such thing as too much screen real estate.
If you haven’t set an external editor, instead displays Edit with External Editor. Choose that item, and Dreamweaver opens the File Type/Editors Preferences, as in step 1.
1. Explore each button in the Coding toolbar.
2. Split the document screen horizontally and then vertically to see Design view in one pane and Code view in the other.
3. Use Live Code view to see how your HTML in the Code view pane changes as you interact with the page in the Design view pane.
4. In Design view, use the Quick Tag Editor’s three modes: Insert, Edit, and Wrap.
5. Make changes to a tag using the Tag Editor dialog.
6. Collapse and expand parts of your code.
7. Add tags and change their attributes using code hints.
8. Use the Tag Inspector panel to modify, add, or delete tag attributes.
9. Extract any embedded JavaScript from a page.
10. Use the Code Reference tab to answer questions you might have about an element.
11. Insert code from the reference into your document.