[ LiB ] |
To work well with CSS layers in Dreamweaver, you must first understand where they're coming from and what they're doing. With that groundwork , you can make the most of them without painting yourself into any nasty corners.
The original CSS1 specification covered various text and other formatting options for page content in HTML. But the W3C wasn't finished there. CSS-P introduced the capability to govern how elements are positioned on the page ( P is for positioning). An expanded version of CSS-P then became part of the CSS2 standard. In addition to positioning, CSS-P allows page elements to be assigned a width, visibility, and even depth (z-index), allowing elements to be layered on the page. (Hence, the term layer. )
Table 12.1 lists some of the style attributes possible with CSS-P and CSS2, and what they can do. Of these, positioning has been the most exciting to web designers. Through positioning controls, any page element can be turned into a free-standing entity, andtheoretically, at leastweb designers need no longer be slaves to the tyranny of tables for simple things such as putting page contents where they want them.
Style Property | Values | Description |
---|---|---|
position | static , absolute , relative , or fixed | Positions the element on the page (see the text for details). |
left , right , top , bottom | Measurement in pixels, percent, and so on | Specifies the element's distance from the edge of its parent element. |
width , height | Measurement in pixels, percent, and so on | Specifies the element's dimensions. |
z-Index | Number | Specifies the element's place in the stacking order. Higher numbers are in front of lower numbers . |
Visibility | true / false | Specifies whether the element is visible (used mainly for scripting purposes). |
Clip | clip: rect ( top right bottom left ) | Specifies whether the entire element will display or whether only a rectangle within the element's width and height will display. |
Overflow | visible , hidden , scroll , auto | Determines what happens to content that won't fit within the width and height of the element. |
Positioning comes in three flavors: absolute, relative , and static. These types work like this:
Static positioning makes the element a static part of the document's normal flow. This is where the element would sit on the page if no positioning were specified. The element appears on the page as part of the normal flow of elements. The code for static positioning of an element might look like this:
<h1 style="position:static;">Howdy!</h1>
Absolute positioning places the page element an absolute distance from the edges of the browser window or the item's parent element. This distance can be specified in pixels, percent, or other units of measurement supported by CSS. The code for absolute positioning of an element might look like this:
<h1 style="position:absolute; left:0px; top:0px; ">Howdy!</h1>
Assuming that this element isn't inside a table, div element, or other page element, it's a direct child of the body tag and hugs the upper-left corner of the browser window, regardless of what other page elements might proceed it in the HTML flow.
Relative positioning places the page element relative to other page content. This is similar to static positioning, in that the element's default position is after or below elements that precede it in the HTML and before or above elements that follow it in the HTML. But a relatively positioned element can be offset from its default position by assigning left , top , right , or bottom values. The code for relative positioning of an element might look like this:
<h1 style="position:relative; left:50px; top:10px; ">Howdy!</h1>
Assuming that this element occurs below a table on the page, the code here will move its left edge to be 50 pixels to the right of where it would otherwise sit below the table, and move its top edge 10 pixels down from where it would otherwise sit.
Fixed positioning is similar to absolute positioning, except that when the page scrolls in the browser window, fixed elements do not scroll with it. The code for fixed positioning of an element might look like this:
<h1 style="position:fixed; left:0px; top:0px; ">Howdy!</h1>
Assuming that this element is not nested in any other elements (except the body itself), it will hug the upper-left corner of the browser window and will stay there even if the user scrolls down the page to view other content. (Unfortunately, fixed positioning is not as well-supported as the other positioning types, even in modern browsers.)
Positioning gets more complex when elements are nested inside other elements. In most browsers, and in the CSS-2 specification, how an element's positioning is interpreted depends on whether the element's parent also has positioning specifiedand specified as nonstatic. If the parent has no position attribute set or has the position set to static , a child with absolute position will be positioned relative to the closest ancestor with absolute or relative positioning. If no such ancestor exists (including the document body), the child will be positioned relative to the window.
A page element can be positioned with an inline style definition, like this:
<h1 style="position:absolute; left:150px; top:80px">Howdy!</h1>
Or, it can be positioned by assigning it a custom class that includes positioning, like this:
.logotest { position: absolute; left:150px; top:80px; } <h1 class="logotest">Howdy!</h1>
And because a particular style definition that includes positioning may be used only once in a document, the CSS style information can also be attached to a page element's unique ID attribute, like this:
#logotest { position: absolute; left: 150px; top: 80px; }; <h1 id="logotest">Howdy!</h1>
If the style definition shown here is placed in an external style sheet document and is attached to a number of HTML pages, each page can have one element with the logotest ID, which will have its placement controlled by the style.
As with all CSS, the inline method is the least efficient because the information can never be reapplied to any other page elements in this or any other document.
Theoretically, any page element can be absolutely positioned using CSS-P. Block elements (such as tables, headings, and paragraphs) are most commonly used for this, although inline elements (such as images) can also be used. But what if you want to position a group of elements, such as a heading and a following paragraph? You would enclose the entire group in a tag that doesn't have any of its own characteristics to interfere with page display, and apply a custom class or inline style to that tag. The two tags used for this are div (block element) and span (inline). Coding for positioning a group of elements might look like this:
<div style="position:absolute; left:150px; top:80px;width:200px"> <h1>Howdy!</h1> <p>Welcome to my brand-new website!</p> </div>
or this:
<span style="position:absolute; left:150px; top:80px; width:200px"> <h1>Howdy!</h1> <p>Welcome to my brand-new website!</p> </span>
div and span elements can be nested inside each other. Other HTML elements nested inside div and span elements can also have positioning applied, like this:
<div style="position:absolute; left:150px; top:80px;"> <h1 style="position:absolute; left:50px; top:50px;">Howdy!</h1> </div>
Dreamweaver layers provide an interface for dealing with CSS-P in Design view. Like any tool, they can be used or abused. Remember to always keep track of what's happening in the code while you're working with layers.
The simplest and most intuitive way to get into building layers is to insert Layer objects with the Draw Layer object, found in the Layout Insert bar (see Figure 12.1). To use this tool, choose it from the Insert bar, position the cursor in the Document window, and drag to draw. When you're done, Dreamweaver displays a layer fitting your dimensions.
You can also choose Insert > Layout Objects > Layer, which inserts a layer with default dimensions and positioning into the document.
If you check Code view after your layer is created, you'll see that Dreamweaver built it as an empty div tag with an inline style where absolute positioning, dimensions, and stacking index are specified:
<div id="Layer1" style="position:absolute; width:200px;height:115px; z-index:1; left: 113px; top: 117px;"></div>
Absolutely positioned elements don't require all of these attributes, but they're all supplied by default.
Select your layer, and the Layer Property Inspector appears (see Figure 12.2). You can use this to change or remove any properties, or to change the tag used for the layer from div to span . To change the defaults for inserting layers, you can also choose Edit > Preferences/Layers (see Figure 12.3). Or, you can change your layer's size and position interactively by moving or resizing it in the Document window.
Because layers created with the Layer object use absolute positioning, their placement in the Design view layout isn't necessarily anywhere near where their code has been inserted. The code for the layer might be immediately after the opening body tag, while the layer itself sits at the bottom of the page. To help you keep track of the code, Dreamweaver gives you the option of seeing code icons where the code itself sits. To enable these, go to Preferences (Edit > Preferences) and, in the Invisible Elements category, make sure the Anchor Points for Layers option is selected. Then from the View Options menu, choose Visual Aids and select Invisible Elements. Figure 12.4 shows these visual aids at work.
The Layers panel (see Figure 12.5) helps you manage your layers. It tells at a glance how many layers your document contains, what their names are, what their stacking order is (which is in front, which is behind), and whether they're visible. (Invisible layers are not too useful for creating page layout, but are a staple of DHTML scripting.) You can also use the Layers panel to select layers that might otherwise be tricky to select, either because they're invisible or because they're stacked behind some other layers.
To access the Layers panel, choose Window > Layers. After the Layers panel has been opened, it appears as part of the Advanced Layout panel group, docked with your other panels.
The layers you've placed on the current page are shown in the panel as a list of names that reflects their stacking order (or z-index). The stacking order can be manipulated by dragging layers up and down in the list.
The left column with the eye icon at the top enables you to change the visibility of layers. See Chapter 14, "Controlling Layers with JavaScript," for more on playing with layer visibility.
You can allow or disallow overlapping layers by checking or unchecking the Prevent Overlaps box. As discussed earlier, overlapping layers are at more risk of misbehaving in browsers than layers that don't overlap. You might also want to disable overlapping if you think you might later want to turn your layers into a table for a more browser-compatible layout. (You can do this with the Modify > Convert > Layers to Table command, although it creates unwieldy table structure.)
If you're a visual sort of person who feels quite at home creating and manipulating design elements in programs such as QuarkXPress or Photoshop, you'll love how intuitive it is to create page designs using layers in Design view.
A layer has three possible selection states, each represented in Design view by a different graphic state (see Figure 12.6):
When the layer is selected, it displays a little white box in its upper-left corner, as well as eight solid black squares around its perimeter. The white box is the selection handle. The black squares are resizing handles.
When the insertion point is inside the layer, the layer appears as a box with a selection handle but no resize handles. The layer appears this way just after it has been inserted because Dreamweaver assumes that you want to start putting content in the newly created layer right away.
When the layer is not selected, it appears as a plain beveled box, with no selection or resize handles.
To select a single layer, do one of the following:
Click one of the layer's borders. (This is straightforward and obvious, but it requires a bit of hand-eye coordination to click precisely on the layer border.)
Click inside the layer so the selection handle shows in the upper-left corner, and click the handle. (This is a slower two-step process, but it requires less-accurate clicking.)
Click inside the layer and use the tag selector at the bottom of the Document window to select the div or span tag that is the layer.
If invisible elements are showing, click the layer's layer marker.
In the Layers panel, click the layer's name .
To select multiple layers from within the Document window, click on the borders or selection handle of the first layer you want to select and then Shift-click anywhere within each additional layer. If invisible elements are showing, you also can Shift-click on each layer marker, in turn. To select multiple layers using the Layers panel, Shift-click the names of the layers you want to select. (Discontiguous selections are allowed.) No matter which selection method you use, the last layer that you select will show its resizing handles in black; other selected layers will show resizing handles in white.
Each layer in a document must have a unique ID. When you create a layer, Dreamweaver gives it an ID based on the naming scheme Layer1, Layer2, Layer3, and so on. Layer IDs are used for CSS reference and also for any scripts that reference the layers. Layer names must be one word only, with no special characters .
Although you don't need to change your layers' default names, it is a good idea to give your layers names that are more descriptive than Layer1 or Layer2 so that you can easily identify them as you work. Even better, name them according to a naming scheme in which each layer's name tells you something about its position or function. For example, you might name a layer containing a navigation bar LayerNav, and a layer holding the page footer LayerFooter.
You can use the Layers panel or Property Inspector to change a layer's name. Double-click the layer in the Layers panel and type the new name into the text field area, or change the name in the Layer ID field of the Property Inspector.
Inserting content into a layer is simple: Just click inside the layer and insert in the usual way. Content can be inserted using the Dreamweaver Insert bar or Insert menu, dragged from the Assets panel or from elsewhere on the page, or pasted from the Clipboard.
To size a layer in the Document window, first select it so that its resize handles show. Then just click and drag any of its resizing handles. Use the corner handles to resize horizontally and vertically at the same time, or use the noncorner handles to resize in one direction only.
To change a layer's position from within the Document window, just grab it either by the selection handle or over the border, and click and drag. The cursor turns into a four-headed arrow when it moves over the selection handle, or a grabber hand when it moves over the border, to indicate that clicking and dragging is possible.
![]() | Be careful not to grab the layer in its middle and drag. You won't be able to move the layer this way, and you might accidentally drag its contents (an image, for instance) out of the layer entirely. |
To nudge a layer a pixel at a time, select it and use the arrow keys to move it. To nudge a layer 10 pixels at a time, hold down Shift while using the arrow keys to move it.
Graphic designers migrating from the print world will be happy to know that Dreamweaver has a lovely feature for aligning layers. To align one or more layers with a border of another layer, do this:
Figure 12.7 shows several layers being aligned to one stationary layer.
Following the procedure for aligning layers, you also can modify layers so that their dimensions match. To do this, just select your layers, go to Modify > Align, and choose Make Same Height or Make Same Width. As with aligning, the last layer selected determines the final height or width of all selected layers.
A nested layer is a layer created inside another layer. The code looks like this:
<div id="Layer1" style="position:absolute; width:200px;height:115px; z-index:1; left: 200px; top: 200px;"> <div id="Layer2" style="position:absolute; width:100px;
height:100px; z-index:1; left: 50px; top: 50px;"> [Layer2 content goes here] </div> [Layer1 content goes here] </div>
Nesting is often used to group layers. A nested layer moves along with its parent layer and can be set to inherit visibility from its parent.
To create a nested layer, use any of the following procedures:
After one layer is nested inside another, the child layer (the nestee) might jump to a new location in the Document window. This is because the layer's position is now being calculated relative to the parent layer's position. This also means that from now on, if you move the parent layer, its child moves with itbut if you move the child, the parent won't move.
Figure 12.8 shows how nested layers appear in the various areas of the Dreamweaver interface. In the Document window, if invisible elements are showing, the gold anchor points indicate nesting status. In the Layers panel, a nested layer is shown with its name indented under its parent layer. Click the plus/minus (+/) button to the left of the parent layer name to show or hide its children.
![]() | Not all browsers respect the relative positioning of parent and child. If accurate and predictable positioning of page content is important to you, test your pages thoroughly in a variety of browsers, or don't use nested layers. |
In this exercise, you'll build a simple page layout using layers. For the Student Ministries home page, you want to place some images on a page so that some of them overlap one another and some are transparent, allowing the images beneath to show through. You've chosen layers to achieve this result. Figure 12.9 shows the final effect you're looking for.
Before you start, download the files from the chapter_12 folder on the book's website at www.peachpit.com to your hard drive. Define a site called Chapter 12, with this folder as the local root folder. All the files for this exercise are in the chapter_12/ministries folder.
![]() | In the Layer Property Inspector, be careful not to confuse the L and T (Left and Top) fields with the W and H (Width and Height) fields. |
[ LiB ] |