Tips on adjusting the color of a chosen tab using the CSS :target Property, no JavaScript needed!

I am currently attempting to modify the color of the active tab using CSS and the :target property. The issue I am facing is that I am unable to identify the clicked tab when using the :target property. Therefore, I would like the active tab to be visible when clicked, and I also want to display the first tab by default. Can anyone assist me in resolving this?

Below is the code snippet:

.tab {
  display: flex;
  flex-direction: row;
  margin-bottom: 30px;
}

.tab li {
  color: var(--dark);
  margin-right: 15px;
  font-size: 22px;
  position: relative;
}

.tab li:after {
  height: 1px;
  content: '';
  background-color: rgb(10, 170, 219) !important;
  width: 100%;
  position: absolute;
  left: 0;
}

.tab li h2 {
  font-size: inherit;
}

.tab li a {
  text-decoration: none;
  color: inherit;
}

#tab .tab-content {
  display: none;
}

#tab .tab-content:target {
  display: block;
}
<div id="tab">
  <ul class="tab">
    <li>
      <h2><a href="#tab1">Tab1</a></h2>
    </li>
    <li>
      <h2><a href="#tab2">Tab2</a></h2>
    </li>
  </ul>
<div id="tab1" class="tab-content">Content1</div>
<div id="tab2" class="tab-content">Content2</div>
</div>

Answer №1

To create a simulated click event in CSS, the most effective method is by utilizing a checkbox. This involves connecting a label to an element using the label's for="" attribute and then utilizing the :checked attribute to identify when the tab has been clicked.

Answer №2

It seems that the functionality for switching tabs is currently not functioning properly. The selectors used to display the .tab-content are incorrect. To fix this, you just need to reposition #tab1 and #tab2 inside the main #tab element. This adjustment will make it easier to apply color styling.

.tab {
  display: flex;
  flex-direction: row;
  margin-bottom: 30px;
}

.tab li {
  color: var(--dark);
  margin-right: 15px;
  font-size: 22px;
  position: relative;
}

.tab li:after {
  height: 1px;
  content: '';
  background-color: rgb(10, 170, 219) !important;
  width: 100%;
  position: absolute;
  left: 0;
}

.tab li h2 {
  font-size: inherit;
}

.tab li a {
  text-decoration: none;
  color: inherit;
}

#tab .tab-content {
  display: none;
}

#tab .tab-content:target {
  display: block;
}
<div id="tab">
  <ul class="tab">
    <li>
      <h2><a href="#tab1">Tab1</a></h2>
    </li>
    <li>
      <h2><a href="#tab2">Tab2</a></h2>
    </li>
  </ul>
    <div id="tab1" class="tab-content">Content1</div>
    <div id="tab2" class="tab-content">Content2</div>
</div>

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

iOS devices experiencing issues with fixed positioning

Currently, I am in the process of implementing a few instances of , but I am encountering some issues with the CSS. When viewing the website on an iPad or iPhone, the calendar is positioned correctly as the container covers the window like a fixed position ...

The elements within the HTML, CSS, and Bootstrap are all clashing and

Website Output https://i.sstatic.net/5JVyg.jpg I've been working on a small website with a bootstrap carousel, and in this specific section of the program, the Bootstrap and HTML/CSS elements overlap by default. I'm not sure how to separate the ...

What is the best way to incorporate multiple versions of jQuery on one webpage?

Is it possible to use multiple versions of jQuery on a single page? I need two different versions for various functions, but they seem to be conflicting with each other. If I implement jQuery noconflict, will the product scripts still work? <script typ ...

Unusual display of feedback text in Bootstrap 4

When I directly copied this code snippet from Bootstrap 4: <div class="form-group has-danger"> <label class="form-control-label" for="inputDanger1">Input with danger</label> <input type="text" class="form-control form-contro ...

I'm struggling with organizing my webpage layout using CSS flexbox and CSS grid

Currently, I am working on creating a checkers game using HTML and CSS with the inclusion of CSS Flexbox and CSS Grid for layout design. However, I have encountered an issue where the page is not responsive vertically. When resizing the browser height in C ...

Ensure the following elements remain in place once an element has been concealed

Imagine a scenario where three elements are present in an HTML file, with one element (specifically the middle one, .div2) hidden from view. The goal is to reveal this hidden element (.div2) and hide the first element (.div1) upon clicking a button using j ...

What is the best way to obscure or conceal images of runners?

I am trying to figure out how to blur or hide the runner thumbnails on different streaming sites, such as Prime Video. I typically use the Amino: Live CSS Editor chrome extension or uBlock Origin to manipulate elements on websites. However, I am struggling ...

Issue with Vue2: encountering an error with the view-router component when attempting to utilize the <router-view> tag

Currently delving into Vue2 for the first time and facing hurdles with routes. Ever since I inserted <router-view></router-view> in index.html, an exception has been popping up: [Vue warn]: Failed to mount component: template or render functi ...

Incorporating HTML with ng-binds through transclusion

I have been developing an angular directive to enhance the functionality of ng-table by adding filtering and exporting features. The main goal is to make this directive reusable for multiple tables, which requires the <td> elements to be dynamic. To ...

Bypassing CSS rules for elements not present in the DOM

In case an element is absent from the DOM, I want to include margin space. Conversely, if the element is present, no margin space should be added. I have experimented with CSS for this purpose: A - To ensure there is no margin-top if the H2 title is displ ...

The incorporation of SVG within a span disrupts the conventional left-to-right arrangement of its child elements

As I was working with some basic HTML, I encountered an issue. <span> <span>Hello</span> <span>World</span> </span> The above code displays: Hello World However, when I tried adding an SVG element like this: <s ...

Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically. The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovere ...

Tips for Personalizing Bootstrap 4.3 Through BootstrapCDN

I'm having trouble customizing the Bootstrap 4.3 BootstrapCDN by linking an external CSS file to override it. Even though I have placed the custom CSS file below the Bootstrap one, it doesn't seem to be taking effect. Can anyone explain why? Th ...

Integrating my HTML-CSS layout into a Ruby on Rails framework

After creating a web page template using a combination of HTML, Bootstrap, and CSS, I am now looking to see it in action on my Rails application. I need guidance on how to accomplish this step by step. Can anyone provide assistance on what steps need to ...

The design of the website is all over the place

I am encountering challenges in creating distinct containers for the header, body, and other sections of my website. The layout is not turning out as planned, and I can't pinpoint where my code is going wrong. Any advice or suggestions on how to resol ...

How can I change :hover to a clickable element instead?

I attempted to create a full-width accordion with the following code: .page { margin: 0; padding: 0; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; height: 100vh; } .content { -webkit- ...

Managing errors in jQuery's .ajax function

Having some issues with jQuery.ajax() while trying to fetch an html code snippet from table_snippet.html and replacing the element in my html code. The error handler in jQuery.ajax() gets triggered instead of the expected success handler. <!DOCTYPE H ...

Is it possible to apply a consistent character width to all glyphs, even the most obscure Unicode symbols, using CSS?

Is it possible to style a single character within a span element to have a specific width and height of 1em? Ideally, I would like the character to be stretched if it is not already fixed-width. This styling must support any character, including rare Unic ...

Incorporate Security Measures into the Form

I am in the process of setting up a form that requires users to enter a specific password provided by me. The user must input "Password123" into the form before they can submit it to my email. If the password is incorrect, the site will redirect to an erro ...

What are the indicators of JSON in a response?

When using ReactJS to make a fetch request to an API, the JSON body response includes the following: { "place": <a href=\"http:\/\/place.com\/ search?q=%23MILKYDOG\" target=\"_blank\">#milkydog<&b ...