HTML5, JavaScript, and jQuery 24-Hour Trainer. Cameron Dane

Чтение книги онлайн.

Читать онлайн книгу HTML5, JavaScript, and jQuery 24-Hour Trainer - Cameron Dane страница 10

HTML5, JavaScript, and jQuery 24-Hour Trainer - Cameron  Dane

Скачать книгу

Any element can be given an id, but, unlike classes, IDs must be unique within a document. The following is an example of a paragraph with an id:

      It is then possible to create a CSS rule that selects this element as follows:

      Notice that the selector begins with a # to indicate it is based on id. This particular example will display the paragraph with the matching id in bold.

      The final common way to select elements is via pseudo-classes. These allow you to select elements based on features that cannot be expressed by the other selectors, for instance, every even numbered row in a table.

      If you consider the firstParagraph example, you may notice that there is a potential issue lurking here. If a new paragraph is added before the current first paragraph, you would need to remember to swap the id onto this element – which would be easy to forget. A better option is to state that you want the first paragraph to be in bold, without specifying which paragraph is the first in the document. This can be achieved as follows:

      This selector first selects all the p elements, and then limits this selection to just the first element found of its type. Because all the elements returned have the type of p, the first-of-type selector will return the first p element in the document. Pseudo-class selectors always begin with a single or double colon.

      Pseudo-classes are also useful for providing styles to elements based on their state. For instance, if you wanted links to turn green when the user hovered over them, you could use the following selector:

      There is no way to perform this selection without pseudo-classes.

      Note

      CSS actually supports two related, but technically distinct, mechanisms: pseudo-classes and pseudo-elements. Technically, the selectors you have looked at are pseudo-classes because they select elements that you could not select via other selectors. CSS also supports pseudo-elements: These allow a portion of an element to be selected, such as the first letter in a paragraph, or the first line in a paragraph.

      Pseudo-element selectors are supposed to use a double colon rather than a single colon, but some browsers do not support the double colon syntax, so the single colon syntax is regularly used for both types of selector.

      When selecting the first paragraph in the document, you are actually combining two types of selector: an element selector and a pseudo-class selector. It turns out that you can combine selectors in many interesting ways.

      For example, if I wanted to select all the h1 elements that had the class redParagraph, I could use the following selector:

      Notice that there is no space between the element selector and the class selector. Alternatively, if I wanted to select all h1 elements that had both the redHeader and pageHeader classes, I could use the following:

      Alternatively, you can select elements only when they are children of elements returned by other selections. For instance, you can specify that the cite element should be capitalized, but only when it is a child of a blockquote element (which, as it happens, it always is):

      Notice in this case there is a space between the two selections. This will match cite elements if they are a descendant of a blockquote element, even if blockquote is not their immediate parent. Another way to think about this is two distinct selections. CSS first selects all the blockquote elements, and then it searches for any cite elements that are descendants.

      With the > operator, it is possible to specify that the selection should only occur if the element is an immediate child of the first selection:

      CSS Files and Inline Styles

      So far, you have used the style element to add CSS to a web page. Although this is an easy way of adding CSS, it has the disadvantage that you cannot use the same CSS across multiple pages.

      It is therefore far more common to place all the CSS in a file with a .css extension and link it to each web page that needs to use it. In order to try this out, save the styles you have added so far in a file called examples.css. Place this in the same folder as the HTML page, but do not include the style element.

      Now, remove the whole style element from the head of the document, and replace it with the following:

      Again, the href attribute is using a relative URL to load the style sheet, but it could also use an absolute URL. If you reload the web page it should display the same as before.

      An alternative way of specifying CSS properties is via the style attribute on individual elements. Although this approach is generally discouraged, it can be useful when a style is unique to a single element. As you will also see, these styles have a higher precedence, so it can be a useful approach for overriding global styles. The following is an example:

      Notice that the inline styles use the same basic syntax: Colons separate names and properties, and semicolons separate styles. Obviously, they do not include a selector because they are applied to the element they are declared on.

      Specificity

      The same element may match multiple CSS rules. When this occurs, all the properties defined in all the rules are applied to the element. You have already seen an example of this with the h1 element.

      Конец ознакомительного фрагмента.

      Текст предоставлен ООО «ЛитРес».

      Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.

      Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.

iVBORw0KGgoAAAANSUhEUgAABAEAAAAxCAYAAACrtrCmAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAeuSURBVHhe7d0LcqJAFAXQrMbdsBzX44ayJ6ebnw30B01I

Скачать книгу