Update the content on the HTML page

I am new to coding with HTML and CSS and recently came across a sidebar example that caught my eye. However, I'm facing an issue where the content appears the same on all the different pages of the sidebar.

How can I make sure that the content changes based on the page?

While navigating through the web pages using the sidebar, I noticed that the URL changes but the content remains constant. I suspect there might be an error in the code or perhaps I need to create separate classes for each page, but I'm unsure about the syntax to achieve this.


                <head>
                <title>OpereX Project</title>
                <style>
                    body {
                        font-family: "Lato", sans-serif;
                    }
                    
                    .sidenav {
                        height: 100%;
                        width: 160px;
                        position: fixed;
                        z-index: 1;
                        top: 0;
                        left: 0;
                        background-color: #99ccff;
                        overflow-x: hidden;
                        padding-top: 20px;
                    }
                    
                    .sidenav a {
                        padding: 6px 8px 6px 16px;
                        text-decoration: none;
                        font-size: 25px;
                        color: black;
                        display: block;
                    }
                    
                    .sidenav a:hover {
                        color: #f1f1f1;
                    }
                    
                    .main {
                        margin-left: 160px;
                        font-size: 28px; 
                        padding: 0px 10px;
                    }
                    
                    @media screen and (max-height: 450px) {
                        .sidenav {padding-top: 15px;}
                        .sidenav a {font-size: 18px;}
                    }
                </style>
                </head>
                <body>

                <div class="sidenav">
                <a href="#about">About</a>
                <a href="#services">Services</a>
                <a href="#clients">Clients</a>
                <a href="#contact">Contact</a>
                </div>

                <div class=main>
                <h2>Project</h2>
                </div>
                
                </body>
                </html>
            

Answer №1

To achieve the desired result, using just HTML is not enough. You will need to incorporate JavaScript in order to replace the content of the 'main' section effectively. This can be done with plain JavaScript or by utilizing popular frameworks like Angular or similar ones. If you prefer sticking to HTML and CSS for simplicity, there are two possible options:

1 - Create separate HTML pages for each piece of content, ensuring that the general layout and sidenav elements are consistent across all pages. Then, modify the navbar links to direct users to the respective HTML pages.

<a href="services.html">Services</a>

While this method is straightforward, it may become challenging to manage as the page expands. However, it allows you to work within your comfort zone and serves as a good learning opportunity for mastering the basics.

2 - Consolidate all content onto a single page and designate named sections for different segments. Maintain the existing links as they target IDs on the current page (using the '#idname' syntax). Simply assign IDs to each section that correspond to the links.

<div id="clients">
    clients content
</div>

These links will smoothly scroll your page down to the specified section upon selection. Although this may not align exactly with your initial goal, it presents a viable solution using only HTML and CSS. Feel free to explore an example of the second approach in action through this fiddle: https://jsfiddle.net/gouk0b8t/

Answer №2

It is advisable to create additional pages, such as about.html, and input relevant content there. Furthermore, make sure to update the anchor tag's href attribute accordingly.

<head>
    <title>OpereX Project</title>
    <style>
    body {
        font-family: "Lato", sans-serif;
    }
    
    .sidenav {
        height: 100%;
        width: 160px;
        position: fixed;
        z-index: 1;
        top: 0;
        left: 0;
        background-color: #99ccff;
        overflow-x: hidden;
        padding-top: 20px;
    }
    
    .sidenav a {
        padding: 6px 8px 6px 16px;
        text-decoration: none;
        font-size: 25px;
        color: black;
        display: block;
    }
    
    .sidenav a:hover {
        color: #f1f1f1;
    }
    
    .main {
        margin-left: 160px;
        font-size: 28px;
        padding: 0px 10px;
    }
    
    @media screen and (max-height: 450px) {
        .sidenav {padding-top: 15px;}
        .sidenav a {font-size: 18px;}
    }
    </style>
    </head>
    <body>
    
    <div class="sidenav">
      <a href="about.html">About</a>
      <a href="services.html">Services</a>
      <a href="clients.html">Clients</a>
      <a href="contact.html">Contact</a>
    </div>
    
    <div class=main>
      <h2>Project</h2>
    </div>
         
    </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

Using a div in React to create a vertical gap between two React elements

I am working with two React Components in my project - the Right Column and the Left Column. I am trying to create space between them using a div tag. Currently, my setup in the App.js file looks like this: import React from 'react'; import LeftC ...

Centering a floating div in IE causes issues

Suppose I have the following HTML: <div id="submitPage" align="center"> <div id="middlecontainer"> <p align="center" id="loading-image">&nbsp;<img src="/cso/images/loading.gif" border="0"> <font colo ...

