Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. ##Nesting##
  2. Nesting in html is the embedding of elements within other elements, creating a parent-child relationship.
  3.  
  4. When an element is nested directly inside of another element the inner element is the child and the outer element is the parent.
  5.  
  6. ***index.html***
  7.  
  8. ```
  9. <body>
  10. <div></div>
  11. </body>
  12. ```
  13. In the example above the \<body> element is the parent and the \<div> element is the child.
  14.  
  15. When there are multiple elements nested within the same element, the outer element is still the parent and the inner elements are all the children of the parent. Also, the children have a sibling relationship with each other.
  16.  
  17. ```
  18. <body>
  19. <p>Brother</p>
  20. <p>Sister</p>
  21. <p>Evil twin</p>
  22. </body>
  23. ```
  24. Above, the \<body> is the parent and the \<p> elements are siblings (also they are each the children of the body element)
  25.  
  26. You can nest elements as deeply as you want to. When an element has multiple layers of nested elements it becomes an "ancestor" and the inner elements are "descendants".
  27.  
  28. ```
  29. <body>
  30. <div>
  31. <div>
  32. <div>
  33. <p>The Abyss</p>
  34. </div>
  35. </div>
  36. </div>
  37. </body>
  38. ```
  39. In the example above, all of the \<div> elements and the \<p> element are descendants of the \<body> element. The \<body> element and \<div> elements are all ancestors of the \<p> element.
  40.  
  41. ##CSS Selectors##
  42. ####In CSS, selectors are patterns used to select the element(s) you want to style.####
  43. ####Let's take a look####
  44.  
  45. In the example below we use an html \<p> element as our selector.
  46.  
  47. ***main.css***
  48.  
  49. ```
  50. p {
  51. color: red;
  52. }
  53. ```
  54.  
  55. Now, any paragraph element in our document will have the color red.
  56.  
  57. ####Class Selectors####
  58.  
  59. You can also use class names as selectors. In html you can assign any class name you want to any element. Multiple elements in a document can have the same class value.
  60.  
  61. ***index.html***
  62.  
  63. ```
  64. <body>
  65. <h1 class="special">Hello World</h1>
  66. <p class="special">Here's Johnny</p>
  67. <p>Live from New York</p>
  68. </body>
  69. ```
  70.  
  71. In your stylesheet, type a full stop (period) before the class name when you use it in a selector.
  72.  
  73. ***main.css***
  74.  
  75. ```
  76. .special {
  77. color: blue;
  78. text-decoration: underline;
  79. }
  80. ```
  81.  
  82. Note that the \<h1> element and the first \<p> element are effected by our styling and the second \<p> element without the class "special" is not effected.
  83.  
  84. ####ID Selectors####
  85.  
  86. ID attributes can be used as selectors as well. In html you can assign any id name you want to any element but the ID name must be unique in the document (only used on one element).
  87.  
  88. ***index.html***
  89.  
  90. ```
  91. <body>
  92. <div id="header">
  93. <h1>Welcome</h1>
  94. </div>
  95. <div>
  96. <p>All of the content</p>
  97. </div>
  98. </body>
  99. ```
  100.  
  101. In your stylesheet, type a number sign (hash) before the ID when you use it in a selector.
  102.  
  103. ***main.css***
  104.  
  105. ```
  106. #header {
  107. width: 100%;
  108. height: 70px;
  109. background-color: blue;
  110. }
  111. ```
  112. ##Selectors based on relationships##
  113.  
  114. CSS has some ways to select elements based on the relationships between elements. You can use these to make selectors that are more specific.
  115.  
  116. ####A E####
  117.  
  118. Any E element that is a descendant of an A element (that is: a child, or a child of a child, etc.)
  119.  
  120. Using the html below the following css would effect any \<p> elements within a \<div> but not the \<p> element outside of a \<div>.
  121.  
  122. ***index.html***
  123.  
  124. ```
  125. <body>
  126. <div>
  127. <p>Wheel of Fortune</p>
  128. <div>
  129. <p>Jeopardy</p>
  130. </div>
  131. </div>
  132. <p>The Price is Right</p>
  133. </body>
  134. ```
  135. ***main.css***
  136.  
  137. ```
  138. div p {
  139. font-style: italic;
  140. }
  141. ```
  142. ####A > E####
  143.  
  144. Any E element that is a direct child of an A element.
  145.  
  146. ***index.html***
  147.  
  148. ```
  149. <body>
  150. <div class="header">
  151. <div>
  152. <div>
  153. <p>The Abyss</p>
  154. </div>
  155. </div>
  156. </div>
  157. </body>
  158. ```
  159. ***main.css***
  160.  
  161. ```
  162. .header > div {
  163. width: 400px;
  164. height: 100px;
  165. background-color: purple;
  166. }
  167. ```
  168. Note that this only effects the direct child of "header" and not further descendants.
  169.  
  170. ##Pseudo-classes##
  171.  
  172. A CSS pseudo-class is a keyword added to selectors that specifies a special state of the element to be selected. For example :hover will apply a style when the user hovers over the element specified by the selector.
  173.  
  174. Add psuedo-classes to other element selectors
  175.  
  176. ***main.css***
  177.  
  178. ```
  179. button {
  180. background-color: blue;
  181. }
  182.  
  183. button:hover {
  184. background-color: red;
  185. }
  186. ```
  187.  
  188. ####Some common psuedo classes####
  189.  
  190. **:active** - activated when the element is clicked
  191.  
  192. **:checked** - used for input type=checkbox
  193.  
  194. **:first-child** - represents any element that is the first child element of its parent
  195.  
  196. **:first-of-type** - accesses the first of the type of element being referenced within the element's parent container
  197.  
  198. **:focus** - upon input or button focus (using tab to cycle through elements in the browser)
  199.  
  200. **:hover** - activated when a user mouses over the element
  201.  
  202. **:last-child** - represents any element that is the last child element of its parent
  203.  
  204. **:last-of-type** - accesses the last of the type of element being referenced within the element's parent container
  205.  
  206. **:visited** - used on anchor elements \<a>Link\</a>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement