Chapter 1:setting The Foundations
The human race is a naturally inquisitive species. We just love tinkering with things. When I recently bought a new IMAC, I had it to bits within seconds, before I ' d even read the instruction manual. We enjoy working things out ourselves and creating our own mental models of how we think things behave. We muddle through and only turn to the manual when something goes wrong or defies our expectations.
Humans are naturally curious species. We like to ask the bottom of things. (Muddle through muddle;d efies difficult to meet)
One of the best ways to learn cascading Style Sheets (CSS) are to jump right in and start tinkering. In fact, I imagine the majority for you learned to code, by picking things up off blogs, and viewing source to see How things worked, and then trying them out on your personal sites. You almost certainly didn ' t start by reading the full CSS specification, which are enough to put anyone to sleep.
The best way to learn CSS is to jump out of limits and keep exploring. (Majority of most)
In this chapter you'll learn about
Structuring Your code
the importance of meaningful documentation
Naming conventions
when to use IDs and class names
microformats
different versions of HTML and CSS
document types, DOCTYPE switching, and browser modes
A Brief History of markup
The early Web is little more than a series of interlinked (interlinked) the using HTML to add basic formatting and Structure. However, as the world Wide Web gained in popularity, HTML started being used for presentational (stated representation) purposes. Instead of using heading elements for page headlines, people would use a combination of font and bold tags to create the V Isual effect they wanted. Tables got co-opted as a layout tool rather than a by displaying data, and people would use blockquote to add WhiteSpa Ce rather than to indicate quotations. Very quickly, the Web lost its meaning and became a jumble of (confusion) font and table tags. Web designers came up with a name for this kind of markup; They called it tag soup (see Figure 1-1).
The above discusses the problem of using table layout: A lot of tags constitute the so-called tags soup
as Web pages became more and more presentational, the code became increasingly difficult to understand and maintain. WYSIWYG (What do you see are) editors offered authors an escape from these complexities and promised a brave new World of visual layout. unfortunately, rather than making things simpler, these tools added their own complicated mar Kup to the mix. Editors like FrontPage or Dreamweaver allowed users to build complex table layouts at the click of a button, cluttering th e code with nested tables and "spacer GIFs" (see Figure 1-2). Sadly, these layouts were extremely fragile (fragile) and prone (easy) to breaking. Because the markup is littered with meaningless code, it is easy to delete the wrong tag and watch the whole layout Crum BLE (crashes). Furthermore, due to the complexity of the code, bugs hunting was almost impossible. It is often easier to code the page from scratch than hunt around in the hope of fixing the bug. Things were further complicatedIf you were working on a large site. Because the presentation of the site is locked into the individual pages, you had to craft complicated "find and replace" Routines to make even the smallest sitewide. I ' ve broken more than one site in my time because of a hastily (cursory) constructed "find and replace" routine. Consequently, your page templates would go out of sync extremely quickly, and a simple change could mean hand editing ever Y page on your site.
Tables were never meant for layout, so David Siegel invented a clever hack to make them work. In order to prevent tables from horizontally or vertically collapsing, Siegel suggested using a 1-pixel transparent GIF. By putting these hidden images in their own table cells and then scaling them vertically or horizontally, you could artifi Cially enforce a minimum width on the cells, thus preserving the layout. Also known as a "shim GIF" because of the file name given to them in Dreamweaver, they were an extremely common sight in O LD School table-based layouts. Thankfully, the practice have now died out, so you no longer see these presentational elements cluttering up your code.
The main idea is that table-style layout is very difficult to maintain, tag is easy to be mistakenly deleted, especially when the code is very large, a very small change will cause heavy changes in the workload. This paper mainly points out the complexity of traditional table-style layout.
(Note: "Shim gif" Also known as a transparent GIF or a spacer GIF.)
It's a transparent picture this cannot be seen
It is used to the open tables cells and can be resized to any dimensions.
)
Rather than being seen as a simple markup language, HTML gained a reputation for being complicated, confusing, and prone t o errors. Consequently, many people were afraid of touching the code, which resulted in a overreliance on visual editors and Spawne D A whole generation of designers that didn ' t understand what to code. By the turn of the millennium, the Web design industry is in a mess, and something needed to is done.
The problem is that there is a solution to be proposed!
Then along came cascading Style Sheets. CSS allowed authors to control what a page looked through an external style sheet making it possible to separate the Conte NT from presentation. Now, sitewide changes could is made in one place and would propagate (propagate) throughout the system. presentational tags like the font tag could be ditched (discarded), and layout could be controlled using CSS instead of tables. Markup could is made simple again, and people began to develop a newfound interest in the underlying (bottom) code.
So the CSS was born!
Most importantly for the context of this book, meaningful markup provides-a simple-to-the-same-to-the-targeting You wish to style. IT adds structure to a document and creates the underlying framework to build upon. You can style elements directly without needing to add other identifiers, and thus avoid unnecessary code bloat (bloat).
CSS Benefits!
IDs and class names Meaningful elements provide a excellent foundation, but the list of available elements isn ' t exhaustive. HTML 4 is created as a simple document markup language rather than an interface language. Because of this, dedicated elements for things such as content areas or navigation bars just don ' t exist. You could the create your own elements using XML, but for reasons too complicated to go into, it's not very practical.
HTML4 is a markup language, not an interface language
HTML 5 hopes to solve some of these problems by providing developers with a richer set of elements to work with. These include structural elements like header, NAV, article, sections, and footer as well as well as new UI features like D ATA inputs and the menu element. In preparation for HTML 5, many developers has started adopting these as naming conventions for their ID and class names.
HTML5 has more elements to handle!
An ID was used to identify a specific element, such as the site navigation, and must are unique to that page. IDs is useful for identifying persistent structural elements such as the main navigation or content areas. They is also useful for identifying one-off elements-a particular link or form element, for example.
While a single ID name can is applied to one element on a page, the same class name can is applied to any number of E Lements on a page. This makes classes much more powerful. Classes is useful for identifying types of content or similar items
ID is used to mark unique elements, must be unique within the page, Class is used to mark a large number of identical elements, this tag has greater power!
naming Your elements:it is often difficult to decide if an element should has an ID or class name . As a general rule, classes should is applied to conceptually similar items that could appear in multiple places on the SAM e page, whereas IDs should is applied to unique elements. However, you and get to a debate about which elements is conceptually similar and which elements are unique.
DIVs and Spans:one element that can help add structure to a document is the DIV element. Many people mistakenly believe, a DIV element has no semantic meaning. However, Div actually stands for division and provides a to dividing a document into meaningful areas. So by wrapping your main content area in a div and giving it a class of content, you is adding structure and meaning to Y Our document. Using too many divs is often described as divitus and are usually a sign this your code is poorly structured and overly com Plicated. Instead, divs should be used to group related items based on their meaning or function rather than their presentation or L Ayout.
DIVS are block-level elements that are often used for "splitting"-splitting documents into different parts, but using too much divs is not a good thing, in fact, Div is used to group related elements together instead of just thinking about the layout and aggregating them together!
Different versions of HTML and CSS: CSS comes in various versions, or levels, so it's important to know which version to use.
CSS 1 became a recommendation at the end of 1996 and contains very basic properties such as fonts, colors, and margins.
CSS 2 is released in 1998 and added advanced concepts such as floating and positioning to the mix, as well as new Selecto Rs like the child, adjacent sibling, and universal selectors.
CSS 3 has been broken down into modules so can be released and implemented independently.CSS 3 contains some exciting new additions, including an advanced layout module, brand newbackground properties, and a host of new selectors.Some of these modules is scheduled for release as soon as thesecond half of. Sadly, we ' ve been here before, and several &N Bsp;modules has been on the verge of release-only-be-pushed-to-"last-call" or "working-draft" status, so it ' s D Ifficult to know how many would actually make the grade. Hopefully, by , we'll see a number of the SE modules become official recommendations. More worryingly, some modules don't appear to having been started, while others haven ' t been updated for &NBSP ; several years. Due to this glacial pace of development, it seems unlikely the CSS 3 would ever be fully complete.
The good news is, despite the numerous delays, many browser vendors has already implemented some of the more interes Ting parts of the CSS 3 specification. As such, it is possible
To start using many of these exciting selectors today
CSS version of the case, CSS 3 from the second half of 2009, but most of the only stay in the "draft" stage, the module is not fully completed. But the good news is that many browsers are already supporting some of the features of CSS3!
HTML 4.01 became a recommendation at the end of the 1999 and is the version of HTML. In January-the created an XML version of HTML 4.01 and named it XHTML 1.0. The main difference between XHTML 1.0 and HTML 4.01 are that it follows the XML coding conventions. This means, unlike in regular HTML, all XHTML attributes must contain quote marks, and all elements must is closed.
xhtml 1.1 was a attempt to take the XHTML even closer to XML. There was very little practical difference between the languages. However there was one big theoretical difference. While it is still considered acceptable to serve up a XHTML 1.0 page as an HTML document, XHTML 1.1 pages were supposed To is sent to the browsers as if they were XML. This meant if your XHTML 1.1 page contained a single error, such as an unencoded ampersand, Web browsers weren ' t supp osed to display the page. This obviously isn ' t ideal for most website owners, so XHTML 1.1 never really took off. There is still some debate as to whether a should serve up a XHTML 1.0 pages as if it were HTML or if you ' re better STI Cking with HTML 4.01. However, it's clear that shouldn ' t is using XHTML 1.1 unless you ' re using the correct MIME type and is happy for your Page not to display if it is contains an error. HTML 5 is relatively new, and as a draft specification are changing all the time. HoWever, it has a lot of momentum and several popular browsers with already started building in support. HTML 5 grew out the frustration (setback) developers had with the slow and archaic (old) development of XHTML 2. So a group of them decided to draft their own specification. This proved so successful the HTML 5 became a official project, and the development of XHTML 2 was sidelined (shelved). &NB Sp
The above describes the development history of HTML 4.01,xhtml 1.0,xhtml 1.1, XHTML 2.0, HTML5, which is not recommended because of the drawbacks of XHTML 1.1, HTML5 is an alternative to XHTML 2!
Document types, DOCTYPE switching, and browser modes:a Document Type Definition (DTD) is A set of MACHINE-READABL The E rules that define are and isn ' t allowed in a particular version of HTML or XML. Browsers is supposed to use these rules when parsing a Web page to check the validity of the page and act accordingly. Browsers know which DTD to use, and hence which version of HTML you is using, by analyzing the page ' s DOCTYPE declaration .
a DOCTYPE declaration is a line or both of code at the start of your HTML document that describes the particular DTD being used. In this example, the DTD being used are for XHTML 1.0 strict:doctype declarations would typically, but don't always, contain A URL to the specified DTD file. So HTML5, for instance, doesn ' t require a URL. Browsers tend to not read these files, choosing instead to recognize common DOCTYPE declarations. Doctypes currently come in flavors, strict and transitional. As the name suggests, transitional doctypes is aimed at people transitioning from older versions of the language. As such, the transitional versions of HTML 4.01 and XHTML 1.0 still allow the use of deprecated elements like the font ele ment. The strict versions of these languages ban the use of deprecated elements to separate content from Presentation. &nbs P
 DTD is for the browser to "see", depending on the DTD browser will be different actions, HTML5 DTD does not need to include the URL field
validation as well as being semantically marked up, a HTML document needs to be written using valid code. If The code is invalid, browsers would try to interpret the markup themselves, sometimes getting it wrong. Worse Still, if an XHTML document was being sent with the correct MIME type, browsers that understand XML simply won ' t disp Lay an invalid page. Because browsers need to know which DTD-to-use-order to-process the page correctly, a DOCTYPE declaration is required F or the page to validate. You can check to see if your HTML are valid by using the validator, a validator bookmarklet, or a plug-in like the fire Fox Web Developer Extension. Many HTML editors now has validators built in, and you can even install a copy of the W3C Validator locally on your computer. The validator would tell you if your page validates, and if isn't, why not!
This section is used to illustrate the importance of validate, through the validator can be known as early as there is no through validate!
[CSS Mastery]chapter 1:setting The Foundations