The use of the `:target` pseudo selector in conjunction with

I am trying to develop a tab system using only CSS.

Currently, my implementation is functional, but I am unsure how to set one tab as visible by default.

The tabs are structured as follows:

<section class="tabs">

  <ul>
   <li><a href="#tab1">1</a></li>
   <li><a href="#tab2">2</a></li>
   <li><a href="#tab3">3</a></li>
  </ul>

  <section id="tab1"> content for 1... </section>
  <section id="tab2"> content for 2... </section>
  <section id="tab3"> content for 3... </section>

</section>

Here is the crucial CSS code:

.tabs section{
 display: none;
}

.tabs section:target{
 display: block;
}

Setting section:first-child to block presents an issue where both the first tab and the target tab are visible if there is an anchor in the URL.

How can I solve this challenge?

Answer №1

If you set your default as the last tab using section:last-child, this solution should work:

.tabs section,
.tabs section:target ~ section {
   display: none;
}

When using the general sibling selector ~, the element must precede the siblings it targets, hence the use of last-child instead of first-child.

EDIT: 11-12-2011, I have found a way to highlight your a tags as active! This modified code is for proof of concept purposes (tested in FF only):

HTML

<section class="tabs">
  <ul class="nav">
   <li><a href="#tab1">1</a></li>
   <li><a href="#tab2">2</a></li>
   <li><a href="#tab3">3</a></li>
  </ul>


  <section id="tab2"><div class="tabActive"></div> content for 2... </section>
  <section id="tab3"><div class="tabActive"></div> content for 3... </section>
  <section id="tab1"><div class="tabActive"></div> content for 1... </section>

</section>

CSS

.nav {
    position: relative;
    z-index: 2;
    margin: 10px .5em 0;
}
.nav li {
    padding: .5em;
    width: 2em;
    text-align: center;
    float: left;
}

.tabs section,
.tabs section:target ~ section {
   display: none;
}

.tabs section:target,
.tabs section:last-child {
    display: block;
    clear: left;
    margin: 0 .5em;
    min-width: 300px;  /* for show only */
    min-height: 200px; /* for show only */
    border: 1px solid black;   
    position: relative;
    z-index: 1;
    padding: 10px;
}

.tabActive { /* set for tab 1 */
    width: 2em;
    height: 2em;
    position: absolute;
    top: -2em;
    left: .5em;
    border: 1px solid black;
    border-bottom: transparent;
    background-color: inherit;
    margin-top: -1px; /* top border height */
    margin-left: -1px; /* left border width */
}

#tab1 {background-color: cyan;}
#tab2 {background-color: yellow;}
#tab3 {background-color: pink;}

#tab2 .tabActive {left: 3.5em;}
#tab3 .tabActive {left: 6.5em;}

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

Getting elements to vertically align in the center and have matching heights using Bootstrap

Does anyone have experience with vertical aligning in Bootstrap using flex box classes like align-items-center? I'm struggling to vertically center the text content in the boxes of the codepen provided below while ensuring each box matches the height ...

AngularJS: Modify Z-Index on Click Event

My current project involves a navigation bar with four items, each accompanied by an icon. These icons are positioned absolutely one behind the other and are loaded into the view from different templates within the same controller. I am attempting to dyna ...

Expanding content based on height using React

I have successfully implemented the show more/show less feature. However, my issue is that I would like it to be based on the number of lines or screen height rather than the number of characters. This is because the current setup may look awkward on certa ...

The Canvas element inside a Bootstrap modal is returning inaccurate mouse coordinates

I am currently troubleshooting an issue with a HTML5 canvas inside a Bootstrap modal. The canvas is designed to be a selection game where objects can be selected and manipulated. Everything works fine in the center of the 600x600px canvas, but there is an ...

Styling spellcheck errors in Chrome with CSS or JavaScript

One issue I am facing is the need to change the language of a spelling error on a website without disabling it: I have attempted various solutions, but none of the properties like ::spelling-error and :webkit-spell-check seem to be supported. Is there a ...

