So far, we have covered the basics of HTML and CSS, discussed different techniques that allow you to position elements in a webpage, and explored the concept of responsive design, which enables you to create webpages that automatically adapt to different screen sizes. In this chapter, we will put everything we have learned so far together to recreate YouTube using only HTML and CSS.
Before we continue, please consider buying the e-book version of this course.
By purchasing this e-book, you will get access to all the accompanied source code. Thank you for your support! Happy coding! 🖥️✨
Let’s start by discussing the basic page layout. We will follow the mobile-first rule and start from the small screen. On a small screen, there should be a top navigation bar and a content section. The navigation bar has the logo on the left and some buttons on the right. The content section should include several card components showing individual videos.
On a medium screen (>= 640px), more icons should be displayed in the navigation bar. We will skip this step for now and revisit this topic when we start building the navigation menu.
On a large screen (>= 768px), the page should have a small sidebar. The navigation bar should display a search box at the center instead of just an icon. The content section should also be multi-column.
And lastly, on an x-large screen (>= 1024px), the webpage should have a full-sized sidebar, a fully expanded navigation bar with all the icons and search boxes, and the content section should have more columns.
Remember your HTML should have all the components required for all screen sizes. If an element is not required for a certain screen, you can remove it by setting display to none.
This example contains a navigation bar, defined by the <nav> element. The navigation bar has three sections, start, center, and end. The class names correspond to their respective positions in the navigation bar. This layout can be created with a flexbox, and the justify-content property should be set to space-between. The center section should be removed until the screen is big enough.
The <main> section has two sidebars, a small sidebar (sidebar-sm) and a full-sized sidebar (sidebar-full). The small sidebar should be displayed on large screens, and the full-sized sidebar should be displayed only on x-large screens.
Lastly, for the content section, you can use a CSS grid with the responsive-layout-without-breakpoint technique we discussed in the previous chapter. For now, we are not going to worry about how the card components are created.
Now we can move on to the CSS file. Let’s remove the default margins and paddings and set the box-sizing property to border-box. You can also add a border to each section to make their respective positions more clear.
Next, the navigation bar should be a flexbox with its direction set to row, and justify-content set to space-between, so that each subsection has equal spaces between each other. The subsections should also be row flexboxes so that the logo and icons are also placed horizontally. However, since we are starting from the small screen, the center section should be removed (display: none;) for now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
nav {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.start {
display: flex;
flex-direction: row;
}
.center {
display: none; /* Not displayed on small screens */}
.end {
display: flex;
flex-direction: row;
}
For large screens, the center section should be designed just like its siblings.
The small sidebar is displayed on large screens. By setting flex to 0, you make sure the sidebar has a fixed size and only the content section changes as the viewport changes.
Next, let’s focus on the navigation bar. The start section has a menu icon and a logo. The center section includes a search form and a search icon. Lastly, the end section contains several icons and a profile photo. In order to make this example shorter, I left out the actual icon elements, defined by <svg>, but you can copy these resources from YouTube directly.
You can also purchase my E-book: “HTML & CSS: A Practical Guide ”, which includes the complete source code for this chapter.
First of all, all the icons should have a unified style. They should be rounded and have equal paddings. The icons should also have a background when the cursor is hovered on top.
On a small screen, most of these icons should be removed, as the horizontal space on a mobile device is very valuable. In this case, we’ll only keep the logo, the search icon, and the profile picture. All the other elements will be removed.
start and end will have equal size, and center will grow faster than its siblings as the viewport grows. center will be removed on a small screen, so for now, there are only start and end.
On a medium screen, more icons will appear, but the center section is still removed.
1
2
3
4
5
6
7
8
9
@mediascreenand(min-width:640px) {
/* Display the menu, mic, camera, and notification icons for medium screen */ .menu-icon, .mic-icon, .camera-icon, .notification-icon {
display: block;
}
}
On a large screen, the center section will be displayed, the mic icon will be moved to the center, and the right search icon will be removed. I left out the styles for the search form.
Now, let’s move on to the sidebar. The small sidebar will be displayed on large screens, and the full-sized sidebar will be displayed on x-large screens.
As for the full-sized sidebar, it should only be displayed on x-large screens. The sidebar should have a fixed width (flex: 0 250px;) so that it does not change as the viewport size changes. The height is defined as 100 percent of the viewport height (100vh), and the overflow is set to auto. This configuration ensures that the sidebar is treated as an individual section in the webpage. When you scroll the sidebar up and down, it will not affect other parts of the page.
In this chapter, we took everything we have learned so far about HTML and CSS and recreated the homepage of YouTube. With this example, we pretty much covered everything you should know about HTML and CSS. In the following chapter, we will discuss some commonly used CSS tools and frameworks that might come in handy when creating your webpage.
If you think my articles are helpful, please consider making a donation to me. Your
support is greatly appreciated.