~uglyduck/uglyduck.srht.site

ref: 327101a41134614ca313db49525e7846b6b7efed uglyduck.srht.site/_site/macos-icon-css/index.html -rw-r--r-- 13.2 KiB
327101a4 — Bradley Taunt Slowly running through past articles to ensure proper layout and styling 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html lang="en">

  <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>
    
      Create a Mac App Icon with Pure HTML &amp; CSS &middot; Ugly Duck
    
  </title>
  <meta name="description" content="User interface designer, developer and hardware tinkerer">
  <link rel="shortcut icon" href="/favicon.png">
  <link rel="alternate" type="application/atom+xml" title="Ugly Duck" href="/feed.xml">
  <link rel="stylesheet" href="/style.css">
</head>


  <body>
    <header role="banner">
    <hr>
<nav role="navigation">
    <a href="/">Home</a>
    <span>&middot;</span>
    <a href="/about">About</a>
    <span>&middot;</span>
    <a href="/projects">Projects</a>
    <span>&middot;</span>
    <a href="/articles">Articles</a>
    <span>&middot;</span>
    <a href="/uses">Things I Use</a>
    <span>&middot;</span>
    <a href="/privacy">Privacy</a>
    <span>&middot;</span>
    <a href="https://en.liberapay.com/uglyduck/">Support</a>
    <span>&middot;</span>
    <a href="/feed.xml">RSS</a>
</nav>
<hr>

</header>

    <main id="main" role="main">
      <header>
        <h1>Create a Mac App Icon with Pure HTML &amp; CSS</h1>
      </header>
      <hr>
      <article class="">
        <p><em>Posted on <time datetime="2021-04-13T00:00:00-04:00">April 13, 2021</time></em></p>
        <hr>
        <p><em>I have always been a huge fan of <a href="https://dribbble.com/bg-d">Bogdan’s work on Dribbble</a></em> and was recently inspired to see if I could replicate one of his awesome icon designs with only HTML &amp; CSS. What was the outcome? I think it’s a half-way decent copy - of course the original will always look significantly better.</p>

<p>Don’t care about reading through the tutorial? No problem! You can <a href="#demo">jump right down to the live demo</a></p>

<h2 id="the-comparison">The Comparison</h2>

<p>Let’s take a look at the original Dribbble shot:</p>

<figure>
    <img src="/public/images/e54ac9b2850f786471d7790bec3844a6.webp" alt="Big Sur Icon" />
    <figcaption>The original Dribbble shot (<a href="/public/images/e54ac9b2850f786471d7790bec3844a6.webp">direct link to image</a>)</figcaption>
</figure>

<p>And now let’s see what we will be creating with only HTML &amp; CSS:</p>

<figure>
    <img src="/public/images/big-sur-css.webp" alt="Big Sur Icon" />
    <figcaption>What we are going to create with pure HTML &amp; CSS (<a href="/public/images/big-sur-css.webp">direct link to image</a>)</figcaption>
</figure>

<p>Like I said - far from perfect but still a fun experiment!</p>

<h2 id="the-html">The HTML</h2>

<p>Let’s jump right in and build out the main skeleton of our project:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;div class="white-square"&gt;&lt;/div&gt;
&lt;div class="blue-square"&gt;
    &lt;div class="row"&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
        &lt;div class="item"&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class="play-button"&gt;
    &lt;div class="triangle"&gt;&lt;/div&gt;
&lt;/div&gt;
</code></pre></div></div>

<ul>
  <li>The <code class="language-plaintext highlighter-rouge">white-square</code> element is the white, rounded square in the background</li>
  <li>The <code class="language-plaintext highlighter-rouge">blue-square</code> is the main blue square of the icon</li>
  <li>The <code class="language-plaintext highlighter-rouge">row</code> elements inside the <code class="language-plaintext highlighter-rouge">blue-square</code> will be our individual <em>lines</em> spread across the icon</li>
  <li>The <code class="language-plaintext highlighter-rouge">play-button</code> is obviously - the play button</li>
</ul>

<p>Right now it will look like nothing, but we can change that by adding the most important part…</p>

<h2 id="the-css">The CSS</h2>

<p>Pasting the entire CSS styling here would end up looking a little daunting. Instead, I’m just going to breakdown each individual section to make things more digestible.</p>

<h3 id="defaults--the-white-square">Defaults &amp; the White Square</h3>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>* {
    box-sizing: border-box;
}
:root {
    --row-distance: 42px;
}

