Once I needed to make tabs on the webpage. As it's a rather routine problem, I didn't want to reinvent the wheel, so I just googled a little bit to see how people solve it. All the solutions I found looked rather run of the mill, something like that:
As you can see, two independent lists are used, one containing the headers, and the other holding the content.
Actually, I don't like such arrangement, mainly because there is no semantic link between the header and the content. Though I'm not addicted to absolutely valid and semantic HTML just for the sake of respect of web geeks, this lack of a link was a drawback. Firstly, the data will be taken from the backend in two steps (first the headers, then the content), not in one; that's not much, but it adds to the volume of code and cannot be good for performance. Secondly, the client-side script for toggling tabs will be more voluminous and complicated, especially if there are several blocks with tabs on the webpage.
So I decided to try and fix those shortcomings, hopefully not overtaxing my brain. :) First of all, when selecting the element structure, I thought about the list, which is just perfect for the task. I guess I'm not the first web developer who makes tabs using that kind of list. .
Surely, you can just copy/paste the code, but I suggest that you first consider how this solution works.
Let's assume we have a set of elements for which the float: left attribute is set:
What happens if we set the float: right attribute for one of the elements?
As you can see, The result is good enough — the elements following are placed on the same line after the preceding ones. However, we need the tab content to be stretched to the whole width, so we set width: 100% for this element:
As expected, the element has broken the document flow. Let's think about it: if an element is snapped to the container's right edge, then changing the container's width will shift the element's left edge (i.e., increases from right to left). Therefore, we need to suppress the influence of the element's left edge, and that can be done by setting margin-left: -100%:
Now we only need to move the element down, which can be accomplished using the margin-top attribute:
That's all! To hide all elements by default, we set the display: none attribute for them, while setting display: block for the selected element. The jQuery-based tab-toggling script is very simple:
To post a comment, you must be registered and logged in. Login or Register
Welcome to Splashnology
Splashnology is an exciting new platform for a Web community dedicated to Web Technology, with a focus on Web Design, Web Development, Gadgets, Graphic Design, Arts, Blogging, SEO, and Freelance. This site's primary purpose is to share Web-related knowledge, resources, and ideas. Anyone — including you — can become an author at Splashnology and post things that might be of interest to our community in personal or general blogs. We love what we do!
We are looking for writers. If you are interested in writing exclusive articles for Splashnology please contact us. Also you can submit any article into your personal blog.
Comments (0)
RSS Collapse / ExpandTo post a comment, you must be registered and logged in. Login or Register