Navigating through pages without using Javascript

While working on creating page numbers, I stumbled upon this intriguing CodePen: https://codepen.io/p-ziegler/pen/bmdaaN

.b,
input[type="radio"] {
    display: inline-block;
    width: 16px;
    height: 16px;
    vertical-align: middle;
}

.b {
    background-color: gray;
    text-align: center;
    color: white;
}

input[type="radio"] {
    opacity: 0.01;
    margin-left: -16px;
    cursor: pointer;
}

.p {
    display: none;
}

#p1:checked ~ .p1,
#p2:checked ~ .p2,
#p3:checked ~ .p3 {
    display: block;
}
<div>
    <div class="b">1</div><input type="radio" name="p" id="p1" checked>
    <div class="b">2</div><input type="radio" name="p" id="p2">
    <div class="b">3</div><input type="radio" name="p" id="p3">
    <div class="p p1">Page 1</div>
    <div class="p p2">Page 2</div>
    <div class="p p3">Page 3</div>
</div>

One notable aspect is that Javascript isn't necessary for this. However, I encountered a problem when attempting to relocate the page numbers to the bottom below the content while placing the content above the number set. Upon making this change, all the contents vanished. Any guidance on how to resolve this issue would be greatly appreciated.

Answer №1

Instead of relying on radio inputs to manage routes, a more efficient approach is to use the :target pseudo class in CSS. This method eliminates the concern for the source order of your HTML elements.

One drawback of this solution is handling the default page. Below is a functional code snippet demonstrating how to set a default page (Home) using CSS:

/* Hide all sections by default */
section {
    display: none;
}
/* Display Home section by default */
#page1 {
    display: block;
}
/* Show targeted section */
section:target {
    display: block;
}
/* Hide siblings of the targeted section that are designated as default */
section:target ~ #page1 {
    display: none;
}

/* Styling */
nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
nav ul li {
    display: inline-block;
}
nav ul li a {
    background: gray;
    color: white;
    padding: 0.25rem;
    text-decoration: none;
}
<!DOCTYPE html>
<html>
<head>
    <title>CSS only SPA</title>
</head>
<body>
    <nav>
        <ul>
            <li>
                <a href="#page1">1</a>
            </li>
            <li>
                <a href="#page2">2</a>
            </li>
            <li>
                <a href="#page3">3</a>
            </li>
        </ul>
    </nav>

    <main>
        <section id="page3">
            <h1>Page 3</h1>
            <p>
                Proin ante arcu, hendrerit ac diam sagittis, sollicitudin posuere ante. Etiam egestas eros at nunc venenatis eleifend eu at tellus.
            </p>
        </section>

        <section id="page2">
            <h1>Page 2</h1>
            <p>
                Integer porttitor vitae est vel suscipit. Fusce placerat justo at libero aliquam maximus.
            </p>
        </section>
        
        <section id="page1">
            <h1>Page 1</h1>
            <p>
                Ut aliquet sit amet urna non porta. Maecenas dignissim, sem eget pulvinar luctus, ex dolor posuere magna, ut elementum quam eros vel nulla.
            </p>
        </section>
    </main>
</body>
</html>

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

The execution of jQuery code from PHP using $.ajax is not functioning properly

I am attempting to run a jQuery code from a PHP page using AJAX. Modify the HTML of div.error Here is the code (Let me know if there is a simpler method): - no $.load HTML/jQuery: jquery.js already loaded <input type='text' id='test&a ...

Ways to prevent styles from being overridden by those specified in JavaScript

Some elements on my page have their style dynamically changed by JavaScript. However, I want one of them to maintain its static style without being overridden. Is there a way to specify that the style of this particular element should not be altered? Than ...

Enhance the functionality of a directive by incorporating the ui-mask directive

Recently, I implemented a custom directive for an input field that adds a calendar icon with a datepicker to the input. I have used this directive in various sections of my application and now I am interested in incorporating ui-mask - another directive I ...

Stop images within contenteditable divs from being easily dragged and dropped into unrelated div elements

In order to maintain data integrity, I have implemented a rule where users are allowed to drag images within a specific div. Later on, I need to retrieve the positions of these images within the div. However, it is crucial for my business requirements tha ...

Using different CSS classes interchangeably in AngularJS

Imagine you have a list with an unknown number of items, ranging from one to potentially dozens. You want to display five CSS classes in a regular alternating pattern. What would be the most effective approach to achieve this? Here is some sample HTML cod ...

Embed HTML code to a website using a PHP script on a separate webpage

As a newcomer to PHP, I may have a silly question. Let's say I have a form: <form id="createNewGallery" name="newgallery" method="GET" action="code.php"><p><strong>please choose the number of pictures you want to upload: </str ...

Custom styles for PrimeNG data tables

Struggling to style the header of a datatable in my Angular project using PrimeNG components. Despite trying various solutions, I am unable to override the existing styles. Even attempting to implement the solution from this PrimeNG CSS styling question o ...

Is it true that by utilizing Vue's v-for, every line of HTML code becomes a

I'm struggling to utilize v-for in order to create multiple div elements, but no matter how I attempt it (even trying to iterate over a list), the v-for doesn't seem to function properly and always turns the line into a comment when executed. No ...

Tips for implementing a search filter in a select dropdown menu using Angular

I am attempting to enhance my select option list by including a search filter. With numerous options available, I believe that having a search function will make it easier for the user to locate their desired selection. I hope my message is clear despite ...

Ways to eliminate the unusual dashed space in ReactJS using Bootstrap

While developing an app with NextJS and Bootstrap, I noticed a strange dashed gap at the top of the screen in the elements tab. Despite checking for margin or padding-top properties on any element, there doesn't seem to be a clear cause for this issue ...

What is the best way to eliminate the gap between header, content, and footer sections when in mobile view?

This issue seems to only occur in mobile mode, where the blueviolet line is coming from the background color. https://i.stack.imgur.com/VBhIp.png link to the image body { background-color: blueviolet; } header, main, footer { background-color: #ff ...

Using a CSS gradient with a variable in React.js - A guide to implementation

Looking to incorporate the following CSS property into the Navlink component. In the CSS file, the property is usually written like this using gradient. .ele { background-image: linear-gradient(45deg, #808080 25%, transparent 25%), li ...

Ways to dynamically eliminate focus around text inputs

I have created an HTML page and here is the link to it: My main goal is to remove the black border around the text input on this page. The challenge I am facing is that the 'things to do' list is generated dynamically, meaning it is empty when t ...

Issue with jQuery select box (roblaplaca) and its custom scroll bar functionality not functioning as expected

Recently, I implemented the custom select box script from . This script allows for two select boxes, where the second one updates dynamically using AJAX when there is a change in the first select box. Below is a sample of the HTML code: <option>One& ...

The scrolling feature in IE 7 with IE 7 standard does not respond to the jquery scroll event, whereas it functions properly in IE8 and IE

I have included an example below which consists of a terms div that contains some terms and conditions as its content. The checkbox is enabled when the user scrolls to the bottom of the div. However, this functionality seems to be not working properly in I ...

When previewing a card on Twitter's validator tool, the image displays properly. However, when attempting to share the image on

Below is the code that I have written on my HTML page: <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@name" /> <meta name="twitter:title" content="Twitter - image share" /> <meta name="twi ...

jQuery UI Sortable: Efficiently Drag Items Across Multiple Interconnected Lists

One feature of using jQuery UI Sortable is the ability to make the sortable item container scroll when an item is dragged. This can be achieved by specifying the scroll option. In my case, I have several sortable lists that are connected within separate c ...

Generating forms dynamically with Vuetify

When working with HTML code, the structure looks like this: <v-col cols="12" md="4" v-for="(leadObj, i) in lead.data" :key="i" > <v-col v-if="leadObj.inputType !=='select'"> ...

Issues arising with transferring information between components

My webpage had a header with a search engine input, a list of posts, and pagination all on one page. I made the decision to separate the header into its own component in a different Vue file. However, after making this change, searching for posts by their ...

Issues encountered when dealing with floating elements and margins

Having some difficulties with aligning elements on my website. As someone who is not highly skilled in coding, I am struggling to define the width and float properties for certain classes. Despite my attempts, it seems that the elements are not positioning ...