CS 318: Lab 18 – Tabbed Viewing
Dear students,
Last time we started implementing dropdown menus. We decided to continue that effort today, though I do want to add one extra and related exercised: a tabbed viewer.
Here’s your TODO list:
- Breathe easy!
See you next time!
Lab
First, we finish up our lab 17 exercise on switching between small and large displays of our nav
links.
Second, create a lab18
directory. Add a partners.html
and include your first names and last initials. In this exercise, we’ll tackle the task of presenting content in tabs.
Tabs
Create a second page named tabs.html
and an accompanying stylesheet named tabs.css
. Your goal is to create a page that allows the viewer to switch between views within a single web page. Something like this:
I suggest tackling this exercise in this order:
- Create a
div
with an ID ofparent
. - Create within
parent
threediv
s of classtab
. Give each a unique ID. Give as content the labels of the tabs. - Style the tabs as inline blocks (so they appear on the same line) and with a light background color, a fixed width, some padding, and rounded top corners. You know, like tabs.
- Switch the tab buttons’ cursor to
pointer
. - Add a
:focus
pseudoselector rule for these tabs so that focusedtab
s have a darker background color. The:focus
pseudo-selector is much like:hover
, but focus can “stick.” It is activated when the viewer clicks on an item. This is important for us, because unlike:hover
, we want a tab to stay selected even after the mouse moves on. - You’ll find that your
:focus
rule doesn’t have any effect. That’s becausediv
s aren’t naturally focusable. In the HTML, add atabindex
attribute of 0 to each of your tab buttons. The name is coincidental.tabindex
refers to the actual Tab key on your keyboard, not the tabbed view that we are trying to create. Pressing the Tab key cycles through the focusable elements in the page, and by giving yourdiv
s atabindex
, they become focusable. The numeric value of thetabindex
imposes an ordering on the focusable elements. After setting it, you should be able to see your focused tab button show prominently against its neighbors. - Disable the outline that appears around your tab button by setting a tab’s
outline
property tonone
. - Add another
div
toparent
for the default view. (Yourparent
should now have four children.) Give it the IDpanel0
and classpanel
. Givepanel0
the muted background color that you used for non-focused tab buttons. It will hold the default content when no tab has been focused. Put whatever you want in it. - Add three more
div
s toparent
for the views that will appear when their corresponding tab is focused. Give them IDspanel1
,panel2
, andpanel3
. Put them all in the classpanel
. Give them each some unique content so that you can test your work. Give one an image, another a paragraph of text, and another a list or quote. - Style the
panel
class to appear below the tabs. This is most easily done with absolute positioning. First, giveparent
relative positioning so it can serve as an anchor point. Then givepanel
absolute positioning. Anchor the left edge against the anchor’s left edge. Offset them from the top of their anchor by the height of the tabs. We don’t know exactly how tall the tabs are, so use a relative measure. You should see them all stack on top of each other below the buttons. - Color all
panel
s by the darker, focused color.panel0
should override this to use the lighter, unfocused color. - Probably your tab views are filling the whole page. Make
parent
display as aninline-block
so it automatically sizes itself around its children and doesn’t greedily eat up the browser window. Alternatively, one could leave it asblock
and just explicitly set the width. - Hide all the
panel
s using thedisplay
property. - Override
panel0
so that it always displays as ablock
element. - Override each of your other panels so that they display as
block
elements only when their corresponding button is focused. We can use the sibling selector to achieve this:Read this as “select the element with an#tab3:focus ~ #panel3 { ... }
id
ofpanel3
, such that it is a sibling element of a focused element with anid
oftab3
.” When the button’s not focused, the generalpanel
rule takes effect and hide that tab’s content. Unfortunately, no comprehensive rule can be written to apply to all three views at once. You will need three separate rules. - Give yourself a high five.
Publish and Validate
Commit and push your work. Verify that all pages have been uploaded by visiting https://USERNAME.github.io/cs318/lab17
and https://USERNAME.github.io/cs318/lab18
in your browser.
Validate your pages with the W3C Validator. Paste in the URLs to your pages, one at a time, and address all concerns raised by the validator. Fix your changes on the local machine, commit, and push until the validator reports no errors or warnings.