Posts Tagged ‘class 3’

Class 3 – Default CSS style sheets for the major web browsers

Saturday, February 20th, 2010

Firefox

There are two default CSS style sheets that Firefox uses to display web pages.  Type these into the address bar of the browser to view them:

resource://gre/res/html.css
resource://gre/res/forms.css

Safari and Chrome

Apple’s Safari and Google’s Chrome browsers are built using an open-source layout engine known as Webkit.

Here is a link to the official WebKit source code for the default CSS style sheets of Safari and Chrome.

Internet Explorer

At this point in you will not be surprised to know that Microsoft Internet Explorer does not provide you access to it’s default CSS style sheet files.  Yet another reason not to use this browser.

Overriding the browsers’ default CSS style sheets

You’ll see in Firefox’s default CSS files that, by default, Firefox attaches an 8px margin on the <body> tag

body {
  display: block;
  margin: 8px;
}

If you don’t want an 8px margin on the <body>, then you can override that CSS setting by creating your own CSS for the <body> tag, for example:

body {
  margin: 0px;
}

The same principle applies to the other major browsers.

The unofficial chart of all browser default style sheets

Here is a nice chart showing the browsers’ default CSS rules

Class 3 – Using container (a.k.a. wrapper) divs

Saturday, October 10th, 2009

Up until now, we have been more-or-less focusing on the grammar and syntax of XHTML and CSS.  Now we will examine some common conventions that web developers use.  These are by no means rules that must be followed, but there is a reason why these techniques are popular and useful, so you should follow them, at least for now.

Put it in a container

In general you want to wrap ALL of the content in the body of your XHTML document inside an outermost div element.  Some people call this outermost div a “wrapper”, and others call it a “container”.  No matter, the idea is the same.  The container div should contain everything else on the page.

For example, the body section of an XHTML document using a container div:

<body>
 <div id="container">
 <!-- all XHTML code for the body of the page goes in here -->
 </div>
</body>

Following this convention gives you more control over how the page is displayed.  For example, some advantages of using container divs:

  • limit the width of the page so it takes up less than the full width of the browser
  • center the entire contents of the page within the browser window
  • easily add margins above and/or below the content of the page
  • apply a background image to just the content area of the page

To sum these advantages up, using a container div allows you to control the contents of a page as a whole, rather than element by element.

Now we will look at some of the CSS code that would allow us to perform each of the above tasks

Limit the width of the page

Setting the width of the page is as simple as setting the width of the container div.  Since all content is inside of the container, all content is restricted to this width:

div#container {
 width: 970px;
}

Center the entire contents of the page

To center the page, you must first give the container a fixed width, as exhibited above.  Otherwise, the container, being a block element, would take up as much width as it can, which in this case would mean the full width of the page.  By reducing the width to a fixed amount, the width of the page is no longer as big as the width of the browser window, so you have room in which to center the content.

Using a container div, centering the page is as simple as adding equal margins to the left and right side of the container.  But how do you know how big to make that margin?  Fortunately, there is a CSS margin setting called “auto” that automatically gives the margins the maximum size available:

div#container {
 width: 970px;
 margin-left: auto;
 margin-right: auto;
}

Easily add margins above and/or below the content of the page

Often, you will also want to remove any top and bottom margins from the container div that the browser may have added by default.  If you recall from the W3Schools margin reference page, the margin property in CSS can take either one, two, or four arguments.  The following code uses the margin property with two arguments to set the top and bottom margins to “0″ and the left and right margins to “auto” in one fell swoop:

div#container {
 width: 970px;
 margin: 0px auto;
}

If you wanted to, you could set the margins for each side of the container individually using the margin setting with four arguments.  The four arguments are in clockwise order: top margin, right margin, bottom margin, and left margin:

div#container {
 width: 970px;
 margin: 0px auto 0px auto;
}

This is equivalent, but much shorter than this code, which does exactly the same thing:

div#container {
 width: 970px;
 margin-top: 0px;
 margin-right: auto;
 margin-bottom: 0px;
 margin-left: auto;
}

Apply a background image to just the content of the page

