diff --git a/files/en-us/learn/css/css_layout/practical_positioning_examples/fixed-info-box.png b/files/en-us/learn/css/css_layout/practical_positioning_examples/fixed-info-box.png index 58fc92191366d92..e419a3b5f6becf4 100644 Binary files a/files/en-us/learn/css/css_layout/practical_positioning_examples/fixed-info-box.png and b/files/en-us/learn/css/css_layout/practical_positioning_examples/fixed-info-box.png differ diff --git a/files/en-us/learn/css/css_layout/practical_positioning_examples/index.md b/files/en-us/learn/css/css_layout/practical_positioning_examples/index.md index d30c24fdbc3ea0e..6b074608ecfb61b 100644 --- a/files/en-us/learn/css/css_layout/practical_positioning_examples/index.md +++ b/files/en-us/learn/css/css_layout/practical_positioning_examples/index.md @@ -31,25 +31,47 @@ This article shows how to build some real-world examples to illustrate what kind The first example we'll look at is a classic tabbed info box — a very common feature used when you want to pack a lot of information into a small area. This includes information-heavy apps like strategy/war games, mobile versions of websites where the screen is narrow and space is limited, and compact information boxes where you might want to make lots of information available without having it fill the whole UI. Our simple example will look like this once we are finished: -![Tab 1 is selected. 'Tab 2' and 'Tab 3' are the other two tabs. Only the contents of the selected tab are visible. When a tab is selected, it's text-color changes from black to white and it's background-color changes from orange-red to saddle-brown.](tabbed-info-box.png) +![Tab 1 is selected. 'Tab 2' and 'Tab 3' are the other two tabs. Only the contents of the selected tab are visible. When a tab is selected, its text-color changes from black to white and its background-color changes from orange-red to saddle-brown.](tabbed-info-box.png) -> **Note:** You can see the finished example running live at [info-box.html](https://mdn.github.io/learning-area/css/css-layout/practical-positioning-examples/info-box.html) ([source code](https://github.com/mdn/learning-area/blob/main/css/css-layout/practical-positioning-examples/info-box.html)). Check it out to get an idea of what you will be building in this section of the article. +> **Note:** You can see the finished example running live at [tabbed-info-box.html](https://mdn.github.io/learning-area/css/css-layout/practical-positioning-examples/tabbed-info-box.html) ([source code](https://github.com/mdn/learning-area/blob/main/css/css-layout/practical-positioning-examples/tabbed-info-box.html)). Check it out to get an idea of what you will be building in this section of the article. -You might be thinking "why not just create the separate tabs as separate webpages, and just have the tabs clicking through to the separate pages to create the effect?" This code would be simpler, yes, but then each separate "page" view would actually be a newly-loaded webpage, which would make it harder to save information across views, and integrate this feature into a larger UI design. +You might be thinking "Why not just create the separate tabs as separate webpages, and just have the tabs clicking through to the separate pages to create the effect?" This code would be simpler, yes, but then each separate "page" view would actually be a newly-loaded webpage, which would make it harder to save information across views, and integrate this feature into a larger UI design. -To start with, we'd like you to make a local copy of the starting HTML file — [info-box-start.html](https://github.com/mdn/learning-area/blob/main/css/css-layout/practical-positioning-examples/info-box-start.html). Save this somewhere sensible on your local computer, and open it up in your text editor. Let's look at the HTML contained within the body: +To start with, we'd like you to make a local copy of the starting files — [tabbed-info-box-start.html](https://github.com/mdn/learning-area/blob/main/css/css-layout/practical-positioning-examples/tabbed-info-box-start.html) and [tabs-manual.js](https://github.com/mdn/learning-area/blob/main/css/css-layout/practical-positioning-examples/tabs-manual.js). Save these somewhere sensible on your local computer, and open `tabbed-info-box-start.html` in your text editor. Let's look at the HTML contained within the body: ```html
- +
+ + + + +
+
-
+

The first tab

-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque turpis nibh, porttitor nec venenatis eu, pulvinar in augue. Vestibulum @@ -60,23 +82,22 @@ To start with, we'd like you to make a local copy of the starting HTML file — urna. Nulla facilisi.

-
-

The second tab

+
+

The second tab

This tab hasn't got any Lorem Ipsum in it. But the content isn't very exciting all the same.

-
-

The third tab

+
+

The third tab

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque turpis nibh, porttitor nec venenatis eu, pulvinar in augue. And now an ordered list: how exciting!

-
  1. dui neque eleifend lorem, a auctor libero turpis at sem.
  2. Aliquam ut porttitor urna.
  3. @@ -87,9 +108,9 @@ To start with, we'd like you to make a local copy of the starting HTML file —
``` -So here we've got a {{htmlelement("section")}} element with a `class` of `info-box`, which contains a {{htmlelement("ul")}} and a {{htmlelement("div")}}. The unordered list contains three list items with links inside, which will become the actual tabs to click on for displaying our content panels. The `div` contains three {{htmlelement("article")}} elements, which will make up the content panels that correspond to each tab. Each panel contains some sample content. +So here we've got a {{htmlelement("section")}} element with a `class` of `info-box`, which contains two {{htmlelement("div")}}s. The first div contains three buttons, which will become the actual tabs to click on for displaying our content panels. The second div contains three {{htmlelement("article")}} elements, which will make up the content panels that correspond to each tab. Each panel contains some sample content. -The idea here is that we will style the tabs to look like a standard horizontal navigation menu, and style the panels to sit on top of one another using absolute positioning. We'll also give you a bit of JavaScript to include on your page to display the corresponding panel when a tab is pressed, and style the tab itself. You won't need to understand the JavaScript itself at this stage, but you should think about learning some basic [JavaScript](/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics) as soon as possible — the more complex your UI features become, the more likely it is that you'll need some JavaScript to implement your desired functionality. +The idea here is that we will style the tabs to look like a standard horizontal navigation menu and style the panels to sit on top of one another using absolute positioning. We'll also give you a bit of JavaScript to include on your page to display the corresponding panel when a tab is pressed, and style the tab itself. You won't need to understand the JavaScript code itself at this stage, but you should think about learning some basic [JavaScript](/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics) as soon as possible — the more complex your UI features become, the more likely it is that you'll need some JavaScript to implement your desired functionality. ### General setup @@ -115,60 +136,57 @@ Next, add the following just below your previous CSS: ```css .info-box { - width: 450px; + width: 452px; height: 400px; - margin: 0 auto; + margin: 1.25rem auto 0; } ``` -This sets a specific width and height on the content, and centers it on the screen using the old `margin: 0 auto` trick. Previously in the course we advised against setting a fixed height on content containers if at all possible; it is OK in this circumstance because we have fixed content in our tabs. It also looks a bit jarring to have different tabs at different heights. +This sets a specific width and height on the content and centers it on the screen using the old `margin: 1.25rem auto 0`. Previously in the course, we advised against setting a fixed height on content containers if at all possible; it is OK in this circumstance because we have fixed content in our tabs. ### Styling our tabs -Now we want to style tabs to look like tabs — basically, these are a horizontal navigation menu, but instead of loading different web pages when they are clicked on like we've seen previously in the course, they cause different panels to be displayed on the same page. First, add the following rule at the bottom of your CSS to remove the default {{cssxref("padding-left")}} and {{cssxref("margin-top")}} from the unordered list: +Now we want to style tabs to look like tabs — basically, these are a horizontal navigation menu, but instead of loading different web pages when they are clicked on like we've seen previously in the course, they cause different panels to be displayed on the same page. First, add the following rule at the bottom of your CSS to make the `tablist` a {{cssxref("flex")}} container and have it span 100% width: ```css -.info-box ul { - padding-left: 0; - margin-top: 0; +.info-box [role="tablist"] { + min-width: 100%; + display: flex; } ``` > **Note:** We are using descendant selectors with `.info-box` at the start of the chain throughout this example — this is so that we can insert this feature into a page with other content already on it, without fear of interfering with the styles applied to other parts of the page. -Next, we'll style the horizontal tabs — the list items are all floated left to make them sit in a line together, their {{cssxref("list-style-type")}} is set to `none` to get rid of the bullets, and their {{cssxref("width")}} is set to `150px` so they will comfortably fit across the info-box. The {{htmlelement("a")}} elements are set to {{cssxref("display")}} inline-block so they will sit in a line but still be stylable, and they are styled appropriately for tab buttons, using a variety of other properties. - -Add the following CSS: +Next, we'll style the buttons to look like tabs. Add the following CSS: ```css -.info-box li { - float: left; - list-style-type: none; - width: 150px; -} - -.info-box li a { - display: inline-block; - text-decoration: none; - width: 100%; - line-height: 3; - background-color: red; - color: black; - text-align: center; +.info-box [role="tab"] { + padding: 0 1rem 0 1rem; + line-height: 3rem; + background: white; + color: #b60000; + font-weight: bold; + border: none; + outline: none; } ``` -Finally for this section we'll set some styles on the link states. First, we'll set the `:focus` and `:hover` states of the tabs to look different when they are focused/hovered, providing users with some visual feedback. Secondly, we'll set a rule that puts the same styling on one of the tabs when a `class` of `active-tab` is present on it. We will set this using JavaScript when a tab is clicked on. Place the following CSS below your other styles: +Next, we'll set the `:focus` and `:hover` states of the tabs to look different when they are focused/hovered, providing users with some visual feedback. ```css -.info-box li a:focus, -.info-box li a:hover { - background-color: #a60000; - color: white; +.info-box [role="tab"]:focus span, +.info-box [role="tab"]:hover span { + outline: 1px solid blue; + outline-offset: 6px; + border-radius: 4px; } +``` -.info-box li a.active-tab { - background-color: #a60000; +Then we'll set a rule that highlights one of the tabs when [`aria-selected`](/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-selected) property is set to `true` on it. We will set this using JavaScript when a tab is clicked on. Place the following CSS below your other styles: + +```css +.info-box [role="tab"][aria-selected="true"] { + background-color: #b60000; color: white; } ``` @@ -182,76 +200,56 @@ First, of all, add the following rule to style the `.panels` {{htmlelement("div" ```css .info-box .panels { height: 352px; - position: relative; clear: both; + position: relative; } ``` -Finally for this section, we will style the individual {{htmlelement("article")}} elements that comprise our panels. The first rule we'll add will absolutely {{cssxref("position")}} the panels, and make them all sit flush to the {{cssxref("top")}} and {{cssxref("left")}} of their {{htmlelement("div")}} container — this part is absolutely key to this whole layout feature, as it makes the panels sit on top of one another. The rule also gives the panels the same set height as the container, and gives the content some padding, a text {{cssxref("color")}}, and a {{cssxref("background-color")}}. - -The second rule we'll add here makes it so that a panel with a `class` of `active-panel` set on it will have a {{cssxref("z-index")}} of 1 applied to it, which will make it sit above the other panels (positioned elements have a `z-index` of 0 by default, which would put them below). Again, we'll add this class using JavaScript at the appropriate time. +Finally for this section, we will style the individual {{htmlelement("article")}} elements that comprise our panels. The first rule we'll add will absolutely {{cssxref("position")}} the panels, and make them all sit flush to the {{cssxref("top")}} and {{cssxref("left")}} of their {{htmlelement("div")}} container — this part is key to this whole layout feature, as it makes the panels sit on top of one another. The rule also gives the panels the same set height as the container, and gives the content some padding, a text {{cssxref("color")}}, and a {{cssxref("background-color")}}. ```css -.info-box article { +.info-box [role="tabpanel"] { + background-color: #b60000; + color: white; position: absolute; + padding: 0.8rem 1.2rem; + height: 352px; top: 0; left: 0; - height: 352px; - padding: 10px; - color: white; - background-color: #a60000; -} - -.info-box .active-panel { - z-index: 1; } ``` -### Adding our JavaScript +The second rule we'll add here makes it so that a panel with a class of `is-hidden` set on it will be hidden. Again, we'll add/remove this class using JavaScript at the appropriate time. When a tab is selected the corresponding panel will have its `is-hidden` class removed and all other panels will have `is-hidden` class set, thus only one panel will be visible at a time. -The final step to getting this feature working is to add some JavaScript. Put the following block of code, exactly as written in between your opening and closing {{htmlelement("script")}} tags (you'll find these below the HTML content): - -```js -const tabs = document.querySelectorAll(".info-box li a"); -const panels = document.querySelectorAll(".info-box article"); - -for (let i = 0; i < tabs.length; i++) { - setTabHandler(tabs[i], i); +```css +.info-box [role="tabpanel"].is-hidden { + display: none; } +``` -function setTabHandler(tab, tabPos) { - tab.onclick = () => { - for (const tab of tabs) { - tab.className = ""; - } - - tab.className = "active-tab"; +### JavaScript - for (const panel of panels) { - panel.className = ""; - } +The final part that makes this feature work is the JavaScript code. The `tabs-manual.js` file has been included using the [` ``` This code does the following: -- First we save a reference to all the tabs and all the panels in two variables called `tabs` and `panels`, so we can easily do things to them later on. -- Then we use a `for` loop to cycle through all the tabs and run a function called `setTabHandler()` on each one, which sets up the functionality that should occur when each one is clicked on. When run, the function is passed a reference to the particular tab it is being run for, and an index number `i` that identifies the tab's position in the `tabs` array. -- In the `setTabHandler()` function, the tab has an `onclick` event handler set on it, so that when the tab is clicked, the following occurs: +- On [window load event](/en-US/docs/Web/API/Window/load_event) it initializes `TabsManual` [class](/en-US/docs/Learn/JavaScript/Objects/Classes_in_JavaScript) for all the `tablist` elements. +- When a `TabsManual` object is created, in the constructor all the tab and panel references are collected in `tabs` and `tabpanels` variables, so we can easily do things to them later on. +- The constructor also registers [`click`](/en-US/docs/Web/API/Element/click_event) and [`keydown`](/en-US/docs/Web/API/Element/keydown_event) event handlers on all the tabs. The event handlers include logic about what should happen when a tab is selected using a click or keypress. +- In the `setSelectedTab(currentTab)` function, the following occurs: - - A `for` loop is used to cycle through all the tabs and remove any classes that are present on them. - - A `class` of `active-tab` is set on the tab that was clicked on — remember from earlier that this class has an associated rule in the CSS that sets the same {{cssxref("color")}} and {{cssxref("background-color")}} on the tab as the panels are styled with. - - A `for` loop is used to cycle through all the panels and remove any classes that are present on them. - - A class of `active-panel` is set on the panel that corresponds to the tab that was clicked on — remember from earlier that this class has an associated rule in the CSS that sets its {{cssxref("z-index")}} to 1, making it appear over the top of the other panels. + - A `for` loop is used to cycle through all the tabs and deselect them by setting `aria-selected` property to `false` and by setting `is-hidden` class on corresponding panels. + - On the selected tab (`currentTab`) the `aria-selected` is set to `true` and `is-hidden` class is removed from the corresponding panel. -That's it for the first example. Keep your code open, as we'll be adding to it in the second one. +- The code also has logic to support keyboard navigation using `Left arrow`, `Right arrow`, `Home`, and `End` keys. ## A fixed position tabbed info-box -In our second example, we will take our first example — our info-box — and add it into the context of a full web page. But not only that — we'll give it fixed position so that it stays in the same position in the browser window. When the main content scrolls, the info-box will stay in the same position on the screen. Our finished example will look like this: +In our second example, we will take our first example — our info-box — and add it into the context of a full web page. But not only that — we'll give it a fixed position so that it stays in the same position in the browser window. When the main content scrolls, the info-box will stay in the same position on the screen. Our finished example will look like this: ![Info-box is a container with 3 tabs with the first tab selected and only the contents of the first tab are displayed. It is given a fixed position. The info-box is positioned at the top left corner of the window with a width of 452 pixels. A container of fake content occupies the rest right half of the window; the fake content container is taller than the window and is scrollable. When the page is scrolled, the right-hand side container moves while the info-box stays fixed in the same position on the screen. ](fixed-info-box.png) @@ -261,7 +259,7 @@ As a starting point, you can use your completed example from the first section o ### HTML additions -First of all, we need some additional HTML to represent the website main content. Add the following {{htmlelement("section")}} just below your opening {{htmlelement("body")}} tag, just before the existing section: +First of all, we need some additional HTML to represent the webpage's main content. Add the following {{htmlelement("section")}} just below your opening {{htmlelement("body")}} tag, just before the existing section: ```html
@@ -303,8 +301,9 @@ It should now look like this: ```css .info-box { - width: 450px; + width: 452px; height: 400px; + margin: 0 auto; position: fixed; top: 0; } @@ -322,6 +321,10 @@ The only thing left for this example is to provide the main content with some st height: 2000px; margin-left: 470px; } + +.fake-content p { + margin-bottom: 200px; +} ``` To start with, we give the content the same {{cssxref("background-color")}}, {{cssxref("color")}}, and {{cssxref("padding")}} as the info-box panels. We then give it a large {{cssxref("margin-left")}} to move it over to the right, making space for the info-box to sit in, so it is not overlapping anything else. @@ -330,7 +333,7 @@ This marks the end of the second example; we hope you'll find the third just as ## A sliding hidden panel -The final example we'll present here is a panel that slides on and off the screen at the press of an icon — as mentioned earlier, this is popular for situations like mobile layouts, where the available screen spaces is small, so you don't want to use up most of it by showing a menu or info panel instead of the useful content. +The final example we'll present here is a panel that slides on and off the screen at the press of an icon — as mentioned earlier, this is popular for situations like mobile layouts, where the available screen space is small, so you don't want to use up most of it by showing a menu or info panel instead of the useful content. Our finished example will look like this: @@ -341,58 +344,54 @@ Our finished example will look like this: As a starting point, make a local copy of [hidden-info-panel-start.html](https://github.com/mdn/learning-area/blob/main/css/css-layout/practical-positioning-examples/hidden-info-panel-start.html) from our GitHub repo. This doesn't follow on from the previous example, so a fresh start file is required. Let's have a look at the HTML in the file: ```html-nolint - - -