CODING HELL


Transform

In this module, we'll be taking a look at all the things we can do with the transform property. Note that you can use negative values for this property.


> Translate

First is translate, which is another way of moving an element along the x and y-axes. Some argue this is better than position, but I digress. Let's append transform: translate(x,y); into our code.

[div=
width: 100px;
height: 100px;
background: red;
transform: translate(20px,20px);][/div]

Output:



One thing you should remember about the values going forward is that you shouldn't put a space where there isn't one. Otherwise, the code will not be parsed correctly.


> Scale

Next up is scale, which allows us to stretch, invert, and flip our elements.

To stretch our element, let's scale it along the y-axis with a value of 1.5. And yes, we can simply use numbers as our values for this one. Just remember that the default value is 1.

Our syntax will look like this: transform: scaley();

[div=
width: 100px;
height: 100px;
background: red;
color: white;
transform: scaley(1.5);]Text[/div]

Output:

Text


Now let's try to invert our element along the y-axis. To achieve this, we'll be using a negative value.
[div=
width: 100px;
height: 100px;
background: red;
color: white;
transform: scaley(-1);]Test[/div]

Output:

Text


Now let's try to flip our element along the x-axis. This time our syntax will be transform: scalex(); Once again, we'll be using a negative value.

[div=
width: 100px;
height: 100px;
background: red;
color: white;
transform: scalex(-1);]Text[/div]

Output:

Text

Should we want to manipulate our element along the x and y-axes at the same time, we can simply use the shorthand transform: scale();

For example:

[div=
width: 100px;
height: 100px;
background: red;
color: white;
transform: scale(-1,-1);]Text[/div]

Output:

Text



> Rotate

We can also rotate our element using transform: rotate(deg); This time, we'll be using degrees as our value.

In the following example, I'll be rotating my element 10 degrees.

[div=
width: 100px;
height: 100px;
background: red;
transform: rotate(10deg);][/div]

Output:




> Skew

Finally, we have skew: x,y; which will distort our element. Once again, we'll be using degrees to define our values.

[div=
width: 100px;
height: 100px;
background: red;
transform: skew(5deg,5deg);]Test[/div]

Output:

Test



# Featured Properties

transform: translate(x,y);
transform: scalex();
transform: scaley();
transform: scale(x,y);
transform: rotate(deg);
transform: skew(xdeg,ydeg);
 
Last edited:

Animation

I had to go digging for this in the site's code, but below are the animations we can use in Iwaku. I haven't checked if there's more, but I'll be sure to update this thread if I find any.








To use these animations, we must append the following syntax to our target element:

animation: name duration timing-function delay iteration-count direction;

  • name tells the browser which animation to use for the target element
  • duration, which can be specified in 's' or seconds, defines the duration of the animation
  • timing-function specifies the speed of the animation, whether it be the same speed all throughout (linear), a slow start (ease in), a slow end (ease out), a slow start and a slow end (ease-in-out), slow-fast-slow (ease), etc.
  • delay, which can be specified in 's' or seconds, defines how long of a wait there should be before the animation begins
  • iteration-count, which can be specified in numbers (1,2,3..) or infinite, defines how many times the animation should be played
  • direction specifies whether the animation is played normally (normal), in reverse (reverse), alternating between normal and reverse (alternate), or alternating between reverse and normal (alternate-reverse)

Let's use the fb_mpn_fade_out animation for our example. Specify the name of the animation you'd like to use and the duration you'd like that animation to play. I'm going to input 2s (2 seconds) for mine:

[div=
width: 100px;
height: 100px;
background: red;
animation: fb_mpn_fade_out 2s;][/div]

Next, let's add the timing-function. I'm going to be using linear. This is default, so nothing too fancy.

[div=
width: 100px;
height: 100px;
background: red;
animation: fb_mpn_fade_out 2s linear;][/div]

Next, we can specify a delay for our animation. I'm going to set mine to 0s since I don't want a delay.

[div=
width: 100px;
height: 100px;
background: red;
animation: fb_mpn_fade_out 2s linear 0s;][/div]

Next, we can specify our iteration-count, or how many times we'd like our animation to play. I'm going to set it to infinite, but it is possible to set the value to a specific number.

[div=
width: 100px;
height: 100px;
background: red;
animation: fb_mpn_fade_out 2s linear 0s infinite;][/div]

