What is the method for setting the default tab using the target attribute?

I'm in the process of creating a basic tabbed interface, but I've hit a roadblock. Everything is functioning correctly, except for setting the default tab on page load.

What I would like to achieve is for tab-1 to be visible when the page loads. Is there a way to accomplish this using only CSS, without relying on jQuery?

Below is the code snippet:

<div class='tabs'>
    <ul class='horizontal'>
        <li><a href="#tab-1">General</a></li>
        <li><a href="#tab-2">Showcase Box</a></li>
    </ul>

    <div id='tab-1' class="tab"> Tab 1 Content Here </div>                  
    <div id='tab-2' class="tab"> Tab 2 Content Here </div>

CSS

.tabs div:not(:target) { 
      display: none; 
}

.tabs div:target { 
    display: block; 
}

Answer №1

Using CSS alone, it may not be possible to achieve this functionality but with JavaScript, you can check if a hash is specified using window.location.hash. If no hash is specified, it will automatically redirect you to #tab-1.

Give this a try:


    <style>
    .tabs div:not(:target) { display:none; }
    .tabs div:target { display:block; } 
    </style>

    <div class='tabs'>
        <ul class='horizontal'>
            <li><a href="#tab-1">General</a></li>
            <li><a href="#tab-2">Showcase Box</a></li>
        </ul>
        <div id='tab-1' class="tab"> Tab1  Content here </div>                  
        <div id='tab-2' class="tab"> Tab 2 Content here </div>
    </div>

    <script>
    (function(){
        if (window.location.hash) {
            window.location.hash;
        } else {
            window.location.hash = '#tab-1';
        };
    })();
    </script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm not sure why the CSS attribute that restricts character numbers exists

https://i.sstatic.net/dkRt3.png The Divtag appears to be malfunctioning due to insufficient properties. I am currently unsure of how to resolve this issue and would appreciate any insights or opinions. <div class="col boardsBox" style="margin-right:0. ...

Applying a CSS class to a jeditable input field

Is there a way to apply a simple class tag to a jeditable input field? The developer's instructions on their website () suggest using the following code snippet under "How to style elements?": $('.editable').editable('http://www.exampl ...

"Learn how to position a div element below the header div and above the footer div while maintaining full height in

Recently delving into the world of reactjs, I find myself facing a challenge with a page that contains 3 distinct blocks formed by divs. Have a look at the layout on my page: My Page This is the code snippet I have worked on: return ( <div> ...

Expanding a non-bootstrap navigation bar upon clicking the menu

I'm having some difficulty getting my navigation menu to show up when I click the navigation menu icon. HTML nav role="navigation" class="navbar"> <div class="nav-header"> <a href="#"><span style="font-family: 'Cab ...

Ways to clear a placeholder each time you leave a search box event

I have implemented an overlay search box with the placeholder "Sök," and I want the text entered by the user to be removed and the placeholder to be reset if the user exits the search box by clicking the 'x' in the upper right corner. How can I ...

jquery method to make entire navigation bar clickable

I have a single menu bar. I recently discovered an interesting way to make the entire menu bar area clickable using jQuery. Design code snippet: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MenuControl.ascx.cs" Inherits="MenuControl"%> ...

The concept of Border-Merging within Material Design Tables

Is there a way to implement the following border style in a Material Design Table: border-collapse: collapse; I tried adding a border to .mat-cell: .mat-cell { border: 1px solid; } However, in some instances, the border appears to be 2px. The reas ...

Troubleshooting Problem with CSS Gradient Text

I'm attempting to recreate the text effect found here: . However, I'm facing issues getting it to work despite using the same code. This is the HTML I am using: <h1><span></span>Sign Up</h1> My CSS looks like this: h1 { ...

Issue with Color Attribute in Heading within an Ionic Content Element

The color attribute doesn't seem to be working in Ionic 2. It works fine with normal Ionic tags, but not with HTML tags. Can someone help me with this? The code is provided below. <ion-content> <div class="page-home"> <h1 color="second ...

What is the best way to extend the lower row items in a flex box to accommodate additional content when clicked?

My latest project involved designing an HTML flex-box to showcase images retrieved from an API. Here is a sneak peek of the layout: [![Preview of the flex-box layout][1]][1] However, I am now seeking to enhance user experience by adding functionality tha ...

The functionality of onClick or onChange seems to be malfunctioning in the preline user interface select component

I recently integrated the preline UI library into my React project to enhance the design of my website. However, I encountered an issue where using the "select" element with the data-hs-select attribute as suggested by preline UI seemed to cause problems w ...

Dimensional adjustments for Semantic-ui dropdowns

Currently, we are attempting to transform bootstrap into semantic-ui. <select id="sayfaArama" class="ui search dropdown"> <option value="">Searching in pages...</option> </select> Take a look at this image I have encountered ...

Position two div elements and an hr tag to create a step progress bar

Check out my progress bar design https://i.sstatic.net/u9SRM.png How can I center the 'created' and 'assigned' divs below the circle while ensuring the 'hr' line touches the circle, and it is responsive across different scre ...

How can I alter the font color of the parent <li> tag when the child <li> tag is being hovered over using CSS?

CSS Question: <ul> <li>LINK 1</li> <li>LINK 2</li> <li>LINK 3 <ul> <li>link 3.1</li> <li>link 3.2</li> </ul> </li> </ul> Is there a way in CSS to change the t ...

Utilizing JavaScript and jQuery libraries in conjunction with periods

I am a bit puzzled about when to include the period before referencing class names. For instance, in this code snippet, why is a period included before the first use of the 'active-slide' class but not for the other two instances? var primary = ...

Navigate through content to reach the bottom of the webpage

I am currently working on a layout that is similar to Facebook's messaging system (facebook.com/messages). I have made some progress with it, but not quite there yet. You can view my work so far here: https://jsfiddle.net/3nd0b17m/23/ https://i.sstati ...

Is it common for the `col` class in Bootstrap 4 Grid System to have no padding?

This is my first time using Bootstrap version 4. Currently, I have a footer that utilizes the new flex col classes. It looks great on desktop, but on mobile, the columns are stacked so closely together that there is no vertical margin or padding. Is this ...

The Bootstrap 4 navigation bar item unexpectedly jumps below and then wraps around

I am working on creating a navbar using the latest version of Bootstrap 4 (alpha6). <nav role="navigation" class="navbar navbar-light bg-faded"> <a class="navbar-brand" href="/"> <img src="/public/img/logo.png" height="36" alt="myLogo"&g ...

When an image is a child of the parent content and another element has opacity set, the CSS stack order will be altered

Recently, I came across an interesting issue while working with CSS, particularly with negative margins to stack elements on top of each other. I am aware of the natural stack order in which elements overlap, where the later element always appears on top ...

Utilize both versions of Bootstrap seamlessly without causing any disruptions or issues

With a unique situation at hand, I find myself needing to display two different versions of Bootstrap on the same page. My Umbraco CMS relies on Bootstrap version 2.x by default, while my frontend pages utilize Bootstrap 4. However, certain Bootstrap 4 wid ...