<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmad Khalid</title>
    <description>The latest articles on DEV Community by Ahmad Khalid (@ahmadkdev).</description>
    <link>https://dev.to/ahmadkdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1252260%2F8c33aa9b-96f3-4c9a-862d-506012d74ede.jpeg</url>
      <title>DEV Community: Ahmad Khalid</title>
      <link>https://dev.to/ahmadkdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmadkdev"/>
    <language>en</language>
    <item>
      <title>Semicircles in CSS are more trickier than you think!</title>
      <dc:creator>Ahmad Khalid</dc:creator>
      <pubDate>Fri, 19 Jan 2024 18:34:24 +0000</pubDate>
      <link>https://dev.to/ahmadkdev/semicircles-in-css-are-more-trickier-than-you-think-2phe</link>
      <guid>https://dev.to/ahmadkdev/semicircles-in-css-are-more-trickier-than-you-think-2phe</guid>
      <description>&lt;p&gt;Semicircles or half circles are common in web design. And while they might seem simple to implement, they can be tricky sometimes. So I'll share my notes about them.&lt;/p&gt;

&lt;p&gt;The first thing to know is that there's is no direct way in CSS to implement a semicircle e.x. we don't have something like &lt;code&gt;top-left-radius: quarter-circle;&lt;/code&gt; or &lt;code&gt;right-side-radius: semi-circle;&lt;/code&gt; so we need to figure things out ourselves to set the proper &lt;code&gt;border-radius&lt;/code&gt; value and get the shape we desire.&lt;/p&gt;

&lt;p&gt;And this can be simple or tricky depending on the box's dimensions i.e. width and height: whether they're equal or not and whether they're fixed or dynamic.&lt;/p&gt;

&lt;p&gt;Let's explore three different boxes assuming we want a semicircle on the right side and a radius of &lt;code&gt;1rem&lt;/code&gt; on the left side:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. A square box
&lt;/h2&gt;

&lt;p&gt;The most basic box to deal with is a square box.&lt;br&gt;
Our magic value here is 50%.&lt;br&gt;
We give it to one corner and get a quarter circle.&lt;br&gt;
We give it to two adjacent corners and get a half-circle.&lt;br&gt;
We give it to the four corner and get a full circle.&lt;br&gt;
and so on...&lt;/p&gt;

&lt;p&gt;We can also mix it with other values and things would work perfectly as in this example:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/ahmadkdev/embed/dyrNBpG?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  2. A rectangle box with &lt;em&gt;fixed&lt;/em&gt; dimensions
&lt;/h2&gt;

&lt;p&gt;With rectangles things start to get more tricky.&lt;br&gt;
Because width and height are not equal anymore, if we used 50% we'll get ourselves totally different result:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/ahmadkdev/embed/jOJmPow?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;To get our desired result we need to use the value of the smallest dimension divided by 2&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/ahmadkdev/embed/KKEmdPX?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Well, we needed to do some math but it's still simple, that's because we &lt;em&gt;&lt;strong&gt;know&lt;/strong&gt;&lt;/em&gt; our box's dimensions in advance.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A rectangle with &lt;em&gt;dynamic&lt;/em&gt; dimensions
&lt;/h2&gt;

&lt;p&gt;Things get really complicated and tricky when we &lt;em&gt;&lt;strong&gt;don't&lt;/strong&gt;&lt;/em&gt; know the dimensions.&lt;/p&gt;

&lt;p&gt;This is where the 999px trick kicks in, you might have seen it somewhere or even used it yourself. Let's see how it behaves then we'll explore how it works:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/ahmadkdev/embed/zYbwqQL?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you see in the snippet it's working and even when the box get resized it still gives us our desired perfect semicircle! You might also have noticed that even though the (999px) radii works well, the other radii (16px) is broken!&lt;/p&gt;

&lt;p&gt;To understand why this happened we need to ask how the 999px trick actually works?&lt;/p&gt;

