Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

HTML-CSS: List with floating section headings

HTML-CSS : Exercise-5 with Solution

Using HTML, CSS create a list with floating headings for each section.

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

HTML Code:

<!--License: https://bit.ly/3GjrtVF-->
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Using HTML, CSS create a list with floating headings for each section</title>
</head>
<body>
<strong>Preview:</strong>
<div class="container">
  <div class="floating-stack">
    <dl>
      <dt>A</dt>
      <dd>Aaban</dd>
      <dd>Aabel</dd>
	  <dd>Aabheer</dd>
	  <dd>Aadam</dd>

      <dt>B</dt>
      <dd>Baabul</dd>
      <dd>Baalaji</dd>
      <dd>Baalkrishan</dd>
      <dd>Baanke Bihaari</dd>

      <dt>C</dt>
      <dd>Caddam</dd>
      <dd>Cameroon</dd>
      <dd>Campbell</dd>
      <dd>Cane</dd>
     
      <dt>D</dt>
      <dd>Daanesh</dd>
	  <dd>Dadvar</dd>
	  <dd>Daghan</dd>
	  <dd>Daivya</dd>
      <dd>Daamini</dd>

      <dt>E</dt>
      <dd>Eadmer</dd>
      <dd>Earnest</dd>
      <dd>Eddward</dd>
      <dd>Edmond</dd>
      </dl>
  </div>
</div>
</body>
</html>

CSS Code:

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

.floating-stack {
  background: #3365a4;
  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;
}

</style>

HTML-CSS Editor:

See the Pen html-css-practical-exercises by w3resource (@w3resource) on CodePen.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



HTML-CSS: Tips of the Day

What is the difference between id and class in CSS, and when should I use them?

Example:

<div id="header_id" class="header_class">
Text
</div>
#header_id {font-color:#fff}
.header_class {font-color:#000}

(Note that CSS uses the prefix # for IDs and . for Classes.)

However color was an HTML 4.01 <font> tag attribute deprecated in HTML 5. In CSS there is no "font-color", the style is color so the above should read:

Example:

<div id="header_id" class="header_class">
Text
</div>
#header_id {color:#fff}
.header_class {color:#000} 

The text would be white.

Ref: https://bit.ly/39Stv4P