Vaishal
Vaishal

Reputation: 197

Flexbox table header left alignment broken with scrollbar

I am creating a table using flexbox. It has fixed headers and scrollable body. On Windows, When scrollbar is added into body using overflow-y: auto, left alignment gets broken between header and rows. Because on windows, scrollbar is taking some extra space from body but header is still getting same space. Is there any way I can make header width compress when scrollbar is added into body?

I want to make sure width of header and body is same when scrollbar is present and even when it is not.

    &__item, &--header__item {
      position: relative;
      flex-basis: 20%;
      flex-grow: 1;
      overflow: hidden;
      text-overflow: ellipsis;
      &.sm, &.lg {
        flex-grow: 0;
      }
      &.sm {
        flex-basis: 40px;
      }
    }
<!-- Header HTML -->

    <ul class="table-list-container">
      <li class="table-list-row">
        <ul class="table-list header">
          <li class="table-list&#45;&#45;header__item sm text-center">
          </li>
          <li class="table-list&#45;&#45;header__item md-padding-left-10">
            Header 1
          </li>
          <li class="table-list&#45;&#45;header__item">
            Header 2
          </li>
          <li class="table-list&#45;&#45;header__item">
            Header 3
          </li>
          <li class="table-list&#45;&#45;header__item text-center">
            Header 4
          </li>
        </ul>
      </li>
    </ul>

<!-- Body HTML -->

    <div style="height: 305px" class="scrollable">
      <ul class="table-list-container">
        <li class="table-list-row">
          <ul class="table-list body">
            <li class="table-list__item sm text-center">
              <input type="checkbox">
            </li>
            <li class="table-list__item md-padding-left-10">Data</li>
            <li class="table-list__item">Data</li>
            <li class="table-list__item">Data</li>
            <li class="table-list__item text-center">Data</li>
          </ul>
        </li>
      </ul>
    </div>

Upvotes: 0

Views: 786

Answers (2)

Vaishal
Vaishal

Reputation: 197

overflow-y: overlay;

worked for me. Since now scroll bar will take space from existing table space and not extra space, column headers and rows are perfectly aligned.

Caution: You need to make sure your last column have enough space to show its content after scrollbar is added because from that's where scrollbar is getting its space.

Upvotes: 3

Syam Pillai
Syam Pillai

Reputation: 5217

Here is a working Demo

body{
	margin: 0;
}
.table{
	display: flex;
	flex-wrap: wrap;
}
ul{
	display: flex;
	width: 100%;
	list-style: none;
	padding: 0;
	margin: 0;
}
li{
	flex: 5;
	/* width: 15%; */
	padding: 10px;
}
.scrollable{
	width: 100%;
	margin-top: 80px;
}
li.empty{
	/* max-width: 12px; */
	flex:1;
}
.header{
	position: fixed;
	height: 80px;
	z-index: 5;
	background: red;
}
<div class="table">

	<ul class="table-list-container header">
		<li class="table-list-row">
			<ul class="table-list header">
				<li class="table-list&#45;&#45;header__item sm text-center empty">
				</li>
				<li class="table-list&#45;&#45;header__item md-padding-left-10">
					Header 1
				</li>
				<li class="table-list&#45;&#45;header__item">
					Header 2
				</li>
				<li class="table-list&#45;&#45;header__item">
					Header 3
				</li>
				<li class="table-list&#45;&#45;header__item text-center">
					Header 4
				</li>
			</ul>
		</li>
	</ul>

	<div style="height: 305px" class="scrollable">
		<ul class="table-list-container"> 
			<li class="table-list-row">
				<ul class="table-list body">
					<li class="table-list__item sm text-center empty">
						<input type="checkbox">
					</li>
					<li class="table-list__item md-padding-left-10">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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li>
					<li class="table-list__item">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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li>
					<li class="table-list__item">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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li>
					<li class="table-list__item text-center">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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li>
				</ul>
			</li>
		</ul>
	</div>
</div>

Upvotes: 1

Related Questions