.white-square {
    background: white;
    border-radius: 105px;
    box-shadow: inset 0 -5px 8px rgba(0,0,0,0.25), 0 12px 10px rgba(0,0,0,0.15), 0 2px 4px rgba(0,0,0,0.1);
    height: 420px;
    left: 165px;
    position: absolute;
    transform: rotate(-8deg);
    top: 95px;
    width: 420px;
}
</code></pre></div></div>

<p>See that <code class="language-plaintext highlighter-rouge">--row-distance</code> variable? That will come into play a bit later. For now, we want to lay the Blue Square on top of this newly creating White Square:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>.blue-square {
    background: linear-gradient(#04BDFD 0%, #0585E4 100%);
    border-radius: 105px;
    box-shadow: inset 0 5px 8px rgba(255,255,255,0.5), inset 0 -5px 8px rgba(0,0,0,0.32), 0 12px 10px rgba(0,0,0,0.18), 0 2px 4px rgba(0,0,0,0.15);
    height: 420px;
    left: 205px;
    padding: 75px 0 0;
    position: absolute;
    top: 75px;
    width: 420px;
}
</code></pre></div></div>

<h2 id="targeting-the-inner-rows">Targeting the Inner Rows</h2>

<p>So far so good. The next part <em>looks</em> like a lot, but I assure you it’s fairly straightforward. We need to include each row inside the Blue Square like in the original Dribbble shot (7 total). First we start with the parent <code class="language-plaintext highlighter-rouge">row</code> styling:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>.blue-square .row {
    display: flex;
    height: 20px;
    justify-content: space-between;
    padding: 0 55px;
    position: absolute;
    width: 100%;
}
</code></pre></div></div>

<p>Now we style each individual row item via the <code class="language-plaintext highlighter-rouge">nth-of-type</code> attribute:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>.blue-square .row:nth-of-type(2) { margin-top: var(--row-distance); }
.blue-square .row:nth-of-type(2) .item:nth-of-type(odd) {
    width: 85px;
}
.blue-square .row:nth-of-type(2) .item:nth-of-type(even) {
    width: calc(100% - 100px);
}
.blue-square .row:nth-of-type(3) { margin-top: calc(var(--row-distance) * 2); }
.blue-square .row:nth-of-type(3) .item:nth-of-type(odd) {
    width: 115px;
}
.blue-square .row:nth-of-type(3) .item:nth-of-type(even) {
    width: calc(100% - 130px);
}
.blue-square .row:nth-of-type(4) { margin-top: calc(var(--row-distance) * 3); }
.blue-square .row:nth-of-type(4) .item:nth-of-type(odd) {
    width: 185px;
}
.blue-square .row:nth-of-type(4) .item:nth-of-type(even) {
    width: calc(100% - 200px);
}
.blue-square .row:nth-of-type(5) { margin-top: calc(var(--row-distance) * 4); width: calc(100% - 115px); }
.blue-square .row:nth-of-type(5) .item:nth-of-type(odd) {
    width: 105px;
}
.blue-square .row:nth-of-type(5) .item:nth-of-type(even) {
    width: calc(100% - 120px);
}
.blue-square .row:nth-of-type(6) { margin-top: calc(var(--row-distance) * 5); width: calc(100% - 140px); }
.blue-square .row:nth-of-type(6) .item:nth-of-type(odd) {
    width: 65px;
}
.blue-square .row:nth-of-type(6) .item:nth-of-type(even) {
    width: calc(100% - 80px);
}
.blue-square .row:nth-of-type(7) { margin-top: calc(var(--row-distance) * 6); width: calc(100% - 160px); }
.blue-square .row:nth-of-type(7) .item:nth-of-type(odd) {
    width: 40px;
}
.blue-square .row:nth-of-type(7) .item:nth-of-type(even) {
    width: calc(100% - 55px);
}
.blue-square .row .item {
    background: white;
    border-radius: 20px;
    box-shadow: inset 0 -2px 4px rgba(0,0,0,0.06), inset 0 2px 4px rgba(255,255,255,0.1), 0 4px 6px rgba(0,0,0,0.05);
    width: 40px;
}
.blue-square .row .item:nth-of-type(even) {
    background: #3FC0F5;
    width: calc(100% - 55px);
}
</code></pre></div></div>

<p>Take a few moments to read everything over - it will help you better understand what’s going on. Basically, we are adding two inner elements to each row element. We calculate the <code class="language-plaintext highlighter-rouge">margin-top</code> distance by using that <code class="language-plaintext highlighter-rouge">--row-distance</code> variable I mentioned earlier. The inner elements are then styled based on their placement inside the row (<code class="language-plaintext highlighter-rouge">nth-of-type</code>).</p>

<h3 id="the-play-button">The Play Button</h3>

<p>Now we finish things off with a much simpler element to style:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>.play-button {
    backdrop-filter: blur(6px);
    border-radius: 9999px;
    box-shadow: inset 0 4px 0 rgba(255,255,255,0.3), inset 0 20px 15px rgba(255,255,255,0.6), 0 8px 12px rgba(0,0,0,0.1), 0 4px 6px rgba(0,0,0,0.05);
    height: 220px;
    overflow: hidden;
    position: absolute;
    right: 140px;
    top: 320px;
    width: 220px;
}
.play-button::before {
    background: rgba(255,255,255,0.9);
    border-radius: 9999px;
    content:'';
    filter: blur(40px);
    height: 150%;
    left: -25%;
    opacity: 0.8;
    position: absolute;
    top: -25%;
    width: 150%;
}

.triangle {
    position: absolute;
    left: calc(50% - 2em);
    background-color: #315074;
    top: calc(50% - 2.1em);
    text-align: left;
}
.triangle:before,
.triangle:after {
    content: '';
    position: absolute;
    background-color: inherit;
}
.triangle,
.triangle:before,
.triangle:after {
    width:  3.2em;
    height: 3.2em;
    border-top-right-radius: 30%;
}

.triangle {
    transform: rotate(-90deg) skewX(-30deg) scale(1,.866);
}
.triangle:before {
    transform: rotate(-135deg) skewX(-45deg) scale(1.414,.707) translate(0,-50%);
}
.triangle:after {
    transform: rotate(135deg) skewY(-45deg) scale(.707,1.414) translate(50%);
}
</code></pre></div></div>

<p><del>We don’t have the ability to <em>perfectly</em> mimic the frosted glass effect like in the original (without the use of SVG background etc), but what we get is close enough.</del> Thanks to <a href="https://m.nintendojo.fr/@meduz/106059826445460903">meduz</a> for pointing out the <code class="language-plaintext highlighter-rouge">backdrop-filter</code> property. This allows for a frosted glass look on Chromium &amp; Safari (although sadly not on Firefox). The <code class="language-plaintext highlighter-rouge">triangle</code> element could also be improved by using an embedded <code class="language-plaintext highlighter-rouge">SVG</code> but I was determined to use only CSS for this experiment :P</p>

<p>That’s really all there is to it! You can see the embedded CodePen example below or <a href="https://codepen.io/bradleytaunt/pen/bGgBRaV">check it out directly here →</a></p>

<hr />

<h3 id="special-thanks">Special Thanks</h3>

<p>Thanks to Bogdan for letting me butcher the original Dribbble shot :D</p>

<ul>
  <li><a href="http://bg-d.net/">bg-d.net</a></li>
  <li><a href="https://dribbble.com/bg-d">Bogdan on Dribbble</a></li>
</ul>

<hr />

<h2 id="demo">Live Demo (CodePen)</h2>

<p>Feel free to explore the <a href="https://codepen.io/bradleytaunt/pen/bGgBRaV&quot;">live CodePen demo here</a>.</p>

      </article>
      <br>
      <footer role="contentinfo" id="menu">
    <hr>
<nav role="navigation">
    <a href="/">Home</a>
    <span>&middot;</span>
    <a href="/about">About</a>
    <span>&middot;</span>
    <a href="/projects">Projects</a>
    <span>&middot;</span>
    <a href="/articles">Articles</a>
    <span>&middot;</span>
    <a href="/uses">Things I Use</a>
    <span>&middot;</span>
    <a href="/privacy">Privacy</a>
    <span>&middot;</span>
    <a href="https://en.liberapay.com/uglyduck/">Support</a>
    <span>&middot;</span>
    <a href="/feed.xml">RSS</a>
</nav>
<hr>

    <small>
        Keep this website ad-free by supporting via <a href="https://en.liberapay.com/uglyduck/">Liberapay</a>.<br>
        Built with <a href="https://jekyllrb.com/">Jekyll</a> & hosted on <a href="https://netlify.com">Netlify</a>.<br>
        Maintained with &hearts; for the web.<br>
        Honorary member of <a href="https://1mb.club">1MB Club</a> &amp; <a href="https://xhtml.club">XHTML Club</a>
    </small>
    <hr>
    <small>The most recent revision of this page was made on <b>November 11, 2021</b></small>
</footer>

    </main>
    
  </body>
</html>