- What is CSS?
- HTML vs. CSS formatting
- HTML defaults
- CSS box model
- Applying CSS styling
- Multiples, classes, and ids, oh my!
- Review questions
- Review answers
Multiples, classes, and ids, oh my!
By taking advantage of the cascade, inheritance, descendant, and specificity theories, you can target formatting to almost any element anywhere on a webpage. But CSS offers a few more ways to optimize and customize the formatting and increase your productivity even further.
Applying formatting to multiple elements
To speed things up, CSS allows you to apply formatting to multiple elements at once by listing each in the selector, separated by commas. For example, the formatting in these rules:
h1 { font-family:Verdana; color:gray; } h2 { font-family:Verdana; color:gray; } h3 { font-family:Verdana; color:gray; }
can also be expressed like this:
h1, h2, h3 { font-family:Verdana; color:gray; }
Using CSS shorthand
Although Dreamweaver will write most of the CSS rules and properties for you, at times you will want, or need, to write your own. All properties can be written out fully, but many can also be written using a shorthand method. Shorthand does more than make the job of the web designer easier; it reduces the total amount of code that has to be downloaded and processed. For example, when all properties of margins or padding are identical, such as:
margin-top:10px; margin-right:10px; margin-bottom:10px; margin-left:10px;
the rule can be shortened to margin:10px;
When the top and bottom and left and right margins or padding are identical, like this:
margin-top:0px; margin-right:10px; margin-bottom:0px; margin-left:10px;
it can be shortened to margin:0px 10px;
But even when all four properties are different, like this:
margin-top:20px; margin-right:15px; margin-bottom:10px; margin-left:5px;
they can still be shortened to margin:20px 15px 10px 5px;
In these three examples, you can see clearly how much code can be saved using shorthand. There are far too many references and shorthand techniques to cover here. Check out tinyurl.com/shorten-CSS to get a full description.
Throughout the book, I’ll use common shorthand expressions wherever possible; see if you can identify them as we go.
Creating class attributes
So far, you’ve learned that you can create CSS rules that format specific HTML elements and ones that can target specific HTML element structures or relationships. In some instances, you may want to apply unique formatting to an element that is already formatted by one or more existing rules. To accomplish this, CSS allows you to make your own custom attributes named class and id.
Class attributes may be applied to any number of elements on a page, whereas id attributes can appear only once per page. If you are a print designer, think of classes as being similar to a combination of Adobe InDesign’s paragraph, character, table, and object styles all rolled into one.
Class and id names can be a single word, an abbreviation, any combination of letters and numbers, or almost anything, but they may not contain spaces. In HTML 4, ids could not start with a number. There doesn’t seem to be any similar restriction in HTML5. For backward compatibility, you should probably avoid starting class and id names with numbers.
Although there’s no strict rule or guideline on how to create them, classes should be more general in nature, and ids should be more specific. Everyone seems to have an opinion, but at the moment there is no absolutely right or wrong answer. However, most agree that they should be descriptive, such as "co-address" or "author-bio" as opposed to "left-column" or "big-text". This will especially help improve your site analytics. The more sense Google and other search engines can make of your site’s structure and organization, the higher your site will rank in the search results.
To declare a CSS class selector, insert a period before the name within the style sheet, like this:
.content .sidebar1
Then, apply the CSS class to an entire HTML element as an attribute, like this:
<p class="intro">Type intro text here.</p>
Or to individual characters or words using the <span> tag, like this:
<p>Here is <span class="copyright">some text formatted differently</span>.</p>
Creating id attributes
HTML designates id as a unique attribute. Therefore, any id should be assigned to no more than one element per page. In the past, many web designers used id attributes to style or identify specific components within the page, such as the header, the footer, or specific articles. With the advent of new HTML5 elements—header, footer, aside, article, and so on—the use of id and class attributes for this purpose became less necessary. But ids can still be used to identify specific text elements, images, and tables to assist you in building powerful hypertext navigation within your page and site. You will learn more about using ids this way in Lesson 10, “Working with Navigation.”
To declare an id attribute in a CSS style sheet, insert a number sign, or hash mark, before the name, like this:
#cascade #box_model
Here’s how you apply the CSS id to an entire HTML element as an attribute:
<div id="cascade">Content goes here.</div> <section id="box_model">Content goes here.</section>
Or to a portion of an element:
<p>Here is <span id="copyright">some text</span> formatted differently.</p>
CSS3 features and effects
CSS3 has more than two dozen new features. Many have been implemented in all modern browsers and can be used today; others are still experimental and are supported less fully. Among the new features, you will find
Rounded corners and border effects
Box and text shadows
Transparency and translucency
Gradient fills
Multicolumn text elements
You can implement all these features and more via Dreamweaver today. The program will even assist you in building vendor-specific markup when necessary. To give you a quick tour of some of the coolest features and effects brewing, I’ve provided a sample of CSS3 styling in a separate file.
Open css3-demo.html from the lesson04 folder.
Display the file in Split view and observe the CSS and HTML code.
Some of the new effects can’t be previewed directly in Design view. You’ll need to use Live view or an actual browser to get the full effect.
If necessary, activate Live view to preview all the CSS3 effects in the Live view window.
The file contains a hodgepodge of features and effects that may surprise and even delight you—but don’t get too excited. Although many of these features are already supported in Dreamweaver and will work fine in modern browsers, there’s still a lot of older hardware and software out there that can turn your dream site into a nightmare. And there’s at least one additional twist.
Even now, some of the new CSS3 features have not been fully standardized, and certain browsers may not recognize the default markup generated by Dreamweaver. In these instances, you may have to include specific vendor properties to make them work properly, such as ms, moz, and webkit.
As you examine the new features demonstrated in the code of the demo file, can you think of ways of using some of them in your own pages?
CSS3 overview and support
The internet doesn’t stand still for long. Technologies and standards are evolving and changing constantly. The members of the W3C have been working diligently to adapt the web to the latest realities, such as powerful mobile devices, large flat-panel displays, and HD images and video—all of which seem to get better and cheaper every day. This is the urgency that currently drives the development of HTML5 and CSS3.
Many of these new standards have not been officially defined yet, and browser vendors are implementing them in varying ways. But don’t worry. This version of Dreamweaver, as always, has been updated to take advantage of the latest changes. This includes ample support for the current mix of HTML5 elements and CSS3 properties. As new features and capabilities are developed, you can count on Adobe to add them to the program as quickly as possible using Creative Cloud.
As you work through the lessons that follow, you will be introduced to and actually implement many of these new and exciting techniques in your own sample pages.
Additional CSS support
CSS formatting and application is so complex and powerful that this short lesson can’t cover all aspects of the subject. For a full examination of CSS, check out the following books:
CSS3: The Missing Manual (4th Edition), David Sawyer McFarland (O’Reilly Media, 2015) ISBN: 978-1-491-91801-2
CSS Secrets: Better Solutions to Everyday Web Design Problems, Lea Verou (O’Reilly Media, 2015) ISBN: 978-1-449-37263-7
HTML5 & CSS3 for the Real World (2nd Edition), Alexis Goldstein, Louis Lazaris, and Estelle Weyl (SitePoint Pty. Ltd., 2015) ISBN: 978-0-987-46748-5
Stylin’ with CSS: A Designer’s Guide (3rd Edition), Charles Wyke-Smith (New Riders Press, 2012) ISBN: 978-0-321-85847-4