Applying a background image to the container is as simple as using the CSS background-image property. This will apply only to the background of the container, so if your container is not as wide as the width of the entire browser window, you will see that the background does not take up the full width of the page.

div#container {
 width: 970px;
 background-image: url(../images/background.jpg);
}

Note: the URL used to indicate the image file to use for the background-image must be relative to where the CSS lives.  So, if you have your project set up in a folder called “project_name”, using our standard project folder structure, you will typically have a folder structure that resembles this:

project_name/
project_name/images/
project_name/css/

If the CSS file is inside of the “css” subfolder, and the image is inside of the “images” subfolder, then you will need to use a URL that indicates that the browser should look for the image file one level up from where the CSS file lives, and then inside of the “images” folder.  So if your image file is named “background.jpg”, the URL to use in the CSS file would be:

../images/background.jpg

Class 3 – Homework Assignment

Saturday, October 3rd, 2009

Your homework assignment for this week is to take the wireframes you completed last week and convert them into a real web page using XHTML and CSS code.  Do not worry about the design elements, like graphics, or fonts – simply focus on the layout.  And don’t get caught up in every last detail of your wireframes:  focus on only one or two wireframes, and try to get the layout of the pages as close as you can to what you have in your diagram(s).

It will help to first go through all of the tutorials and examples posted on the class 2 XHTML page of the blog and the class 3 CSS page of the blog up to and including “The secret to layout: float and clear“.  That post is especially useful in understanding how to get elements of your pages to sit where you want them, which is what this assignment is meant to explore.  The posts below that post are not necessary to understand this assignment, but might be interesting to read.

Be sure to post links to your completed assignments on your blog.  Your blog post should include a link to each of your assignments so that I and other students can see them live in the web browser.

Class 3 – Assignment #2

Saturday, October 3rd, 2009
Wireframe for Class 2 Assignment

Wireframe for Class 2 Assignment

Your assignment in class today is to convert this wireframe into XHTML and CSS code.

The XHTML code will involve headings, paragraphs, and images.

Tips

You will undoubtedly want to look up the reference for the <span> tag on the W3Schools site in order to figure out how to change the colors of some words inside the first paragraph.  Using the <span> tag in combination with the CSS techniques of applying styles to individual elements using the XHTML “id” attribute will allow you to style just those words in CSS.

Getting things to sit side-by-side is always an “issue”.  You will find that out when you try to make the image of the mouse sit side-by-side with the paragraph next to it.  And there are generally two ways of approaching the solution:

We will discuss this further in class.

Class 3 – Margins and Padding

Friday, October 2nd, 2009

It is important to become comfortable with the basic CSS properties, “margin” and “padding”.  All “block elements” have both margin and padding properties that affect the way those elements sit in relation to the elements nearby them.

Margin and padding are probably best understood from a diagram.  In the following diagram, we see three block elements (elements that are rectangular in shape), one inside the other.   We are only concerning ourselves with the properties of the yellow element in this example, in order to keep the discussion simple.

Margin and padding of an element

Red arrows indicate the margins of the yellow element, and the green arrows represent the padding of the yellow element

You can see that the margins, represented by red arrows, are the spacing between the outer edge of the yellow element and the inner edge of its parent element (the element that contains it).  Increasing the yellow element’s margins will increase this spacing, decreasing the yellow element’s margins decreases this spacing.

Similarly, the padding, represented by green arrows, is the spacing between the inner edge of the yellow element and the outer edge of the element nested inside of it.  Like the margins, the padding can also be adjusted using CSS.

Margins and padding do not have to be equal on all sides.  View the CSS margin examples and padding examples on the W3Schools site to see how margins and padding can be set individually for each side.

Note that we have only been talking about block elements.  Inline elements also have margin and padding properties, but they behave inconsistently… one you have mastered margin/padding for block elements as outlined here, see this site for a good explanation of how inline elements differ in this regard.

Class 3 – Block vs. Inline Elements

