Building Social Media Apps · Course book
Chapter 1: Laying Out a Social Feed
A social network's home page looks complicated: a bar of icons across the top, a column of shortcuts on the left, a scrolling feed of stories and posts in the middle, and lists of events and contacts on the right. Underneath, it is built from a handful of ordinary HTML elements arranged with a few CSS layout techniques. This chapter explains how to plan and build such a page with plain HTML, CSS and a little JavaScript, the way a front-end developer would approach a design like a well-known social network. It covers planning the page as boxes, building the navigation bar and its profile menu, laying out three columns with flexbox, building the feed's stories and posts, adding a light and dark theme with CSS variables, and making the whole page work on a phone.
Planning the page as boxes
Every layout starts as a set of rectangles. Before writing code, sketch the page and name each region: a navigation bar fixed at the top; a container below it holding a left sidebar, a main content column and a right sidebar; and, inside the main column, a row of stories, a box for writing a new post and a list of posts. Naming the regions first gives you the class names for your HTML and keeps the CSS organised.
Semantic HTML elements make the structure meaningful to browsers and to assistive technology such as screen readers. The table shows a sensible mapping.
| Region | Element | Contents |
|---|---|---|
| Top bar | <nav> | Logo, icons, search box, profile picture |
| Left sidebar | <aside> | Shortcut links and groups |
| Feed | <main> | Stories, the new-post box and posts |
| Each post | <article> | Author, time, text, image, reactions |
| Right sidebar | <aside> | Events, a promoted box, contacts |
Images need meaningful alt text, such as "Profile picture of the author", while purely decorative icons can use an empty alt="" so screen readers skip them. Clickable icons should be real <a> or <button> elements, so they work with a keyboard as well as a mouse.
The navigation bar and profile menu
The navigation bar is a single row, which makes it a natural fit for flexbox. Setting display: flex, align-items: center and justify-content: space-between on the <nav> places the left group, with the logo and notification icons, at one end and the right group, with the search box and the profile picture, at the other, vertically centred. Making the bar position: sticky with top: 0 keeps it visible while the feed scrolls underneath, and a z-index keeps it above the content.
Clicking the profile picture opens a settings menu: a panel holding the user's name, links to settings, help and logout, and a theme switch. The panel is placed with position: absolute relative to the navigation bar, and hidden by default with a max-height of 0 and overflow: hidden. A small JavaScript function toggles a class that sets a larger max-height, and a CSS transition on max-height makes the menu slide open instead of appearing abruptly.
const menu = document.querySelector(".settings-menu");
const profileButton = document.querySelector(".nav-user-icon");
profileButton.addEventListener("click", () => {
menu.classList.toggle("settings-menu-height");
const open = menu.classList.contains("settings-menu-height");
profileButton.setAttribute("aria-expanded", String(open));
});
Updating aria-expanded tells screen-reader users whether the menu is open. Using a <button> for the profile picture, rather than a plain image with a click handler, means it can also be opened with the Enter or Space key.
Three columns with flexbox
Below the navigation bar, a container holds the three columns side by side. Giving the container display: flex, a maximum width and automatic side margins centres it on wide screens. Each column then gets a share of the space with the flex property. With flex: 1 on both sidebars and flex: 2 on the main column, the feed gets twice the width of each sidebar.
gap of 24 pixels between columns. Two gaps take 48 pixels, leaving 1,152 pixels to share. The flex values add up to 1 + 2 + 1 = 4 parts, so each part is 1,152 / 4 = 288 pixels. The sidebars are 288 pixels each and the feed is 2 × 288 = 576 pixels. On a 1,000-pixel screen, with 16 pixels of padding on each side, the container is 968 pixels wide, so each part is (968 − 48) / 4 = 230 pixels and the feed is 460.Flexbox only distributes the remaining space after each item's starting size, its flex-basis, which is why setting flex-basis: 0 (as the shorthand flex: 1 does) gives exact proportions. The sidebars also use position: sticky so that they stay in view as the feed scrolls, a common feature of social sites.
Stories, the post box and posts
The row of stories is another flex row: five tall cards, each with a background image, a small circular profile picture and a name at the bottom. A linear-gradient layered over the background darkens the lower part of the card so that white text stays readable on any photo. Circular pictures come from border-radius: 50% on square images, and object-fit: cover crops images to fill their boxes without stretching.
The new-post box shows the user's picture, a <textarea> with placeholder text and a row of buttons for adding a live video, a photo or a feeling. Each post is an <article> with a header row, the author's picture, name and time, then the text, an image and a footer row of reactions with counts. Because every post shares one structure, the same CSS styles them all, and later, when the posts come from a server, JavaScript can create them from a single template.
The right sidebar holds smaller boxes for upcoming events, a promoted item and a list of online contacts, each contact with a green dot positioned over the corner of the picture with position: absolute. A button at the end of the feed, styled as a full-width bar, suggests loading more posts.
A feed page carries many images, so their size matters. Profile pictures shown at 40 pixels do not need to be 1,000-pixel photos; saving them at roughly twice their display size keeps them sharp on high-density screens without wasting bandwidth. Modern formats such as WebP are typically much smaller than JPEG at similar quality. Adding loading="lazy" to images further down the feed tells the browser to fetch them only when they are about to scroll into view, and giving every image a width and height lets the browser reserve the right space in advance, so the page does not jump about as pictures arrive.
Light and dark themes, and small screens
A theme switch is easiest when every colour is defined once as a CSS custom property, also called a variable, on the root element. The page uses var(--bg-color), var(--card-color) and var(--text-color) everywhere. A dark theme then only needs a class on the <body> that redefines those few variables, and JavaScript toggles the class when the switch is clicked. Saving the choice in localStorage keeps it when the page is reloaded.
Dark themes need care with contrast. The Web Content Accessibility Guidelines ask for a contrast ratio of at least 4.5 to 1 for normal text. The ratio compares the relative luminance L of two colours, (Llighter + 0.05) / (Ldarker + 0.05). Light grey text, #e4e6eb with L = 0.791, on a near-black background, #18191a with L = 0.0096, gives (0.791 + 0.05) / (0.0096 + 0.05) = 14.1, comfortably readable. A pale grey of #9a9a9a on white gives only 2.81 and fails, while #626262 on white reaches 6.10 and passes. A typical bright blue on white, #1876f2, scores 4.27, fine for large headings and buttons but slightly under the threshold for small body text.
On a phone, three columns do not fit. A media query such as @media (max-width: 900px) hides the sidebars, or moves their key links into the navigation bar, and lets the feed take the full width. The graph shows how the feed's width changes with the screen in the layout described above.
Building mobile-first, writing the single-column styles first and adding the sidebars inside a min-width media query, usually leads to simpler CSS, because the phone layout needs fewer rules. Test at 375 pixels wide, the size of many phones, and check that nothing scrolls sideways.
Practice tasks
- Using the flex values 1, 2 and 1 and a gap of 24 pixels, compute the feed width for a container of 1,080 pixels.
- Change the layout so the feed is three times as wide as each sidebar. What flex values do you use, and how wide is the feed in a 1,200-pixel container?
- A colour has relative luminance 0.2 on a white background. Compute the contrast ratio and say whether it passes for normal text.
- Write the CSS variables for a light theme and a dark theme, and the JavaScript that toggles between them and remembers the choice.
- Explain why the profile picture that opens the menu should be a
<button>. - Describe the media query you would add so that the right sidebar disappears below 1,100 pixels but the left one remains until 900 pixels.
This course book is available to read online only at tech-academy.chrysolites.org.