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

How can I show dots ("...") in a span with hidden overflow?

For this you can use text-overflow: ellipsis; property. Write like this

span {
    display: inline-block;
    width: 180px;
    white-space: nowrap;
    overflow: hidden !important;
    text-overflow: ellipsis;
}
<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book</span>

Ref: https://bit.ly/2RqEbdh