&lt;p&gt;Well, It simply triggers the &lt;a href="https://www.w3.org/TR/css-backgrounds-3/#corner-overlap"&gt;Overlapping Curves&lt;/a&gt; rule which states:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Corner curves must not overlap: When the sum of any two adjacent border radii exceeds the size of the border box, UAs must proportionally reduce the used values of all border radii until none of them overlap. The algorithm for reducing radii is as follows:&lt;br&gt;
Let &lt;em&gt;f&lt;/em&gt; = min(&lt;em&gt;Li&lt;/em&gt;/&lt;em&gt;Si&lt;/em&gt;), where &lt;em&gt;i&lt;/em&gt; ∈ {top, right, bottom, left}, &lt;em&gt;Si&lt;/em&gt; is the sum of the two corresponding radii of the corners on side &lt;em&gt;i&lt;/em&gt;, and &lt;em&gt;Ltop&lt;/em&gt; = &lt;em&gt;Lbottom&lt;/em&gt; = the width of the box, and &lt;em&gt;Lleft&lt;/em&gt; = &lt;em&gt;Lright&lt;/em&gt; = the height of the box. If &lt;em&gt;f&lt;/em&gt; &amp;lt; 1, then all corner radii are reduced by multiplying them by &lt;em&gt;f&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To understand this formula assume a box with 600px width and 350px height and we give it &lt;code&gt;border-radius: 16px 999px 999px 16px;&lt;/code&gt; the formula will go as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Ltop&lt;/em&gt; / &lt;em&gt;Stop&lt;/em&gt; = 600 / (16 + 999) = 0.5911330049261&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Lbottom&lt;/em&gt; / &lt;em&gt;Sbottom&lt;/em&gt; = 600 / (16 + 999) = 0.5911330049261&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Lleft&lt;/em&gt; / &lt;em&gt;Sleft&lt;/em&gt; = 350 / (16 + 16) = 18.75&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Lright&lt;/em&gt; / &lt;em&gt;Sright&lt;/em&gt; = 350 / (999 + 999) = 0.1751751751751&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;f = 0.1751751751751 (min result)&lt;/p&gt;

&lt;p&gt;It's &amp;lt; 1 so &lt;em&gt;&lt;strong&gt;all&lt;/strong&gt;&lt;/em&gt; corner radii will be reduced by multiplying them by f&lt;br&gt;
16px becomes 2.8028028028028px&lt;br&gt;
999px becomes 175px&lt;/p&gt;

&lt;p&gt;Now we know why it works and why it brokes the other radii as well.&lt;/p&gt;

&lt;p&gt;We need a better solution then. Well, remember what was our real problem? &lt;strong&gt;We do &lt;em&gt;not&lt;/em&gt; know the dimensions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So how can we &lt;em&gt;&lt;strong&gt;know&lt;/strong&gt;&lt;/em&gt;? Using CSS, we simply can't. We can use javascript but this is beyond our topic.&lt;/p&gt;
&lt;h3&gt;
  
  
  My solution using pure CSS
&lt;/h3&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/ahmadkdev/embed/gOELjbG?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I created an element &lt;code&gt;div.rad&lt;/code&gt; that takes its parent's full size. It's a &lt;code&gt;flex&lt;/code&gt; container so I can easily size its pseudo-elements. And it's positioned absolutely with &lt;code&gt;z-index: -1&lt;/code&gt; so our main box can overlap it.&lt;br&gt;
I used the &lt;code&gt;::after&lt;/code&gt; pseudo-element for the semicircle radius giving it a 100% height and &lt;code&gt;aspect-ratio: 1&lt;/code&gt; so it'll always be square.&lt;br&gt;
Then I use the &lt;code&gt;::before&lt;/code&gt; pseudo-element for the &lt;code&gt;1rem&lt;/code&gt; radius giving it the remaining size but not less than &lt;code&gt;1rem&lt;/code&gt; so it won't disappear at some point and we lose the &lt;code&gt;1rem&lt;/code&gt; radius.&lt;/p&gt;

&lt;p&gt;I was able to achieve both the &lt;code&gt;1rem&lt;/code&gt; radius and the semicircle radius in pure CSS.&lt;br&gt;
Of course, this solution isn't perfect and I'd be happy to get your thoughts on my solution and the topic in general.&lt;br&gt;
Thanks for reading and I hope it deserved your time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Useful Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/q/29966499"&gt;Border-radius in percentage (%) and pixels (px) or em&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/css-backgrounds-3/#corner-overlap"&gt;Overlapping Curves&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>frontend</category>
      <category>learning</category>
    </item>
    <item>
      <title>I'm not guilty it's firefox!</title>
      <dc:creator>Ahmad Khalid</dc:creator>
      <pubDate>Fri, 12 Jan 2024 08:31:41 +0000</pubDate>
      <link>https://dev.to/ahmadkdev/to-w3c-validator-im-not-guilty-its-firefox-2dd7</link>
      <guid>https://dev.to/ahmadkdev/to-w3c-validator-im-not-guilty-its-firefox-2dd7</guid>
      <description>&lt;p&gt;I'm working on a project which has radio buttons to select between Metric and Imperial measurement systems and show inputs based on selection and the default checked button is Metric. Then I came across this weird behavior of firefox not returning to the default checked button on page reload!&lt;br&gt;
