List With Floating Section Headings

CSSCSSBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the fundamentals of CSS programming and learn how to create beautiful and responsive web layouts. Through hands-on exercises, we will cover topics such as CSS selectors, box model, and positioning, as well as advanced techniques like flexbox and grid. By the end of this lab, you will have the skills and knowledge to create stunning websites that look great on any device.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL css(("`CSS`")) -.-> css/BasicConceptsGroup(["`Basic Concepts`"]) css(("`CSS`")) -.-> css/BasicStylingGroup(["`Basic Styling`"]) css(("`CSS`")) -.-> css/CoreLayoutGroup(["`Core Layout`"]) css(("`CSS`")) -.-> css/AdvancedLayoutGroup(["`Advanced Layout`"]) css(("`CSS`")) -.-> css/IntermediateStylingGroup(["`Intermediate Styling`"]) css(("`CSS`")) -.-> css/ResponsiveandAdaptiveDesignGroup(["`Responsive and Adaptive Design`"]) css(("`CSS`")) -.-> css/CSSPreprocessorsGroup(["`CSS Preprocessors`"]) css/BasicConceptsGroup -.-> css/selectors("`Selectors`") css/BasicStylingGroup -.-> css/colors("`Colors`") css/BasicStylingGroup -.-> css/fonts("`Fonts`") css/CoreLayoutGroup -.-> css/box_model("`Box Model`") css/CoreLayoutGroup -.-> css/margin_and_padding("`Margin and Padding`") css/CoreLayoutGroup -.-> css/borders("`Borders`") css/CoreLayoutGroup -.-> css/width_and_height("`Width and Height`") css/CoreLayoutGroup -.-> css/display_property("`Display Property`") css/CoreLayoutGroup -.-> css/positioning("`Positioning`") css/AdvancedLayoutGroup -.-> css/flexbox("`Flexbox`") css/AdvancedLayoutGroup -.-> css/grid_layout("`Grid Layout`") css/IntermediateStylingGroup -.-> css/backgrounds("`Backgrounds`") css/ResponsiveandAdaptiveDesignGroup -.-> css/mobile_first_design("`Mobile First Design`") css/CSSPreprocessorsGroup -.-> css/nesting("`Nesting`") css/IntermediateStylingGroup -.-> css/pseudo_classes("`Pseudo-classes`") subgraph Lab Skills css/selectors -.-> lab-35199{{"`List With Floating Section Headings`"}} css/colors -.-> lab-35199{{"`List With Floating Section Headings`"}} css/fonts -.-> lab-35199{{"`List With Floating Section Headings`"}} css/box_model -.-> lab-35199{{"`List With Floating Section Headings`"}} css/margin_and_padding -.-> lab-35199{{"`List With Floating Section Headings`"}} css/borders -.-> lab-35199{{"`List With Floating Section Headings`"}} css/width_and_height -.-> lab-35199{{"`List With Floating Section Headings`"}} css/display_property -.-> lab-35199{{"`List With Floating Section Headings`"}} css/positioning -.-> lab-35199{{"`List With Floating Section Headings`"}} css/flexbox -.-> lab-35199{{"`List With Floating Section Headings`"}} css/grid_layout -.-> lab-35199{{"`List With Floating Section Headings`"}} css/backgrounds -.-> lab-35199{{"`List With Floating Section Headings`"}} css/mobile_first_design -.-> lab-35199{{"`List With Floating Section Headings`"}} css/nesting -.-> lab-35199{{"`List With Floating Section Headings`"}} css/pseudo_classes -.-> lab-35199{{"`List With Floating Section Headings`"}} end

List With Floating Section Headings

index.html and style.css have already been provided in the VM.

To create a list with floating headings for each section, follow these steps:

  1. Apply overflow-y: auto to the list container to allow for vertical overflow.
  2. Use display: grid on the inner container (<dl>) to create a layout with two columns.
  3. Set headings (<dt>) to grid-column: 1 and content (<dd>) to grid-column: 2.
  4. Finally, apply position: sticky and top: 0.5rem to headings to create a floating effect.

Here is the HTML code:

<div class="container">
  <div class="floating-stack">
    <dl>
      <dt>A</dt>
      <dd>Algeria</dd>
      <dd>Angola</dd>

      <dt>B</dt>
      <dd>Benin</dd>
      <dd>Botswana</dd>
      <dd>Burkina Faso</dd>
      <dd>Burundi</dd>

      <dt>C</dt>
      <dd>Cabo Verde</dd>
      <dd>Cameroon</dd>
      <dd>Central African Republic</dd>
      <dd>Chad</dd>
      <dd>Comoros</dd>
      <dd>Congo, Democratic Republic of the</dd>
      <dd>Congo, Republic of the</dd>
      <dd>Cote d'Ivoire</dd>

      <dt>D</dt>
      <dd>Djibouti</dd>

      <dt>E</dt>
      <dd>Egypt</dd>
      <dd>Equatorial Guinea</dd>
      <dd>Eritrea</dd>
      <dd>Eswatini (formerly Swaziland)</dd>
      <dd>Ethiopia</dd>
    </dl>
  </div>
</div>

And here is the CSS code:

.container {
  display: grid;
  place-items: center;
  min-height: 400px;
}

.floating-stack {
  background: #455a64;
  color: #fff;
  height: 80vh;
  width: 320px;
  border-radius: 1rem;
  overflow-y: auto;
}

.floating-stack > dl {
  margin: 0 0 1rem;
  display: grid;
  grid-template-columns: 2.5rem 1fr;
  align-items: center;
}

.floating-stack dt {
  position: sticky;
  top: 0.5rem;
  left: 0.5rem;
  font-weight: bold;
  background: #263238;
  color: #cfd8dc;
  height: 2rem;
  width: 2rem;
  border-radius: 50%;
  padding: 0.25rem 1rem;
  grid-column: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.floating-stack dd {
  grid-column: 2;
  margin: 0;
  padding: 0.75rem;
}

.floating-stack > dl:first-of-type > dd:first-of-type {
  margin-top: 0.25rem;
}

Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.

Summary

Congratulations! You have completed the List With Floating Section Headings lab. You can practice more labs in LabEx to improve your skills.

Other CSS Tutorials you may like