Publishers of technology books, eBooks, and videos for creative people

Home > Articles

This chapter is from the book

Working with CSS preprocessors

One of the biggest additions to Dreamweaver over the last few years was support for industry-standard CSS preprocessors. Known by the acronyms LESS (Leaner CSS), Sass (Syntactically Awesome Style Sheets), and SCSS (Sassy CSS), these are scripting languages that enable you to extend the capabilities of cascading style sheets with a variety of productivity enhancements that can then be compiled in a standard CSS file. These languages provide a variety of benefits for designers and developers who prefer to write their code by hand, including speed, ease of use, reusable snippets, variables, logic, calculations, and much more. No other software is needed to work in these preprocessors, but Dreamweaver also supports other frameworks, such as Compass and Bourbon.

In this exercise, you’ll get a taste of how easy it is use preprocessors with Dreamweaver as well as what advantages they offer compared to a regular CSS workflow.

Enabling a preprocessor

Support for CSS preprocessors is site-specific and must be enabled for each site defined in Dreamweaver, as desired. To enable LESS, Sass, or SCSS, you first define a site and then enable the CSS Preprocessors option within the Site Definition dialog.

  1. Select Site > Manage Sites.

    The Manage Sites dialog appears.

  2. Select lesson04 in the Manage Sites window.

    Click the Edit icon icon_edit.jpg at the bottom of the Manage Sites window.

    The Site Definition dialog for lesson04 appears.

  3. Select the CSS Preprocessors option in the Site Definition dialog.

    The CSS Preprocessors option contains six subcategories, including General, Source & Output, and options for various Compass and Bourbon frameworks. You can check out the Dreamweaver Help topics for more information on these frameworks. For this exercise, you need only the features that are built into the program itself.

  4. Select the General category.

    When selected, this category features the on/off switch for the LESS, Sass, or SCSS compiler, as well as various options for how the languages operate. For our purposes, the default settings will work fine.

  5. Select the Enable Auto Compilation On File Save checkbox to enable the preprocessor compiler, if necessary.

    When this is enabled, Dreamweaver will automatically compile your CSS from your LESS, Sass, or SCSS source files whenever they are saved. Some designers and developers use the root folder of the site for compilation. In this case, we’ll separate the source and output files in distinct folders.

  6. Select the Source & Output category.

    This category enables you to designate the source and output folders for your CSS preprocessor. The default option targets the folder where the source file is saved.

  7. Select the Define Output Folder option.

    When you enable this option, Dreamweaver displays a file path pointing to a folder (css). This folder doesn’t exist yet, but Dreamweaver will create it automatically. If you want to use a different folder, you will have to use the Browse For Folder icon_browse.jpg icon to select or create the folder.

  8. Click the Browse For Folder icon beside the Source Folder field.

  9. Navigate to the Site Root folder.

  10. Select the existing Sass folder, and click Select Folder/Choose.

  11. Save the changes and click Done to return to your site.

The CSS preprocessor is enabled, and the source and output folders are now designated. Next, you’ll create the CSS source file.

Creating the CSS source file

When using a preprocessor workflow, you do not write the CSS code directly. Instead, you write rules and other code in a source file that is then compiled to the output file. For the following exercise, you’ll create a Sass source file and learn some of the functions of that language.

  1. Select Standard from the Workspace menu.

  2. Choose Window > Files to display the Files panel, if necessary.

    Select lesson04 from the Site List dropdown menu, if necessary.

  3. If necessary, open myfirstpage.html and switch to Split view.

    The webpage is unstyled at the moment.

  4. Choose File > New.

    The New Document dialog appears. This dialog allows you to create all types of web-compatible documents. In the Document Type section of the dialog, you will see the LESS, Sass, and SCSS file types. We’ll use SCSS in the following exercises. SCSS is a flavor of Sass that uses a syntax that is similar to regular CSS and that many users find easier to learn and work with.

  5. Choose New Document > SCSS.

    Click the Create button.

    A new blank SCSS document appears in the document window.

  6. Save the file as favorite-styles.scss in the Sass folder you targeted as the Source folder in the previous exercise.

    You don’t need to create the CSS file; the compiler in Dreamweaver will do that for you. You’re all set to start working with Sass. The first step is to define variables. Variables are programmatic constructs that enable you to store CSS specifications you want to use multiple times, such as colors in your site theme. By using a variable, you have to define it only once. If you need to change it in the future, you can edit one entry in the style sheet and all the instances of the variable will update automatically.

  7. Insert the cursor into line 2 of favorite-styles.scss. Type $logoyellow: #ED6; and press Enter/Return.

    You’ve created your first variable. This is the main yellow color of the site theme. Let’s create the rest of the variables.

  8. Type $darkyellow: #ED0;

    $lightyellow: #FF3;

    $logoblue: #069;

    $darkblue: #089;

    $lightblue: #08A;

    $font-stack: "Trebuchet MS", Verdana, Arial,

    Helvetica, sans-serif;

    and press Enter/Return to create a new line.

    Entering the variables on separate lines makes them easier to read and edit but does not affect how they perform. Just make sure you add a semicolon (;) at the end of each variable.

    Let’s start the style sheet with the base or default styling of the body element. SCSS markup in most cases looks just like regular CSS, except in this case you’ll use one of your variables to set the font family.

  9. Type body and press the spacebar.

    Type { and press Enter/Return.

    When you typed the opening brace ({), Dreamweaver created the closing brace automatically. When you created the new line, the cursor was indented by default, and pressing Enter/Return moved the closing brace to the following line. You can also use Emmet to enter the settings more quickly.

  10. Type ff$font-stack and press Tab.

    The shorthand expands to font-family: $font-stack;.

  11. Press Enter/Return to create a new line within the body rule. Type c and press Tab.

    The shorthand expands to color: #000;. The default color is acceptable.

  12. Hold the Alt/Cmd key and press the Right Arrow key to move the cursor to the end of the current line of code.

  13. Press Enter/Return to create a new line. Type m0 and press Tab.

    The shorthand expands to margin: 0; completing the basic styling for the body element. Before you save the file, this is a good time to see how preprocessors do their work.

Compiling CSS code

You have completed the specifications for the body element. But you have not created the styling directly in a CSS file. Your entries were made entirely in the SCSS source file. In this exercise, you will see how the compiler that is built into Dreamweaver generates the CSS output.

  1. Display the Files panel, if necessary, and expand the list of site files.

    The site consists of one HTML file and three folders: Sass, images, and css.

  2. Expand the view of the css and Sass folders.

    The Sass folder contains favorite-styles.scss and _base.scss. The css folder contains favorite-styles.css. This file did not exist when you started the lesson. It was generated automatically when you created the SCSS file and saved it into the site folder defined as the Source folder. At the moment, the CSS file should contain no CSS rules or markup. It’s also not referenced in the sample webpage.

  3. Select the document tab for myfirstpage.html.

    Choose View > Split > Code-Live.

    Choose View > Split > Horizontally, if necessary.

    The document window is split into two windows top to bottom, showing the rendered webpage in one and the code in the other. The page shows only default HTML styling at this point.

  4. In the Code view window, insert the cursor after the opening <head> tag and press Enter/Return to insert a new line.

  5. Type link and press Tab.

    The shorthand expands to a <link> reference for a style sheet. It comes in with two attributes, rel and href. You’ll use the href attribute to link the webpage to the generated CSS file.

  6. Insert the cursor between the quotation marks in the href attributes.

  7. Type /css/

    As you type, Dreamweaver displays a hinting menu for the file structure of the site. Once you type the second backslash, you should see the CSS file created automatically by the preprocessor.

  8. Press the Down Arrow key to highlight the filename favorite-styles.css.

  9. Press Enter/Return.

    The URL to the CSS output file appears in the attribute. The link to the style sheet is now complete.

    The CSS output file is now referenced by the webpage. In the Live view window, there should be no difference in the styling, but you should now see favorite-styles.css displayed in the Related Files interface.

  10. Select favorite-styles.css in the Related Files interface.

    Code view displays the contents of favorite-styles.css, which is empty at the moment. An asterisk appears next to the filename in the document tab for favorite-styles.scss, indicating that the file has been changed but not saved.

  11. Choose Window > Arrange > Tile.

    The webpage and the SCSS source file appear side by side in the program window.

  12. Insert the cursor anywhere in the favorite-styles.scss document window and choose File > Save All.

After a moment, the display of myfirstpage.html changes, showing the new font and margin settings. The Code view window also updates to display the new contents of favorite-styles.css. Each time you save the SCSS source file, Dreamweaver will update the output file.

Nesting CSS selectors

Targeting CSS styling to one element without accidentally affecting another is a constant challenge for web designers everywhere. Descendant selectors are one method for ensuring that the styling is applied correctly. But creating and maintaining the correct descendant structure becomes more difficult as the site and style sheets grow in size. All preprocessor languages offer some form of nesting for selector names.

In this exercise, you will learn how to nest selectors while styling the navigation menu. First, you’ll set the basic styling for the <nav> element itself.

  1. In the favorite-styles.scss window, insert the cursor after the closing brace (}) on line 13 for the body rule.

  2. Create a new line; type nav { and press Enter/Return.

    The nav selector and declaration structure are created and ready for your entry. Emmet provides shorthand entries for all CSS properties.

  3. Type bg$logoyellow and press Tab. Press Enter/Return.

    The shorthand expands to background: $logoyellow, which is the first variable you created in the SCSS source file. This will apply the color #ED6 to the nav element.

  4. Type ta:c and press Tab. Press Enter/Return.

    The shorthand expands to text-align: center.

  5. Type ov:a and press Tab. Press Enter/Return.

    The shorthand expands to overflow: auto.

  6. Save the source file.

    The <nav> element in myfirstpage.html displays the color #ED6. The menu doesn’t look like much yet, but you’ve only just begun. Next, you’ll format the <ul> element. Note that the cursor is still within the declaration structure for the nav selector.

  7. Type ul { and press Enter/Return.

    The new selector and declaration are created within the nav rule.

  8. Type lis:n+m5 and press Tab.

    The shorthand expands to list-style: none and margin: 5px. These properties reset the default styling of the unordered list, removing the bullets and indent. Next, you’ll override the styling of the list items.

  9. Press Enter/Return and type li { Press Enter/Return again.

    As before, the new selector and declaration are fully within the ul rule.

  10. Create the property d:ib and press Tab. Press Enter/Return.

    The property display: inline-block will display all the links in a single row, side by side. The last element to style is the <a> for the link itself.

  11. Type a { and press Enter/Return. Type m:0+p:10-15+c:$logoblue+td:n+bg:$lightyellow and press Tab.

    The shorthand expands to show the properties margin, padding, color, text-decoration, and background styling the rule a entirely within the li rule. Each of the rules styling the navigation menu has been nested one inside the other in a logical, intuitive manner and will result in an equally logical and intuitive CSS output.

  12. Save the file.

The navigation menu in myfirstpage.html is reformatted to display a single line of links, side by side. The CSS output file displays several new CSS rules. The new rules are not nested as in the source file. They are separate and distinct. More surprisingly, the selectors have been rewritten to target the descendant structures of the menu, such as nav ul li a. As you can see, nesting rules in the SCSS source file eliminates the chore of writing complex selectors.

Importing other style sheets

To make CSS styling more manageable, many designers split their style sheets into multiple separate files, such as one for navigation components, another for feature articles, and still another for dynamic elements. Large companies may create an overall corporate standard style sheet and then allow various departments or subsidiaries to write custom style sheets for their own subdomains, products, and purposes. Eventually, all these CSS files need to be brought together and called by the webpages on the site, but this can create a big problem.

Every resource linked to a page creates an HTTP request that can bog down the loading of your pages and assets. This is not a big deal for small sites or lightly traveled ones. But popular, heavily traveled sites with tons of HTTP requests can overload a web server and even cause pages to freeze in a visitor’s browser. Too many experiences like this can cause visitors to flee and never return.

Reducing or eliminating superfluous HTTP calls should be the goal of any designer or developer, but especially those working on large enterprise or highly popular sites. One important technique is to cut down on the number of individual style sheets called by each page. If a page needs to link to more than one CSS file, it’s usually recommended that you designate one file as the main style sheet and then simply import the other files into it, creating one large universal style sheet.

In a normal CSS file, importing multiple style sheets would not produce any benefit, because the import command creates the same type of HTTP request that you’re trying to avoid in the first place. But since you are using a CSS preprocessor, the import command happens before any HTTP request occurs. The various style sheets are imported and combined. Although this makes the resulting style sheet larger, this file is downloaded only once by the visitor’s computer and then cached for their entire visit, speeding up the process overall.

Let’s see how easy it is to combine multiple style sheets into one file.

  1. Open myfirstpage.html and switch to Split view, if necessary. Open favorite-styles.scss and choose Window > Arrange > Tile.

    The two files are displayed side by side to make it easier to edit the CSS and see the changes as they occur.

  2. In myfirstpage.html, click favorite-styles.css in the Related Files interface.

    Code view displays the content of favorite-styles.css. It contains the output of rules written in the SCSS source file.

  3. In favorite-styles.scss, insert the cursor before the body rule (around line 9). Type @import "_base.scss"; and press Enter/Return to insert a new line.

    This command imports the contents of the file _base.scss stored in the Sass folder. The file was created ahead of time to style other portions of your page. At the moment, nothing has changed, because favorite-styles.scss has not been saved yet.

  4. Save favorite-styles.scss and observe the changes in myfirstpage.html.

    If you correctly followed the instructions on how to create the HTML structure earlier in this lesson, the page should be entirely formatted now. When you examine favorite-styles.css, you will see that dozens of rules were inserted before the body rule. Imported content will be added starting at line 2. This may be confusing at first since the SCSS file had seven lines of code before the body rule. Although the @import command was at line 9 in the SCSS file, the variables are not passed to the final CSS directly. They are parsed and rendered into each of the rules they affect. Once the content has been imported, normal CSS precedence and specificity take effect. Just make sure that all rules and file references appear after the variables; otherwise, the variables won’t work.

  5. Save and close all files.

In this section, you created an SCSS file and learned how to work with a CSS preprocessor. You experienced various productivity enhancements and advanced functionality and have glimpsed just a bit of the breadth and scope of what is possible.

Learning more about preprocessors

Check out the following books to learn more about CSS preprocessors and supercharging your CSS workflow:

Beginning CSS Preprocessors: With SASS, Compass.js, and Less.js, by Anirudh Prabhu, Apress (2015); ISBN: 978-1484213483

Instant LESS CSS Preprocessor How-to, by Alex Libby, Packt Publishing (2013); ISBN: 978-1782163763

Jump Start Sass: Get Up to Speed with Sass in a Weekend, by Hugo Giraudel and Miriam Suzanne, SitePoint (2016); ISBN: 978-0994182678

Using linting support

Dreamweaver provides live code error checking. Linting support is enabled by default in Preferences, which means the program monitors your code writing and flags errors in real time.

  1. Open myfirstpage.html and switch to Code view. If necessary, select Source Code in the Related Files interface.

  2. Insert the cursor after the opening <article> tag and press Enter/Return to create a new line.

  3. Type <h1>Insert headline here</h1>

  4. Delete the closing </h1> tag.

  5. Save the file.

    You failed to close the <h1> element in step 3. When an error occurs, a red X will appear at the bottom of the document window whenever you save the page.

  6. Click the X icon icon_linterror.jpg.

    The Output panel opens automatically and displays the coding errors. In this case, the message says that the tag must be paired and identifies what line it thinks the error occurs on. The message erroneously targets line 27, but this can happen because of the nature of HTML tags and structures.

  7. Double-click the error message.

    Dreamweaver focuses on the article in the Code view window that it identifies as containing the error. Since Dreamweaver is looking for the closing tag for the <h1> element and flags it. Unfortunately, the error message is not always this accurate. Dreamweaver’s linting function will get you close to the error, but often you will have to look earlier in the code to track down the actual issue yourself.

  8. Insert the cursor at the end of the code <h1>Insert headline here. Type </h1>

    Dreamweaver should now be closed. If not, go ahead and finish it properly.

  9. Save the file.

    Once the error is corrected, the red X is replaced by a green checkmark.

  10. Right-click the Output panel tab and select Close Tab Group from the context menu.

It’s important to be alert for this icon as you save your work. No other error message will pop up indicating any problems, and you’ll want to catch and correct any errors before uploading your pages to the web server.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020