Finally, we can specify the direction of our element. I'm going to say reverse for mine.

[div=
width: 100px;
height: 100px;
background: red;
animation: fb_mpn_fade_out 2s linear 0s infinite reverse;][/div]

Output:




# Featured Properties

animation: name duration timing-function delay iteration-count direction;
 
Last edited:

FontAwesome Icons

So, let's say you want to add some icons to your code, just like this:



First, choose an icon you'd like from this list of free icons. Enclose it in [fa][/fa]:

[fa]fa-comment[/fa]


Output:


You can resize the icon using font-size: value;

[div=
font-size: 2.5em;][fa]fa-comment[/fa][/div]

Output:


You can also change the color. It goes like this:

[div=
font-size: 2.5em;
color: red;][fa]fa-comment[/fa][/div]

Output:


That's it!


# Featured Properties

[fa][/fa]
font-size: value;
 
Last edited:
  • Love
Reactions: rissa

Columns


As of writing, I don't think I've ever seen anyone discuss columns, which is a shame because it's pretty powerful. With this, you can pretty much achieve a responsive layout without having to use flexbox at all. What's more, it's fairly easy to do!

First, let me show you what this property does in its basic form. Let's say you have a huge amount of text that you want to separate into three columns. All you have to do is enclose your text in [div][/div] tags, and define columns: 3 in your opening div, like so:

[div=
columns: 3;
]Lorem[/div]


That's not quite enough to make it responsive though. So why don't we specify a minimum width? In this example, I'll be using a min-width of 100px. We can simply input our minimum width in the same declaration as our column number:

[div=
columns: 3 100px;
]Lorem[/div]


Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non cursus metus. In quis est vitae turpis interdum tempus. Suspendisse aliquam commodo purus sit amet maximus. Morbi lobortis lectus quis consequat elementum. Vivamus ultricies nibh vel lorem malesuada condimentum. Sed id ultricies erat, et ornare lorem. Suspendisse at felis non metus elementum feugiat at eu leo. Duis lacus mauris, pretium sit amet nisi malesuada, placerat volutpat sapien. Vivamus a tempor purus. Pellentesque vehicula faucibus tortor, sit amet placerat dolor tempor eget. Vivamus in rutrum dolor, ultricies dignissim enim. Maecenas auctor mattis auctor. Etiam tempus tempor enim, non euismod neque convallis eget. Integer sagittis erat urna, vitae cursus lectus accumsan non. Nam ultricies lectus turpis, quis eleifend nisi gravida at.

Try making your browser smaller. Watch these three columns merge once they hit their minimum width.

But wait, we're not done yet! You can also style these columns. Say you want to add a border between them. This can be done by appending the following syntax:

column-rule: width style color;

And yes, these are the same styling values that you would use for regular borders. Check the Cheat Sheet for more styling options (Box > Border).

Now, I'm thinking I want a red border between my columns. I'm going to append a width of 4 pixels, but you can use other lengths in your code.

[div=
columns: 3 100px;
column-rule: 4px double red;
]Lorem[/div]

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non cursus metus. In quis est vitae turpis interdum tempus. Suspendisse aliquam commodo purus sit amet maximus. Morbi lobortis lectus quis consequat elementum. Vivamus ultricies nibh vel lorem malesuada condimentum. Sed id ultricies erat, et ornare lorem. Suspendisse at felis non metus elementum feugiat at eu leo. Duis lacus mauris, pretium sit amet nisi malesuada, placerat volutpat sapien. Vivamus a tempor purus. Pellentesque vehicula faucibus tortor, sit amet placerat dolor tempor eget. Vivamus in rutrum dolor, ultricies dignissim enim. Maecenas auctor mattis auctor. Etiam tempus tempor enim, non euismod neque convallis eget. Integer sagittis erat urna, vitae cursus lectus accumsan non. Nam ultricies lectus turpis, quis eleifend nisi gravida at.

I don't like how close they are to each other, but luckily I can easily solve this issue with column-gap: value. We can use lengths as the value. In my example, I'm going to go with 40 pixels so you can see the distinction.