Interactions with jQuery and the .load() method

Encountering a new issue with jQuery that I haven't seen before. Here's the code snippet: $(document).ready(function() { $('#browser_cat a').click( function() { $('#browser_cat').hide(); $('#browser_c ...

Container slide-show fill error

I'm attempting to create a slide show with an overlapping caption that appears when hovering over the container or image. The image needs to fit exactly inside the container so no scroll bar is shown and the border radius is correct. I managed to achi ...

I'm curious as to why a webpage tends to load more quickly when CSS files are loaded before script files. Can anyone shed some

While watching a video, I came across some concepts that were a bit difficult for me to grasp. The video mentions that scripts are 'serialized' and can block subsequent files from loading. According to this explanation, Script 1 would load first ...

Equal-height columns in a responsive grid system

I'm currently facing an issue with a layout that seems simple I am working on a design featuring 4 headline boxes. In the "desktop" view, all 4 boxes need to be of equal height. The same applies when viewed across 2 boxes in the "tablet" mode. Howeve ...

I am looking to optimize my custom tailwindcss file by reducing its size

Here is the structure of my package.json file. "scripts": { "start": "yarn run watch-css && craco start", "build": "yarn run build-css && craco build", "test": "craco test ...

Modify the appearance of certain text within an input field

I need some help adding style to specific text within an input field. For example, if the user types in "Hello world" and the special text is "world" I want to achieve this result: https://i.sstatic.net/Ozd6n.png This is what my HTML code looks like: & ...

Adjusting the columns of two tables when one table is held in place

My dilemma involves managing two tables: In the scenario, Table 1 remains fixed in place serving as a header that is always visible. Meanwhile, Table 2 acts as the data table. I am not a fan of fixed widths for my tables; I prefer them to dynamically adj ...

What is the best way to resolve the CSS border styling issue surrounding the anchor element?

I created a simple sign-up form for my school project where users can choose their roles. I'm facing an issue with the border under the anchor tag and I need help fixing it. How can I ensure that the space at the top is equal to the bottom as well? T ...

What could be causing my form to not be centered on the screen?

Currently working on creating a form for adding books to a fictional library website. The form tag is enclosed within a div, but despite the CSS styling applied, it remains fixed to the left side of the screen and I'm struggling to understand why. .fo ...

Is there a more efficient method for transforming an HTML form into an object through serialization?

I'm in the process of creating a form that includes both radio and checkbox inputs. Since multiple elements share the same name, I have developed the following solution. Is there a more efficient and simpler way, using only pure JavaScript, to conver ...

Ways to choose tags that do not contain a particular tag within them

I am trying to utilize querySelectorAll to select all i elements that do not contain a font tag. The selector I attempted is: document.querySelectorAll('i > *:not(font)') <i> <font color="008000">Here goes some text</fon ...

Is the row border camouflaged by the background?

feed_page: { margin: 'auto' }, feed_list: { margin: 'auto' }, feed_item: { textAlign: 'center', backgroundColor: '#fff', borderBottom: '1px solid #e0e0e0', margin: '10 ...

When using the `display: table-cell` property on a div element, it does not automatically respect

How can I restrict the width of columns by defining a width attribute on the div.dcolumn DOM element to preserve the layout with overflowing cells? I want the content of any overflowing cell (like the 9px column) to be hidden while maintaining the specifie ...

When images are placed within a div, they become tightly packed together and do not allow

I am looking to display the uploaded images in a div while maintaining their width, height, and spacing between them. They should also be horizontally scrollable if they overflow the div. document.querySelector("#files").addEventListener("change", (e) ...

Modify the color of a hotspot area when the mouse is hovered over it

I have a task of dynamically generating HotSpots on an Image Map control. Below is the code snippet that accomplishes this: // Creating a RectangleHotSpot programmatically. RectangleHotSpot Rectangle1 = new RectangleHotSpot(); Rectangle1.Top = 50; Rectang ...

` `CSS hover effect not displaying``

I am struggling with a CSS menu that features drop downs. While I have experience building drop down menus and working with CSS hover effects in the past, this particular menu is proving to be quite challenging. After researching similar CSS :hover and CS ...

Tips on utilizing jQuery to trim and manipulate the current URL

Suppose the current URL I am working with is: https://example.com/test?id=W2FiY11beHl6XVsxMjNd . The data following ?id is [abc][xyz][123] and it is base64 encoded. My objective is to manipulate the current URL in such a way that the content displayed on ...

Automatically adjust height directive to fill up the remaining space

Is there a way to dynamically adjust the height of an element to fill the remaining space within its container? In my current layout, I have a fixed-height header (or menu) in pixels, a content area that may overflow the window height and require scroll b ...