Responsive Layout with Bootstrap
Purpose:
The class project requires you to make your pages responsive to different widths, so users on wide devices and users on phones can both use your site. The main requirement is that there is no horizontal scrolling or cut-off content. It also requires you to make your non-text elements accessible for those with screen-readers using "aria-label" and similar attributes. This exercise focuses on doing this to the nav bar, since the nav is one of a difficult element to make responsive and accessible. This exercise uses the Bootstrap library.
Required Reading
Please review the following materials about responsive design using Bootstrap and CSS3:
Bootstrap Nav Components
Using @media Queries for Accessibility
Accessiblity Labels (label, aria-label, and legend)
Make a Minimal Collapsible Toggle Button
Please re-type the following code into a new file (name it something like test.html) and pay attention to the notes below:
Please review the following notes on the code above. Please change each feature described below at least once to prove to yourself what they do:
- Note that Boostrap CSS is linked in on the top, but Bootstrap JS is linked in on the bottom.
That is because parts of Bootstrap JS needs to run after the DOM has already been rendered.
- Bootstrap classes "navbar" and "navbar-expand-lg" to make it so the collapse-expand process is
triggered when width drops below or rises above a "large" size (lg).
- Bootstrap class "container-fluid" makes the nav fill the full width and keep everything on one line when not collapsed even with divs inside
- Bootstrap class "navbar-toggler" identifies this button with the collapse-expand process
- Bootstrap class "navbar-toggler-icon" makes the hamburger-menu icon
- Bootstrap classes "collapse" and "navbar-collapse" make the element disappear and reappear properly on collapse and expand
- Bootstrap data attribute "data-bs-toggle" = "collapse" identifies the collapse-expand behavior
- Bootstrap data attribute "data-bs-target" = "#elementid" points the collapse-expand process at this element id
Some of these effects are not hard to achieve using vanilla javascript and CSS, and some of them require several lines of code. Bootstrap is useful because it puts all these effects together in one place.
Add aria-label to the Hamburger Menu for Accessibility
The "aria-label" attribute lets a screen reader know what to say when a non-text element is touched (like the hamburger menu for example). When the element is collapsible, the following aria attributes are needed to direct the screen reader:
aria-controls="elementid"
aria-expanded="false"
aria-label="Toggle navigation"
Please add these attributes to the <button> element.
Make Nav and Footer Responsive, Accessible, and Professional-Looking
Start with the code you made in exercise 1.3 and re-type the following code to replace nav.html and footer.html.
nav.html
footer.html
In index.html, about.html, and contact.html, include bootstrap CSS at the top, as follows:
and bootstrap JS at the bottom, as follows:
Also, add the following CSS to complete the professional look (either in a <style> tag or in a separate CSS file):
Please review the following notes on features that weren't in the previous example. Please change each added feature described below at least once to prove to yourself what they do:
- Bootstrap classes "bg-dark" and "navbar-dark" make a dark nav (bg-dark makes the background dark and nav-bar dark makes the nav links light)
- Bootstrap class "sticky-top" makes an element stick to the top of the viewport
- Bootstrap class "navbar-brand" indicates this content is a company logo or brand. It makes the font slightly different.
- Bootstrap classes "navbar-nav", "nav-item", and "nav-link" work together to transform the <ul>-<li> list into a professional-looking nav
- CSS attribute display:flex (which invokes CSS5 flexbox)
- CSS attribute flex-direction:column (treats everything like it's in a single column)
- CSS attribute height:100vh (sets height to 100% of the viewport height)
- CSS attribute flex-grow:1 (makes background fill space vertically when parent element has flex-direction: column)
- CSS attribute justify-content:center (centers vertically when parent elment has flex-direction: column)
- CSS attribute align-items:center (centers horizontally)
The whole page should work like the following:
Note on best practices
Some browsers will not behave right unless you include <meta> tags that specify things like character set and viewport scale. Particularly, phone browsers tend to pretend they are wider than they are unless you use this meta tag. It is considered best practices to include the following types of tags and to wrap all <meta> and <link> tags inside the <head> tag (as well as js includes, unless the js needs the DOM to be rendered first), as follows:
You can use AJAX includes to include this type of head information, but beware: it is not wise to try to include <script> tags in an AJAX-included file. Since we are using the innerHTML attribute to import the included code, most browsers will not run the included script tags. If you do include such tags, you will end up needing to write more functions to run the script tags.