forked from geoffreycrofte/flexbox.ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
149 lines (123 loc) · 3.68 KB
/
index.html
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
<div class="container">
<p>The most common case for this kind of pattern is when you need to build buttons for social network sharing.</p>
<h2>Defining our needs</h2>
<p>As usual, I like to define some goals so that the CSS solution does make sense to you. What I need is:</p>
<ul>
<li>Vertical aligned buttons</li>
<li>Buttons with vertical aligned icon and text</li>
<li>If I don't have enough space, the overflowing buttons go to a new line.</li>
<li>Buttons cover the entire space available by distributing the available space.</li>
<li>Keep the HTML code as simple and accessible as possible, as usual :p</li>
</ul>
<p>The last part is optional.</p>
<h2>Let's code this</h2>
<h3>Our HTML basics</h3>
<pre class="code"><code class="html"><ul class="social-buttons">
<li>
<a href="https://twitter.com/">
<i class="icon-twitter" role="presentation"></i>
Twitter
</a>
</li>
<li>
<a href="https://www.facebook.com/">
<i class="icon-facebook" role="presentation"></i>
Facebook
</a>
</li>
<li>
<a href="https://instagram.com">
<i class="icon-instagram" role="presentation"></i>
Instagram
</a>
</li>
<li>
<a href="http://weibo.com/">
<i class="icon-weibo" role="presentation"></i>
Weibo
</a>
</li>
<li>
<a href="https://www.linkedin.com/">
<i class="icon-linkedin" role="presentation"></i>
Linkedin
</a>
</li>
</ul></code></pre>
<p>I used <code>role="presentation"</code> to avoid assistive technology reading the decorative icon. Read more about that thanks to the excellent article by Scott O'Hara "<a href="https://www.scottohara.me/blog/2018/05/05/hidden-vs-none.html">Know your ARIA: 'hidden' VS 'none'</a>".</p>
<h3>CSS to flex the things</h3>
<p>Ok now we are using Flexbox to make the magic happen.</p>
<pre class="code"><code class="css">/**
* The container and the item are both
* into flex layout. To align items
* to each other.
*/
.social-buttons,
.social-buttons li {
display: flex;
padding: 0;
margin: 0;
}
/**
* Force to occupy the space available
* and allow item being on several lines
*/
.social-buttons {
width: 100%;
list-style: none;
flex-wrap: wrap;
}
/**
* Items tend to occupy 25% of
* the available width but are
* allow to grow.
*/
.social-buttons li {
flex-basis: 25%;
flex-shrink: 0;
flex-grow: 1;
}
/**
* The anchor is also in Flex
* to align icon and text and
* center their content.
*/
.social-buttons a {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
padding: .5em 1em;
font-weight: 500;
text-decoration: none;
color: #FFF;
}
/**
* Some decorations for the
* next lines.
*/
.social-buttons i {
margin-right: .5em;
}
.social-buttons [href*="twitter.com"] {
background: #1da1f2;
}
.social-buttons [href*="facebook.com"] {
background: #3b5998;
}
.social-buttons [href*="instagram.com"] {
background: #c13584;
}
.social-buttons [href*="weibo.com"] {
background: #e6162d;
}
.social-buttons [href*="linkedin.com"] {
background: #0077b5;
}</code></pre>
<p>The magic parts are here the <code>wrap</code> option and the fact that <code>.social-buttons li</code> are allow to <code>grow</code> even if their width is set thanks to <code>flex-basis: 25%</code> which is not a strong constraint</p>
<p>Here is an alternative code with margin:</p>
<pre class="code"><code class="css">.social-buttons li {
margin: 2px;
}</code></pre>
<p>Have fun!</p>
</div><!-- .container -->