250 likes | 528 Views
The CSS Box Model. No More Table Layouts. Tables should not be used for layout in XHTML/CSS Tables are not deprecated – they are essential for the display of content Remember the reasons why they are a bad idea – especially for people not using a full-sized computer screen.
E N D
No More Table Layouts • Tables should not be used for layout in XHTML/CSS • Tables are not deprecated – they are essential for the display of content • Remember the reasons why they are a bad idea – especially for people not using a full-sized computer screen.
Last time in Dreamweaver • We created two paragraphs and used CSS to: • Add background colo(u)rs and borders • Set their widths and heights • ‘Float’ them to the left Now we’re going to see how this works!
The Box Model • An HTML ‘paragraph’ <p>…</p> is an example of a block tag • If a block tag is not formatted with CSS it will be 100% wide and as high as required to contain its content • All block tags can have their heights and widths set in CSS • All block tags can have borders, margins and padding – just like tables
What does this rule do? p { height: 200px; width: 400px; background-color: blue; color: white; border: 1px solid #000000; } Why would this rule be likely to be of little use in practise?
Box Model CSS rules • Unless we want all of our paragraphs to be identically formatted (we don’t as we are going to be using CSS to position them soon) redefining the <p> tag in this way is not a good idea. • How else could we do this?
Using Class Rules This is a better way of doing this as we can apply the class only to the block tags we want to format in this way BigBlueRectangleWithWhiteText { height:200px; width:400px; float:left; background-color:blue; color:white; border: 1px solid #000000; } <p class=“BigBlueRectangleWithWhiteText”>
CSS rules – the end is nigh! • We’ve seen CSS used to: • Redefine XHMTL tags (like <p>) to change the appearance of all tags of that type. • Create classes that we can use when we want <p class=“myClass”> • Create pseudo-classes to modify the four separate states of the <a> tag • There’s only one more…
CSS ID Rules CSS classrules start with a dot: .bigRed {font-size:24pt; color:red;} And are used like this: <p class=“bigRed”> CSS IDrules start with a hash: #para1{font-size:24pt; color:red;} And are used like this: <p id=“para1”> What’s the difference?
CSS ID • ID rules are designed to apply only to a single block tag. Note the ID rule name on the previous slide • ID rules should be used only once in a page. • Web browsers are supposed to enforce this.
Block Tags • Tags like <p>…</p> can contain inline tags (like <a>, <br /> and <img> but must not contain other block tags – this is an XHTML rule. • Block tags often have some form of formatting built into them. • What we need is a block tag that does nothing – except let you connect CSS rules to it.
The <div> tag • <div>…<div> is a do-nothing block tag • You can attach CSS to it – use an ID rule! • You can enclose other block tags inside it <div id=“outer”> ‘outer’ rule sets appearance/position of this div <div id=“title”> ‘title’ rule sets appearance/position of this div <h1> This is the title of the page </h1> </div> ‘title’ div finishes here <div id=“content”> ‘content’ rule applies here <p> Hi, I’m the content of this quite dull page </p> </div> end of the ‘content’ div </div> ‘end of the outer’ div
Why use <div>? • Our first look at the box model in the last session formatted <p> tags which would have only allowed a single paragraph inside each <p>…</p> box. You can put what you like (including other <div> tags) in a <div> tag • CSS formatting is inherited. Consider the following: <div id=‘1’> <div id=‘2’> </div> </div> Formatting applied to <div id=‘1’> applies to <div id=‘2’> (unless replaced.) So CSS applied to the ‘outer’ parts is available to the ‘inner.’
The <span> tag • Class and ID rules apply to an entire block. Remember <p class = ‘myclass’> and <div id=“para1”> • Use the <span> tag to apply a class to only part of a block. <p> This part is in <span class=“boldface”>Important</span> text</p>
Exercise 1 Getting used to the box model
The Box Explained Margin Padding Border Content (size defined by height and width) Total width = margin-left + border + padding-left + width + padding-right + border + margin-right
Box Details • Margins, padding and borders are all optional • Separate top, bottom, left and right options are available for each • It is not possible to change the appearance (colour etc) of margins and padding. • Borders are available in several types and can be coloured
CSS Text Sizing • By name: xx-small, x-small, small, medium, large, x-large, xx-large • By percentage e.g. 120% would be 20% larger than inherited size • By ems • By pixel size – possibly will not be user enlargable • By point size – possibly not user enlargable
Exercise Create the Richmond pet foods layout using the box model