How can CSS 3 hide an element when :target is different and show it when :target is empty?

Is there a way to achieve this concept using only CSS3?

I have an idea where if the user goes to http://example.com, I want to display a specific element on my page, such as <section id="main" />. However, if they visit http://example.com/#pics, I would like to showcase a photo gallery instead.

I am aware that this task can be accomplished with JQuery, but I am eager to explore the possibilities of utilizing CSS3. This website is just for experimental purposes, so cross-browser compatibility is not essential, though I am open to incorporating fallback options if needed.

Answer №1

To utilize this solution, ensure you are targeting elements with the "hideme" class. Make adjustments as necessary for your specific scenario.

You can achieve any "if not" condition using the :not() pseudo-class. In this instance:

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

However, as browser support for this approach is limited, consider an alternative method like:

selector {take action}
selector:target {reverse the action}

Dealing with the CSS property display can be complex due to varying default values for different elements. Therefore, you could opt for changing visibility:

.hideme {visibility: hidden;}
.hideme:target {visibility: visible;}

Another possibility is to employ position:absolute and position:static to detach the selected item from the document's layout flow.

Answer №2

If you're looking to hide specific elements based on the :target selector or the :not selector, consider the following code snippet:

div:not(:target){
  display:none
}

#main:target {
   display:block;
}

#pics:target {
    display:block;
}

For more information, refer to:
http://www.w3.org/TR/selectors/ – Section 6.6.7 covers the :not selector (no target from their file)

as well as

http://www.w3.org/TR/selectors/#target-pseudo

Answer №3

If you find yourself needing to target multiple elements in your code, you can achieve this by wrapping your code in various containers and using the target pseudo-class for selection.

Illustration

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" version="1.1">
<style>

/* Start with hiding elements */
.hide {
  display: none;
}
/* Display targeted elements */
#one:target .one,
#two:target .two {
display: block;
}

/* Additional elements selection */
#one:target .default,
#two:target .default {
display: none;
}

/* Color customization */
.one {
fill: red;
}
.two {
fill: yellow;
}


</style>
<g id="one">
<g id="two">
   <rect width="200" height="100" stroke="black" stroke-width="2" fill="green"/>
      <circle class="hide one" cx="50" cy="50" r="20"></circle>
      <circle class="hide two" cx="100" cy="50" r="20"></circle>
      <circle class="default" cx="150" cy="50" r="20"></circle>
</g>
</g>

</svg>

This approach may not be the most elegant, but it gets the job done effectively.

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

Finding all instances of a specific class in Sublime Text 2

Is there a way in Sublime Text 2 to search for every instance of a specific class applied to an element? For example, if I have 20 .jsp's and I want to find every element with the class "sample," is there a method to do this beyond just searching for ...

How can I use CSS to customize the appearance of the elements within an iframe?

<iframe src="#link"> #document <!doctype html> <html> ... </html> </iframe> Is there a way to customize the styling of elements within the #document using CSS only, without needing to use Jquery? ...

Scrolling to the bottom of the page triggers jQuery to load additional content

Is your jQuery script not working properly when attempting to load more content upon reaching the bottom of the page? It seems to be functional on Safari/iPad and Android Mozilla browsers, but encountering issues on default Android and Chrome browsers. ...

Bootstrap is experiencing some glitches

While working on implementing an admin panel into my bootstrap template, I encountered a problem. When I tried to change the slider elements into a loop to retrieve data from the database and display them as list items, the theme ended up broken. Despite h ...

Tips for Preventing Page Scrolling When Clicking on a Hashed A Tag in Content with a Fixed

Currently stuck dealing with a challenging problem related to hashed anchor links. Here's a basic representation of the HTML code: <div class="main-wrap"> <header></header> <aside> <nav> <ul> ...

Javascript editing enhancement for real-time changes

Are there any tools for in-place editing of Javascript code? I'm looking for something similar to Firebug, which is great for instant CSS editing and previewing but doesn't have the capability to edit JavaScript directly. Is there a tool or addon ...

Checking the Value of the Second Child Element Using jQuery

I am struggling with a DIV structure that looks like this: <div class="metafield_table"> <div class="metafield"></div> <div class="metafield"></div> <div class="metafield"> ...

Should pages be indexed to index.php or should the top and bottom parts of the page be created and included in all content pages?

I've been out of the webpage creation game for a while, but I've recently decided to get back into it. Like everyone else, I want to learn the best way to do this. However, I find myself facing a dilemma on how to structure my pages. My initial i ...

Guide to positioning Navigation Bar in the center

I'm currently in the process of updating my website, but I've encountered a problem that I can't seem to solve. The issue revolves around the positioning of my navigation bar. Currently, it is situated below some JavaScript content and on t ...

The layout of the table is not formatted in a single continuous line

I recently implemented the material-ui table and noticed that the header has a multiline break space. I am looking for a way to make it display in a single line instead. Is there any solution for achieving this using material UI or CSS? Feel free to chec ...

JQuery: the art of concealing and revealing

My current setup seems to be functioning, but I can't shake the feeling that it's not quite right. CSS: #logo { display:none; } JQuery: $("#logo").delay(800).fadeIn(800); While this configuration works, I'm concerned about whether j ...

What is the process for changing an image on a webpage based on a dropdown list selection?

When I came across the site , I was impressed by their feature that allows users to select their currency. By changing the currency, the symbol beside the amount entered also changes accordingly. I am interested in incorporating a similar feature on my we ...

Ensure the webpage is set to have a minimum width of 1024 pixels

Is it possible to make a webpage that utilizes bootstrap, Angular 2x, and Scss stop being responsive once the screen width reaches 1024px? I would like the layout to stay fixed at this size and enable horizontal scrolling for users who resize their browser ...

How can I use Jquery to animate an element to the right edge of the window?

Exploring the realm of jQuery animations, I am currently experimenting with animating a div to the right side of the window while changing its color using jQuery UI. This is just a fun project without any specific purpose in mind. Below is the code snippet ...

How to add a background image in CSS with HTML's help

I am a beginner learning HTML and CSS, attempting to incorporate a Full Screen Background Image using CSS. Unfortunately, the code I have written is not working as expected despite following various tutorials. Below is my code: HTML file <!DOCTYPE htm ...

Guide on generating tabs with the power of Javascript and Jquery

Is it possible to create multiple tabs on a page using jquery or javascript, where clicking buttons on the menu slides in a new page without changing the URL? I already have the HTML and CSS prepared. div.tabContent.hide { display: none; } nav { bac ...

Curved corners when an image is located on the border

Looking to create a basic website with React using Reactstrap and Bootstrap. I'm trying to add a custom border-radius to my Cards, but facing an issue where the image inside the Card is overlapping the container on the upper side. Is there a CSS prope ...

Alignment of a Definition List with Omissions of Definitions

Often, I come across applications with forms containing optional fields. In the Show view, these fields tend to "collapse" together and lose alignment with their corresponding labels. For instance, if I have 3 fields that should be displayed as: Phone: 3 ...

Font malfunctioning on Microsoft Edge and Internet Explorer

Apologies if this seems like a silly question, but I recently imported a font from my computer into a CSS file. While it displays correctly in Chrome, it's not working in Microsoft Edge or Internet Explorer. Unfortunately, I don't have access to ...

A different way to center elements using CSS

Many criticize the center tag, but I find that it consistently aligns things perfectly for me. Despite its deprecation, I still rely on it. I've come across suggestions to use the mysterious CSS margin: 0 auto;, but I struggle to make it work (refer ...