Display or conceal information depending on the anchor; reveal the first one automatically

Utilizing only HTML and CSS, we can achieve the functionality of displaying and hiding content using an anchor tag:

#red { background: red; }
#blue { background: blue; }
#green { background: green; }
.box {
  width: 200px;
  height: 200px;
  display: none;
}
.box:target {
  display: block;
}
<a href="#red">Red item</a> | <a href="#blue">Blue item</a> | <a href="#green">Green item</a>

<div class="box" id="red"></div>
<div class="box" id="blue"></div>
<div class="box" id="green"></div>

To automatically display the first item (red) during page load, additional styling or scripting may be required.

Answer №1

If you're comfortable making changes to the html code and want the red box to appear last, then you can try this approach:

#red {
  background: red;
  display: block;
}
#blue { background: blue; }
#green { background: green; }
.box {
  width: 200px;
  height: 200px;
  display: none;
}
.box:target {
  display: block;
}
.box:target ~ #red {
    display: none;
}

Answer №2

While I can't reveal the solution here, it's actually quite simple. If you're incorporating this code into a webpage, all you have to do is append the anchor of the first a tag to the URL in order to activate its target. It should look something like this:

wwww.page.html#red

Below is a screenshot showing the end result:

https://i.sstatic.net/6IuvT.png

This method allows you to toggle between different options without needing to modify the code itself.

Answer №3

If you're looking to experiment with some code, give this a try:

#red { background: red; display:block;}
#blue { background: blue; }
#green { background: green; }
.box {
  width: 100%;
  height: 100%;
  display: none;
  position: absolute;
  top: 0;
  left: 0;
}
.box:target {
  display: block;
}
.wrapper {
  position: relative;
  width: 200px;
  height: 200px;
}
<a href="#red">Red item</a> | <a href="#blue">Blue item</a> | <a href="#green">Green item</a>

<div class="wrapper">
  <div class="box" id="red"></div>
  <div class="box" id="blue"></div>
  <div class="box" id="green"></div>
</div>

Answer №4

Solutions for Colorful Display

<style>
#red {
  background: red;
}
#blue {
    background: blue;
}
#green {
    background: green;
}
.box {
  display: none;
}
.box:target {
    display: block;
}

</style>
<a href="#red">Red item</a> | 
<a href="#blue">Blue item</a> | 
<a href="#green">Green item</a>

<div class="box" id="red">Red</div>
<div class="box" id="blue">Blue</div>
<div class="box" id="green">Green</div>

Explanation of Code:

I have discovered a way to make the items appear colorfully. However, for it to work correctly, you must specify a background color. I hope this is the desired outcome.

Notes and Sources:

At this time, I do not have any specific sources for the provided code snippet. Please remember to add a background color to ensure proper functionality.

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

When clicking on a name in the leaderboard, receive comprehensive information in a separate window or a popup

