We'll be stepping through some of the process of styling a React app. What we'll be doing is actually not specific just to React. We'll be talking about good strategies for styling any application. We will be changing some of the React code to be able to make this work.
We're starting with an application that Dan Abramov made for his Redux series for Egghead. You can see here that it doesn't have any styling whatsoever. We just have a box. We can add a to-do, we can add to-do items, we can toggle those to-do items, and then we can filter which ones we're actually looking at.
I've put together a design, and I made it in Illustrator. I didn't add any interactions, or I didn't try and design out any interactions, because I actually prefer to do that part of the process in the browser. I just start with a static comp with some basic idea of what we're going to be working towards. Then, I tweak from there so that I can test it, how it feels in the browser.
I was originally planning on not changing the DOM or the markup at all, but we didn't have any hooks in here to be able to make some nice selectors. If you look, as I inspect this, you'll see that we have one div with an ID of root. That's actually there so that React can render the DOM inside of that.
Other than that, we don't have anything with any classes. They're just divs. We have an unordered list and a paragraph. If I wanted to, using CSS selectors, I could try and target specific things in there.
You can see here, the CSS, it's not semantic, it's not useful, it's not readable. I don't know what that paragraph is. In fact, it's a little bit weird to me that this is actually a paragraph. It's not readable. It's hard to maintain.
On top of that, it also could potentially have some problems with specificity. If we wanted to override things, we have to make sure that we have the same amount or more specificity than the previous rule. This becomes a little bit difficult.
The goal is we want to keep everything using flat classes, meaning we don't have combined classes or any other combined selector so that everything has the same specificity. It makes it really easy for updating.
We could call it root, as well, but that actually can be a little bit confusing, because there is a reserved word for root, which actually refers to the whole HTML element itself. Although we could have a class like that, we're going to go ahead and call it container just to keep things a little bit separate.
On this, we can already start to add some stylings. Let's go ahead and change the font size on the root element. Let's go ahead and set the font size to 16 pixels. I like to set the font size on the root element so that I can use rems as a measurement later on and it refers back to this measurement.
Then, we're going to add a font family. We're going to actually add Open Sans. There we go. We're going to have a fallback of just a regular Sans Serif. I haven't added the library yet, so it's just using that Sans Serif. Then, I've got the font weight. I know the smallest font weight I'm going to use is actually 600.
I'm going to go ahead and link the style sheet for Open Sans, and I'm just going to stick it in the head here underneath the script tags. There we go.
Now, I actually can go ahead and use the shorthand for font. I can set it to 600 for the weight first, and then 16 pixels, and then we can do the Open Sans and the fallback. You can see with the Google Web Fonts, I'm just bringing in 600 and 700 as the weights.
We can go ahead and change the background color on the body. We're going to set it to BDC367, which is this grayish color, kind of cloudy. We're going to set the margin to zero, because body has a margin on it, which just gets in the way.
Next, we can go ahead and start to style container now that we have that, as well. I know that this application has the content on the bottom, so I know that container needs to be at least the size of the screen. To do that, I'm going to say min-height, I'm going to set it to 100 vertical heights, or VHs.
I'm going to set a max width. I don't want it to get beyond 730 pixels, but I do want it to try and use up as much space as possible, so the width will be 100 and the max width is 730.
We're going to need the position to be relative, because what we want to do is center this thing. The way you do that is you set the position to relative, and then you set the margin to zero auto. Let's go ahead and turn off some of this stuff. That way, it'll stay 730 pixels, and it'll center that horizontally on the window.
To make everything go to the bottom, we're going to set the display to flex. We're going to use Flexbox. Even though container, if we inspect it, only has one div as its only child, but we can still use Flexbox to push that down to the bottom.
We're going to use display flex, and then we're going to say flex-direction, and we're going to set it to column. Then, we're going to say align-items, we're going to set that to center, which will center things inside of this container.
We're going to go ahead and set the justify-content to flex-end, which is the bottom of the page, so it'll push all the content down there, and anything that grows will grow up from that starting point.
Now we have it horizontally centered and positioned at the bottom.