 Since I started this YouTube channel almost seven years ago, which is kind of crazy to think about there have been people requesting I make this video Getting into tables is something that I've sort of avoided and put off for a really long time Probably because I started creating websites using tables as layouts and I just have this like a version to using them But sometimes we need a table on our website. There's still good reasons to use them But we live in a world where we need responsive tables and tables were created before responsiveness was a thing So it can be a little bit of a challenge We're gonna tackle that today by looking at a very simple solution and then one that's slightly more complicated But that is probably the type of thing that you're looking for when you're thinking of a responsive table So with that in mind, let's dive right into the code and take a look at what we're going to be doing or what We're starting with which is not very much. I have an H1 here on my page In my CSS, I don't have anything related to tables So we're really starting from zero and we're gonna work our way up And of course that means the very first thing that we want is a table We want to throw a table in here and then one thing that you don't see too often is actually a caption and caption is an interesting part of our table because it's something that you might not have seen before and That's because their their purpose is to be used with tables and it's basically to give your table a name Now it took me a little while to come up with a something that I want to make a table around But we're gonna do the last 14 world champions and 14 might seem like a very arbitrary number And it definitely is but I'll explain my thinking on that one as we go through this a little bit One thing that we'll notice with tables is like when I first put this in here It does not look very good and that's because we don't have any content in our table yet Tables do require some fixing up to make them look decent But you know don't worry too much about how they look until you actually have all the content That's going to be in there now before we get to the content The caption does need no matter where you want to put this caption I've seen somewhere. It's placed at the bottom rather than being at the top There's ways of moving it around but because the title I'm gonna leave mine on the top But no matter where it's positioned it does have to be the first child of the table That's one of the requirements for the caption. So just to let you know and again Just think of the caption like the title for the table itself And I want to we're gonna be talking a little bit about Accessibility as far as our table goes as well because I do think it that's important And it's why I'm actually using a table rather than just throwing a bunch of divs and using display grid on it And so we're gonna sort of circle back around to that in a little bit But because we're actually using a table rather than just some divs It's a little weird to organize and it's one of the things I've never liked about tables But the first thing we need is table rows which we do with the TR table row Nice and straightforward and I'm gonna have one That's gonna be my headings across the top and then all the regular data under that now I'm actually gonna need 15 in all but let's do a TR here and we'll do times 14 since I already have one at the top So this first one is gonna be for my headings and then we have all the rest of the data coming in there Don't worry. I won't make you sit through me doing them all but starting with this first one that we have right here What I'm gonna do is let's do ETH and that's for table heading So it's like the heading of each one of my columns and I'm going to need six of them So I'm just using emit here to quickly put in six Like that and now like if I hit save nothing nothing's gonna change yet But let me bring in some content here and I'll see you in just a second and okay, there we go So I'm gonna have my name my polls podiums wins career points championships If you know nothing about formula one, don't worry too much about it The content here isn't terribly important But you see those all came across here They're automatically bold and now my title or captions actually stretching across the top It's no longer squished away. So the content will play an important sort of role in what we're doing here So with that set up, we sort of had just have to do the same thing for each of the next things along the way here So instead of th I'm gonna use a TD and once again times six and for this I just need to bring in the information for each one of the people that I'm going to be talking about So here is the first one, which is Max Verstappen We're just putting these in chronological order and he was the first or the most recent champion And so we have the name then we have the polls the podiums the wins career points and how many championships he has won so far in his career and One thing I want to look at with this before we get to the responsiveness of it Is this working with tables is kind of weird because like I have an 80 here and what's the 80 and like we look at it Here it makes sense, but when you look at it here, it's a little bit awkward So two choices are one thing I sometimes do is just use comments and I do like name and then I come here and I do What would this one be polls and so on and so forth? And the advantage of doing it this way you sort of set one up And then you just copy and paste when you're setting everything up, right? So if you come in the next row you paste you know what everything has to be So if you like that better just to stay more organized and when you're looking at a big cable This can really be a lifesaver The other alternative instead of doing that is to use a data attribute and this can come back to help us a little bit later When we get to the slightly more complex solution that I'm going to look at So we could say like data cell is equal to and I quit name and we just do that for all of them So I'm just gonna middle click down here and drag and do data with data cell Is equal to and then I can bring in all of them So I do my polls I do my podiums and so on and so forth For all of these either one is completely fine Or if you don't mind just looking at something like this and you can keep track of what is what then that's great But I really find either having the comments or having the data attribute here can be really helpful And because we are going to be using as I said, I can use this I'll show an alternative solution if you don't use the data attributes when we get to the more complex solution But I'm gonna go ahead and fill them all out using the data attributes and I'll see you in just a second All right, so we got everything in place here And we have all of them all the way down to art and center here And the reason I went here is because I was originally going to stop at my favorite driver Who's Nigel Mansel growing up because my dad is British But then, you know, I was the one just before him. I figured we'd stop there instead of rounding up to 15 So yeah, that's why we're stopping at 14 drivers because that's I think you know got to my favorite And I said I can't exclude Senna so we went with including him as well And just a side note randomly, I just realized I definitely made a mistake here with Alonzo I'm not gonna worry about it too much here. And if you're just wondering what the points The point systems have changed over the years. So in the old days, it was lower point scales. Anyway Let's not worry too much about the organization because there's probably other mistakes in here as I was copying and pasting all this information from Wikipedia And let's jump into actually styling it up by jumping on over to our CSS But before we get into the CSS here I just want to say that the responsive side of this and some of the styling in here was heavily inspired by a blog post by Adrian Roselli that goes into a lot more detail than I'm gonna go here We're gonna go into a very simple approach and we're actually gonna use something that Adrian created To help a little bit later on in this and we'll be sharing that link and all the links to that stuff is in the description It goes into more detail more edge cases and extra sort of styling if you have longer text and other stuff So if you're looking for something that's a little bit more detailed or robust definitely check out the link to that article down below And with that let's get started on this So the very first thing we're gonna do here is I'm gonna stable just stable style my table and On here, I'm gonna start just by giving it a white background color because I want to show you something interesting So background white and what's really interesting here is if we take a look even though My caption is inside of the table there. Notice how it doesn't actually get that background color super weird, right? So this is important to know if you're giving like your table a dark background color. Just make sure that You know because here I would do like color black then if I wanted to do this And then I can't read my title or my caption anymore because it got the color black But it did not get the background of white So you'd want to you know, whether it's doing the caption here or separately setting the background or whatever it is I'm gonna style my caption separately anyway, but I just wanted to throw that out there as something to watch out for So let's get rid of that for now. And I'm definitely not gonna go with a black back or white background I should say so we can get rid of that and get rid of that and we can come in here Just with a dark color that stands out a little bit from the background and while we're here I think a little bit of padding will also help out and so we can you know get something that's decent looking now I'm gonna choose my th and my TD just because TD we're gonna come back to these in a second But for now, I'm also gonna give these a color of white just because I want to show you that we do get like this weird spacing in Between this is just from the old days of this little default borders that come with tables So on here, we're also gonna add in a border collapse of collapse Which gets rid of all of those because they generally you don't want them and you're gonna be bringing in your own Borders or other stuff if you want them and that border thing was always a nightmare So just border collapse collapse saves saves us a lot of trouble there and by doing the border collapse though You might have noticed that the padding also disappears and this is a bit of a shame, but that's okay We're gonna fix things up and to do that what we can do is we can select our TDs But we also want the th is actually so th and TD and we can give these all a padding of one rim and that basically gives us that padding around but it also spaces these out which does look a little bit nicer and In general for the caption that's up here. We sort of want that as well So we can say caption and include that so it just spaces things and the reason I like doing that is because then if I come down on here to my caption And on that we can do a text align left because it just tends to look a little bit nicer Depends on what you want though, but now the left align there will match this and you'll notice that these are also not text Aligned left to kind of speak right now So we can do our th's also get the left alignment just because I think it's a little bit better than mixing alignments which can be a little bit strange and Now what we want to do is I'm gonna move my face down because we're gonna take a little bit more room up here because We want to make this responsive So I'm just gonna give this a width of 100% and That is the first step in making something responsive because you can see it will actually smush and stretch And it's just limited by the the wrapper that I have it inside of here And if you've never seen a wrapper or container type thing done like this I'll link to a video either a card or a link in the description But it's a little bit off topic for what we're doing now. Basically. It's a max width of 900 pixels we have on here But you can see it's working, but of course, we're not responsive when we get to here Everything looks terrible. We are gonna fix that up But I am gonna style things up to look a little bit nicer here and we'll start with our caption itself where we can just give this a background and Let's just do an HSL because HSL is the easiest to work with in my opinion 0% 0% to make it completely black just to separate it a little bit from the background there And it is a title so it's a bit weird that the font size is so small on these So we can make the font size bigger and the font weight can also be bold Yeah, spell weight properly, but we'll get there in the end and just you know, maybe a text trance transform of Upper case on that as well And then we get the last 14 will champions perfect now We've given this a different background color so we might as well give this one a different background color as well and Because the padding is on the th and not on anything else We can just take our th and give that a background color. I think it's the easiest way to work So here we can say the th and I'm gonna give actually we're gonna be doing the same thing We'll use some HSL. So I'll just copy and paste and The nice trick with this though is if we just come here and because I'm using the space separated syntax Which you might not be used to just instead of comma separating everything no commas But you put a forward slash if you want opacity And I can come in with a point five here or whatever and you can see it darkens it up Because we're just having some darkness without going all the way to black and it's overlaying on top of this and We can take that same type of thing because you know if you're looking at this 91 and you're I guess you could get to Michael Schumacher There or whatever, but it's hard to sort of track your way around and again I did switch these two around Alonso definitely doesn't have 2,000 wins, but it's okay We're just in demo world here. So Yeah, making it easier to read we will do a tr and then we can just do n of Type there it is and the type and to end so selecting every second one And we can use this exact same trick on there But maybe a point zero five just to make it this really light difference or a point one I don't like my zebra patterns to be really obvious like too big because if this was going to like, you know That I find that actually it's easier. I guess to follow along with the eye But just that high contrast is really annoying to look at So I tend to like being pretty subtle just enough to help you out. It is a very low contrast change So we do have to be aware of that But I think personally going too high just makes it really hard on the eyes as well So something subtle like that for me works well and with that done We're actually ready to make this responsive now I did say we'd have two different ways that we can solve this the first one being the simple one and the second one being A little bit more complex though. I don't find it too bad, but we'll start with the very simple one which is actually To come up and here I have my table So let's select the whole table So we'll start there and go all the way down and select the whole thing I'm going to do a control shift key To get my command palette up here and then I'm going to do emit And I want to actually let's do wrap There it is wrap with abbreviation and I'm going to say dot table container The reason I'm doing that is if we come all the way back up It just puts a div around it called table container. We're going to remove this for the second solution But basically we just do table container And max width is 100 and then we can do an overflow overflow x of Scroll and this isn't perfect and I mean you can see the scroll bar that's coming up But when we get to here because that's overflowing we get our scroll bar I'm going to just scroll on over for stuff Now the issue with this is I have to scroll down to get to the scroll bar, which is not ideal Right. So we have to do that. You don't want to do that and I'm pretty sure you can see it, but just there's the scroll bar right there So we don't want to be doing something where we have to scroll down to get to it And usually if you're at an hour of view ports that does happen But that's by far like this is the simplest possible solution and of course you could do this as an auto as well With auto there's no scroll bar that you know that one that was already sort of hiding there But then if I shrink this down then it will appear. So maybe you like that one better So if you're going to do it that way and you want the no frills very simple solution Or you have a very small table that needs a little bit of side scrolling That's sort of the simple way to do it But we're going to delete that and we're going to come back here We'll get rid of that table container as well and we're going to look at a more Involved solution. It's really not that bad, but it works really really well And one thing with this I am going to take a desktop first approach Which normally I always advocate for the path of least resistance Which means write the least amount of code possible to make things work And that tends to usually be mobile first Because when you're writing stuff most things are mobile, you know You put all your base styles and things just work on the phone and then you add complexity later Tables are a desktop first thing. They work on desktop. They don't work on mobile So because of that we're adding the complexity at mobile screen sizes So for me it makes more sense to just add that complexity now Instead of making that the default and then overwriting it back to what the defaults were in the first place So I know people always get mad at me when I do amax with Media query, but just why we're doing it right there And I'm going to do maybe I don't know 650 pixels We depends on your table and what media queries you're using and everything But we're going to go with that and we're going to be doing a few different things And this is where it would be, you know, sometimes you'll even see like placing a display grid or whatever to try and style things from the very beginning We're just going to sort of default to stacking everything and this does have a consequence that I'll talk about in a second But what we can do is a td and actually we might actually go to grid after are we well But I'm going to do a display of block for the moment Just so we can see what happens and here we can do an inspect and people always ask me about this when I use it So in your dev tools, you can just click this if you're in firefox. It's on this side And I forget where it is in safari You just click that and then you get responsiveness Basically, you know your responsive mode and by doing that when we get to these small screen sizes You can see that, you know, what's happening right there Which is they're becoming display blocks. So they're all stacking on top of each other now at the moment This looks really awkward So what we're actually going to do is we're going to come up here I'm going to see th and we're going to do a display of none And that fixes a lot of the problems and basically we had a whole bunch of columns And then we had these that were fitting into the first column. It was being kind of awkward Um and taking those these and like how you know, this doesn't make sense going across the top anymore Anyway, so we can just turn them off at this screen size and now we have a responsive table This is going to work. We have the last 14 world champions max for stopping But then we just get these random information that we don't actually know what it is Which is not very convenient. And this is actually where having the data cells here helps Not only is it make it easier to do what you're looking at when you're here looking at a 23 just like here the 23 doesn't make any sense Um, but you know, so if I wanted to fix that thing with Alonzo when I came down If I didn't have my points and my wins here, this would actually be kind of weird So we can just take this put it here take the 32 put it there And I know exactly what I want to do and just the ordering everything. It makes it a little bit easier Um to understand or if we have the comments there as well and it means we can link into it because With css, there's a really awesome thing we can do and I'm going to include this inside the media query Because I don't want this to happen at sizes bigger than 650 But we can do this really cool thing where we're going to say on the td We're going to go on the after td after And this is where on the content property you can access attributes by doing attr This is a value or a function That is supposed to be able to be used in lots of other ways as well Uh, so right now it's limited to the content property if ever we get this in other places It's going to be fantastic But even just having it here is great because I can just put in here data cell And hit save and that information comes in and I did it as the after I should have done this as my before I'm just there we go Because if not it was max or sap and then name on the other side. So we're getting it before there Now it doesn't look fantastic Everything's a little mixed up and hard to read. So let's also give this a font weight Of 700 to make it bold. So it sort of serves more of a purpose We can also come here and I can add some string text. So I'm going to do a colon and a space so we get name and then we get the space there It would be kind of nice if they were starting with capital letters So we can also do a text transform on these transform of Capitalize which is not one I use very often, but there we go It works really well And so right away you can see we have those lining up across the top And then when we get to the smaller size we see them here and we can read it a little better The spacing on everything is a little weird. So here we could come and change our padding to like a 0.5 rem rem one rem Just to sort of bring things a little bit closer together And I think it just you know, maybe a 0.75 whatever you want You could play around with your spacing you could increase the spacing between them as well But just something to make it a little bit better You could even use some viewport units or something in here to space things But anyway, you sort of get the idea of how this is coming together and it becomes a little bit more readable And if you actually if you do want more space here, what we could do is we get to say TD first child is going to be padding Top could be one rem And then we could duplicate that line and say that the last child is a pad and bottom And of course these could be logical properties or something But then you could just sort of you know, give yourself a bit more room So each one is grouped a little bit more as well That looks a little better I think now I did also mention that maybe you didn't want to take this approach with these data attributes And if that's the case what I'm going to do here is I'm going to strip these ones the first row off here And give an alternative just to show you and I'm just taking these ones off just to show you that it doesn't work And this is you know, if you just have something like this or maybe for whatever reason you don't have control over the markup Instead of doing it like we had it before with our content on the before Basically, you just have to do this for each one of them. So we don't have this anymore So we could actually keep that just like this. So the styling of everything is okay And then we just wanted to do td td of Type and I'm just going to choose the first one and then my content would be name And that would bring in except this should also be my four. Sorry, there we go And then the name comes in And then I could duplicate that this one would be polls I think it was And this would be my nth of type two and then the next one that wasn't polls. I don't know what that one was Oh me it is. Yes polls. This is podiums here podiums and so on and that's my third one And we could bring it in that way and you can see by doing it this way It's doing it on all of them I don't have to go on like every single one and put in the data cell So part of it depends on how you want to work if you like the data attributes It's one way, but of course if ever this had to become, you know, poll positions It's you know, just updating one little thing here This also comes. Do you want the content coming from the css or do you prefer the content in the html and that type of thing I'm going to go back to, you know, I'm not undo all of this just to go back to what we had But it it really just depends on Which way around you prefer working at the end of the day or where you have the control over things Depending on if you're in a cms Where maybe you don't have the data attributes you can play with whatever it is Giving you the different options that you do have right there So we sort of get this and it's already off to a good start And one more thing before we get into fixing the accessibility of this because we actually broke it when we did our display block here And we're going to switch this actually from a display block to a display grid Which basically just forces everything to be stacked The reason i'm doing this is because then we can use a grid template columns template columns And you can do something like I don't know 15 ch and then auto Which should be fine And i'm doing 15 ch characters. This is I think 14 or 15 letters long. So it sort of just balances it out So if you prefer that and of course again, you don't we could go back to this just being block If you like it like this you could leave it like that if you like it a little bit more organized depends on the type of content You're doing We could do that and having a gap on here Maybe that would be too much but having a small gap on here could also be useful just to make sure nothing gets too close to one another And we could have our content like that and so it's not looking fantastic by any means, but it's definitely looking better Uh, it's responsive now. It works here. We get the small screen sizes It stacks until we get to like really small sizes where you might run into more issues But we're getting you know, we're pushing the limits a little bit of what we'd want to do there But having it like this i'm pretty happy with Except for that issue of this display grid Actually, or if we use display block as well It strips the accessibility away and all the semantics away I should say of using a table because all of these are no longer Going to be data table the browser sees them as just regular block level content And it doesn't have any meaning to it anymore And so you're going Kevin, I could have just used grid or whatever from the beginning and then not worried about it But we should use the right elements for the right things whether that's tables for tables links for links buttons for buttons Headings to view headings. We should be using the right things and luckily I mentioned adrian helped it out a little bit before I'm going to cheat a little bit just by coming down here and doing this instead of a separate JS file, but we could obviously do this as your own JS file And so that's where this amazing blog post for adrian rocelli comes in This isn't the blog post where I talked about earlier This is one where it's just looking at functions to add area rules to your tables and lists I'm not a big fan of copy pasta in general. I think we should know what we're doing But uh, so I'm going to leave this here so you can give it a bit more reading And I would definitely encourage that you read the longer post about building them out And again, that goes into like other edge cases and other things print styles A lot of really good stuff that you can add But what I'm going to do is take his for the roles and I'm just going to come here We could definitely have this as its own script tag Right, so you're just linking to it instead of using an actual script tag here But I'm just going to paste it in the bottom here just to go really fast And basically what it's doing is it's running through our page And it's bringing the semantics back in for our trs our tds It's looking at all of these different things our caption that we had at the top the table And other elements that are part of tables that you might be using And if we come and take a look it's adding the roll in so roll is row row is roll is sell The table roll is table So even if we strip away the semantics when we do a display block this will bring that back in And it prevents the semantics of our table from falling apart, which is really good And the other nice thing with it And actually look it even came in with the t body And so it's it's trying to organize things for us and make sure that it is actually semantic Which is fantastic And this is why we wouldn't want to start with a grid in the first place We used a grid with a whole bunch of divs Actually manually coming in and doing this would sort of be a bit of a nightmare So yeah, that's how we can get set up like this and have everything working So I'm pretty happy with the end result. I hope you're pretty happy with the end result Of course more styling can come into this table in general But just as something that works at different screen sizes, there's no big issues that come up And I think it's fairly simple to set up So there we have it And if you do struggle with responsiveness in general and not just putting tables together I have a video right here with a lot of simple tips on making your life easier to deal with making responsive websites And with that, I would really like to thank my enablers of awesome Johnny Michael Simon and Tim as well as all my other patrons for their monthly support And of course until next time don't forget to make your court on the internet. Just a little bit more awesome