{"id":2397,"date":"2022-08-30T13:48:18","date_gmt":"2022-08-30T13:48:18","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=2397"},"modified":"2022-08-30T13:48:18","modified_gmt":"2022-08-30T13:48:18","slug":"an-advanced-css-interview-question-implement-if-else-in-css-2","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=2397","title":{"rendered":"An Advanced CSS Interview Question: Implement if-else in CSS"},"content":{"rendered":"\n<p id=\"1cf0\">One of my friends recently came across a funny CSS interview question during an interview. When I first saw the question, I thought it was a usual question. However, after careful research, I discovered the interesting part of this question.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p id=\"9c90\">The interview question: please implement the following CSS effect.<\/p>\n\n\n\n<p id=\"0d9d\">There are some numbers on the page showing the number of articles reads. If the number is less than 100, then the color of the number is gray. If the number is greater than or equal to 100, then the number is brown.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*YUZlDMVDM4eXlzgH_NzGDQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"bcec\">And this color can be adjusted dynamically rather than preset in advance.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/519\/1*Xh9Asp2UP001oLzmNIS2dw.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"1127\">Finally, the interviewer asked to use pure CSS to solve the problem.<\/p>\n\n\n\n<p id=\"c4ed\">Do you know how to achieve this effect?<\/p>\n\n\n\n<h1 id=\"cc18\">Analysis<\/h1>\n\n\n\n<p id=\"c383\">What is the essence of this problem?<\/p>\n\n\n\n<p id=\"4abd\">The essence of this problem is very simple. That is, this is actually an if-else problem.<\/p>\n\n\n\n<p id=\"412c\">If we describe this question in pseudocode, it should look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let color;if (reads &lt; 100){<br>  color = 'gray'} else {<br>  color = 'brown'<br>}<\/pre>\n\n\n\n<p id=\"f6cc\">So now the question becomes: how do we implement this if-else logic in CSS? Remember that there is no such thing as an&nbsp;<strong>if<\/strong>&#8211;<strong>else<\/strong>&nbsp;keyword in CSS.<\/p>\n\n\n\n<h1 id=\"8a52\">Implement if-else in CSS<\/h1>\n\n\n\n<p id=\"cdc8\">The logic of implementing if-else in CSS is the core skill examined in this question. Let\u2019s complete this logic below. If you learn this trick, you can use it to achieve many powerful CSS effects.<\/p>\n\n\n\n<p id=\"c669\">First, let\u2019s learn about a function called&nbsp;<code>clamp<\/code>.<\/p>\n\n\n\n<p id=\"bb3f\">From&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/clamp\" rel=\"noreferrer noopener\" target=\"_blank\">MDN<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>The clamp() CSS function clamps a value between an upper and lower bound. clamp() enables selecting a middle value within a range of values between a defined minimum and maximum.<\/p><\/blockquote>\n\n\n\n<p id=\"df15\">Basic format:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>clamp(min, var, max)<\/p><\/blockquote>\n\n\n\n<p id=\"a685\">We can understand the&nbsp;<code>clamp<\/code>&nbsp;function as pseudocode like this:<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/medium.com\/media\/496c6e1d0412e76d201431fffbb4ecaa\n<\/div><\/figure>\n\n\n\n<p id=\"d383\">So:<\/p>\n\n\n\n<ul><li>clamp(10, 13, 20) \u2192 13<\/li><li>clamp(10, 2, 20) \u2192 10<\/li><li>clamp(10, 30, 20) \u2192 20<\/li><\/ul>\n\n\n\n<p id=\"1a7e\">And usage example:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*vB8R3jhAAOfMnO3mE3d4dw.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"4edb\">The value of&nbsp;<code>font-size<\/code>&nbsp;will not exceed 20px, nor will it be lower than 10px.<\/p>\n\n\n\n<p id=\"6e00\">This is the basic usage of the&nbsp;<code>clamp<\/code>. If you still have questions about&nbsp;<code>clamp<\/code>, you can refer to the MDN documentation or leave a comment here.<\/p>\n\n\n\n<p id=\"1758\">Next, we implement this function in CSS.<\/p>\n\n\n\n<p id=\"35c7\">The value of the&nbsp;<code>result<\/code>&nbsp;changes according to the value of&nbsp;<code>var<\/code>:<\/p>\n\n\n\n<ul><li>When the value of&nbsp;<code>var<\/code>&nbsp;is less than 100, the value of the&nbsp;<code>result<\/code>&nbsp;is 10;<\/li><li>When the value of&nbsp;<code>var<\/code>&nbsp;is greater than or equal to 100, the&nbsp;<code>result<\/code>&nbsp;becomes 20.<\/li><\/ul>\n\n\n\n<p id=\"b42d\">If we describe this question in pseudocode, it should look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let result;if(var &lt; 100){<br>  result = 10} else {<br>  result = 20<br>}<\/pre>\n\n\n\n<p id=\"2afb\">This requirement is similar to the clamp function, but it is not the same. The&nbsp;<code>clamp<\/code>&nbsp;can limit the value of var to a range, but we now want the value of the result to be either 10 or 20.<\/p>\n\n\n\n<p id=\"61d9\">How to do it?<\/p>\n\n\n\n<p id=\"cde1\">There is a special trick: We can amplify the change of&nbsp;<code>var<\/code>&nbsp;so that its value either reaches the upper limit or the lower limit of the interval.<\/p>\n\n\n\n<p id=\"6934\">That is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let result = clamp(10, (var-99) * 20, 20)<\/pre>\n\n\n\n<p id=\"9459\">This creates an effect:<\/p>\n\n\n\n<ul><li>If the value of&nbsp;<code>var<\/code>&nbsp;is 99, then the expression becomes<br>clamp(10, 0, 20), takes 10.<\/li><li>If the value of&nbsp;<code>var<\/code>&nbsp;is 100, then it becomes<br>clamp(10, 20, 20), takes 20.<\/li><\/ul>\n\n\n\n<p id=\"2617\">Explained with a diagram:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*x27NP_waV-xdNiEmTdRRJg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"5a14\">Likewise, if we wish to:<\/p>\n\n\n\n<ul><li>When the value of&nbsp;<code>var<\/code>&nbsp;is less than 50, the value of the&nbsp;<code>result<\/code>&nbsp;is 5.<\/li><li>When the value of&nbsp;<code>var<\/code>&nbsp;is greater than or equal to 50, the value of the&nbsp;<code>result<\/code>&nbsp;is 15.<\/li><\/ul>\n\n\n\n<p id=\"c0fa\">We just need to write like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let result = clamp(5, (var-49) * 15, 15)<\/pre>\n\n\n\n<p id=\"43c9\">Have you noticed: that this is actually the effect of if-else.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*UdYToy-sJSTlBdDgHioIiA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"921a\">We did it.<\/p>\n\n\n\n<h1 id=\"5ba4\">Switch Colors in CSS<\/h1>\n\n\n\n<p id=\"cde9\">Back to the original interview question.<\/p>\n\n\n\n<p id=\"0376\">In order for us to use CSS for variable calculations later, we need to put the value in a CSS variable, so the HTML can be written like this:<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/medium.com\/media\/5e5fb09aac3554e97e8c1a31a20f065a\n<\/div><\/figure>\n\n\n\n<p id=\"b6c1\">If we don\u2019t need to consider HTML semantics or SEO factors, both \u201cnumbers\u201d and \u201creads\u201d here can be generated by pseudo-elements:<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/medium.com\/media\/874343221f569ced200fc34b69a05a65\n<\/div><\/figure>\n\n\n\n<p id=\"3cdf\">If you are not familiar with&nbsp;<code>content<\/code>&nbsp;and&nbsp;<code>counter-reset<\/code>&nbsp;, you can check MDN documentation.<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/content\" rel=\"noreferrer noopener\" target=\"_blank\">content<\/a><\/li><li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/counter-reset\" rel=\"noreferrer noopener\" target=\"_blank\">counter-reset<\/a><\/li><\/ul>\n\n\n\n<p id=\"7f94\">Now, here is an online demo:<\/p>\n\n\n\n<p>https:\/\/cdn.embedly.com\/widgets\/media.html?src=https%3A%2F%2Fcodepen.io%2Fbytefishmedium%2Fembed%2Fpreview%2FVwQrGEb%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DVwQrGEb&#038;display_name=CodePen&#038;url=https%3A%2F%2Fcodepen.io%2Fbytefishmedium%2Fpen%2FVwQrGEb%3Feditors%3D1100&#038;image=https%3A%2F%2Fshots.codepen.io%2Fbytefishmedium%2Fpen%2FVwQrGEb-512.jpg%3Fversion%3D1653533650&#038;key=a19fcc184b9711e1b4764040d3dc5c07&#038;type=text%2Fhtml&#038;schema=codepen<\/p>\n\n\n\n<p id=\"17eb\">The brown color is&nbsp;<code>#aa540e<\/code>, which is represented by HSL color as&nbsp;<code>hsl(27, 50%, 36%)<\/code>, as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*kAtD2lKPlZijUMwIEKnrLg.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*UT3RmBx5T9rq3Ir72M4AhA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"e503\">Its saturation controls how vivid the color is. The higher the saturation, the more vivid the color, and the lower the saturation, the duller the color. When the saturation is reduced to 0, it becomes a complete gray, as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*-txhGQ3PfLoEzRmTVtaDOQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"4f5a\">To switch color between gray and brown, that is, switch between&nbsp;<code>hsl(27, 85%, 36%)<\/code>&nbsp;and&nbsp;<code>hsl(27, 85%, 36%)<\/code>&nbsp;.<\/p>\n\n\n\n<p id=\"c283\">That is:<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/medium.com\/media\/588b463466a4b069a6f40c5809a41d5f\n<\/div><\/figure>\n\n\n\n<p id=\"0180\">Ok, this is the final demo:<\/p>\n\n\n\n<p>https:\/\/cdn.embedly.com\/widgets\/media.html?src=https%3A%2F%2Fcodepen.io%2Fbytefishmedium%2Fembed%2Fpreview%2FWNMXabm%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DWNMXabm&#038;display_name=CodePen&#038;url=https%3A%2F%2Fcodepen.io%2Fbytefishmedium%2Fpen%2FWNMXabm&#038;image=https%3A%2F%2Fshots.codepen.io%2Fbytefishmedium%2Fpen%2FWNMXabm-512.jpg%3Fversion%3D1653535390&#038;key=a19fcc184b9711e1b4764040d3dc5c07&#038;type=text%2Fhtml&#038;schema=codepen<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*269m8U4FgDdmn3-aGaW_Yg.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h1 id=\"95e7\">Conclusion<\/h1>\n\n\n\n<p id=\"459f\">We implement the if-else effect in CSS through the&nbsp;<code>clamp<\/code>&nbsp;function, and finally, let the color switch according to the variable&#8217;s value.<\/p>\n\n\n\n<p id=\"d010\">Isn\u2019t it fun?<\/p>\n\n\n\n<p id=\"288f\">In fact, there is another part of the original interview question, which is simply: let the color switch between multiple values. Just using&nbsp;<code>if-else<\/code>&nbsp;is not enough for this requirement. If you are interested in this topic, you can please follow me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my friends recently came across a funny CSS interview question during an interview. When I first saw the question, I thought it was a usual question. However, after careful research, I discovered the interesting part of this question.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[132],"tags":[],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2397"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2397"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2397\/revisions"}],"predecessor-version":[{"id":2398,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2397\/revisions\/2398"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}