I have a unique leaderboard setup here featuring four JavaScript tabs that showcase different leaderboards when clicked. To run the code snippet, please see below: $(document).ready(function() { $('.tab a').on('click', function(e) ...

Unique design for circular checkbox inside Bootstrap popover

I am experimenting with a custom round checkbox .round { position: relative; } .round label { background-color: #fff; border: 1px solid #ccc; border-radius: 50%; cursor: pointer; height: 28px; left: 0; position: absolute; top: 0; wi ...

Ensuring the input field and button are positioned in a horizontal line

Currently, I am developing a to-do list application using React and Chakra UI. I am trying to align the input field and the button on the same line for a more organized layout. This is the current layout: image I aim to achieve a design similar to this: ...

How can I create curved text in Three.js?

I'm currently experimenting with Three.js in an attempt to create an object featuring curved text. Would using TextGeometry be the most appropriate approach for this task? var material = new THREE.MeshPhongMaterial({ color: 0xdddddd }); var ...

What is the best way to display value in a dropdown option field using Laravel?

I am currently working with 3 tables: hosts, visitors, and visitor_types. My objective is to display the host name and visitor type in a drop-down option button. However, I find myself a bit perplexed when it comes to the controller and route code. I have ...

Unable to hear sound - JavaScript glitch

Spent countless hours trying to figure this out - Can't get the audio file to play when clicking an HTML element. var sound = document.querySelector(".soundfile"); function playAudio() { sound.play(); } document.querySelector(".btn-hold").addE ...

Discovering elements with Selenium through XPATH or CSS Selector

Having trouble locating the "import" element using Selenium Webdriver in C#. I've attempted the following codes but haven't had any luck: driver.FindElement(By.XPath("//*[@class='menu_bg']/ul/li[3]")).Click(); driver.FindElement(By.XPa ...

When provided with no input, the function will output true

I'm having trouble understanding this problem! I've implemented a basic jQuery validation that checks if an input field is empty when a button is clicked. http://fiddle.jshell.net/fyxP8/3/ I'm confused as to why the input field still retu ...

Wicked_PDF struggles to display Bootstrap 4 styles

After addressing my previous issue with wicked_pdf, I am now facing a problem where the PDF generated is not displaying the Bootstrap styling of the application. The technologies I'm using include bootstrap 4, ruby '2.4.2', and gem 'ra ...

Capture an image of the canvas and showcase it on a different HTML page using solely JavaScript

Currently, I am attempting to capture an HTMLCanvasElement and showcase the result on a different webpage without the need for uploading the image to a server. Initially, my plan was to store the image in local storage as a base64 data URI, but upon inspec ...

Enhancing WordPress Menus with Icons in PHP

Is there a way to customize the HTML output of a menu created with wp_nav_menu()? I want to include < i > tags within the link < a > of each < li > item in the menu. I understand that I could achieve this using background-images in the C ...

How can I modify the text that appears when hovering over an element?

Can the displayed text be altered on mouse hover? For instance, can I change the text of a H1 tag when hovering over it using HTML, CSS, and JavaScript? ...

I am looking to remove the magnificent popup jQuery effect

Appreciate the assistance so far. This issue is a bit tricky for me, as it involves jquery which I'm not well-versed in. My aim is to make my image clickable. <div class="col-lg-6 col-sm-12 mt-3"> <a class="portfolio-box" href="im ...

Encountering an issue in Angular 8 where a class is not being added after the page loads, resulting in an

Looking to dynamically add a class to a div element using Angular? I have a condition isTrue, and am utilizing the angular function ngAfterViewInit() to add the class hideOnLoad after the page loads when isTrue becomes true. Instead of traditional javascri ...

I am currently working on a one-page website that will feature both a Careers form and a Contact Us form. However, I am facing a challenge with the submission

I am currently working on integrating Careers FORM and Contact Us FORM into a single page website. However, I am facing an issue with the form Submit Buttons. The problem arises during validation of the input boxes. How can I differentiate between the two ...

php$json_data = json_decode($response);

I am brand new to PHP coming from a background in Python. I was fairly comfortable with retrieving JSON results from websites in Python, but for some reason, I can't seem to get it working in PHP. Here is the code snippet I'm using: <?php $ ...

Container Slides Along Despite Accurate Measurements

In a recent project of mine, I utilized two containers positioned side by side. Upon clicking a button, my goal was to have the content container (essentially a false body) decrease its width while the menu container would slide in. Despite my best effort ...

Ways to position cards within a carousel using Bootstrap 5

My Bootstrap 5 carousel contains simple cards. The issue I'm facing is that when a card has less text, its height becomes smaller and the carousel jumps while sliding. Is there a way to make all the cards in the carousel stretch to the same height as ...

While experimenting with Express and node, I decided to utilize Fetch to send some data to the server. However, I encountered a frustrating 405 error. I am currently at a loss on how to troubleshoot and resolve this

Recently, while working with Express and Node, I encountered an issue when trying to use Fetch to POST some data to the server. Unfortunately, I received a 405 error message. At this point, I'm not sure how to troubleshoot and resolve this issue. http ...

Issue with Bootstrap 4 nested collapse: "data-parent" attribute malfunctioning

I'm looking to implement the collapse data-parent options in a way that mimics traditional accordion behavior: all collapsible elements under a specified parent should close when a new one is opened. However, I am encountering issues with this setup. ...