Display the data from the database in a label based on the selection made in the combo box

In an attempt to pass values into a label based on the selected option from a combobox, let's say if I choose A, the label should display 1 and if I select B, it should display 2. Here is my code: kota.php: $conn = mysql_connect('localhost&apo ...

Sending information from a dynamic table to a database using PHP

I've created an HTML webpage with a table that fetches data from my database. Each row in the table has a button that, when clicked, dynamically adds the data into another table on the webpage using JavaScript. The dynamic table includes a submit butt ...

Using pre-existing CSS state background colors in GTK3

I am currently in the process of adapting a PyGTK2 application. This particular application performs tasks such as: self.normal_color = editor.style.base [Gtk.STATE_NORMAL] self.insensitive_color = editor.style.base [Gtk.STATE_INSENSITIVE ...

Enable scrolling for a div positioned beneath another div

I am working with an iPad frame and trying to implement a feature where a larger image scrolls behind it as the user scrolls down the page. Although my CSS is more complex than the example provided in this link, I am struggling to get even that basic funct ...

JavaScript toggle display function not functioning properly when clicked

I've been attempting to create a drop-down list using HTML and JavaScript, but for some inexplicable reason, it's just not functioning as expected despite scouring through countless tutorials on YouTube. Below is the snippet of code that I'm ...

"Failure to manipulate the style of an HTML element using Vue's

<vs-tr :key="i" v-for="(daydatasT, i) in daydatas"> <vs-td>{{ daydatasT.Machinecd }}</vs-td> <vs-td>{{ daydatasT.Checkdt }}</vs-td> <vs-td>{{ daydatasT.CheckItemnm }}< ...

Is the post secure if a HTTP web page sends an ajax request to a HTTPS url?

If I developed an HTML/jQuery widget to be embedded on third-party websites with users of very limited technical knowledge and potentially missing SSL certificates, would the information posted securely through AJAX Post to a secure URL remain protected? ...

Stop the MatSort feature from activating when the space key is pressed in Angular Material

I am currently using angular material tables and have implemented filters for each column. The filter inputs are located in the row header of each column, which is working fine. However, I am facing an issue where whenever I type and press the space key, ...

Ways to update images without having to reload the page following an ajax request

My current script uploads images to the server and updates the image source in HTML upon completion. However, I am facing an issue with my PHP code renaming the files to predefined names (ext-pix0.png, ext-pix1.png, etc.) during upload. This leads to the p ...

Is it possible to include Helvetica or a similar font in my web application for shipping?

My web-app is Java/HTML based and hosted on the cloud. Users will access it using IE, Chrome or Mozilla. I would like to use Helvetica or a similar font, but they are not readily available on most systems (windows/IE/Chrome/Mozilla). Is there a way for me ...

How can you activate color printing in CSS when using ng-style in AngularJS?

My application generates data in a table, where each cell has a unique combination of background color and text color determined by its properties. The code to achieve this is simple: ng-style='{"background-color": lt.backgroundColor, "color": lt.te ...

Unable to create circular dots within the Slick Slider widget

While attempting to create a slider with dots, I encountered an issue where the dots appeared as ellipses instead of circles, despite setting the width and height to be the same. For instance, I specified width: 12px; height: 12px; but it resulted in a sha ...

Element failing to adhere to CSS grid layout

I am currently working with grid layout and aiming to position the following content at the bottom and center, but it is currently aligning to the left: .body { margin: 0; background-color: rgb(255, 237, 237); display: grid; grid-template-rows: ...

Error encountered with the item properties in vue-simple-calendar 'classes'

For my Vue.js project, I am utilizing the npm calendar package called `vue-simple-calendar` and aiming to implement dynamic classes for each event item like background-color, etc. However, the `classes` property doesn't seem to be functioning as expec ...

What is the best way to extract text using Selenium in Java?

My goal is to extract the text $1.00 from the HTML code snippet below (I already have the xpath, so no need to worry about that). Let's assume the xpath is //*[@id="price-string"] <strong id="price-string">$1.00</strong> ...