The standard target styling in CSS

I am facing an issue with my CSS and HTML setup. The CSS code I have is as follows:

<style type="text/css>
.tab {
  display: none;
}
.tab:target {
  display: block;
}
</style>

And this is the structure of my HTML:

<div class="tab_container">

  <ul class="tabs">
    <li><a href="#updates-list">List</a></li>
    <li><a href="#updates-map">Map</a></li>
  </ul>

  <ul class="tab list" id="updates-list">
    Eh.. Hello!
  </ul>
  <div class="tab map" id="updates-map"></div>
</div>

The problem I am encountering is that when no target is specified in the URL and no tab is clicked yet, my page appears empty. My goal is to display ul#updates-list by default.

If anyone has any suggestions on how to achieve this, I would greatly appreciate it. Thank you!


Update: In addition to the above issue, I am also facing problems with my Google Map if it is not the initial target. Does anyone have a solution for this?

Answer №1

My immediate inclination would lead me to suggest that the only feasible solution available is to unfortunately resort to using JavaScript. An example code snippet could be:

<script type="text/javascript>
  if (document.location.hash == "" || document.location.hash == "#")
    document.location.hash = "#updates-list";
</script>

UPDATE:

After further investigation, I have discovered a CSS workaround. However, this method necessitates positioning the default entry #updates-list as the last element (after #updates-map and any other tabs you may introduce):

.tab, .tab:target ~ #updates-list  {
  display: none;
}
#updates-list, .tab:target {
  display: block;
}

Answer №2

While this question may be considered old, it still holds relevance and I would like to offer an additional idea:

<a href="#section1">Section 1</a>
<a href="#section2">Section 2</a>

<a id="section1"></a>
<a id="section2"></a>

<div class="tab section1 default-tab">Some text content</div>
<div class="tab section2">Some other text content</div>

Furthermore, here is the corresponding CSS code:

.tab {display:none}

.default-tab {display:block}

:target ~ .default-tab {display:none}

#section1:target ~ .section1,
#section2:target ~ .section2 {
  display: block
}

This method allows for flexibility in tab order placement. When generating HTML dynamically, ensure to also produce the CSS dynamically or set a maximum number of tabs to support within the initial CSS coding.

Answer №3

Here is a concise solution that redirects users to the updates list:

window.location.href = location.hash || "#updates-list";

This code snippet ensures the user is directed to a valid hash if their current hash is empty or nonexistent.

With approximately 98% of users having JavaScript enabled, utilizing a JS solution should not pose any challenges.

Answer №4

To address this issue, one potential approach could be to target a specific element within the tab container using CSS selectors. However, due to limitations with the :not() pseudo-class, it is not feasible to achieve the desired result in this manner.

Two possible solutions could be considered:

  • Implementing JavaScript to set the target to the first tab upon page load or ensuring that the anchor is present to avoid display issues.

  • Creating a custom override class for the initial active tab and removing it (using JavaScript) after the first tab switch.

While the first option may impact browser history, the second option might result in a more complex code structure. It may be beneficial to consider using JavaScript to detect the current target as a more comprehensive solution, ensuring compatibility with older browsers and addressing this specific challenge in a cleaner manner.

Answer №5

Helpful:

.tab#tabControl { display:block; }

.tab:not(:target) { display:none; }

.tab:target ~ #tabControl { display:none; }

Answer №6

Although not a purely CSS solution, this method involves a small amount of JavaScript that greatly simplifies all the code:

location=location.hash||"#one"

(Credit to: )

Here is a minimalist Single Page Application (SPA) showcasing this concept. The initial "page" is immediately visible, but is then hidden when other links are clicked.

<!doctype html>
<title>personal web page</title>

<style>

 section:target {
  display: block;
}

section {
  display: none;
}

</style>

<h1>About me</h1>

<nav>
  <a href="#one">Travel</a>
  <a href="#two">Hobbies</a>
  <a href="#three">Links</a>
</nav>

<section id="one">  
  <h2>Travel</h2>
  <p>Places I like to travel to.</p>
</section>  

<section id="two">  
   <h2>Hobbies</h2>
   <p>Things I like to do.</p>
</section> 

<section id="three">  
  <h2>Links</h2>
  <p>My favourite links.</p>
</section>

<script>
  location=location.hash||"#one"
</script>

Answer №7

Here is a unique solution that I discovered, different from any other solutions provided so far. This method may only work on modern browsers (I have tested it on a Chromium browser from late 2023), but the following CSS code worked well for me with just a minor HTML tweak:

<style type="text/css">
.tab {
  display: none;
}
.tab:target, :not(:has(.tab:target)) .tab.default {
  display: block;
}
</style>

In addition to this, I found another even better solution after posting this, which involves changing the ul element with the id updates-list to a div, or wrapping it in a div with a class attribute containing "tab":

.tab {
  display: none;
}

.tab:target {
  display: block;
}

.tab_container {
  &:not(:has(.tab:target)) {
    & .tab:first-of-type {
      display: block;
    }
  }
}

(Returning to discussing the first solution now.)

To implement this in the HTML, I simply added "default" to the class list of the #updates-list ul tag:

