Advertisement
decembre

CSS - CALC v.1

May 8th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.53 KB | None | 0 0
  1. ============================
  2. ======== CSS - CALC v.1 - POSTé
  3. ============================
  4.  
  5. http://css-tricks.com/a-couple-of-use-cases-for-calc/
  6. ============================
  7.  
  8. ============================
  9. ======== CALC ()The Fab Four technique to create Responsive Emails without Media Queries
  10. == https://medium.freecodecamp.com/the-fab-four-technique-to-create-responsive-emails-without-media-queries-baf11fdfa848#.y32dtpwl8
  11. The solution involves the CSS calc() function and the three width, min-width and max-width properties.
  12. Or as I like to call them all together: the Fab Four (in CSS).
  13. On top of the calc() function, the solution I found involves these three CSS properties.
  14. In order to fully understand how it works, here’s a reminder of how width, min-width and max-width behave when used together
  15. (as clearly summarized by fellow french front-end developer Raphaël Goetter).
  16.  
  17. If the width value is greater than the max-width value, max-width wins.
  18. If the min-width value is greater than the width or max-width values, min-width wins.
  19.  
  20. Can guess what the width of a box with the following styles would be ?
  21.  
  22. .box {
  23. width:320px;
  24. min-width:480px;
  25. max-width:160px;
  26. }
  27.  
  28. (Answer : the box would be 480px wide.)
  29. Introducing calc() and the magic formula
  30.  
  31. Without further ado, here is an example of the Fab Four to create two columns that will stack and grow below 480px.
  32.  
  33. .block {
  34. display:inline-block;
  35. min-width:50%;
  36. max-width:100%;
  37. width:calc((480px — 100%) * 480);
  38. }
  39.  
  40. Let’s break it down for each width property.
  41.  
  42. min-width:50%;
  43.  
  44. The min-width property defines our column widths on what we could call our desktop version.
  45. We can change this value to add more columns (for example, 25% for a four columns layout),
  46. or set columns with fixed pixel widths.
  47.  
  48. max-width:100%;
  49.  
  50. The max-width property defines our column widths on what we could call our mobile version.
  51. At 100%, each column will grow and adapt to the full width of their parent container.
  52. We can change this value to keep columns on mobile (for example, 50% for a two columns layout).
  53.  
  54. width:calc((480px — 100%) * 480);
  55.  
  56. Thanks to the calc() function, the width property is where the magic happens.
  57. The 480 value matches our desired breakpoint value.
  58. The 100% corresponds to the width of the parent container of our columns.
  59. The goal of this calculation is to create a value bigger than our max-width or smaller than our min-width,
  60. so that either one of those property is applied instead.
  61.  
  62.  
  63. ============================
  64. ======== CALC() comes to the rescue!
  65. == http://www.dreamdealer.nl/articles/css3_calc_min_and_max_functions.html
  66. <div class="left">left column</div>
  67. <div class="right">right column</div>
  68.  
  69. - You want two containers floating next to each other with a gap of 50 pixels between them.
  70. But the columns need to have a variable width...
  71. Setting both columns to 50% and a right-margin of 50px to the left column
  72. is going to break your layout because the sum of all widths is more than 100%...
  73.  
  74. .left, .right {
  75. float: left;
  76. width: 50%;
  77. }
  78. .left {
  79. margin-right: 50px;
  80. }
  81. - But what if you could set the widths of the two columns to 50% minus 25px (half of the gap)
  82. on both columns?.
  83. That's what calc() enables you to do! That looks like this:
  84.  
  85. .left, .right {
  86. float: left;
  87. width: calc(50% - 25px);
  88. }
  89. .left {
  90. margin-right: 50px;
  91. }
  92.  
  93. No hacks needed!
  94.  
  95. And those darn borders? Also easy!
  96.  
  97. .left, .right {
  98. float: left;
  99. border: 10px solid red;
  100. width: calc(50% - 25px - 20px); /* subtract 20px for a 10px border on both sides */
  101. }
  102. .left {
  103. margin-right: 50px;
  104. }
  105.  
  106. ============================
  107. ======== CALC() VOIR Stylsheet ( using them in combination with the new vh and vw units)
  108. == http://advent2012.digitpaint.nl/17/
  109. Calc()ulating layouts & backgrounds with CSS
  110. Stop! Demo-time!
  111. - This is a percentage width block, it should scale!
  112. The image next to it has a fixed width of 100px.
  113. The height is calculated automatically.
  114. We use calc(100% - 100px /* image width */ - 10px * 2 /* padding */ - 10px /* margin */)
  115.  
  116. - Two adjecent floats: one percentage width block next to fixed width image.
  117.  
  118. - Centering a fixed-width block with left and right spanning background
  119. and a gradient that automatically adjusts to the width.
  120.  
  121. - Center content with retaining backgrounds by using:
  122. padding: 0 calc(50% - WIDTHpx / 2);
  123.  
  124. - Put a fixed width float next to a percentage width float:
  125. width: calc(100% - FIXED_FLOAT_WIDTHpx)
  126.  
  127.  
  128. ============================
  129. ======== CALC()
  130. == http://www.zonecss.fr/style_css/feuille_css_calc.html
  131. - Définition de calc :
  132. La fonction de feuille de style css calc permet d’exécuter un calcul dans votre feuille css.
  133. .identifiant{
  134. width:calc(50% + 12px);
  135. }
  136.  
  137. La fonction de feuille de style calc css retourne comme valeur une longueur.
  138. La fonction de feuille de style css expression peut prendre accepte comme opérateur :
  139. - "+" pour l'addition.
  140. - "-" pour la soustraction.
  141. - "/" pour la division, la division par 0 génère une erreur donc une valeur invalide.
  142. - "*" pour la multiplication.
  143.  
  144. - L'opérateur doit être suivit et précédé d'un espace :
  145. left:calc(50% +10px); // Pas bon
  146. left:calc(50% + 10px); // Bon
  147.  
  148. - Il est possible de mettre plusieurs fonctions de feuille de style css calc à la suite :
  149. margin:calc(1px + 1px) calc(1px + 10px) ;
  150.  
  151. - La fonction de feuille de style calc css accepte accepte de faire un calcul
  152. avec des unités différentes, à condition que ces dernières soient de même types.
  153. Par exemple :
  154. calc(50% + 10px - 20s)
  155. // Pas possible car il y a un mélange entre des longueurs et des seconde
  156.  
  157.  
  158. - Exemple de code calc :
  159. La fonction de feuille de style css calc est applicable
  160. sur toutes propriétés de feuille de style acceptent une longueur ou position.
  161.  
  162. - Problèmes des calc :
  163. La fonction de feuille de style calc css pose des problèmes
  164. car elle n'est interprétée que par Internet Explorer 9+ et par Firefox 16+
  165.  
  166. ============================
  167. ======== CALC()
  168. == http://davidwalsh.name/css-calc
  169. The calc routine is especially useful when calculating relative widths.
  170. Calculations can be additions, subtractions, multiplications, and divisions. Have a look:
  171.  
  172. /* basic calc */
  173. .simpleBlock {
  174. width: calc(100% - 100px);
  175. }
  176.  
  177. /* calc in calc */
  178. .complexBlock {
  179. width: calc(100% - 50% / 3);
  180. padding: 5px calc(3% - 2px);
  181. margin-left: calc(10% + 10px);
  182. }
  183.  
  184. ============================
  185. ======== CALC() Calculated values
  186. == http://alistapart.com/article/love-the-boring-bits-of-css
  187. When you’re working fluidly and/or responsively,
  188. you’ll doubtless come across the problem of mixing units—wanting
  189. to have a grid that’s sized in percentages but with fixed margins.
  190.  
  191. For example:
  192. div {
  193. margin: 0 20px;
  194. width: 33%;
  195. }
  196. If your layout only uses padding and border,
  197. then you can use box-sizing to help you get around that,
  198. but it won’t help you with margins.
  199. A better, more flexible approach is to use the calc() value function,
  200. which lets you perform mathematical operations with different units, such as:
  201. div {
  202. margin: 0 20px;
  203. width: calc(33% - 40px);
  204. }
  205. You’re not limited to using it only on width;
  206. you can use it anywhere length values are permitted—and if you want
  207. to go really deep down the rabbit hole, you can also use calc() inside calc().
  208.  
  209. IE9+ has this unprefixed (!),
  210. Firefox has it with the -moz- prefix (which should be unprefixed in release 16 or 17),
  211. and Chrome and Safari have implemented it with the -webkit- prefix.
  212. It doesn't seem to be in mobile WebKit yet, however.
  213.  
  214.  
  215. ============================
  216. ======== CALC()
  217. ==http://www.sitepoint.com/css3-calc-function/
  218. ==http://css3clickchart.com/?prop=calc
  219. ==http://www.sitepoint.com/css3-calc-function/
  220. ==http://hacks.mozilla.org/2010/06/css3-calc/
  221. Allows you to use a function in place of a length value
  222. to calculate the size or shape of an element instead of defining it explicitly:
  223. .element {
  224. width: -moz-calc(100% + 20px);
  225. width: calc(100% + 20px);
  226. }
  227.  
  228. ============================
  229. ======== CALC() - I have a dynamic width - To: CSS3 - Information and samples
  230. ==http://www.sitepoint.com/css3-calc-function/
  231. ==http://robertnyman.com/css3/calc/calc.html
  232. ==https://developer.mozilla.org/en/CSS/-moz-calc
  233. ==http://hacks.mozilla.org/2010/06/css3-calc/
  234. Using CSS3 calc() to calculate a value dynamically.
  235. calc {
  236. width: 100px;
  237. height: 100px;
  238. border: 1px solid #f00;
  239. padding: 10px;
  240.  
  241. /* Firefox */
  242. width: -moz-calc(75% - 100px);
  243. /* WebKit */
  244. width: -webkit-calc(75% - 100px);
  245. /* Opera */
  246. width: -o-calc(75% - 100px);
  247. /* Standard */
  248. width: calc(75% - 100px);
  249. }
  250. Support the min() and max() functions, which could be used like this:
  251. div {
  252. height: -moz-min(36pt, 2em);
  253. width: -moz-max(50%, 18px);
  254. }
  255.  
  256.  
  257. ============================
  258. ======== CALC() - CSS3 calc() - - I have a dynamic width
  259. ==http://www.sitepoint.com/css3-calc-function/
  260. ==https://developer.mozilla.org/en/CSS/-moz-calc
  261. ==http://hacks.mozilla.org/2010/06/css3-calc/
  262. The calc() CSS function can be used anywhere a <length> is required.
  263. With calc(), you can perform calculations to determine the size and shape of objects.
  264. Syntax:
  265. <a_css_property>: -vendor-calc(expression)
  266. Values :
  267. The expression can be any simple expression combining the following operators:
  268.  
  269. + Addition
  270. - Subtraction
  271. * Multiplication
  272. / Division
  273.  
  274. You can't divide by a length; instead, this is used to, for example,
  275. say that you want your resulting length to be a given fraction of an existing length.
  276. mod Unimplemented
  277. Modulo; this returns the remainder left over after dividing two values.
  278. The operands in the expression may be any length syntax value.
  279. You can use different units for each value in your expression, if you wish.
  280. You may also use parentheses to establish computation order when needed.
  281. Note: Division by zero results in an error being generated by the HTML parser.
  282. Note: The + and - operators must always be surrounded by whitespace.
  283. The operand of calc(50 -8px) for instance will be parsed as a percentage
  284. followed by a negative length, an invalid expression, while the operand of calc(50% - 8px) is
  285. a percentage followed by a minus sign and a length.
  286. (The * and / operators do not require whitespace, but adding it for consistency is a good idea.)
  287.  
  288. == CALC Examples : Positioning an object on screen with a margin
  289. calc() makes it easy to position an object with a set margin.
  290. In this example, the CSS creates a banner that stretches across the window,
  291. with a 40-pixel gap between both sides of the banner and the edges of the window:
  292. .banner {
  293. position:absolute;
  294. left: 40px;
  295. width: -moz-calc(100% - 80px);
  296. width: -webkit-calc(100% - 80px);
  297. width: calc(100% - 80px);
  298. border: solid black 1px;
  299. box-shadow: 1px 2px;
  300. background-color: yellow;
  301. padding: 6px;
  302. text-align: center;
  303. }
  304. == CALC Examples : Automatically sizing form fields to fit their container
  305. Another use case for calc() is to help ensure that form fields fit in the available space,
  306. without extruding past the edge of their container, while maintaining an appropriate margin.
  307. Let's look at some CSS:
  308. input {
  309. padding: 2px;
  310. display: block;
  311. width: -moz-calc(100% - 1em);
  312. width: -webkit-calc(100% - 1em);
  313. width: calc(100% - 1em);
  314. }
  315. #formbox {
  316. width: -moz-calc(100%/6);
  317. width: -webkit-calc(100%/6);
  318. width: calc(100%/6);
  319. border: 1px solid black;
  320. padding: 4px;
  321. }
  322. Here, the form itself is established to use 1/6 of the available window width.
  323. Then, to ensure that input fields retain an appropriate size,
  324. we use calc() again to establish that they should be the width of their container minus 1em.
  325. Then, the following HTML makes use of this CSS:
  326. <form>
  327. <div id="formbox">
  328. <label>Type something:</label>
  329. <input type="text">
  330. </div>
  331. </form>
  332.  
  333. ============================
  334. ======== CALC : Cross Browser CSS Calc()
  335. ==http://kempwire.com/css/cross-browser-css-calc.html
  336. #foo {
  337. width: 200px;
  338. width: -webkit-calc(50% - 100px);
  339. width: -moz-calc(50% - 100px);
  340. width: calc(50% - 100px);
  341. }
  342.  
  343.  
  344.  
  345.  
  346. ============================
  347. ======== CALC : LIMIT TO HOW MANY OPERANDS you can have within a CSS calc() function?
  348. == http://stackoverflow.com/questions/14967421/css-calc-not-working
  349. This works:
  350. div {
  351. left:calc((100%/54)*26);
  352. left:-webkit-calc((100%/54)*26);
  353. }
  354. This does NOT work:
  355. div {
  356. left:calc(((100%/54)*14)-140px);
  357. left:-webkit-calc(((100%/54)*14)-140px);
  358. }
  359.  
  360. But maybe that yes (espaces between - + ):
  361.  
  362. div {
  363. left:calc(((100%/54)*14) - 140px);
  364. left:-webkit-calc(((100%/54)*14) - 140px);
  365. }
  366.  
  367.  
  368. ============================
  369. ======== CALC : A simple use case
  370. == http://webdesignernotebook.com/css/the-wonderful-calc-function/
  371. To show calc’s usefulness further, I’ve created a basic layout where I’ve applied it generously.
  372.  
  373. The layout consists of a main content container on the left, a sidebar on the right,
  374. and a footer, below the previous two.
  375. Here is the basic markup with only part of the text being shown, for brevity:
  376.  
  377. <div class="wrapper">
  378.  
  379. <div id="main">
  380. <h1>Far far away…</h1>
  381. <p>Far far away, behind the word mountains…</p>
  382. </div>
  383.  
  384. <div id="accessory">
  385. <ul>
  386. <li><a href="#">Far far away…</a></li>
  387. <li><a href="#">Separated they live…</a></li>
  388. <li><a href="#">A small river named…</a></li>
  389. </ul>
  390. </div>
  391.  
  392. <div id="footer">
  393. Visit the article…
  394. </div>
  395.  
  396. </div>
  397.  
  398. After the (hopefully) self-explanatory simple reset
  399. (please check the full CSS in the example document),
  400. font properties and defining the links, I will define the look of my body element:
  401.  
  402. body {
  403. background: #E8EADD;
  404. color: #3C323A;
  405. padding: 20px; }
  406.  
  407. After that, I will say that the main container div should be
  408. full width (100%) minus 40px and aligned horizontally in the middle of the viewport:
  409.  
  410. .wrapper {
  411. width: 1024px; /* Fallback for browsers that don't support the calc() function */
  412. width: -moz-calc(100% - 40px);
  413. width: calc(100% - 40px);
  414. margin: auto; }
  415.  
  416. In the CSS above, I have also added a fallback width
  417. for browsers that don’t support the calc() function, and the vendor prefix for Firefox 4.
  418.  
  419. Following this, I will set the borders,
  420. width and margins of the main container div, and I will float it left:
  421.  
  422. #main {
  423. border: 8px solid #B8C172;
  424. float: left;
  425. margin-bottom: 20px;
  426. margin-right: 20px;
  427. padding: 20px;
  428. width: 704px; /* Fallback for browsers that don't support the calc() function */
  429. width: -moz-calc(75% - 20px*2 - 8px*2);
  430. width: calc(75% - 20px*2 - 8px*2); }
  431.  
  432. So what is happening in this calc() function?
  433. I am stating that I want my container div to be 75% wide (75% of its parent container, remember),
  434. but from these 75% I need to subtract “20px*2” to account
  435. for the padding on each side, and “8px*2” to account for the border on each side.
  436.  
  437. Moving onto the sidebar, I want it to occupy the remaining 25% inside the parent container,
  438. but this value will have to allow for padding, borders and the margin of the sidebar div (20px).
  439.  
  440. #accessory {
  441. border: 8px solid #B8C172;
  442. float: right;
  443. padding: 10px;
  444. width: 208px; /* Fallback for browsers that don't support the calc() function */
  445. width: -moz-calc(25% - 10px*2 - 8px*2 - 20px);
  446. width: calc(25% - 10px*2 - 8px*2 - 20px); }
  447.  
  448. Deconstructing “calc(25% - 10px*2 - 8px*2 - 20px)”, we have the original 25%, minus “10px*2” to account
  449. for the padding on each side, minus “8px*2” to account for the border on each side, minus “20px” to account
  450. for the main container’s right margin.
  451.  
  452. The footer is simply a full width div, I shan’t bore everyone by explaining that.
  453.  
  454. Because the sidebar becomes too small after a certain point,
  455. I’ve also included a simple media query that unfloats the main and sidebar containers,
  456. and recalculates their widths so they are 100% wide minus their respective padding and border values.
  457.  
  458. @media screen and (max-width: 768px) {
  459. #main, #accessory {
  460. float: none; }
  461. #main {
  462. margin-right: 0;
  463. width: -moz-calc(100% - 20px*2 - 8px*2);
  464. width: calc(100% - 20px*2 - 8px*2); }
  465. #accessory {
  466. width: -moz-calc(100% - 10px*2 - 8px*2);
  467. width: calc(100% - 10px*2 - 8px*2); }
  468. }
  469.  
  470. This is a very simplistic use case, but hopefully it’s enough to spike your interest
  471. and prompt you to find out more about wonderful calc().
  472.  
  473.  
  474. ============================
  475. ======== CALC :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement