Using :target in CSS for transitions

I'm in the process of setting up a system that allows me to control which div is displayed on the screen at any given time. I have two divs that should be displayed using the buttons "page1" and "page2".

My approach involved giving each div an absolute position and a width of 100%. I then applied a CSS transform to shift both DIVs 100% to the left so they are offscreen.

Next, I aimed to use the :target pseudo-class to remove the transform from the selected div.

#page1 {
position: absolute;  
text-align: center;
background: blue;
float: left;
width: 100%;
transform: translateX(-100%);
}

#page2 {
position: absolute;
text-align: center;
background: red;
float: left;
width: 100%;
transform: translateX(-100%);
}

:target {
transform: translateX(0);
}

Manually setting the transform to 0 in the CSS results in the DIV displaying correctly. However, clicking the link doesn't produce any effect. Am I overlooking something here? Thank you.

Here is the full HTML/CSS file:

<html>
<head>
<style>
body {
margin: 0;
padding: 0;
}

.controlBar {
text-align: center;
float: left;
width: 100%;
background-color: green;
}

.pageContain {
float: left;
width: 100%;
}

#page1 {
position: absolute;  
text-align: center;
background: blue;
float: left;
width: 100%;
transform: translateX(-100%);
}

#page2 {
position: absolute;
text-align: center;
background: red;
float: left;
width: 100%;
transform: translateX(-100%);
}

:target {
transform: translateX(0);
}
</style>
</head>
<body>
<div class="controlBar">
    <h1>Control Bar</h1>
    <a href="#page1">Page 1</a>
    <a href="#page2">Page 2</a>
</div>
<div class="pageContain">
    <div id="page1">
            <h1>page 1</h1>
    </div>
    <div id="page2">
        <h1>page 2</h1>
    </div>  
</div>
</body> 
</html>

Answer №1

Make sure to assign unique IDs to your target <div> elements in order for them to be correctly identified by the hash links.

For example:

<div class="section1" id="section1">
    </h1>Section 1</h1>
</div>

Answer №2

#section1:target,#section2:target{
   -webkit-transform:translateX(0);
   -ms-transform:translateX(0);
   transform:translateX(0);
}

Feel free to check out this demonstration on jsfiddle: http://jsfiddle.net/FjeaE/

Answer №3

If this is the code you are currently using, it appears that the transform styles are not being applied at all. The CSS refers to classes .page1 and .page2, but the corresponding divs lack these classes. You can either update your CSS to target #page1 and #page2, or add the missing classes to the divs. Here's an example:

<div class="pageContain">
    <div id="page1" class="page1">
            <h1>page 1</h1>
    </div>
    <div id="page2" class="page2">
        <h1>page 2</h1>
    </div>  
</div>

By making these adjustments, the provided CSS should work as intended, at least in Firefox.

Answer №4

The individuals you are aiming for are making use of the id attribute, while your CSS is targeting the class attribute:

#page1{
position:absolute;  
text-align:center;
background:blue;
float:left;
width:100%;
transform:translateX(-100%);
}

#page2{
position:absolute;
text-align:center;
background:red;
float:left;
width:100%;
transform:translateX(-100%);
}

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

Alter the color of the text within the `<li>` element when it is clicked on

Here is a list of variables and functions: <ul id="list"> <li id="g_commondata" value="g_commondata.html"> <a onclick="setPictureFileName(document.getElementById('g_commondata').getAttribute('value'))">Variable : ...

Encasing distinct HTML text with a custom color palette

How can I create a specific color scheme for my HTML table's Status parameter, where the values can be SUCCESS, FAILURE, or IN PROGRESS? I'm utilizing Angular 4 (HTML and TypeScript) for this task. Any tips on how to achieve this? ...

A crisscrossing dashed design running along the edges of the text block

<div class="search"> <p>SEARCH</p> </div> https://i.sstatic.net/yxOga.png I am attempting to incorporate a dashed lateral padding similar to the one shown in the image, but only on the sides of the text. Is there a method I can ...

Conceal and Reveal Navigation Bar

On a mobile screen, here is how my navigation bar looks: Just to clarify, I am currently only focusing on making the nav bar responsive and not other content. The stylus code for the navigation bar is as follows: -> code in stylus!!! .nav display ...

