You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

themes-css.asciidoc 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. ---
  2. title: Introduction to Cascading Style Sheets
  3. order: 2
  4. layout: page
  5. ---
  6. [[themes.css]]
  7. = Introduction to Cascading Style Sheets
  8. ((("CSS", "introduction", id="term.themes.css", range="startofrange")))
  9. Cascading Style Sheets or CSS is the basic technique to separate the appearance
  10. of a web page from the content represented in HTML. In this section, we give an
  11. introduction to CSS and look how they are relevant to software development with
  12. Vaadin.
  13. As we can only give a short intruction in this book, we encourage you to refer
  14. to the rich literature on CSS and the many resources available in the web. You
  15. can find the authoratitative specifications of CSS standards from the
  16. link:http://www.w3.org/Style/CSS/[W3C
  17. website]
  18. ifdef::web[]
  19. and other literature, references, and tutorials from the
  20. link:http://www.dmoz.org/Computers/Data_Formats/Style_Sheets/CSS/[Open Directory
  21. Project page on CSS], as well as from other
  22. sources
  23. endif::web[]
  24. .
  25. [[themes.css.basics]]
  26. == Applying CSS to HTML
  27. Let us consider the following HTML document that contains various markup
  28. elements for formatting text. Vaadin UIs work in essentially similar documents,
  29. even though they use somewhat different elements to draw the user interface.
  30. [subs="normal"]
  31. ----
  32. <html>
  33. <head>
  34. <title>My Page</title>
  35. <link rel="stylesheet" type="text/css"
  36. href="mystylesheet.css"/>
  37. </head>
  38. <body>
  39. **<p>**This is a paragraph**</p>**
  40. **<p>**This is another paragraph**</p>**
  41. <table>
  42. <tr>
  43. **<td>**This is a table cell**</td>**
  44. **<td>**This is another table cell**</td>**
  45. </tr>
  46. </table>
  47. </body>
  48. </html>
  49. ----
  50. The HTML elements that will be styled later by matching CSS rules are emphasized
  51. above.
  52. The [elementname]#link# element in the HTML header defines the CSS stylesheet.
  53. The definition is automatically generated by Vaadin in the HTML page
  54. that loads the UI of the application. A stylesheet can also be embedded in the
  55. HTML document itself, as is done when optimizing their loading in Vaadin
  56. TouchKit, for example.
  57. [[themes.css.basics]]
  58. == Basic CSS Rules
  59. A stylesheet contains a set of __rules__ that can match the HTML elements in the
  60. page. Each rule consists of one or more __selectors__, separated with commas,
  61. and a __declaration block__ enclosed in curly braces. A declaration block
  62. contains a list of __property__ statements. Each property has a label and a
  63. value, separated with a colon. A property statement ends with a semicolon.
  64. Let us look at an example that matches certain elements in the simple HTML
  65. document given in the previous section:
  66. [source, css]
  67. ----
  68. p, td {
  69. color: blue;
  70. }
  71. td {
  72. background: yellow;
  73. font-weight: bold;
  74. }
  75. ----
  76. The [literal]#++p++# and [literal]#++td++# are element type selectors that match
  77. with [elementname]#p# and [elementname]#td# elements in HTML, respectively.
  78. The first rule matches with both elements, while the second matches only with
  79. [elementname]#td# elements.
  80. Let us assume that you have saved the above style sheet with the name [filename]#mystylesheet.css# and consider the following HTML file located in the same folder.
  81. [[figure.themes.basic.1]]
  82. .Simple Styling by Element Type
  83. image::img/themes-css-match-1.png[]
  84. [[themes.css.basics.inheritance]]
  85. === Style Inheritance in CSS
  86. CSS has __inheritance__ where contained elements inherit the properties of their
  87. parent elements. For example, let us change the above example and define it
  88. instead as follows:
  89. [source, css]
  90. ----
  91. table {
  92. color: blue;
  93. background: yellow;
  94. }
  95. ----
  96. All elements contained in the [elementname]#table# element would have the same properties.
  97. For example, the text in the contained [elementname]#td# elements would be in blue color.
  98. [[themes.css.basics.element-types]]
  99. === HTML Element Types
  100. HTML has a number of element types, each of which accepts a specific set of
  101. properties. The [elementname]#div# elements are generic elements that can be
  102. used to create almost any layout and formatting that can be created with a
  103. specific HTML element type. Vaadin uses [elementname]#div# elements
  104. extensively to draw the UI, especially in layout components.
  105. ((("Google Web Toolkit", "themeing")))
  106. Matching elements by their type as shown above is, however, rarely if ever used
  107. in style sheets for Vaadin applications. We used it above, because it is the
  108. normal way in regular HTML documents that use the various HTML elements for
  109. formatting text, but it is not applicable in Vaadin UIs that consist mostly of
  110. [elementname]#div# elements. Instead, you need to match by element class, as
  111. described next.
  112. [[themes.css.matching-by-class]]
  113. == Matching by Element Class
  114. Matching HTML elements by the __class__ attribute is the most common form of
  115. matching in Vaadin stylesheets. It is also possible to match with the
  116. __identifier__ of a unique HTML element.
  117. The class of an HTML element is defined with the [parameter]#class# attribute as
  118. follows:
  119. [subs="normal"]
  120. ----
  121. <html>
  122. <body>
  123. **<p class="normal">**This is the first paragraph**</p>**
  124. **<p class="another">**This is the second paragraph**</p>**
  125. <table>
  126. <tr>
  127. **<td class="normal">**This is a table cell**</td>**
  128. **<td class="another">**This is another table cell**</td>**
  129. </tr>
  130. </table>
  131. </body>
  132. </html>
  133. ----
  134. The class attributes of HTML elements can be matched in CSS rules with a
  135. selector notation where the class name is written after a period following the
  136. element name. This gives us full control of matching elements by their type and
  137. class.
  138. [source, css]
  139. ----
  140. p.normal {color: red;}
  141. p.another {color: blue;}
  142. td.normal {background: pink;}
  143. td.another {background: yellow;}
  144. ----
  145. The page would look as shown below:
  146. .Matching HTML Element Type and Class
  147. image::img/themes-css-match-class-2.png[]
  148. We can also match solely by the class by using the universal selector
  149. [literal]#++*++# for the element name, for example [literal]#++*.normal++#. The
  150. universal selector can also be left out altogether so that we use just the class
  151. name following the period, for example [literal]#++.normal++#.
  152. [source, css]
  153. ----
  154. .normal {
  155. color: red;
  156. }
  157. .another {
  158. blackground: yellow;
  159. }
  160. ----
  161. In this case, the rule will match with all elements of the same class regardless
  162. of the element type. The result is shown in <<figure.themes.match.class>>. This
  163. example illustrates a technique to make style sheets compatible regardless of
  164. the exact HTML element used in drawing a component.
  165. [[figure.themes.match.class]]
  166. .Matching Only HTML Element Class
  167. image::img/themes-css-match-class-3.png[]
  168. To ensure future compatibility, we recommend that you use only matching based on
  169. the classes and __do not__ match for specific HTML element types in CSS rules,
  170. because Vaadin may change the exact HTML implementation how components are drawn
  171. in the future.
  172. For example, Vaadin earlier used [elementname]#div# element to draw [classname]#Button# components, but later it was changed to use the special-purpose [elementname]#button# element in HTML.
  173. Because of using the [literal]#++v-button++# style class in the CSS rules for the button, styling it has changed only very little.
  174. [[themes.css.matching-by-descendants]]
  175. == Matching by Descendant Relationship
  176. CSS allows matching HTML by their containment relationship. For example,
  177. consider the following HTML fragment:
  178. [subs="normal"]
  179. ----
  180. &lt;body&gt;
  181. &lt;p class="mytext"&gt;Here is some text inside a
  182. paragraph element&lt;/p&gt;
  183. &lt;table class="**mytable**"&gt;
  184. &lt;tr&gt;
  185. &lt;td class="**mytext**"&gt;Here is text inside
  186. a table and inside a td element.&lt;/td&gt;
  187. &lt;/tr&gt;
  188. &lt;/table&gt;
  189. &lt;/body&gt;
  190. ----
  191. Matching by the class name [literal]#++.mytext++# alone would match both the [elementname]#p# and [elementname]#td# elements.
  192. If we want to match only the table cell, we could use the following selector:
  193. [source, css]
  194. ----
  195. .mytable .mytext {color: blue;}
  196. ----
  197. To match, a class listed in a rule does not have to be an immediate descendant
  198. of the previous class, but just a descendant. For example, the selector "
  199. [literal]#++.v-panel .v-button++#" would match all elements with class
  200. [literal]#++.v-button++# somewhere inside an element with class
  201. [literal]#++.v-panel++#.
  202. [[themes.css.cascading]]
  203. == Importance of Cascading
  204. CSS or Cascading Stylesheets are, as the name implies, about __cascading__
  205. stylesheets, which means applying the stylesheet rules according to their
  206. origin, importance, scope, specifity, and order.
  207. For exact rules for cascading in CSS, see the section
  208. link:http://www.w3.org/TR/css3-cascade/#cascading[Cascading] in the CSS
  209. specification.
  210. [[themes.css.cascading.importance]]
  211. === Importance
  212. Declarations in CSS rules can be made override declarations with otherwise
  213. higher priority by annotating them as [literal]#++!important++#. For example, an
  214. inline style setting made in the [literal]#++style++# attribute of an HTML
  215. element has a higher specificity than any rule in a CSS stylesheet.
  216. [source, css]
  217. ----
  218. <div class="v-button" style="height: 20px;">...
  219. ----
  220. You can override the higher specificity with the [literal]#++!important++#
  221. annotation as follows:
  222. [source, css]
  223. ----
  224. .v-button {height: 30px !important;}
  225. ----
  226. [[themes.css.cascading.specificity]]
  227. === Specificity
  228. A rule that specifies an element with selectors more closely overrides ones that
  229. specify it less specifically. With respect to the element class selectors most
  230. commonly used in Vaadin themes, the specificity is determined by the number of
  231. class selectors in the selector.
  232. [source, css]
  233. ----
  234. .v-button {}
  235. .v-verticallayout .v-button {}
  236. .v-app .v-verticallayout .v-button {}
  237. ----
  238. In the above example, the last rule would have the highest specificity and would
  239. match.
  240. As noted earlier, style declarations given in the style attribute of a HTML
  241. element have higher specificity than declarations in a CSS rule, except if the
  242. [literal]#++!important++# annotation is given.
  243. See the CSS3 link:http://www.w3.org/TR/selectors/#specificity[selectors module
  244. specification] for details regarding how the specificity is computed.
  245. [[themes.css.cascading.order]]
  246. === Order
  247. CSS rules given later have higher priority than ones given earlier. For example,
  248. in the following, the latter rule overrides the former and the color will be
  249. black:
  250. [source, css]
  251. ----
  252. .v-button {color: white}
  253. .v-button {color: black}
  254. ----
  255. As specificity has a higher cascading priority than order, you could make the
  256. first rule have higher priority by adding specificity as follows:
  257. [source, css]
  258. ----
  259. .v-app .v-button {color: white}
  260. .v-button {color: black}
  261. ----
  262. The order is important to notice in certain cases, because Vaadin does not
  263. guarantee the order in which CSS stylesheets are loaded in the browser, which
  264. can in fact be random and result in very unexpected behavior. This is not
  265. relevant for Sass stylesheets, which are compiled to a single stylesheet. For
  266. plain CSS stylesheets, such as add-on or TouchKit stylesheets, the order can be
  267. relevant.
  268. [[themes.css.hierarchy]]
  269. == Style Class Hierarchy of a Vaadin UI
  270. Let us give a real case in a Vaadin UI by considering a simple Vaadin UI with a
  271. label and a button inside a vertical layout:
  272. [source, java]
  273. ----
  274. // UI has v-ui style class
  275. @Theme("mytheme")
  276. public class HelloWorld extends UI {
  277. @Override
  278. protected void init(VaadinRequest request) {
  279. // VerticalLayout has v-verticallayout style
  280. VerticalLayout content = new VerticalLayout();
  281. setContent(content);
  282. // Label has v-label style
  283. content.addComponent(new Label("Hello World!"));
  284. // Button has v-button style
  285. content.addComponent(new Button("Push Me!",
  286. new Button.ClickListener() {
  287. @Override
  288. public void buttonClick(ClickEvent event) {
  289. Notification.show("Pushed!");
  290. }
  291. }));
  292. }
  293. }
  294. ----
  295. The UI will look by default as shown in <<figure.themes.css.hierarchy.initial>>.
  296. By using a HTML inspector such as Firebug, you can view the HTML tree and the
  297. element classes and applied styles for each element.
  298. [[figure.themes.css.hierarchy.initial]]
  299. .An Unthemed Vaadin UI
  300. image::img/example-ui-default.png[]
  301. Now, let us look at the HTML element class structure of the UI, as we can see it
  302. in the HTML inspector:
  303. [subs="normal"]
  304. ----
  305. &lt;body class="**v-generated-body v-ff v-ff20 v-ff200 v-gecko v-lin**"
  306. scroll="auto"&gt;
  307. &lt;div id="bookexamplesvaadin7helloworld-447164942"
  308. class="**v-app mytheme**"&gt;
  309. &lt;div class="**v-ui v-scrollable**"
  310. tabindex="1" style="height: 100%; width: 100%;"&gt;
  311. &lt;div class="**v-loading-indicator first**"
  312. style="position: absolute; display: none;"&gt;&lt;/div&gt;
  313. &lt;div class="**v-verticallayout v-layout v-vertical v-widget v-has-width**"
  314. style="width: 100%;"&gt;
  315. &lt;div class="**v-slot**"&gt;
  316. &lt;div class="**v-label v-widget v-has-width**"
  317. style="width: 100%;"&gt;Hello World!&lt;/div&gt;
  318. &lt;/div&gt;
  319. &lt;div class="**v-slot**"&gt;
  320. &lt;div class="**v-button v-widget**"
  321. tabindex="0" role="button"&gt;
  322. &lt;span class="**v-button-wrap**"&gt;
  323. &lt;span class="**v-button-caption**"&gt;Push Me!&lt;/span&gt;
  324. &lt;/span&gt;
  325. &lt;/div&gt;
  326. &lt;/div&gt;
  327. &lt;/div&gt;
  328. &lt;/div&gt;
  329. &lt;/div&gt;
  330. ...
  331. &lt;body&gt;
  332. ----
  333. Now, consider the following theme where we set the colors and margins of various
  334. elements. The theme is actually a Sass theme.
  335. [source, css]
  336. ----
  337. @import "../valo/valo.scss";
  338. @mixin mytheme {
  339. @include valo;
  340. /* White background for the entire UI */
  341. .v-ui {
  342. background: white;
  343. }
  344. /* All labels have white text on black background */
  345. .v-label {
  346. background: black;
  347. color: white;
  348. font-size: 24pt;
  349. line-height: 24pt;
  350. padding: 5px;
  351. }
  352. /* All buttons have blue caption and some margin */
  353. .v-button {
  354. margin: 10px;
  355. /* A nested selector to increase specificity */
  356. .v-button-caption {
  357. color: blue;
  358. }
  359. }
  360. }
  361. ----
  362. The look has changed as shown in <<figure.themes.css.hierarchy.themed>>.
  363. [[figure.themes.css.hierarchy.themed]]
  364. .Themed Vaadin UI
  365. image::img/example-ui-themed.png[]
  366. An element can have multiple classes separated with a space. With multiple
  367. classes, a CSS rule matches an element if any of the classes match. This feature
  368. is used in many Vaadin components to allow matching based on the state of the
  369. component. For example, when the mouse is over a [classname]#Link# component,
  370. [literal]#++over++# class is added to the component. Most of such styling is a
  371. feature of Google Web Toolkit.
  372. [[themes.css.compatibility]]
  373. == Notes on Compatibility
  374. ((("CSS", "compatibility")))
  375. ((("compatibility")))
  376. CSS is a standard continuously under development. It was first proposed in 1994.
  377. The specification of CSS is maintained by the CSS Working Group of World Wide
  378. Web Consortium (W3C). Versioned with backward-compatible "levels", CSS Level 1
  379. was published in 1996, Level 2 in 1998, and the ongoing development of CSS Level
  380. 3 started in 1998. CSS3 is divided into a number of separate modules, each
  381. developed and progressing separately, and many of the modules are already Level
  382. 4.
  383. While the support for CSS has been universal in all graphical web browsers since
  384. at least 1995, the support has been very incomplete at times and there still
  385. exists an unfortunate number of incompatibilities between browsers. While we
  386. have tried to take these incompatibilities into account in the built-in themes
  387. in Vaadin, you need to consider them while developing your own themes.
  388. Compatibility issues are detailed in various CSS handbooks.
  389. (((range="endofrange", startref="term.themes.css")))