Home > Web Front-end > CSS Tutorial > Can a Second Horizontal Scrollbar Be Created at the Top of a Table Using Only HTML and CSS?

Can a Second Horizontal Scrollbar Be Created at the Top of a Table Using Only HTML and CSS?

Patricia Arquette
Release: 2024-12-18 22:43:11
Original
663 people have browsed it

Can a Second Horizontal Scrollbar Be Created at the Top of a Table Using Only HTML and CSS?

Mimicking a Second Horizontal Scrollbar at the Top of a Table

Problem:

Enhancing a massive table with a horizontal scrollbar at the bottom is desired. However, replicating this scrollbar at the top is also sought. Is it feasible to accomplish this purely with HTML and CSS?

Solution:

Utilizing a "dummy" div simulates a second horizontal scrollbar above an element. Positioned directly atop the primary element, this dummy div is sized sufficiently to accommodate a scrollbar. By attaching event handlers to both the dummy and primary elements, synchronization is ensured when either scrollbar is used. The dummy div replicates the appearance and functionality of a second top scrollbar.

Live Example:

Explore this fiddle for a practical demonstration.

Code:

HTML:

<div class="wrapper1">
  <div class="div1"></div>
</div>
<div class="wrapper2">
<div class="div2">
  <!-- Content Here -->
</div>
</div>
Copy after login

CSS:

.wrapper1, .wrapper2 {
  width: 300px;
  overflow-x: scroll;
  overflow-y:hidden;
}

.wrapper1 {height: 20px; }
.wrapper2 {height: 200px; }

.div1 {
  width:1000px;
  height: 20px;
}

.div2 {
  width:1000px;
  height: 200px;
  background-color: #88FF88;
  overflow: auto;
}
Copy after login

JS:

$(function(){
  $(".wrapper1").scroll(function(){
    $(".wrapper2").scrollLeft($(".wrapper1").scrollLeft());
  });
  $(".wrapper2").scroll(function(){
    $(".wrapper1").scrollLeft($(".wrapper2").scrollLeft());
  });
});
Copy after login

The above is the detailed content of Can a Second Horizontal Scrollbar Be Created at the Top of a Table Using Only HTML and CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template