Wednesday, July 15th, 2009
The display property in CSS is responsible for what shape an element takes on the page and how an element aligns with the elements around it.  In general, all XHTML elements fall into one of two display categories: block or inline.
Block elements take up a rectangular shape, and take as much width as they can. They also break the flow of the page by introducing line breaks before and after themselves automatically.

Inline elements take the shape of whatever content is inside of them. They stretch to fit only the width of the content inside of them, not any more. And they do not break the flow of the page with automatic line breaks.

Here is an example file highlighting the shape differences between a div and a span. Divs are block elements, while spans are inline elements.  In fact, <divs> and <spans> are identical in all other ways except this difference.

It is possible to convert a block element into an inline element and vice versa using CSS.  You simply adjust the display property of the element in the CSS.

Here is an example where we have converted an element that normally has a block display into one that has an inline display by overriding the default display setting in the CSS code.

Common “block” tags:

  • div
  • ul, ol, and li
  • h1 to h6
  • p
  • pre
  • table
  • blockquote
  • form

Common “inline” tags:

  • span
  • a
  • strong
  • em
  • img
  • br
  • input

Other display types

Note: In reality, the browsers internally use more than just these two display properties to generate the layout behavior of elements on the page.  There are varieties of each of them for elements with special layouts, like table cells, list items, etc.  However, it is useful to generalize all elements into one of these two types to simplify how we code, as well as to overcome browser inconsistencies in how they deal with some of the more specialized types of display.  For a full list of display types and information on browser support for each of them, see this Quirksmode.org page.

Class 3 – Homework Assignment

Friday, March 6th, 2009

I just realized that I haven’t yet made an “official” post about the homework assignment for this week, but you all are no-doubt already working on it.  For the record, the homework this week is to convert these two design comps into actual XHTML and CSS code:

Design comp for BlackBook Guides app

Design comp for BlackBook Guides app

Design comp for MTV Music Videos app

Design comp for MTV Music Videos app

All the images you will need for each design are in my folder under class3/assignment2/images/ and class3/assignment1/images/.  Please make a copy of these images and use them in your own pages.

Try to get your pages as picture-perfect as possible.  It might help to use the trick of placing the actual design comp as a background image behind your page as you work on it.  This will allow you to easily compare the placement and size of the elements you create in code on the page with those that were originally in the actual design comp.  The former will be superimposed over the latter.

Class 3 – Tools for Reverse Engineering CSS & HTML

Wednesday, March 4th, 2009

When you take an existing product (including websites) and try to determine how it was created, you are doing what is called reverse engineering.  There are several tools at your disposal that may help you discover how a particular website was created in terms of XHTML and CSS code.

View Source

Each web browser allows you to view the source code of the site.  This is the crudest technique for determining how a page was built.  In Firefox, if you click the “View” menu at the top of the browser, you will see a “Page Source” option.  This allows you to view the HTML code of any website.

Firefox menu with option to "view source"

Firefox menu with option to "view source"

To view the corresponding CSS code, find the <link> or <style> tags in the XHTML code.  If the XHTML is loading an external CSS file, enter the URL of that file (found in the “href” attribute of the <link> tag) directly in the browser address bar.

View Selection Source

If you select any text on the page, and then right-click (i.e., control-click on Mac), you will see the “View Selection Source Option.  This allows you to view the HTML code for only the elements on the pag that you selected.

Firefox context menu, showing "View Selection Source"

Firefox context menu, showing "View Selection Source"

Outline Current Element  (using Web Developer Add-on)

With Firefox’s Web Developer Add-on, you can mouse over any element on the page to put a temporary red border around that element, as well as see the CSS selector that applies to the element.  To use this, click on the “Outline” button in the Web Developer toolbar, and select “Current Element”.  This can be useful for determining the approximate size and position of an element that seems to be behaving differently than you expect – the red border will show you its true outline.

Web developer toolbar's "Outline Current Element"

Web developer toolbar's "Outline Current Element"

This tool will also show you, directly  underneath the toolbar buttons, how the currently selected element fits into the hierarchy of elements on the page.  This information is shown in a CSS selector-style format, although it is meant for your information on hierarchical structure only, and is usually not how the actual CSS selectors for this element actually appear in the code.

