I’m looking to write / emulate a tab strip using HTML & CSS only.
I will progressively enhance this with JavaScript and do not need any help with that.
I’ve taken ideas from this page Example 6
Here is a live example
Please feel free to be pedantic about my current HTML and CSS markup. I’m looking to write this from scratch properly using HTML5 and CSS3 standards and am hoping to avoid my usual “If it works the quality of the markup doesn’t matter. jQuery will come along and fix it for me!” attitude.
Here is my HTML currently have a markup of :
<div class="tabs">
<section>
<a> link 1 </a>
<div> Content 1 </div>
</section>
<section>
<a> link 2 </a>
<div> Content 2 </div>
</section>
</div>
Here is the (stripped) CSS :
.tabs {
width: 100%;
}
.tabs > section > a {
position: relative;
display: block;
float: left;
}
.tabs > section {
display: inline;
}
.tabs > section:not(:target) > div {
position: absolute;
padding: 20px;
top: 50px;
display: block;
width: 100%;
}
There are 3 css issues that I am troubled with.
- The
div.tabsonly has floated and absolute children so does not have any “real content” and there is no border around it. How do I get it to have a proper border? - the
.tabs > section > divhas a width of 100% but does not match the 100% width of the parentdiv.tabsdue to the extra padding. My understanding of the box model is lacking here. How do I set it to have a width matching thediv.tabsparent? - How do I make the content
.tabs > section > divSit just underneath the<a>of the tab strip without setting the csstopvalue to a finicky"43px". What’s the proper way to do generic positioning in these situations?
Disclaimer I don’t care about proper browser support. Feel free to use HTML5 / CSS3. I’ll use JavaScript to enhance the IE8 functionality properly.
If it helps this is the full CSS
.tabs {
width: 100%;
border: #000 solid 1px;
}
.tabs > section > a {
position: relative;
display: block;
float: left;
padding-left: 10px;
margin-left: 5px;
margin-right: 5px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
border: 1px solid red;
}
.tabs > section {
display: inline;
}
.tabs > section > div {
z-index: -2;
background: white;
border: 1px solid blue;
}
.tabs > section:not(:target) > div {
position: absolute;
padding: 20px;
top: 50px;
display: block;
width: 100%;
}
I was able to get a working version of your tabs. Here is a jsFiddle version. (However, I wasn’t able to solve all your dilemmas. Read below.)
CSS:
HTML:
Addressing the three questions:
position: relative.section > divs aware of the anchor height.PS. I liked the use of :target and was able to get your content block to switch as well as have a default.