ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

A Tutorial Series CSS for Beginners: Cascading Style Sheets - An Introduction

Updated on March 18, 2014

Starting at the Beginning

This is a good place to begin your understanding of styling information contained in web pages. Because we assume that you are beginning working with HTML, implementing styling of pages is a skill you will soon need to develop to create eye catching web pages. Being new to HTML as well you might want to follow my tutorials on that and work in tandem with these tutorials. The anchor for that series is "A Web Tutorial Series: Creating Your Own Presence on the World Wide Web - Introducing HTML 5".

What is CSS and Why Consider It Necessary?

CSS is the acronym for Cascading Style Sheets, a product added on with the introduction of HTML 4.0 to solve the problem related to styling which of web pages. The problem can be simply stated that in the early days of the world wide web, the "solution" was to style web pages through the use of elements and attributes added to the HTML coded, HTML formatting.

In the simple examples presented in this tutorial it should become apparent in that other than for insignificant changes to the "look" of a web page, which HTML formatting can provide, the inclusion of styling elements in an HTML is not the answer. Formatting in HTML can become:

  • cumbersome and repetitious, and
  • distracting from the objective of providing "best content". The true objective of HTML, providing the "best" content.


The Following Images Demonstrate Elements of the Problem

The next screen shows the result (distilled into one HTML file) of a hypothetical situation in which a group of developers were assigned the task of developing a web site. The intention of the Web architect was that the web design would have the same look and feel throughout the web site. Headings would be the cast in the same size, font and color. The developers were told that all heading one (h1) tags would be green.

Each of the developers had their own concept of green and came up with the resulting three colors. Also, if you look at the code, they each found a different way of implementing that style element: one just styled to the color green, another used a rgb (red,green, blue) designation, while the third used a hexadecimal coding for the color. There would have to be rework there! Look also at the second bookmark, the content is dwarfed by the need to eliminate the underline.

Also, and this is a simple example, look at how much of the "screen real estate" was dedicated to setting the color. Now, if font information was also needed to be specified, more space would be taken up by the coding of style. The "meat" of the web implementation, the content, seems to get lost. If instead the developers concentrated on the page structure and content rather than styling, they could just concentrate on the fact that their page would start with an heading tag. Styling decisions would not take their focus away from the content. Styling could be centralized and handled by an expert in typography. Any changes to these styling elements could take place later across the whole development project almost effortlessly.

This is the real purpose of CSS. It goes hand and hand with HTML to enhance the look of the content. From here on out you should begin to think:


HTML = content

CSS = styling


Looking at A Small Example

This example illustrated the fact that with just the styling of color for a heading. There are not only different colors possible, there are different ways of achieving that color.
This example illustrated the fact that with just the styling of color for a heading. There are not only different colors possible, there are different ways of achieving that color.
This is the  output of the previous screenshot.
This is the output of the previous screenshot.

CSS Syntax

The syntax of CSS is relatively simple. It begins with:

  • a selector - which is generally the name of the element we with to style
  • s declaration (or group of declarations) enclosed within curly brackets which state:
  • a property which is the style attribute which you wish to modify
  • the value of the property
  • a delimiter of a semi-colon for each declaration (property-value pair)

In our example of the <h1> tag. The equivalent CSS code would be:

h1 { color: green;}

Tip: Even if there is only one declaration terminating it with a ; is a good coding practice.

Comments in CSS

Comments in CSS are delineated by the pair: /* and */.

Example:

/* We used #e3e3e3 which is a nice grey color */


Being More Specific About Applying Styling

Our original example above, it was indicated that needing to specify styling in each occurrence of the <h1> tags which occurred within a web page or site was not desirable. This formed the rationale for having CSS centralize the formatting of the element by creating a CSS statement such as:

#h1 { color:green;}

was probably what the web designer would want. However, suppose the intention was to apply styling for that tag and alternative styling for the other <h1> tags in the site.

CSS provides for two mechanisms to accomplish this: the id and class selectors.

The id selector is used when there is a requirement to style one unique element within the document or site. The relationship between the element and the CSS specification is performed as follows:

when creating the CSS styling definition prepend a # (pound sign) to a identifying name and complete the definition. The id name where the id name is the value of the HTML id attribute.

For example to apply the color style "blue" to a paragraph one might do the following:

#blueparagraph { color:blue;}

and the paragraph would appear as follows:

<p id="blueparagraph">

............text of the paragraph..........

</p>

Remember, you want to use this for a single, unique element.

Suppose you wanted to alternate colors of paragraphs on a web page you would use the class selector. An example might be alternating red and blue paragraphs. Using the class selector could accomplish this.

The class selector is specified by a . (period) followed by a class name which is then associated with the HTML elements by specifying the HTML class attribute. The paragraph example would be as follows:

The CSS declarations might be:

.blueparagraph {color: blue;}

.redparagraph {color: red;}

While the paragraphs would be coded as follows:

<p class="blueparagraph">

........paragraph text..........

</p>

<p class="redparagraph">

.......paragraph text..........

</p>

<p class="blueparagraph">

........paragraph text ......

</p>

etc.



Examples of Using CSS ( # and .) With HTML Attributes id= and class=

In this example we styled a single element  as green and centered. The paragraphs we alternated red and blue using class.
In this example we styled a single element as green and centered. The paragraphs we alternated red and blue using class.
Output of the preceding code. Note that while the comments are within the  tags, they are inside the  tag thus CSS comment form is required. This would be a common mistake.
Output of the preceding code. Note that while the comments are within the tags, they are inside the tag thus CSS comment form is required. This would be a common mistake.
Creating document style with CSS can make you web pages outstanding.
Creating document style with CSS can make you web pages outstanding.

Tutorial Recap and What's Next

In this tutorial we explained the rationale for CSS. When features for styling were added to HTML 3.2 for styling (color, font, etc.) the content of the document was dwarfed by the styling.

We have covered most of the syntax for using the HTML attributes for id and class. There are several other considerations for their use which we will cover at the beginning of the next tutorial.

The next tutourial when then embark on looking at other aspect of formatting with CSS: specifically we will look at fonts and text formatting as well as the formatting of links.

We will also be moving our CSS definitions to their own file and demonstrate how that is done.

Please Rate this Tutorial for Completeness & Clarity

5 out of 5 stars from 1 rating of Rate this tutorial.
working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)