Sayfalar

5 Şubat 2011 Cumartesi

Structuring HTML Markup


There are endless ways to format the same chunk of HTML. Nested tags can be indented, and long tags
are often broken over several lines for better readability. However, the exact amount of indentation and
the preferred line length vary from person to person.
Because of these variations, Visual Studio doesn’t enforce any formatting. Instead, it always
preserves the capitalization and indenting you use. The drawback is that it’s easy to be inconsistent and
create web pages that use widely different formatting conventions or have messily misaligned tags.
To help sort this out, Visual Studio offers an innovative feature that lets you define the formatting
rules you want to use and then apply them anywhere you want. To try this, switch to the source view for
a page. Now, highlight some haphazard HTML, right-click the selection, and choose Format Selection.
Visual Studio will automatically straighten out the selected HTML content, giving it the correct
capitalization, indenting, and line wrapping.

Of course, this raises an excellent question—namely, who determines what the correct formatting
settings are? Although Visual Studio starts with its own sensible defaults, you have the ability to fine-tune
them extensively. To do so, right-click anywhere in the HTML source view, and choose Formatting and
Validation. This shows the Options dialog box, positioned at the Text Editor ➤ HTML ➤ Formatting
group of settings (see Figure 2-6).

This section lets you control what capitalization settings are used and how long lines can be before
they have to wrap. By default, lines don’t wrap until they hit an eye-straining 80 characters, so many
developers choose to decrease this number. You can also control how attributes are quoted and set
whether Visual Studio should automatically add the matching closing tag when you add an opening tag.


If you’re even more ambitious, you can click the Tag Specific Options button to set formatting rules
that apply only to specific tags. For example, you can tell Visual Studio to add line breaks at the
beginning and end of a <div> tag. Or, you can tell Visual Studio to use different colors to highlight
specific tags, such as tags that you often need to locate in a hurry or tags you plan to avoid. (For example,
developers who are planning to move to a CSS-based layout might try avoiding <table> tags and use
color-coding to highlight them.)
Along with the formatting settings, the Options dialog box also has several useful settings in the
subgroups of the HTML group:
General: Lets you configure Visual Studio’s automatic statement completion, use automatic
wrapping, and turn on line numbers to help you locate hard-to-remember places in your pages.
Tabs: Lets you choose the number of spaces to insert when you press Tab.
Miscellaneous: Includes the handy Format HTML on Paste option, which isn’t enabled by default.
Switch this on, and your formatting rules are applied whenever you paste new content into the
source view.

Validation: Lets you set the browser or type of markup you’re targeting (for example, HTML 4.01 or
XHTML 1.1). Depending on your choices, Visual Studio will flag violations, such as the use of
deprecated elements. (You can also change this option using the HTML Source Editing toolbar,
where the option appears as a drop-down list.)
As these settings show, Visual Studio is a great partner when adding ordinary HTML content to
ASP.NET pages.



Hiç yorum yok:

Yorum Gönder

Bing'ı Seviyoruz. :)
Google'ı Seviyoruz. :)