The position of the current element in the document's hierarchy

The position of the current element in the document's hierarchy

View Element Information (using Web Developer Add-on)

The Web Developer Firefox Add-on allows you to view the complete information of any element on the page.  To use it, click the “Information” button on the Web Developer toolbar, and then select “Display Element Information”.

Web developer toolbar's "Display Element Information"

Web developer toolbar's "Display Element Information"

This tool allows you to view any CSS settings or id and class XHTML attributes, as well as other information about parent and child elements.  Once you have selected “Display Element Information” from the toolbar, click on any element on the page to get that element’s information.  The selected element will show a red border around it:

Element information about the "history" tab on Wikipedia

Element information about the "history" tab on Wikipedia

Viewing and Editing Source Temporarily (using the Firebug Add-on)

The Firebug Firefox Add-on allows you to not only view XHTML and CSS source, but also edit it to make quick changes and see immediately how they affect the page.  To activate the Firebug Add-on you need to click the firefly icon at the bottom-right corner of your browser window.

Click the grey firefly to activate Firebug

Click the grey firefly to activate Firebug

To view XHTML or CSS source code for the page, simply click the “HTML” or “CSS” tabs in the Firebug sub-window.  To edit either the XHTML or the CSS, simply click the “Edit” tab at the top – bear in mind that you cannot save these edits, except by cutting and pasting the code into your text editor (NotePad++ for PC or TextWrangler for Mac)

Viewing CSS code with Firebug

Viewing CSS code with Firebug

Note that some pages may use multiple external style sheet files.  To swtich between style sheet files in Firebug, click on the drop-down list of file names at the top of the Firebug sub-window:

Switching between multiple CSS files in Firebug

Switching between multiple CSS files in Firebug

Viewing Element Information (using the Firebug Add-on)

Similar to the the Web Developer Add-on, Firebug allows you to view the CSS and XHTML attribute information of any element on the page.  To use this, select the “HTML” tab in the Firebug sub-window, and simply click on any tag name in the XHTML code that is displayed.  Firebug will highlight that element on the page, as well as show you the corresponding CSS code in the right panel. Even if the corresponding CSS code is spread between several different external files, Firebug will compile them all in the right panel.

Viewing element information in Firebug

Viewing element information in Firebug

Additionally, Firebug allows you to visually see any element’s layout information by selecting “Layout” in the right panel:

Viewing element layout information in Firebug

Viewing element layout information in Firebug

Determining Exact Colors of Elements (using Colorzilla Add-on)

Using the Colorzilla Add-on, you can determine the exact color of any element on any page.  To activate Colorzilla, click the eye-dropper icon on the bottom left of the browser window.

Activate Colorzilla by clicking on the eye-dropper at the bottom-left of the browser

Activate Colorzilla by clicking on the eye-dropper at the bottom-left of the browser

Once activated, you can click on any element of the page, and the color information will show up in the bottom-left of the browser.  Color codes are displayed in terms of both RGB (useful for PhotoShop work), as well as hexadecimal codes (useful in your CSS code).

Using Colorzilla to detect the color of the "Details" link at http://nyu.edu

Using Colorzilla to detect the color of the "Details" link at http://nyu.edu

Class 3 – A Tip When Working With Design Comps

Saturday, February 28th, 2009

As you work on the in-class assignment today, you will find that you constantly need to compare your work in XHTML and CSS with the original design composition (called a “comp”) to make sure the widths, heights, and styles of each of the elements matches those in the design comp.

There are several ways to do this.  One easy way to do this is to apply the original design comp as a background image to the container div.  That way, as you are working, you will constantly have the original design comp as a backdrop to your page.

Here is an example of applying the design comp as a background-image to the container div.

You will want to check out both the XHTML and the CSS files.

Class 3 – In-Class Assignment #1

Saturday, February 28th, 2009

Your in-class assignment today is to convert this screenshot into XHTML/CSS code:

BlackBook Guides Facebook App

BlackBook Guides Facebook App

All the images you need are in my spring2009/class3/assignment2/images folder on the server.