Magic tabs that resize themselves according to their container's width. Tab widths default to a percentage value, which is set automatically in CSS by counting child elements. If a tab's content overflows its available width, then the tabs adjust their widths to fit. If they can't fit on the screen then the tabs start to scroll horizontally.
Created
June 3, 2016 09:58
-
-
Save edwinwright/2b12a29f33005948c510cc61d33c7fe5 to your computer and use it in GitHub Desktop.
Responsive Scrolling Tabs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<div class="tablist"> | |
<ul class="inner"> | |
<li class="tablist__tab">Tab 1</li> | |
<li class="tablist__tab">Tab 2</li> | |
</ul> | |
</div> | |
<div class="tablist" role="tablist"> | |
<ul class="inner"> | |
<li class="tablist__tab">Lorem ipsum dolor sit</li> | |
<li class="tablist__tab">amet</li> | |
</ul> | |
</div> | |
<div class="tablist" role="tablist"> | |
<ul class="inner"> | |
<li class="tablist__tab">Tab 1</li> | |
<li class="tablist__tab">Tab 2</li> | |
<li class="tablist__tab">Tab 3</li> | |
<li class="tablist__tab">Tab 4</li> | |
<li class="tablist__tab">Tab 5</li> | |
</ul> | |
</div> | |
<div class="tablist" role="tablist"> | |
<ul class="inner"> | |
<li class="tablist__tab">Lorem</li> | |
<li class="tablist__tab">ipsum dolor</li> | |
<li class="tablist__tab">sit</li> | |
<li class="tablist__tab">amet consetetur</li> | |
<li class="tablist__tab">sadipscing</li> | |
<li class="tablist__tab">elitr</li> | |
</ul> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.container { | |
width: 30%; | |
margin: auto; | |
} | |
.tablist { | |
margin: 1rem 0; | |
padding: 0; | |
overflow-x: scroll; | |
> .inner { | |
display: table; | |
width: 100%; | |
margin: 0; | |
padding: 0; | |
} | |
@for $i from 1 through 5 { | |
&__tab:first-child:nth-last-child(#{$i}), | |
&__tab:first-child:nth-last-child(#{$i}) ~ &__tab { | |
width: (100% / $i); | |
} | |
} | |
&__tab { | |
box-sizing: border-box; | |
display: table-cell; | |
margin: 0; | |
padding: 15px 20px 12px; | |
font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; | |
text-align: center; | |
line-height: 1; | |
white-space: nowrap; | |
color: #fff; | |
background-color: #253038; | |
cursor: pointer; | |
&:hover { | |
background-color: #444; | |
box-shadow: inset 0 3px 0 0 #db0011; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment