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.

550 lines
20 KiB

  1. ---
  2. layout : 'default'
  3. css : 'guide'
  4. title : 'CSS'
  5. description : 'Suggestions for CSS development practices'
  6. type : 'UI Guide'
  7. ---
  8. <%- @partial('header') %>
  9. <div class="main container">
  10. <div class="peek">
  11. <div class="ui vertical pointing secondary menu">
  12. <a class="active item">Techniques</a>
  13. <a class="item">Style Guide</a>
  14. <a class="item">Units</a>
  15. </div>
  16. </div>
  17. <h2 class="ui dividing header">Techniques</h2>
  18. <h3 class="ui header">Conditional Content Formatting</h3>
  19. <p>Sometimes content should be formatted differently depending on whether other content exists alongside it. A useful way to do that is use the next adjacent sibling selector '+'. The content that should have the conditional formatting should always appear second because there is no previous sibling selector in css.</p>
  20. <div class="code" data-type="css">
  21. .ui.widget .image {
  22. float: left;
  23. width: 2em;
  24. }
  25. .ui.widget .content {
  26. color: #000000;
  27. }
  28. /* content is only floated if image exists */
  29. .ui.widget .image + .content {
  30. margin-left: 3em;
  31. }
  32. </div>
  33. <h3 class="ui header">Box Shadow Inheritance</h3>
  34. <p>Unfortunately box shadow must be defined as a single rule, without the ability, like border, to be broken up into separate parts for positioning, color and distance. There is however one trick to allowing for a single box shadow definition to appear as multiple colors. Box shadow inherits its color from the color attribute if no color is specified.</p>
  35. <div class="code" data-type="css">
  36. .ui.widget {
  37. -webkit-box-shadow: 0em 0.2em 0em inset;
  38. -moz-box-shadow: 0em 0.2em 0em inset;
  39. box-shadow: 0em 0.2em 0em inset;
  40. }
  41. /* This will also have a red box shadow */
  42. .ui.red.widget {
  43. color: #FF0000;
  44. }
  45. </div>
  46. <h3 class="ui header">Not Sure the Size? Table It</h3>
  47. <p>If content needs to fill a specified size exactly, but may be made up of parts with arbitary size, it can be useful to use display table and table-cell. This will make sure the parent width is always obeyed regardles of alterations to an element's width.</p>
  48. <p>Keep in mind any table-cell element must have a parent with <code>display:table</code> to appear formatted correctly.</p>
  49. <div class="code" data-type="css">
  50. .ui.input {
  51. display: table;
  52. }
  53. /* these two elements will always fill the input exactly regardless of the text content of the button */
  54. .ui.input input {
  55. display: table-cell;
  56. }
  57. .ui.input .button {
  58. display: table-cell;
  59. }
  60. </div>
  61. <h3 class="ui header">Fixing Whitespace with Inline Block</h3>
  62. <p>In HTML usually whitespace doesn't change a page's layouts. However, when using inline block any whitespace will create unwanted space between consecutive inline-block elements. The simple and effective fix is to make sure font-size on the parent element is 0, making all whitespace 0 pixels. Then you can reset font-size on the child element to body font size or 1 relative em.</p>
  63. <div class="code" data-type="css">
  64. /* remove whitespace */
  65. .ui.group {
  66. font-size: 0em;
  67. }
  68. .ui.group .item {
  69. display: inline-block;
  70. font-size: 1rem;
  71. }
  72. </div>
  73. <h3 class="ui header">CSS :not is Awesome</h3>
  74. <p>Inheritance can be hell sometimes. Consider changing the opacity of an element. There is no way to "cancel out" of that opacity change on content inside.</p>
  75. <p>Sometimes the only way to avoid creating inheritance problems is to specify what elements should not receive a set of rules. Consider blurring content while a dimmer is present. Blurring should only apply to elements that are not inside the dimmer, but there is no way to know what the content is beforehand.</p>
  76. <div class="code" data-type="css">
  77. /* blur everything but the dimmer */
  78. body.dimmed :not(.dimmer){
  79. -webkit-filter: blur(15px) grayscale(0.7);
  80. -moz-filter: blur(15px) grayscale(0.7);
  81. }
  82. </div>
  83. <h3 class="ui header">Prevent Accidental Highlighting</h3>
  84. <p>Sometimes text can be accidentally highlighted when a user double clicks an element. No need to pull out javascript to solve this.</p>
  85. <div class="code" data-type="css">
  86. .ui.thingy {
  87. -webkit-user-select: none;
  88. -moz-user-select: none;
  89. -ms-user-select: none;
  90. user-select: none;
  91. }
  92. </div>
  93. <h3 class="ui header">Multiple Highlight Colors</h3>
  94. <p>It's fairly commonly known that you can set text selection colors in css for a page, but its less commonly known that you can use multiple highlight colors in a single page.</p>
  95. <div class="code" data-type="css">
  96. /* text selected turns highlighted yellow */
  97. ::-webkit-selection {
  98. background-color: #FFFFCC;
  99. color: #555555;
  100. }
  101. ::-moz-selection {
  102. background-color: #FFFFCC;
  103. color: #555555;
  104. }
  105. ::selection {
  106. background-color: #FFFFCC;
  107. color: #555555;
  108. }
  109. /* headers selected turn red */
  110. h1::-moz-selection,
  111. h2::-moz-selection,
  112. h3::-moz-selection {
  113. background-color: #F1C1C2;
  114. color: #222222;
  115. }
  116. h1::selection,
  117. h2::selection,
  118. h3::selection {
  119. background-color: #F1C1C2;
  120. color: #222222;
  121. }
  122. </div>
  123. <h3 class="ui header">Shading Borders</h3>
  124. <p>When you have colored content that needs a border to match its current color, try using a 1 pixel inset box shadow with an RGBA value.</p>
  125. <div class="code" data-type="css">
  126. /* this border will be grey */
  127. .ui.thingy {
  128. -moz-box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.1) inset;
  129. -webkit-box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.1) inset;
  130. box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.1) inset;
  131. }
  132. /* this border will be red tinted */
  133. .ui.red.thingy {
  134. background-color: #FFF8F8;
  135. }
  136. </div>
  137. <h3 class="ui header">Joining borders</h3>
  138. <p>Sometimes bordered content must sit next to other bordered content. If each element uses border the borders will double. Consider using either outline or a box shadow to accomplish the same effect but without overlapping borders.</p>
  139. <div class="code" data-type="css">
  140. /* this might not go so well */
  141. .ui.thingy {
  142. border: 1px solid #DDDDDD;
  143. }
  144. /* rgba is great, but keep in mind the overlapping borders will be added together to create a darker shade */
  145. .ui.thingy {
  146. outline: 1px solid rgba(0, 0, 0, 0.1);
  147. }
  148. /* classic but works */
  149. .ui.thingy {
  150. outline: 1px solid #DDDDDD;
  151. }
  152. /* this works too */
  153. .ui.thingy {
  154. -moz-box-shadow: 0px 0px 0px 1px #DDDDDD;
  155. -webkit-box-shadow: 0px 0px 0px 1px #DDDDDD;
  156. box-shadow: 0px 0px 0px 1px #DDDDDD;
  157. }
  158. </div>
  159. <h3 class="ui header">Using transparency</h3>
  160. <p>RGBA colors in css allow you to specify colors with a transparency channel. This is very useful.</p>
  161. <p>Consider for example, defining the text states of an element. If the elements color changes, the text might appear more complementary as a shade of black with a portion of the original color. This can be done easily with rgba</p>
  162. <div class="code" data-type="css">
  163. .ui.thingy {
  164. background-color: #FAFAFA;
  165. color: rgba(0, 0, 0, 0.7);
  166. }
  167. .red.ui.thingy {
  168. background-color: #FF0000;
  169. }
  170. </div>
  171. <h3 class="ui header">Nothing is always Fixed</h3>
  172. <p>CSS fixed allows you to have content stick to an offset position in a page regardless of the scroll position of the page. Fixed menus will use the page as the offset parent, in all circumstances except when css transform is used on a parent element. <b>This will change the offset context to the transformed element instead of the page</b>.
  173. <p>This behavior may seem unexpected, but using this quirk can allow you to have fixed position content relative to any element in a page.</p>
  174. <div class="code" data-type="css">
  175. .ui.content {
  176. transform: rotateZ(0deg);
  177. overflow: scroll;
  178. }
  179. /* fixed will stay in its current offset relative to the parent container */
  180. .ui.content .fixed.thingy {
  181. position: fixed;
  182. top: 0px;
  183. width: 100%;
  184. }
  185. </div>
  186. <h3 class="ui header">Centering Content with Transform</h3>
  187. <p>Using percentages in CSS will give you a ratio based on the size of the parent element. For example, setting content to be left 50% will set content to start at exactly halfway across its parent.</p>
  188. <p>If you want content to positioned relative to its own size use transform. Percentages specified inside a transform are unique in that they are based on the current element size not its parent.</p>
  189. <div class="code" data-type="css">
  190. /* doesnt work */
  191. .ui.modal {
  192. width: 800px;
  193. height: 500px;
  194. left: 50%;
  195. top: 50%;
  196. margin: -50% 0px 0px -50%x;
  197. }
  198. /* works with measurements */
  199. .ui.modal {
  200. width: 800px;
  201. height: 500px;
  202. left: 50%;
  203. top: 50%;
  204. margin: -250px 0px 0px -400px;
  205. }
  206. /* with transform no measurements needed */
  207. .ui.modal {
  208. position: absolute;
  209. width: 800px;
  210. top: 50%;
  211. left: 50%;
  212. transform: transformX(-50%) transformY(-50%);
  213. }
  214. </div>
  215. <h3 class="ui header">Consider alternatives to floats</h3>
  216. <p>CSS floats can create issues with the containing element not receiving the size of its children. Using overflow:hidden to clear floats means that no peice of an element can be shown outside the bounding box of the element, which limits the possibilities in an element. Clearfixes can use up one of two available pseudo class which can often be useful for styling elements.</p>
  217. <p>Consider using another means of putting content side by side like inline-block or table-cell. These provide more freedom than floated block elements, and can add additional benefits.</p>
  218. <p>To avoid issues with inline-block causing spacing between elements, specify no font size on the group and 1rem on the floated content</p>
  219. <div class="code" data-type="css">
  220. /* not the best */
  221. .ui.thingy {
  222. display: block;
  223. overflow: hidden;
  224. }
  225. .ui.thingy .part {
  226. display: block;
  227. float: left;
  228. }
  229. /* these do the same thing */
  230. .ui.thingy {
  231. display: block;
  232. font-size: 0rem;
  233. }
  234. .ui.thingy .part {
  235. display: inline-block;
  236. font-size: 1rem;
  237. }
  238. </div>
  239. <h3 class="ui header">Onion Skinning</h3>
  240. <p>One technique that is useful for allowing for multiple color variations of an element, without having to completely reskin each variation and shade is to use background-image gradients to define shading and state, and background-color to define the color of an element. If done well you can add a variety of colors with very little code.</p>
  241. <div class="code" data-type="css">
  242. .ui.thingy {
  243. background-color: #FAFAFA;
  244. }
  245. .ui.red.thingy {
  246. background-color: #FF0000;
  247. }
  248. .ui.green.thingy {
  249. background-color: #00FF00;
  250. }
  251. .ui.blue.thingy {
  252. background-color: #0000FF;
  253. }
  254. .ui.thingy:hover {
  255. background-image:
  256. -webkit-gradient(linear, 0 0, 0 100%, from(rgba(0, 0, 0, 0.1)), to(rgba(0, 0, 0, 0.05)))
  257. ;
  258. background-image: -webkit-linear-gradient(
  259. rgba(0, 0, 0, 0.1) 0%,
  260. rgba(0, 0, 0, 0.05) 100%)
  261. ;
  262. background-image: -moz-linear-gradient(
  263. rgba(0, 0, 0, 0.1) 0%,
  264. rgba(0, 0, 0, 0.05) 100%)
  265. ;
  266. background-image: -o-linear-gradient(
  267. rgba(0, 0, 0, 0.1) 0%,
  268. rgba(0, 0, 0, 0.05) 100%)
  269. ;
  270. background-image: linear-gradient(
  271. rgba(0, 0, 0, 0.1) 0%,
  272. rgba(0, 0, 0, 0.05) 100%)
  273. ;
  274. }
  275. </div>
  276. <h2 class="ui dividing header">Style Guide</h2>
  277. <h3 class="ui header">Don't Hyphenate</h3>
  278. <p>All elements are designed to include their own namespace. As long as rules descend from their parent element there is no possibility of rule collision.</p>
  279. <p>Hyphenated class names often describe the intersection of separate concepts, and can be better written to represent each concept separately.</p>
  280. <div class="code" data-type="css">
  281. /* no no no */
  282. .ui.button-active-red {
  283. background-color: #FF0000;
  284. font-weight: bold;
  285. color: #FFFFFF;
  286. }
  287. /* better */
  288. .red.ui.button {
  289. background-color: #FF0000;
  290. color: #FFFFFF;
  291. }
  292. .active.ui.button {
  293. font-weight: bold;
  294. }
  295. </div>
  296. <h3 class="ui header">CSS Legibility</h3>
  297. <p>Adding extra formatting can help increase clarity in your code. We suggest separating css selectors on separate lines, adding a space after css properties with commas (like box-shadow) and placing a zero before any decimal value.</p>
  298. <div class="code" data-type="css">
  299. /* not so easy to read */
  300. .ui.widgets .widget, ui.widget, .ui.widget.type, ui.widgets .widget.type {
  301. color: #FFFFFF;
  302. }
  303. /* this should have spaces after commas and a zero before any decimal value */
  304. .ui.widget {
  305. color:rgba(0,0,0,.3);
  306. }
  307. /* easier to read */
  308. .ui.widgets .widget,
  309. .ui.widget,
  310. .ui.widgets .widget.type,
  311. .ui.widget.type {
  312. color: #FFFFFF;
  313. }
  314. /* properly formatted */
  315. .ui.widget {
  316. color: rgba(0, 0, 0, 0.3);
  317. }
  318. </div>
  319. <h3 class="ui header">Keep Things Ordered</h3>
  320. <p>Although css rule order may be considered a chore, grouping related rules together can help keep css code organized.</p>
  321. <p>An easy way to do this is to consider ordering rules from the outside in. First describing positioning rules, then border rules, margin, sizing, padding, font-size, line height and ending with vendor prefixed attributes.</p>
  322. <div class="code" data-type="css">
  323. /* not so easy to read */
  324. .ui.widgets .widget, ui.widget, .ui.widget.type, ui.widgets .widget.type {
  325. color: #FFFFFF;
  326. }
  327. /* more legible */
  328. .ui.widgets .widget,
  329. .ui.widget,
  330. .ui.widgets .widget.type,
  331. .ui.widget.type {
  332. color: #FFFFFF;
  333. }
  334. /* i have some extra time so lets group these rules */
  335. .ui.widget {
  336. position: relative;
  337. top: 0em;
  338. left: 0em;
  339. margin: 1em;
  340. border: 1px solid rgba(0, 0, 0, 0.1);
  341. width: 100px;
  342. height: 100px;
  343. padding: 1em;
  344. font-size: 1.2em;
  345. color: rgba(0, 0, 0, 0.8);
  346. line-height: 1.2em;
  347. -webkit-border-radius: 0.3em;
  348. -moz-border-radius: 0.3em;
  349. border-radius: 0.3em;
  350. }
  351. </div>
  352. <h3 class="ui header">Avoid non-semantic tags, make wrappers groups</h3>
  353. <p>UI elements should be designed to include the minimum footprint of an element. If extra styling is needed, consider using pseudo selectors :after and :before. This allows for the creation of two extra divs inside each div context which can almost always be enough to accomodate extra styling.</p>
  354. <p>If there is no other option than wrapping content in a containing HTML element, consider using a singular/plural relationship by having the wrapper describe its contents instead of an arbitrary class name like <code>wrapper</code></p>
  355. <div class="code" data-type="css">
  356. /* not tops */
  357. .ui.message .wrapper .title,
  358. .ui.message .wrapper .description {
  359. }
  360. /* better */
  361. .ui.message .content .title,
  362. .ui.message .content .description {
  363. }
  364. </div>
  365. <h3 class="ui header">Margins</h3>
  366. <p>Adding default margins to your content allow for it to have vertical rhythm in a page, but be careful about forcing margin in all circumstances. Here's a useful way to make sure content inside container elements doesn't receieve unnecessary padding.</p>
  367. <div class="code" data-type="css">
  368. .ui.thingy {
  369. margin: 1em 0em;
  370. }
  371. .ui.thingy:first-child {
  372. margin-top: 0em;
  373. }
  374. .ui.thingy:last-child {
  375. margin-bottom: 0em;
  376. }
  377. </div>
  378. <h3 class="ui header">Grammatical order</h3>
  379. <p>Consider using similar class syntax as if you were actually describing the element in English. Although this is by no means required it may help provide clarity in some circumstances.</p>
  380. <div class="code" data-type="css">
  381. /* confusing word order */
  382. .ui.red.button.pointing {
  383. }
  384. /* much more semantic */
  385. .red.pointing.ui.button {
  386. }
  387. </div>
  388. <h3 class="ui header">Use Border Box</h3>
  389. <p>Border box fixes the box model, and allows padding to be included as part of width and height definitions. Using it opens up another world of possibilities for sizing content to fit fluidly</p>
  390. <div class="code" data-type="css">
  391. .two.ui.thingies .ui.thingy {
  392. width: 50%;
  393. padding: 1em;
  394. -webkit-box-sizing: border-box;
  395. -moz-box-sizing: border-box;
  396. -ms-box-sizing: border-box;
  397. box-sizing: border-box;
  398. }
  399. </div>
  400. <h2 class="ui dividing header">Units and Measurements</h2>
  401. <h3 class="ui header">Unit Consistency</h3>
  402. <p>CSS provides a broad selection of measurements for values. It is helpful to keep unit definitions consistent across a single property definition. Including consistent units for 0 values also allows for quicker tweaking and shows greater precision in a property definition.</p>
  403. <div class="code" data-type="css">
  404. /* not tops */
  405. .ui.widget {
  406. padding: 0 16px .6em;
  407. }
  408. /* hex are uppercase */
  409. .ui.widget {
  410. color: #09ffda;
  411. }
  412. /* good */
  413. .ui.widget {
  414. padding: 0em 0em 0em 0.2em;
  415. color: #009FDA;
  416. }
  417. </div>
  418. <h3 class="ui header">Inheritance is Magical</h3>
  419. <p>Many CSS properties can use the value 'inherit' to specify, quite unintuitively, to take on the value of parent element for values that do not normally inherit. For example, an element can use inherit to double whatever its parents padding size is, or to receive the same type of border as a parent without knowing what it is beforehand.</p>
  420. <div class="code" data-type="css">
  421. /* inheriting borders */
  422. .ui.widget {
  423. border: 2px solid rgba(0, 0, 0, 0.1);
  424. }
  425. /* will recieve same border treatment as parent */
  426. .ui.widget .foo {
  427. border: inherit;
  428. }
  429. /* inheriting positioning */
  430. .ui.widget {
  431. padding: 0.5em 1em;
  432. }
  433. /* will use the same padding as parent */
  434. .ui.widget .foo {
  435. padding: inherit;
  436. }
  437. </div>
  438. <h3 class="ui header"><em>Relatively</em> Relative</h3>
  439. <p>EMs are defined so that 1em is equal to the current font size inside of an element. Using EMs can allow you to size content inside an element in proportion to the overall size of the element. Be careful though because this will stack with nested elements.</p>
  440. <div class="code" data-type="css">
  441. .ui.thingy {
  442. font-size: 14px;
  443. }
  444. /* this is 28 pixels */
  445. .ui.thingy .thing {
  446. font-size: 2em;
  447. }
  448. /* woah this is now 48 pixels */
  449. .ui.thingy .thing .thing {
  450. font-size: 2em;
  451. }
  452. /* .ui.thingy .thingy should grow as well */
  453. .ui.large.thingy {
  454. font-size: 16px;
  455. }
  456. </div>
  457. <h3 class="ui header"><em>Recursively</em> Relative</h3>
  458. <p>Using EMs multiplicative nature can be used to your advantage. Instead of defining multiple tiers of a menu system, consider using ems to reduce each tier's sizing. As you continue to nest menu elements each nested menu will computer its values with smaller proportions.</p>
  459. <div class="code" data-type="css">
  460. .ui.menu {
  461. font-size: 1rem;
  462. }
  463. .ui.menu .menu {
  464. margin-left: 0.5em;
  465. font-size: 0.9em;
  466. }
  467. </div>
  468. <h3 class="ui header"><em>Absolutely</em> Relative</h3>
  469. <p>Relative EMs (rems) are calculated relative to the font size of the entire page. This is needed to explain how content should be sized related to the overall size of elements on the page, and will not increase geometrically when nested like EMs.</p>
  470. <div class="code" data-type="css">
  471. /* i am a weird page with very tiny fonts */
  472. html {
  473. font-size: 10px;
  474. }
  475. /* thats ok i am sizing everything relative to your tiny fonts */
  476. .ui.menu {
  477. font-size: 1rem;
  478. }
  479. /* i am sizing relative to the menu element */
  480. .ui.menu .menu {
  481. font-size: 0.8em;
  482. }
  483. /* i am the same size as the rule above */
  484. .ui.menu .menu .menu {
  485. font-size: 0.8em;
  486. }
  487. </div>
  488. </div>