[div=
columns: 3 100px;
column-rule: 4px double red;
column-gap: 40px;
]Lorem[/div]

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non cursus metus. In quis est vitae turpis interdum tempus. Suspendisse aliquam commodo purus sit amet maximus. Morbi lobortis lectus quis consequat elementum. Vivamus ultricies nibh vel lorem malesuada condimentum. Sed id ultricies erat, et ornare lorem. Suspendisse at felis non metus elementum feugiat at eu leo. Duis lacus mauris, pretium sit amet nisi malesuada, placerat volutpat sapien. Vivamus a tempor purus. Pellentesque vehicula faucibus tortor, sit amet placerat dolor tempor eget. Vivamus in rutrum dolor, ultricies dignissim enim. Maecenas auctor mattis auctor. Etiam tempus tempor enim, non euismod neque convallis eget. Integer sagittis erat urna, vitae cursus lectus accumsan non. Nam ultricies lectus turpis, quis eleifend nisi gravida at.

By now you may be thinking, this looks a little too plain for your tastes. It's nothing like flexbox, you say. But hold your horses, because it's about to get real. Do note that you may have to read the other guides first before proceeding (namely Color, Box Model, and Background Images).

Alright, with that out of the way: let's say you want to make two adjacent boxes, with the first box holding your character picture, and the other box holding your content. Let's add our columns:

[div=
columns: 250px 2;
][/div]

I'm added a min-width of 250px as I find that it's best for mobile responsiveness.

Next, let's input the actual items that will be going into our two columns: namely, our character image box and content box. We're going to need to specify a height for each of them, and we'll have to nest them inside our existing div tags.

[div=
columns: 250px 2;][div=
/* Character Image */
height: 400px;
][/div][div=
/* Content */
height: 400px;
][/div][/div]

Okay, let's input a background for each of our boxes. In the first box, I'll be using the character image as the background, and in the second box, I'll be using a white background.

