ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Tutorial: How to Make Tabbed Panel Using JavaScript, CSS and HTML

Updated on May 15, 2018

Here, I'm presenting a simplest method to create a tabbed panel with just only 3 lines of JavaScript, Little bit HTML and with an easily understandable CSS. I tried many times and with many languages but I was not cleared with basic concepts to understand this. You can also assume me as a beginner in JavaScript. So, Here is the Javascript code of which I'm talking about.

You should put this script in between the '<head>' tag. We are defining a function with name 'change' which accepts an argument from caller and storing it in variable named as 'namee', after that we are replacing or I say changing the innerHTML (content) of div block (on which content is to be displayed) by source (content to be displayed after on clicking tab). If you were still unsure about what is going on here, then I can only refer you to DHTML Tutorial section of W3school's website.

<script type="text/javascript">

var namee;
function change(namee){
document.getElementById("Display_content").innerHTML=document.getElementById(namee).innerHTML;

//optional Javascript code below - To change color of clicked tab
var livetab, i;
switch(namee){
	case 'tab1':
		livetab='t1';
	break;
	case 'tab2':
		livetab='t2';
	break;
	case 'tab3':
		livetab='t3';
	break;
}
for(i=1;i<=3;i++){
	document.getElementById("t"+i).style.backgroundColor="gray";
}
document.getElementById(livetab).style.backgroundColor="silver";
//optional code ends here

}

</script>

Below is the CSS code that does the work of giving a proper look to our tab Menu and panel, Change this as per your convinience and it's a very simple process just look at attributes of HTML Tags and match them with the main keywords (near to curly brackets) in the CSS code below, Explained in detail at the end of article.

<style type="text/css" rel="stylesheet">
div.content_tab {
	display:none;
}
#tabbed_section {
	border: 2px solid gray;
	width: 300px; /* width of tabbed panel */
	}
#menu {
	clear: both;
	width: 300px;
}
#Display_content {
	clear: both;
	min-height: 44px;
	padding: 2px;
	border: 1px solid gray;
}
#ul_tabs {
	clear: both;
	padding: 0px;
	margin: 0px;
}
.lh {
	padding:5px;
	background-color: gray;
	list-style: none;
	width: 88px;
	height: 25px;
	float: left;
	border: 1px solid #FFFFFA;
	text-align:center;
}
li.lh:hover {
	background-color: white;
	cursor: pointer;
}

</style>

In the Above CSS,

  • The name starting with # (Hash) are referring to 'id' attributes of various tags used in HTML code below,
  • Name starting with '.' (dot) symbol are referring to Class attribute of various tags, classes are used to change look and design of more the one element ( tags ),
  • 'div.content {}' is referring to all div blocks whose class name is '.content' and we are setting their display property to none to restrict it from acquiring extra space on webpage,
  • ┬áin '.lh {}', we are setting 'list-style' property to none to remove the dot mark (symbol) from beginning and property named 'float' is doing the work of aligning each list element in row.

Now, Below is the HTML Code to display data on web page, here we are using "ul" - Unordered List, "li" - List and Div tag plus it's attributes. Proper formatting is done for ease to understand

<div id="tabbed_section">
  <div id="menu">
    <ul id="ul_tabs">
      <li class="lh" id="t1" onClick="change('tab1')">
    Head1
      </li>
      <li class="lh" id="t2" onClick="change('tab2')">
    Head2
      </li>
      <li class="lh" id="t3" onClick="change('tab3')">
    Head3
      </li>
    </ul>
  </div>
  <div id="Display_content" onload="change('h1')">
  </div>
    <div id="tab1" class="content_tab">
	content in tab1111
    </div>
    <div id="tab2" class="content_tab">
	content in tab2222
    </div>
    <div id="tab3" class="content_tab">
	content in tab3333
    </div>
</div>
Tabbed Menu Panel
Tabbed Menu Panel

In HTML code above,

  1. First, We are creating a div block in which tabbed panel will be displayed,
  2. Then, we are creating 'Tab Menu' by using Unordered list whose look is changed into tab style by CSS,
  3. Then, we creating another div block (Blank content) inside the main tabbed panel div block where data will be displayed after clicking on any one tab, as a default it will display Null data (Means no content).
  4. Then, we are creating three more div blocks just below the div block created in step 3 to store information which will be displayed after clicking a relative tab with the help of 'Id' attribute of this div block, These blocks are set to hidden by css and will be only visible after any event/trigger occurs to call them.

Comments

    0 of 8192 characters used
    Post Comment

    • itech profile imageAUTHOR

      Krishna 

      3 years ago from Dausa, India

      Yep! It is possible. You just have to brainstorm a little bit and try & test the things.

    • profile image

      Tom 

      3 years ago

      Although I can gte the tabs to work, I can't gte forms to work within the tabbed panels. Is it impossible?

    • profile image

      marcos 

      5 years ago

      Can I put two sets of these tabs separately in the same page? for example have tab1 and tab2 changing content between each other, and then have tab3 and tab4 doing the same thing separately.

    • itech profile imageAUTHOR

      Krishna 

      7 years ago from Dausa, India

      @lloyd, You're always welcome.

    • profile image

      lloyd 

      7 years ago

      Forget the previous post, I missed the addition of the id="t1" just before the onclick event. Duh...

      This now works great!!!

      Thanks for your quick response.

      Lloyd

    • profile image

      lloyd 

      7 years ago

      I have copy and pasted the optional javascript into my html file, but with no affect. It is part of the change function and the original change(namee) still works just like before. I even modified the single quote to double quote on the 'tab1' and 't1' but still no difference. The following is what I have:

      //optional - To change color of clicked tab

      var livetab, i;

      switch(namee){

      case 'tab1': livetab='t1'; break;

      case 'tab2': livetab='t2'; break;

      case 'tab3': livetab='t3'; break;

      }

      for(i=1;i

    • itech profile imageAUTHOR

      Krishna 

      7 years ago from Dausa, India

      Thanks for commenting lloyd... I Did some changes in code snippets of javascript and html shown above... as per your requirements... Very easy to understand.

      You can try it now.

      Glad to see you here on my article.

    • profile image

      lloyd 

      7 years ago

      Would like to change the tab background-color when the function change(namee) is invoked. I have tried various CSS with no luck, but logically it should be a part of the change function I would think. I am new to JavaScript so any help would be much appreciated.

      I have googled this and found a link that uses over 250 lines of javascript, which is over-whelming. Yours, only uses less than 10 lines.

      Thanks in advance.

      Lloyd

    • profile image

      delphi 

      7 years ago

      Can u just give me where i should put ''

    • profile image

      delphi 

      7 years ago

      Can u just give me where i should put ''

    • itech profile imageAUTHOR

      Krishna 

      7 years ago from Dausa, India

      Well, this is just the simple method to make u learn how tabbed panel work and as per your question you can do that by inserting this code in the starting body tag i.e like this '&lt;body onload="change('tab1')"&gt;'

    • profile image

      guest 

      7 years ago

      is there any way to have the first content displayed as default?

    • profile image

      guest 

      7 years ago

      thanks!

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)