Hide default content upon clicking tabs in CSS

I am in need of a solution that must be achieved using only CSS, with no JavaScript involved.

JSFiddle: http://jsfiddle.net/hen75c3g/3/

Upon loading the page, the default content is displayed. When the first tab is clicked, the corresponding content is shown. I want the default content to disappear and the related content to appear when other tabs are clicked.

This should all be accomplished exclusively through CSS without any Javascript. Is it possible?

    .accordion {
        width:830px;
        overflow:hidden;
        margin:10px auto;
        color:#474747;
        padding:10px;
        border:1px solid black;
    }
    .accordion section h2 {
        display:none;
    }
    .accordion section h2 a {
        float:left;
        overflow:hidden;
        cursor:pointer;
        margin:10px;
        padding:10px;
        font-size:12px;
        color: #000;
        text-decoration:none;
        width:100%;
    }
    .accordion section p {
        display:none;
    }
    .accordion section:after {
        font-size:24px;
        color:#fff;
        font-weight:bold;
    }
    .accordion section h2 {
        position:relative;
    }
    .accordion section p.default {
        clear:both;
        display:block;
    }
    .accordion section:target p {
        clear:both;
        display:block;
    }
    <div class="_m-container">
        <div class="accordion">
            <ul class="_m-menu">
                <li><a href='#tab-1'>Tab 1</a>
    
                </li>
                <li><a href='#tab-2'&
I would appreciate any help or advice on achieving this layout only using CSS.
Thank you.

Answer №1

It doesn't seem possible to achieve this using just CSS. The closest option for a pure CSS tab implementation would probably involve hover effects.

However, if you're open to using jQuery, you can accomplish what you want by following these steps: (please note that I have included additional classes in the elements, check out http://jsfiddle.net/hen75c3g/18/)

$('.accordion').on('click', '.tab-link', function(){
// save the link's href as a variable
var tabSelector = $(this).attr('href');    

// hide any previously visible content
$('.tab-content').removeClass('active');

// add the active class to the selected tab
$(tabSelector).addClass('active');

return false;

});

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

Experimenting with jQuery in a .php file on a local server but experiencing issues with functionality

Currently, I am working on a login page that involves a specific functionality. When the user clicks on the "I'm a Student" button, I want to achieve a slideDown effect to hide one div and show another div with a slideUp effect. While I have confidenc ...

What is the best method for placing text over an image in Dreamweaver?

What steps should I take to overlay text on an image using Dreamweaver? Similar to the example linked below: https://i.stack.imgur.com/8GvkW.png Below is a snippet of my code: HTML <body> <main> <header> <img src="images/headerimag ...

Methods for scaling the size of CSS background images

Can anyone assist me with scaling background image size properly? code: http://codepen.io/AnilSimon123/pen/JRBRZa Below is the code snippet: .bgimage{ background:url('http://www.thedesignlove.com/wp-content/uploads/2012/08/free-blue-abstract-vec ...

Retrieve PHP variable from a PHP CSS file

I'm facing an issue where I am unable to retrieve a PHP variable from a CSS file that is loaded in this manner from my index.php: <link href="css/style.php" rel="stylesheet"> Within the style.php file, the code looks like this: <?php heade ...

Tips for successfully sending a nested function to an HTML button and dropdown menu

I'm working on two main functions - getChart() and fetchData(). The goal is to retrieve chart data and x/y axes information from a database. Within the getChart function, I'd like to incorporate a dropdown menu for displaying different types of c ...

The new version 5.1 of Bootstrap modal does not disable scrolling on the <body> element

I'm currently working on a project in Angular 11 using Bootstrap (v5.1.3). My main goal is to create a functionality where clicking on a card, similar to a blog post, will trigger a Bootstrap modal popup displaying the content of that specific post. B ...

Dealing with undefined variables when importing into create-react-app Sass: SassError

I am using create-react-app with SCSS style-files. My project includes an App.scss file where I import all other .scss stylesheets, and a variables.scss file where I define all project variables. However, when I import variables.scss into App.scss and the ...

What is the proper way to apply :hover on the last child element using material-ui?

Currently, I am working with React and material-ui to create a menu component. This menu has a list of items, with the last item being unique in terms of styling. I have successfully used the :last-child selector for this purpose, but I am facing an issue ...

Express Form Validation: Ensuring Data Accuracy

I have recently learned that client-side form validations may not be sufficient to prevent malicious actions from users. It is recommended to also validate the form on the server-side. Since I am new to using express, can someone provide guidance on what s ...

What could be the reason for Chrome breaking up this straightforward bootstrap header into two lines?

Why is it that the code below displays correctly in one line in both FF and IE, but Chrome for some reason is showing it on two lines as if both span and button elements had "display:block;"? Even though the button has a computed display of "inline-block," ...

The placement of an HTML canvas within a webpage

I've been experimenting with a canvas script that resembles dottydots. Everything is functioning correctly, but I'm looking to adjust its position on the canvas. I attempted using position:absolute, which did move the canvas visually, but the wor ...

Struggling with determining the perfect transition speed for the sidemenu display

I'm a newcomer to web development and facing an issue with setting the transition speed for opening and closing this side menu. Despite adding transitions in the CSS and specifying duration in the Javascript, the menu continues to open instantly. I di ...

Unique creation of card deck using Bootstrap 4

Is there a way to create a card grid inside a container using Bootstrap 4? I'm having trouble understanding how to add text in the container if the text is within a div (card). <section class="second__offers"> <div class="container"> ...

Manipulating elements repeatedly using jQuery

One of the issues I encountered while working with jQuery was appending a YouTube video when a button is clicked. To do this, I used the following code: onclick="$('#videogallery').append('<iframe width=725 height=398 src=http://www.yout ...

Utilize the HTML Tidy executable in combination with PHP, as opposed to compiling it

I have created a PHP application specifically for shared hosting environments. Dealing with the inconsistency of hosts providing HTML Tidy can be challenging for me. Is there a way to integrate the tidy executable directly into my PHP application and proce ...

Issue with URL redirection and reloading in Safari and IE browsers

My current method for redirecting to the Item page and performing a full page reload works well in most browsers, but unfortunately not in Safari and IE. In those two browsers, the current page is reloaded instead. Does anyone have any suggestions for an ...

Efficiently handling multiple form submissions using a single jQuery Ajax request

I'm working on a page that has 3-4 forms within divs, and I want to submit them all with just one script. Additionally, I want to refresh the content of the specific div where the form is located. However, I'm unsure of how to specify the current ...

Update the value of a JavaScript variable in an HTML template by targeting the element with a specific

Within my testFile.html HTML file, the following code is present: <div id="image-type-placeholder">marinaa</div> In my JavaScript file: const CourseImageUpload = BaseComponent.extend({ /** * @property {function} */ templat ...

I need to find a way to position my login in the center of the page, even if my HTML body doesn't take up the entire space. This is

I am having trouble centering my login Component in the red zone on the page. It seems that my html and body tags are not taking up all the available space on the page. If you want to view my project, you can find it on my GitHub at https://github.com/SIGX ...

What is the reason for index always being not equal to 0?

<script> var index = 0; $.getJSON("./data/data.json", function(json) { $.each(json, function(data) { var html = ""; if(index == 0) { console.log(index + " fir ...