[div=
columns: 250px 2;][div=
/* Character Image */
height: 400px;
background: url(
https://i.imgur.com/LTnVkvn.png) 50% 50%/cover;][/div][div=
/* Content */
height: 400px;
background: white;
][/div][/div]

Output:


I don't like how it takes up the whole post container, so let's go back to our first div tag, in which we specified the width for our columns. Let's input a width and add margin: auto to center our columns.

[div=
width: 700px;
margin: auto;

columns: 250px 2;][div=
/* Character Image */
height: 400px;
background: url(https://i.imgur.com/LTnVkvn.png) 50% 50%/cover;][/div][div=
/* Content */
height: 400px;
background: white;][/div][/div]

Output:


Now let's add some text and give it a black font color

[div=
width: 700px;
margin: auto;
columns: 250px 2;][div=
/* Character Image */
height: 400px;
background: url(https://i.imgur.com/LTnVkvn.png) 50% 50%/cover;][/div][div=
/* Content */
height: 400px;
background: white;
color: black;
]This is content[/div][/div]

Output:

This is content

Add padding so that our text isn't too close to the edge. Let's also input overflow-y, so we can scroll through our content.

[div=
width: 700px;
margin: auto;
columns: 250px 2;][div=
/* Character Image */
height: 400px;
background: url(https://i.imgur.com/LTnVkvn.png) 50% 50%/cover;][/div][div=
/* Content */
height: 400px;
background: white;
color: black;
padding: 20px;
overflow-y: scroll;
]This is content[/div][/div]

Output:

This is content

And that's it! Try resizing your browser.

As always, you can style your columns differently. Feel free to play around with all these codes.


# Featured Properties

columns: column-count min-width;
column-gap: value;
column-rule: width style color;
 
Last edited:
  • Like
  • Thank You
Reactions: Bubs and Kuno

Flexbox

There are many ways to style a flexbox, but I'll keep this one simple so that it'll be easier to follow.

First, I want to illustrate what flexbox actually does. While it is an alternative to displays, floats, and other layout properties, it is a lot better as it makes our design responsive—that is to say, mobile-friendly. Check out these two examples and see how they respond to your browser as you resize it:

With Inline-Block and %:


With Flexbox:


As you can see, flexbox enables us to arrange items easily, and once our viewport shrinks, the second box moves to the next line. Now, the latter isn't anything new and is something you could easily achieve with columns. Personally though? I prefer flexbox as it allows for better control and styling. Now, if you're going for something simple, columns may be your best bet.

So how do we actually make a flexbox? We're going to need two different components: a container and its items. Let's get started with creating our container.

First, we're going to input display: flex; You can add a width for your container, and margin: auto; to center it. However, width and margin are optional. It depends on what design you're going for.

[div=
width: 600px;
margin: auto;
display: flex;
][/div]

Next, let's nest our items. In this example, I'll be using two items. However, you can use as few or as many as you'd like.

Let's give our first nested item a red background and 100px height so we can actually see what we're working with. We're going to do the same for its sibling, except we're going to give that one a blue background.

[div=
width: 600px;
margin: auto;
display: flex;][div=
background: red;
height: 100px;
][/div][div=
background: blue;
height: 100px;
][/div][/div]

Let's add flex: 1 to our red box and flex: 2 to our blue box.

[div=
width: 600px;
margin: auto;
display: flex;][div=
background: red;
height: 100px;
flex: 1;
][/div][div=
background: blue;
height: 100px;
flex: 2;
][/div][/div]

flex: 2; is basically making our blue box grow 2x faster than our red box, making it larger. If you make this value 3, it will be 3x as fast. And so on and so forth. If you use a value of 1 for both boxes, however, they will be the same size.

Next, we're going to append min-width: 230px to each of our flexboxes. This stands for minimum width.

[div=
width: 600px;
margin: auto;
display: flex;][div=
background: red;
height: 100px;
flex: 1;
min-width: 230px;
][/div][div=
background: blue;
height: 100px;
flex: 2;
min-width: 230px;
][/div][/div]

Output:


Alright. Personally, I don't like how these boxes are sticking too close together, so let's add a gap, shall we? How about a 10px gap? Let's add it to our container.

[div=
width: 600px;
margin: auto;
display: flex;
gap: 10px;
][div=
background: red;
height: 100px;
flex: 1;
min-width: 230px;][/div][div=
background: blue;
height: 100px;
flex: 2;
min-width: 230px;][/div][/div]

Output:


If you try resizing your browser now, you'll notice that the second box doesn't go to the next line. Not yet, anyway. We need one last thing to make this perfectly mobile-friendly, and that's flex-wrap: wrap;

Let's append it to our container once again.

[div=
width: 600px;
margin: auto;
display: flex;
gap: 10px;
flex-wrap: wrap;
][div=
background: red;
height: 100px;
flex: 1;
min-width: 230px;][/div][div=
background: blue;
height: 100px;
flex: 2;
min-width: 230px;][/div][/div]

Output:


flex-wrap is working with our minimum width. As soon as the screen becomes too small to contain our flexboxes at their minimum width, the second box moves to the next line. I find that a 230px-250px of minimum width or each flexbox is best for mobile-viewing.

Now, we also have the option of using flex-wrap: wrap-reverse instead of flex-wrap: wrap. By using this, we are making our red box move to the next line instead.


Try making your browser smaller!

Before we end this section, I want to add that there's actually another way to do this, but it's finicky and very difficult to get right especially if you're a beginner, as it requires the right container value and flex values, and so on and so forth. What I've showed you thus far is the easiest method.

Now, if that's all you wanted to know, you can stop reading here. It should be enough to help you create responsive designs. However, if you'd like to know more about flexboxes and what we can do with them, read on. Just be warned that it may get confusing at times! Flexbox is an advanced topic, so I'd suggest playing around with what I've shown you so far before diving into the gritty details.


> Grow, Shrink, Basis

Earlier, I refrained from dissecting flex: value as there simply wasn't enough space to discuss it in detail. It definitely warrants its own section as there is a lot to say about them, but don't be overwhelmed. They're easy to understand once you get the hang of them. As always, it just takes practice.

The syntax that I used in the previous article is actually a shorthand, flex: grow shrink basis; Once again, I will be using visual aid to demonstrate how it is used. However, do bear in mind this will make a lot more sense if you view it on a desktop, where you can resize your browser to see the effects taking place.

Alright, with that out of the way, let's begin.

flex-basis defines the starting value of our items. It is the base value, the actual width of our flexboxes. You can use lengths, like px, to specify its value.

flex-basis: 100px;
flex-basis: 100px;
flex-basis: 100px;

See that extra space on the right?

flex-grow, makes the element grow according to the container size or window size. It defines how much of the extra space should be distributed among the items. We use numbers to specify its value.

If we set it to 0 (the default value), none of the items will take up the extra space. They will remain at their basis, which is 100px in our example.

So, instead, let's try setting our first item to flex-grow: 1; Watch what happens:

flex-grow: 1;
flex-grow: 0;
flex-grow: 0;

As you can see, it took up all the extra space for itself. Make your browser smaller. Now, slowly drag to make it bigger. See how the red box grows? That's flex-grow doing the work.

Alright, let's set the second item to 2 and see what happens.

flex-grow: 1;
flex-grow: 2;
flex-grow: 0;

Try resizing your browser again. As you can see, the blue box grows twice as fast as the red box.

This time, let's try giving all the boxes a value of 1.

flex-grow: 1;
flex-grow: 1;
flex-grow: 1;

If they all have a value of 1, the space will be distributed equally among all the items.

flex-shrink, on the other hand, makes the items shrink according to the window size. I'm going to change the basis to 300px real quick just so I can better demonstrate to you what it does.

The default value for flex-shrink is 1.

flex-shrink: 1
flex-shrink: 1
flex-shrink: 1

Make your browser smaller. You can see that at 1, they will all shrink at an equal rate and can even become smaller than our basis of 300px.

Let's try setting our red box to a shrink value of 0.

flex-shrink: 0
flex-shrink: 1
flex-shrink: 1

If we set an item to 0, it will not shrink. However, the other items still shrink in our example as they still have the same shrink value of 1.

Let's see what happens if we set our green box to 5.

flex-shrink: 0
flex-shrink: 1
flex-shrink: 5


If we set our third item to 5, it will shrink five times faster than our second item, which has a value of 1.

Now, all these three can simply be specified with a shorthand, which is as follows:

flex: grow shrink basis;


> Flex Item Order

We can also change the order that our items present themselves without restructuring our code. To do this, we simply append the order property and input numbers.

A
B
C

Let's try to move these letters around.

[div=
display: flex;
height: 50px;
color: white;][div=
flex-grow: 1;
order: 2;

background: red;]A[/div][div=
flex-grow: 1;
order: 3;

background: blue;]B[/div][div=
flex-grow: 1;
order: 1;

background: green;]C[/div][/div]

Output:

A
B
C

Pretty easy, right?


> Main Axis & Cross Axis + Justify-Content

An important component of flexbox are the axes, namely the main axis and the cross axis. By default, the main axis would be referring to the row, whereas the cross axis would be referring to the column. However, it's not that straightforward, which is probably where a lot of the confusion regarding flexboxes come from.

By default, our boxes would be positioned along a row, like so:

A
B
C

Now, if we want to change where our items are situated within that row, we use justify-content by default. There are many values associated with this property, and they are as follows:

  • flex-start - positions items at the beginning of the container
  • flex-end - positions items at the end of the container
  • center - positions items at the center of the container
  • baseline - positions items at the baseline of the container
  • space-around - items are evenly distributed along the line, with spaces around them
  • space-between - items are evenly distributed along the line, with spaces between them
  • space-evenly - items are distributed so that the spacing before, between, and after two adjacent items is the same

We've already seen flex-start in action, as it is the default, so let's see what flex-end does.

[div=
height: 50px;
display: flex;
justify-content: flex-end;

border: 1px solid yellow;
color: white;][div=
flex: 0 1 100px;
padding: 10px;
background: red;]A[/div][div=
flex: 0 1 100px;
padding: 10px;
background: blue;]B[/div][div=
flex: 0 1 100px;
padding: 10px;
background: green;]C[/div][/div]

Output:

A
B
C


And center:
A
B
C


BUT--and this is where it gets confusing--justify-content does not always position items along the x-axis, no sir! It goes hand-in-hand with flex-direction. Now, if you omit flex-direction from your code, it will default to row, thus enabling justify-content to align items in a row.

However, if you specify flex-direction: column, that will flip things around, meaning justify-content does not align items in a row anymore! It aligns things in a column. Capeesh?

Yeah, I know, it's a lot. But a simpler way to remember this is that they both always mirror each other.

TL;DR
  • If flex-direction is default row, justify-content will align items along a row.
  • If flex-direction is column, justify-content will align items along a column.

Okay, so let's try appending flex-direction: column into our code.

I'll be changing some stuff around, so you can better see how things are working.

[div=
height: 300px;
border: 1px solid yellow;
display: flex;
color: white;
flex-direction: column;
][div=
flex: 0 1 50px;
padding: 10px;
background: red;]A[/div][div=
flex: 0 1 50px;
padding: 10px;
background: blue;]B[/div][div=
flex: 0 1 50px;
padding: 10px;
background: green;]C[/div][/div]

Output:

A
B
C

Alright, what do you think will happen if I append justify-content: center; Will it align our items horizontally or vertically?

[div=
height: 300px;
border: 1px solid yellow;
display: flex;
color: white;
flex-direction: column;
justify-content: center;
][div=
flex: 0 1 50px;
padding: 10px;
background: red;]A[/div][div=
flex: 0 1 50px;
padding: 10px;
background: blue;]B[/div][div=
flex: 0 1 50px;
padding: 10px;
background: green;]C[/div][/div]

Output:

A
B
C

Yup, it aligns our items along a column to mirror justify-content: column!

But now you must be wondering: well, there must be another property related to this right? Surely this guy has a twin that aligns our items in the opposite arrangement?

You're right. This is where align-items comes in.


> Align-Items

Unlike justify-content which aligns items horizontally by default, align-items aligns our items vertically by default.

When the flex-direction is flipped to column, however, align-items begins affecting horizontal alignment.

Let's check our first example. For now, I'll be using the default direction, which is row.
A
B
C

The default value for align-items is stretch, meaning the items stretch to fit the container along the main axis. However, we can use some of the same values as the ones we used for justify-content. To refresh your memory, they are as follows:
  • flex-start - positions items at the beginning of the container
  • flex-end - positions items at the end of the container
  • center - positions items at the center of the container
  • baseline - positions items at the baseline of the container

To use any of these values, simply append them after align-items: Don't forget to put them in the div holding the container!

This is how flex-start would look like:

[div=
height: 150px;
border: 1px solid yellow;
display: flex;
color: white;
align-items: flex-start;
][div=
flex-basis: 100px;
padding: 10px;
height: 33.33%;
background: red;]A[/div][div=
flex-basis: 100px;
padding: 10px;
height: 33.33%;
background: blue;]B[/div][div=
flex-basis: 100px;
padding: 10px;
height: 33.33%;
background: green;]C[/div][/div]

For the purposes of this demonstration, I will be adding a height of 150px for our container, as well as a yellow border so that you see its dimensions. I allocated 33.33% height and 100px basis for each of our items to make them smaller, enabling you to see them moving around in the box.

Output:

A
B
C

Now, what if we changed the alignment to flex-end?

Output:

A
B
C

And center?

Output:

A
B
C

Now, let's try to change our flex-direction. Once again, I'll be changing some stuff around (namely the flex property) so you can better see how things are working.

[div=
height: 300px;
border: 1px solid yellow;
display: flex;
color: white;
flex-direction: column;
][div=
flex: 0 1 50px;
padding: 10px;
background: red;]A[/div][div=
flex: 0 1 50px;
padding: 10px;
background: blue;]B[/div][div=
flex: 0 1 50px;
padding: 10px;
background: green;]C[/div][/div]

Output:

A
B
C

So, like I said before, by using flex-direction: column, we have flipped our axes around. Now, align-items will now control the cross axis, which is now the x-axis:

[div=
height: 300px;
border: 1px solid yellow;
display: flex;
color: white;
flex-direction: column;
align-items: flex-start;
][div=
flex: 0 1 50px;
padding: 10px;
background: red;]A[/div][div=
flex: 0 1 50px;
padding: 10px;
background: blue;]B[/div][div=
flex: 0 1 50px;
padding: 10px;
background: green;]C[/div][/div]

Output:

A
B
C

Flex-end will look like this:

A
B
C

Center:

A
B
C



> Individual Item Alignment

So you want to align items individually? One of the properties we can use is align-self. Instead of putting this in our div container, we place it in the div defining our items. Like so:

[div=
height: 150px;
border: 1px solid yellow;
display: flex;
color: white;][div=
flex-basis: 100px;
padding: 10px;
height: 33.33%;
background: red;
align-self: flex-start;
]align-self: flex-start;[/div][div=
flex-basis: 100px;
padding: 10px;
height: 33.33%;
background: blue;
align-self: center;
]align-self: center;[/div][div=
flex-basis: 100px;
padding: 10px;
height: 33.33%;
background: green;
align-self: flex-end;
]align-self: flex-end;[/div][/div]

Output:

align-self: flex-start;
align-self: center;
align-self: flex-end;


On the other hand, we can't really align items individually with justify-content. However, we can use margin to aid us with this.

Let's say we want C to go all the way to the right. By using margin-left: auto we will be pushing C from the left:

[div=
height: 50px;
display: flex;
border: 1px solid yellow;
color: white;][div=
flex: 0 1 100px;
padding: 10px;
background: red;]A[/div][div=
flex: 0 1 100px;
padding: 10px;
background: blue;]B[/div][div=
flex: 0 1 100px;
padding: 10px;
background: green;
margin-left: auto;
]C[/div][/div]

Output:

A
B
C

Now, if we append the same declaration to B, look what happens:

[div=
height: 50px;
display: flex;
border: 1px solid yellow;
color: white;][div=
flex: 0 1 100px;
padding: 10px;
background: red;]A[/div][div=
flex: 0 1 100px;
padding: 10px;
background: blue;
margin-left: auto;
]B[/div][div=
flex: 0 1 100px;
padding: 10px;
background: green;]C[/div][/div]

Output:

A
B
C

Pretty cool, right?


> Centering Items

Now that we know how to manipulate the main axis and the cross axis, let's talk about centering items in our flex container.

Let's say you want to put some text in the middle.

First, let's make our base. As usual, it's width, height, and a red background.

[div=
width: 200px;
height: 200px;
background: red;
][/div]

Output:


Let's input our text next. We'll make the color white so we can see it clearly.

We will center our text later.

[div=
width: 200px;
height: 200px;
background: red;
color: white;
]Hello world.[/div]

Output:

Hello world.

Now for the fun part. Once again, we'll be using display: flex

[div=
width: 200px;
height: 200px;
background: red;
color: white;
display: flex;
]Hello world.[/div]

Output:

Hello world.

Then, use justify-content: center to center our item horizontally. Append align-items: center to center items vertically.

[div=
width: 200px;
height: 200px;
background: red;
color: white;
display: flex;
justify-content: center;
align-items: center;
]Hello world.[/div]

Output:

Hello world.

You can also center other elements using this process!

[div=
width: 200px;
height: 200px;
background: red;
color: white;
display: flex;
justify-content: center;
align-items: center;
][div=
background: blue;
width: 100px;
height: 100px;
border-radius: 100%;][/div][/div]

Output:


And there you have it. Flex makes everything simple for us.

Now that you know how to flexbox, go and flex your skills!


# Featured Properties

display: flex;
flex: grow shrink basis;
flex-grow: value;
flex-shrink: value;
flex-basis: value;
order: value;
min-width: value;
justify-content: value;
align-items: value;
align-self: value;
margin-left: value;
margin-right: value;
 
Last edited:
  • Love
Reactions: Bubs

Mp3 Player

Iwaku has its own shiny audio player. Unfortunately, it only plays mp3 files sourced from the internet, which limits our options. YouTube, on the other hand, provides us with a variety of music to choose from. Fortunately, we can embed YouTube videos and fiddle around with some coding to create a fully functional mp3 player of our own.

What we need to do, first and foremost, is link our music player. Once again, I'll do it step-by-step. However, I won't be able to show you the results of each step like I've done previously, as Iwaku only allows a limited number of media to be shown in a post.

Let's say I want to use this song for my music player. Copy the content ID, which can be found after the equals (=) sign in the link.

https://www.youtube.com/watch?v=tVCUAXOBF7w


Input the content ID between the [MEDIA=youtube][/MEDIA] tags. It should look like this:

[MEDIA=youtube]tVCUAXOBF7w[/MEDIA]


Now, let's specify a width and height for our music player. How does 30x30 sound?

[div=
width: 30px;
height: 30px;
][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div]


Let's add something to cover it up, like a play and pause button. We'll be using FontAwesome icons for this.

[div=
width: 30px;
height: 30px;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][FA]fa-play-circle[/FA]


Let's put this in a 30x30 container, and let's make our icon bigger and center it while we're at it.

[div=
width: 30px;
height: 30px;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 30px;
height: 30px;
font-size: 20px;
text-align: center;
][FA]fa-play-circle[/FA][/div]


Now we're going to pull our button up so that it sits above the video player. Since our container is 30px, -30px should do it.

[div=
width: 30px;
height: 30px;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 30px;
height: 30px;
font-size: 20px;
text-align: center;
top: -30px;
][FA]fa-play-circle[/FA][/div]


Okay, but now we can't click our button. This is easily solved by pointer-events: none; which allows us to click through the element.

[div=
width: 30px;
height: 30px;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 30px;
height: 30px;
font-size: 20px;
text-align: center;
top: -30px;
pointer-events: none;
][FA]fa-play-circle[/FA][/div]


Now let's lower the opacity of our media player. 0.01 ought to do it. We should also remove that space it leaves at the bottom with height: 30px and overflow: none;

[div=[PLAIN][COLOR=#34987c]
height: 30px;
overflow: none;[/COLOR][PLAIN]][div=
width: 30px;
height: 30px;
opacity: 0.01;
][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 30px;
height: 30px;
font-size: 20px;
text-align: center;
top: -30px;
pointer-events: none;][FA]fa-play-circle[/FA][/div][/div]

Output:



Alternatively, we could add a background for the button. I'm going to use white, so we'll have to add a font color too so that we can actually see our button.

[div=
height: 30px;
overflow: none;][div=
width: 30px;
height: 30px;
opacity: 0.01;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 30px;
height: 30px;
font-size: 20px;
text-align: center;
top: -30px;
pointer-events: none;
background: white;
color: black;
][FA]fa-play-circle[/FA][/div][/div]

Output:




> Mp3 Player (Bar)

So that one is a popular design you'll see floating around. However, this next one is what I personally like to use.

We'll be using the latest code we did for our previous one, except we'll be removing text-align and we'll be changing the width to 300px.

This time, we'll be creating a 50px width container for our media player.

[div=
height: 30px;
overflow: none;][div=
width: 50px;

opacity: 0.01;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 300px;

height: 30px;
background: #34987c;][/div][/div]

We do much of the same thing we did last time, except this time we'll be using a width of 300px for our second div, as well as a teal background to match Iwaku.

Next, we will be inputting the display: flex; so that we can better control the various elements we'll be placing in our box.

[div=
height: 30px;
overflow: none;][div=
width: 50px;
opacity: 0.01;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 300px;
height: 30px;
background: #34987c;
display: flex;
][/div][/div]

Now, we can go ahead and add our icons. This time I'll be adding a pause icon as we have more space to work with. We'll be coloring it white and adding a font-size of 20px.

[div=
height: 30px;
overflow: none;][div=
width: 50px;
opacity: 0.01;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 300px;
height: 30px;
background: #34987c;
display: flex;][div=
font-size: 20px;
color: white;
][FA]fa-play-circle[/FA][FA]fa-pause-circle[/FA][/div][/div][/div]

Next to the icons, we'll be opening another div where we'll be placing our song name. Input a font-size and a color.

[div=
height: 30px;
overflow: none;][div=
width: 50px;
opacity: 0.01;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 300px;
height: 30px;
background: #34987c;
display: flex;][div=
font-size: 20px;
color: white;][FA]fa-play-circle[/FA][FA]fa-pause-circle[/FA][/div][div=
font-size: 12px;
color: white;
]Pixies - Hey[/div][/div][/div]

Let's add some padding to the left and right sides of our icons so that they aren't sticking too close to the edge. Let's add padding to the div holding our text too.

[div=
height: 30px;
overflow: none;][div=
width: 50px;
opacity: 0.01;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 300px;
height: 30px;
background: #34987c;
display: flex;][div=
font-size: 20px;
color: white;
padding: 0 5px;
][FA]fa-play-circle[/FA][FA]fa-pause-circle[/FA][/div][div=
padding: 5px;

font-size: 12px;
color: white;]Pixies - Hey[/div][/div][/div]

Let's move the element holding our play button -28 pixels from the top. Add pointer-events so that we can click through the element.

[div=
height: 30px;
overflow: none;][div=
width: 50px;
opacity: 0.01;][MEDIA=youtube]tVCUAXOBF7w[/MEDIA][/div][div=
width: 300px;
height: 30px;
background: #34987c;
display: flex;
top: -28px;
pointer-events: none;
][div=
font-size: 20px;
color: white;
padding: 0 5px;][FA]fa-play-circle[/FA][FA]fa-pause-circle[/FA][/div][div=
padding: 5px;
font-size: 12px;
color: white;]Pixies - Hey[/div][/div][/div]

Output:

Pixies - Hey

Feel free to play around with the width and the height.


# Featured Properties

pointer-events: none;
 
Last edited:
  • Bucket of Rainbows
Reactions: Bubs