The process of configuring a tab to appear automatically upon the webpage loading

Jsfiddle: http://jsfiddle.net/87YGW/

Presently, to display content for tab 1, I have to click on it. However, I want the tab 1 content to be shown automatically when the page is opened.

HTML:

<div id="tab2" class="css-tabs">
        <ul class="noint11_menu">
            <li id="item-1"> <a href="#item-1">Tab 1</a>
                <div>tab conten1
                   </div>
            </li>
            <li id="item-2"> <a href="#item-2">Tab 2</a>
                <div>
                    <p>Tab Content 2</p>
                </div>                
            </li>
            <li id="item-3"> <a href="#item-3">Tab 3</a>
                <div id="notice">
                    <p>tab content 3 </p>
                </div>
            </li>
            <li id="item-4" title="click for Tab 4"> <a href="#item-4">Tab 4</a>
                <div>
                    <p>Tab Content 4</p>
                </div>
            </li>
        </ul>
    </div>​

CSS:

.css-tabs {
    position: relative;
    text-align: left; /* This is only if you want the tab items at the center */
    height: auto
}
.css-tabs ul.noint11_menu {
    list-style-type: none;
    display: inline-block; /* Change this to block or inline for non-center alignment */
}
.css-tabs ul.noint11_menu > li {
    display: block;
    float: left;
}
.css-tabs ul.noint11_menu li > a {
    color: #EEEEEE;
    text-shadow: 1px 1px 1px #000;
    font-family: 'MuliLight', Arial, sans-serif;
    font-size: 14px;
    text-decoration: none;
    display: block;

}
.css-tabs ul.noint11_menu li > div {
    display: none;
    position: absolute;
    width: 100%;

}
.css-tabs ul.noint11_menu li > div > p {
    border: transparent;
    padding: 10px;
    margin: 0;
}
.css-tabs ul.noint11_menu li > a:focus {
    border-bottom: 1px solid #fff;
}
.css-tabs ul.noint11_menu li:target > a {
    cursor: default;
    border-bottom: 1px solid #fff;
}
.css-tabs ul.noint11_menu li:target > div {
    display: block;
}​

I am unable to utilize Javascript, Jquery, or iframe

Answer №1

Unfortunately, I can't provide a demonstration in the fiddle due to the use of iframes. However, if you directly link to the first tab like this:

http://www.yoururl.com/page#item-1
, the content of the first tab will be visible when the page loads.

You can test this by placing all your code into a new blank HTML file (ensuring CSS and markup are correctly placed) and saving it as "test.html." Then create another HTML file in the same folder with the following code:

<html>
  <body>
    <a href="test.html#item-1">link</a>
  </body>
</html>

Save and open the second file, click the link to navigate to your styled page. You'll notice that the content of the first tab is visible while tab switching still functions as expected.

It seems unlikely that you can achieve the same result using only CSS without JS for redirection or changing the window location.

Here is an article that explains why this method works: The :target pseudo selector in CSS activates when the hash in the URL matches the id of an element.


As an additional workaround, here's a slightly unconventional but purely CSS "solution":

To keep the first tab always visible, use #item-1 div {display: block} and assign a background color to all tabs:

.css-tabs ul.noint11_menu li > div {
    display: none;
    position: absolute;
    width: 100%;
    left: 0;
    color: #ccc;
    text-align: left;
    padding: 0;
    margin-left: 32px;
    background: #fff; /* added this */
}

Now, when other tabs are selected, they will hide the first tab. Check out the demo here.

Answer №2

To style your elements with CSS, you can include the following code:

#item-1 div {display: block}

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 struggling to grasp the reason behind the error message stating that x is not a function