I found out it's a known feature/bug on firefox as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio"&gt;MDN&lt;/a&gt; states:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unlike other browsers, Firefox by default &lt;a href="https://stackoverflow.com/questions/5985839/bug-with-firefox-disabled-attribute-of-input-not-resetting-when-refreshing"&gt;persists the dynamic checked state&lt;/a&gt; of an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; across page loads. Use the autocomplete attribute to control this feature.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And if I got it right the only solution is to use &lt;code&gt;autocomplete="off"&lt;/code&gt; attribute either on the &lt;code&gt;form&lt;/code&gt; or on the radio inputs themselves. I chose the latter as I don't want to disable the autocomplete feature for the whole form!&lt;br&gt;
I was relieved until the &lt;a href="https://validator.w3.org/nu/"&gt;W3C Validator&lt;/a&gt; yelled at me:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Error: Attribute autocomplete is only allowed when the input type is color, date, datetime-local, email, hidden, month, number, password, range, search, tel, text, time, url, or week.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I was like&lt;br&gt;
&lt;a href="https://i.giphy.com/media/9Vx51uYaQasyCSkNlx/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/9Vx51uYaQasyCSkNlx/giphy.gif" alt="I'm not a criminal" width="480" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And after a lot of searching and thinking I ended up checking on every page-load is imperial radio checked? and if so uncheck and recheck the default one!&lt;/p&gt;

&lt;p&gt;Thank you firefox what a great feature!&lt;/p&gt;

&lt;p&gt;cover image source: &lt;a href="https://thequotes.me/2021/11/05/its-a-bug/"&gt;The quotes - It’s a bug&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>w3c</category>
      <category>frontend</category>
      <category>firefox</category>
    </item>
    <item>
      <title>CSS: Selector vs declaration duplication dilemma</title>
      <dc:creator>Ahmad Khalid</dc:creator>
      <pubDate>Tue, 09 Jan 2024 18:50:14 +0000</pubDate>
      <link>https://dev.to/ahmadkdev/css-selector-vs-declaration-duplication-dilemma-2apg</link>
      <guid>https://dev.to/ahmadkdev/css-selector-vs-declaration-duplication-dilemma-2apg</guid>
      <description>&lt;p&gt;If you used CSS analyzer tools e.g. &lt;a href="https://www.projectwallace.com/analyze-css"&gt;CSS Analyzer by Project Wallace&lt;/a&gt; you may have noticed the relation between selector duplication and declaration duplication.&lt;/p&gt;

&lt;p&gt;Let's say we want to apply a &lt;code&gt;padding&lt;/code&gt; of &lt;code&gt;1rem&lt;/code&gt; to three different selectors then we have multiple choices:&lt;/p&gt;

&lt;p&gt;Choice one: &lt;strong&gt;Repeat the declaration for all selectors&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.selector1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result: Higher declaration duplication score and higher declarations per ruleset.&lt;/p&gt;

&lt;p&gt;Choice two: &lt;strong&gt;Combine selectors that share the same declaration&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.selector1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nc"&gt;.selector2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nc"&gt;.selector3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result: lower declaration duplication but higher selector duplication.&lt;/p&gt;

&lt;p&gt;Choice three: &lt;strong&gt;Using of utility classes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.padding-1rem&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.selector3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;/* other declarations */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result: lower score of both selector and declaration duplications but it violates the Separation of concerns concept and we end up with markup like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"padding-1rem margin-0 text-bold text-center etc"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;...&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is not really good for maintainability.&lt;br&gt;
So, as an experienced developer how you deal with this dilemma?&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>css</category>
      <category>codequality</category>
    </item>
    <item>
      <title>I need feedback on my first website</title>
      <dc:creator>Ahmad Khalid</dc:creator>
      <pubDate>Tue, 09 Jan 2024 08:04:09 +0000</pubDate>
      <link>https://dev.to/ahmadkdev/i-need-feedback-on-my-first-website-1fl7</link>
      <guid>https://dev.to/ahmadkdev/i-need-feedback-on-my-first-website-1fl7</guid>
      <description>&lt;p&gt;Hello everyone. I hope you having a good day.&lt;br&gt;
I'm Ahmad - 32 yo - from Egypt. I'm currently learning frontend web development and I've completed my first project: &lt;strong&gt;Single-page developer portfolio&lt;/strong&gt; challenge on Frontend Mentor.&lt;br&gt;
I'd be happy to get your feedback on it.&lt;br&gt;
Thanks in advance.&lt;br&gt;
Code: &lt;a href="https://github.com/ahmadkdev/developer-portfolio"&gt;https://github.com/ahmadkdev/developer-portfolio&lt;/a&gt;&lt;br&gt;
Live demo: &lt;a href="https://devportfoliosp.netlify.app/"&gt;https://devportfoliosp.netlify.app/&lt;/a&gt;&lt;br&gt;
My solution page on Frontend Mentor: &lt;a href="https://www.frontendmentor.io/solutions/responsive-developer-portfolio-website-using-modern-css-techniques-iFqDLAczRk"&gt;https://www.frontendmentor.io/solutions/responsive-developer-portfolio-website-using-modern-css-techniques-iFqDLAczRk&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>css</category>
      <category>html</category>
    </item>
  </channel>
</rss>