(Note: you could still use ID selection instead of adding a new class, but I personally found this approach cleaner. I also included some text within #updates-map for testing purposes.)

<div class="tab_container">

  <ul class="tabs">
    <li><a href="#updates-list">List</a></li>
    <li><a href="#updates-map">Map</a></li>
  </ul>

  <ul class="tab list default" id="updates-list"gt;
    Hello!
  </ul>
  <div class="tab map" id="updates-map"gt;
    [Placeholder for map]
  </div>
</div>

Explanation:

The second selector in the CSS code snippet,

:not(:has(.tab:target)) .tab.default
, identifies a scenario where there is no .tab item with the :target attribute. In such cases, the style rule display: block; is applied to any .tab element with the additional class of .default.

This method has several advantages:

  1. It does not rely on a specific order, unlike some other approaches;
  2. Multiple tabs could potentially have the default attribute, resulting in multiple items being displayed if no specific target is selected.

On a side note, credit goes to Amy Kapernick for inspiring me to explore this JavaScript-free solution through her talk and demo site showcasing similar techniques without JavaScript.

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

Unable to insert flag image into the <option> tag

Struggling to add a flag image to the options using any method (html,css)! <select> <option value="rus" selected>Rus</option> <option value="Uzb" >Uzb</option> <option value="eng">Eng</option> </s ...

Dealing with customized protocol responses in JavaScript

My web server is set up to return a response like: HTTP/1.1 302 Found message://ActualMessage While this setup works seamlessly for UI clients like Android and iOS, I'm faced with the challenge of handling this case on a web browser. For instance, ...

Guide on how to save a collection of images from a website to a local directory with nodejs

I am looking to retrieve a folder of images and save them to my local directory. I have successfully downloaded a single image by specifying the image name, but I am unsure how to download multiple images at once. Below is the code I have used: var http = ...

Embedding parent HTML in a div and creating a link back to it from child HTML

Within a parent HTML file, I have included a child section by specifying an id in the div. My goal is to develop a button that will clear out the content of the child section and return the focus back to the parent within the same div. How should I code ...

Changed static divs into flexible divs

I am currently working on designing a webpage layout where the header occupies 100% of the width and 20% of the height in conjunction with a left navbar (20% width) and a main body adjacent to it (80% width). Additionally, I want the page to be responsive ...

Implementing Bootstrap Datepicker within the dynamically generated table rows

I am currently in the process of developing a data grid for a project and I am encountering an issue with implementing Bootstrap Datepicker on the Date field within the table. The problem arises when trying to use the datepicker on subsequent rows that are ...

Make sure that a specific div or image is loaded first

When loading the following code, my priority is for "div1" to load before anything else. <HTML> <body> <div id="div1">This div should be loaded first</div> <img src="/" title="Other large images"&g ...

Is it possible to alter the cursor according to the position of the mouse?

Is it possible to change the cursor from default to pointer when the mouse enters the specific rectangle area (50, 50, 100, 100) of the body? The dimensions are in pixels. Instead of defining a separate div and setting the cursor style on it, I'm loo ...

I'm attempting to switch between different "screens" by utilizing divs with CSS and JavaScript, using the display property to toggle between none and block. Everything seems to be functioning smoothly except for the

I thought I grasped the concept of this because all my toggles are working fine, except for the last one. The frustrating part is that it's written in the same manner as the others before it. The problematic button is identified by id="startYearOne". ...

What is the best way to set the screen height without needing to scroll vertically?

How can I make the orange and blue area extend to the end of the screen or have the full screen size minus the height of the navbar? When using `vh-100`, it fills the full height but creates a vertical scrollbar which is not desired. Is there a way in Boot ...

Enhance the appearance of the input range by styling it with an additional class both before and

I have a JSF page where I am using an input range element. I want to add a different style class before and after the input range. Specifically, I want to display a plus icon (ui-icon-plusthick) before the input and a minus icon (ui-icon-minus) after the i ...

Stop Mat-chip from automatically inserting a row upon selection

I am working on preventing the automatic addition of a row by the mat-chip module after a single chip has been selected. Even though the max chip count is set to 1, the input remains enabled and adds a new row beneath it as if the user can still type more ...

Achieving alignment of the label with the top of the div

I'd like to align a label with a div vertically, but I'm having trouble. Take a look at the picture below to see what's happening. The corrections in blue show what I want it to look like. I've included the relevant HTML and CSS below, ...

Change justify-content-between to something else

After successfully fixing the issue with aligning the navbar brand in the middle of my navigation bar, I am now facing a new challenge. I want to add a button right before the right navigation toggle, but all my attempts have been unsuccessful so far. I&ap ...

What is the best way to showcase the values linked to their corresponding ids in a table?

With the query below, I am joining two tables using the order_id and displaying all the values from the user_orders table. My goal is to only display the order_Id rows that match the order_manager table, as shown in the image below. public functio ...

Delay in background color for hamburger menu in Bootstrap 4

I recently delved into the world of coding and I am just putting the finishing touches on my basic portfolio site. The one issue that has been bothering me is the hamburger menu background. While I have successfully applied a background color to the menu ...

What causes the width of a card to vary between Windows and a Macbook Pro?

Here is the code I have: <div id="app"> <v-app id="inspire"> <v-content> <v-container> <v-card max-width="1000" class="mx-auto" > <v-form v-model="valid"> ...

Leveraging an external Typescript function within Angular's HTML markup

I have a TypeScript utility class called myUtils.ts in the following format: export class MyUtils { static doSomething(input: string) { // perform some action } } To utilize this method in my component's HTML, I have imported the class into m ...

"Trouble with image height not scaling correctly in Internet Explorer when using the img tag

I am facing an issue that can be observed by visiting this link using Internet Explorer. Interestingly, everything functions perfectly in all other browsers (although Chrome displays little blue squares next to the images for some unknown reason). Here i ...

Using jQuery to eliminate tags within an email hyperlink

Looking for a solution to remove tags within a hyperlink using jQuery? Check out this post on Stack Overflow. Having trouble sending an email from a hyperlink? Previous solutions have not worked. Any help would be appreciated! Check out this fiddle which ...