Can someone help explain why I keep getting the error message "5Style is not a function"? NationalLevelCategoriesChosenList = [ ["5Style", "5MelodyHarmony", "5RhythmTempo", "5TextureStructureForm", "5Timbre"] ], [ [] ]; if (NationalLevelCategoriesC ...

Having trouble resolving errors encountered while running the `npm run build` command, not sure of the steps to rectify

I am currently working on my first app and attempting to deploy it for the first time. However, I have encountered an error that I am unsure of how to resolve. When running "npm run build", I receive the following: PS C:\Users\julyj\Desktop& ...

creating a dropdown menu using HTML5 and JavaScript

In my HTML page, I need to display a dropdown list containing 250 countries. I can achieve this easily using the following code: <select id="country" name="country"> <option value="1">Afghanistan</option> <option value="2">Albania& ...

Creating and improving processes within the WAMP environment

Can someone help me with outputting errors in WAMP? I'm aware of production and development modes. How can I easily switch between these modes? Is there a way to define whether I am currently in development or production? Or am I misunderstanding som ...

Sleek dialog sliding animation with Svelte

I'm struggling with a svelte component that I have and I'm trying to implement a slide down animation when it closes. The slide up animation is functioning correctly, but for some reason the slide down animation is not working. Does anyone have a ...

Is it possible for a div nested within another div to still occupy space on the page despite being set

<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $("select#choosebook").change(function(){ $(".title").slideDown(" ...

What is the JavaScript design for creating plugins?

[I'm brand new to the world of Javascript, so please bear with me.] I am developing an application in node.js that will feature a collection of plugins. Each "plugin" will consist of a function, or possibly two, that can manipulate a string in some w ...

Unpacking the information in React

My goal is to destructure coinsData so I can access the id globally and iterate through the data elsewhere. However, I am facing an issue with TypeScript on exporting CoinProvider: Type '({ children }: { children?: ReactNode; }) => void' is no ...

Error: The object does not have the property createContext necessary to call React.createContext

I'm currently exploring the new React Context API in my app. I've also implemented flow for type checking. However, when I add // @flow to a file that contains the code: const MyContext = React.createContext() An error pops up stating: Cannot ...

How to convert int64 Objects to strings in Node.js using protobuf.js

Currently, I am utilizing the impressive module found at protobuf.js to work with protocol buffers in node.js. One particular challenge I am facing involves an int64 field type (which is supported by google-protocol) and my attempt to display it on the sc ...

"Mongo server is rejecting the connection, and the reason is unclear to me

I created a MongoDB model with the following structure:- var mongoose = require('mongoose'); const itemsModel = new mongoose.Schema({ _id: { type: String, }, userName: { type: String, required: true }, ...

Having trouble retrieving the attribute of an appended element in jQuery?

I am facing an issue where I am unable to retrieve the ID-attribute of an element that has been appended into my HTML. Each time I try, the result is always 'undefined'. How can I resolve this problem? jQuery('form#formular').append(&a ...

Error: 'm' is not defined when using the discord.js message collector

I am in the process of creating a Discord collector similar to this: let user = message.author; let guest = message.mentions.users.first() const collector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id || m.auth ...

Is there a way to display my <i> tags inline without any issues?

I am facing an issue with my code and need assistance. I have two links that I want to display but my code doesn't seem to respond as expected. The first link is: https://i.sstatic.net/fryPH.png The second link is: https://i.sstatic.net/j849M.png A ...

Why does my website layout suddenly move to the top left corner of the browser screen when I zoom out?

Why does my website layout shift to the top left of the browser screen when I zoom out, while other websites like yahoo.com stay centered? ...

Having difficulty swapping out images on an HTML website template for Internet Explorer

Recently, I obtained an HTML website template that I am currently working on customizing. One of the tasks I have been tackling is replacing some of the existing images with my own. After resizing the new images to match the dimensions of the originals, ...

The form is failing to transmit the button data

I'm encountering a strange issue with my form. It successfully sends the data from the textboxes, but it fails to send the button data along with it. Here is the structure of my form: <form name="login_registro" method="POST" id="login_registro"& ...

What is the method to activate sequential selection through JSON response?

I just started exploring angularjs and I'm interested in selecting values step by step. But first... Here's the JSON response I received: [ { "countryname": "India", "states": [ { "statename": "Karnataka", ...

SignalR 2.2 application encountering communication issues with client message reception

I am facing an issue in my application where clients are not receiving messages from the Hub when a user signs in. Here is my Hub class: public class GameHub : Hub { public async Task UserLoggedIn(string userName) { aw ...

What is the best way to isolate the elements from the specified dictionary that contain valid data?

I need to extract only the data for Flipkart from this array and create a new array containing just that information. json = [ { "amazon": [] }, { "flipkart": { "product_store": "Flipkart", ...