The alignment of SVG is not in sync with the aspect ratio

I've been struggling with this issue for some time now, trying to align the SVGs correctly for different width and height values. My viewBox has an aspect ratio of 16:9, and I want to scale the SVG accordingly. It's working fine for certain combi ...

"Retrieve data from an Excel file and either present it in a textarea or store it as an array

Is it possible to use JavaScript to read an Excel file and convert a single column (identified by name or index) into an array in JavaScript? ...

Adjusting the text of a button when hovering over it will also trigger a resizing of the

Currently, I am facing an issue where the bootstrap button's size changes when hovered over. My intention is to have the bootstrap button remain a fixed size while only the text inside it changes using javascript for mouseover and mouseout events. How ...

Center the p tag vertically

To ensure the text inside the p tag aligns vertically in the middle, I've set a specific height for the tag. While this works perfectly for single-line text, it shifts to the top of the p tag when there are two lines of text. It's important to k ...

Problem with Internet Explorer version 9 and above and the Flip Card feature

I have a cool feature for one of my clients where clicking on one div causes another div to flip in its place, like a card flipping over. It's kind of like those portfolio image flippers, but instead of images, it flips divs with content inside. How ...

Tips for integrating CSS3 into Android WebView

Currently, I am working on an Android application that utilizes HTML5. My goal is to implement element rotation using CSS3 transformations. However, I need to ensure that the app remains compatible with Android 2.3. What would be the most effective metho ...

Is there a way to prevent a web page from automatically refreshing using JavaScript?

I would like my webpage to automatically refresh at regular intervals. However, if a user remains inactive for more than 5 minutes, I want the refreshing to stop. There is an example of this on http://codepen.io/tetonhiker/pen/gLeRmw. Currently, the page ...

Retrieving a value from an array at random to assign to a different variable

I have different options for a specific variable depending on the scenario --> var lowSpeed = Math.random() * (45 - 30) + 30; var mediumSpeed = Math.random() * (60 - 45) + 45; var highSpeed = Math.random() * (80 - 60) + 45; var highwaySpeed = Math.rando ...

Load a new texture dynamically in THREE.JS and GLTF during runtime

I am relatively new to Three.JS and have made progress, but I am in need of assistance. I have successfully loaded a GLTF object into the scene and now I am looking to implement a feature that allows users to customize the object by selecting different te ...

Using AngularJS to add an element to an array based on a specified condition

dashboardCtrl Data app.controller('dashboardCtrl', ['$scope','$rootScope', function ($scope, $rootScope) { //Color $scope.Color = [{ colorname: "Red", number: "(3)" }, { colorname: "Brown ...

Bootstrap navbar partially collapsed with inner items concealed not at the edges

Many discussions have come up regarding partially collapsing Bootstrap navbars, such as: Item1 Item2 Item3 Item4 Item5 Is it possible to achieve the following collapsed format: Item1 Item2 Item3 =menu= Or: =menu= Item3 Item4 Item5 This method ...

Embed schema information into HTML tags using JavaScript

Is there a way to insert text, specifically schema data, into an HTML div tag using JavaScript? While I know how to modify existing values within a tag like class, href, and title, I am struggling to find a method to add something entirely new. Essentiall ...

Dynamic web designs can be achieved by utilizing a combination of technologies such as Ajax

Encountering issues while attempting to use Ajax and Fancybox.js photo viewer simultaneously. The website is initially set up as web 1.0 with standard navigation using hyperlinks. For html5 browsers, a JavaScript is utilized to create a web 2.0 experienc ...

Complete the JQuery code by closing the div and ul tags, then proceed to open a

Need help with structuring HTML <div class="content"> <p>Lorem ipsum dolor sit amet</p> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <span>Dolor set amet conse ...

Ways to correct the issue of multiple <div>s overlapping each other after being rotated by 90 degrees

Can someone assist me with rotating multiple divs without them overlapping? Below is a simplified version of my code: <div> <div class="rect"></div> <div class="rect"></div> <div class="rect"></div> & ...

JavaScript communication between clients and servers

I am looking to develop two scripts, one for the client-side and one for the server-side. I came across a snippet that allows for asynchronous calling of JavaScript: <html> <head> </head> <body> <script> (function() { ...