Local HTML files are failing to link with CSS, whereas everything is functioning properly on Github Pages

Working on a beginner Web Development project, I encountered an issue with linking my CSS file to my portfolio.

The page displays correctly on GitHub pages, but fails to load properly when accessed locally on any browser even after clearing the cache.

Here is the HTML code:

<!DOCTYPE html>
<html>
    <head>
        <meta charset='UTF-8'>
        <meta name=“viewport” content=“width=device-width, initial-scale=1.0”>
        <meta http-equiv=“X-UA-Compatible” content=“ie=edge”>
        <link href='styles.css' rel='stylesheet' type='text/css' />
        <title>Mohsen Bakhit</title>
        <script src="script.js" ></script>
    </head>
    <body>
        <div class='header'>
            <h1>Mohsen Bakhit</h1>
            <h2>Developer For Hire</h2>
            
        </div>
    </body>
</html>

And this is the CSS file:

html {
    font-family: 'Montserrat', 'Open Sans', monospace;
    font-size: 16px;
}

.header {
    position: relative;
    text-align: center;
    color: black;
    background-image: url('/resources/vancouver.jpg');
}

.header h1{
    color: white;
    font-size: 2rem;
    position: absolute;
    top: 50%;
    bottom: 50%;
    transform: translate(-50%, -50%);
    
}

.header h2{
    font-size: 1.25rem;
    color: white;
    position: absolute;
    top: 50%;
    bottom: 50%;
    transform: translate(-50%, -50%);
}

None of the styles or fonts are showing up, despite both files being in the same folder and no need for a relative path in the link element. Would really appreciate some assistance.

Answer №1

Make sure to include the correct relative path for your files. To do this, you should use:

<link href='./styles.css' rel='stylesheet' type='text/css' />
 <script src="./script.js" ></script>

Instead of:

<link href='styles.css' rel='stylesheet' type='text/css' />
<script src="script.js" ></script>

Assuming that script.js, style.css, and index.html are all located in the same folder.

Answer №2

Let's take a look at some steps you can follow to resolve your issue:
In the beginning, double-check if the name of the css file matches the one you have specified in the href. Ensure that your css file is indeed named as 'styles' as mentioned in your code under href="styles.css".
Next, if your css file resides in the same directory as your html file, it should function properly. However, if your css file is located in a different folder other than where the html file is stored, here's what you need to do:
If your css file is in a separate folder, Follow these guidelines:
Condition 1: If there exists a folder called "CssStyles" alongside your html file which houses your css file, you should make a modification to your html like so:

<link rel="stylesheet" type="text/css" href="CssStyles/styles.css" />

Conditon 2: Suppose there is a folder titled "CssStyles" containing your html file, within this folder, there lies another folder named "css", and inside that folder, you'll find your css file. In such a scenario, follow these instructions:

<link rel="stylesheet" type="text/css" href="CssStyles/css/styles.css">

Answer №3

It was quite funny how the problem stemmed from me naming my file "styles.css". Once I made the simple switch to name it style.css and adjusted the relative URL accordingly, everything fell into place and began functioning perfectly.

Answer №4

Your issue lies with the URL link, it needs to match your folder name exactly.

For instance, if my folder name is "CSS" and I had in my URL link as which wasn't functioning properly. Upon further inspection, I realized that my "css" wasn't capitalized, so I corrected it to and now all styles are displaying correctly.😊

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 NodeJS to fetch external page data and return Javascript variable information

I need to retrieve the entire DOM of a specific page by sending a request, essentially crawling the website. The HTML directly includes a variable with some data, instead of it being in a separate script file. With my NodeJS backend, I am utilizing request ...

A versatile Material UI paper that adjusts its dimensions dynamically

Utilizing Material-UI's Paper component (https://mui.com/components/paper/), I've encountered a scenario where the content within the Paper element needs to be dynamic. <Paper className="modal" elevation={3}> ...Content </Paper ...

Tips for inserting an HTML tag within an object

How can I format a list from an array of objects in React? The 'answer' key consists of the text... Question: blah, Answer: `List title The top advantages of Kin are: 1. 2. 3. 4. ` I want to create so ...

Tips for effectively resizing an iframe within a grid layout

Task Tailwind React When adjusting the scale of an iframe, it disrupts its position within the grid. Expected Outcome: The iframe should maintain its position after scaling, but with a smaller size. Current Behavior: The iframe's position is be ...

How can I format the input type number with a thousand separator as 123.456.789?

When entering the number 123456, I want to see 123.456 displayed in the input field. I tried using my code, but it displays 123,456 instead. Is there a way to ensure that the thousand separator is a dot and not a comma? You can view my code at this link ...

The issue of JQuery Datatables columns overlapping arises during the application of column filtering

I have integrated a Jquery DataTable inside a Bootstrap division. The Bootstrap row is split between the DataTable and a Chartjs chart. Initially, everything displays correctly without any overlapping. However, when I implemented code to enable column fil ...

Setting up conditional select options in Angular

I need to create a select list with specific options based on the data in an object. For instance, if the data property is 0, I want to display an option as 0 with the ability to switch to 1, and vice versa. However, when rendered in HTML, no option value ...

Adjusting Border Lengths with Media Queries

I'm currently using the following code, but I'm facing an issue with media queries on iPAD where the right border is too close to the second Div. I'd like the border to align properly for all devices and browsers when using media queries. An ...

How can you style a two-item list in Material-UI React to display the items side by side?

I have a list that contains two items: 'label' and 'value'. This is the current layout: https://i.stack.imgur.com/HufX7.png How can I rearrange the items on the right to be positioned next to the label on the left? https://i.stack.im ...

What steps can be taken to address issues with the counting system in a lootbox opening

I am currently developing a virtual loot box simulator and have encountered a challenging issue. My goal is to track the quantity of Normals, Legendaries, Epics, and Rares obtained by the user. However, I am facing a problem where instead of updating the c ...

Designing a dropdown menu form that populates with data from a SQL foreign

Any assistance is appreciated! I am looking to create a dropdown for the foreign key customerID in my address form. The relationship in the table can be viewed here: https://i.sstatic.net/DQSkZ.png I also want the first name and last name to be displayed ...

Centering text under a round image in Bootstrap 3: A step-by-step guide

When using bootstrap 3, I have noticed that the text is not perfectly aligned under a circular image. This issue becomes more apparent on various screens, in bootstrap modals, and when dealing with different languages. I would like to avoid adding margin-l ...

implement a new directive in AngularJS that references another directive in HTML

Check out the Fiddle here Upon button click, a modal window appears. Inside this modal, there is a <template-placeholder></template-placeholder>. When the button is clicked, an if-check is performed to ensure it's the correct button. If ...

Is it possible to enhance the appearance of the select2 options in any way?

I am currently using a select2 plugin to handle select boxes, where the options are generated by concatenating two values together. However, I am looking for a way to style one of the values in the select2 box with a different color. Are there any options ...

What is the best way to position a background image at the top of a div without overlapping the

I have this HTML code: <div class="page"> <div id="header"> ... </div> </div> Below is my CSS: .page { width: 1028px; background-color:#103250; margin-left: auto; margin-right: auto; } ...

Choose a JavaScript function by clicking on the HTML text

As a beginner in the world of coding, I have been diving into JavaScript, HTML, and CSS. Inspired by fictional supercomputers like the Batcomputer and Jarvis, I've challenged myself to create my own personal assistant to manage tasks, games, programs, ...

PHP bug causing inaccuracies in output results

I've written a PHP script to determine a student's final grade, taking into account weight and target grades. However, the output value it generates is incorrect. Below is my current PHP code: <?php $numassignments = isset($_GET['numass ...

Styled-components is not recognizing the prop `isActive` on a DOM element in React

In my code, I have an svg component that accepts props like so: import React from 'react'; export default (props) => ( <svg {...props}> <path d="M11.5 16.45l6.364-6.364" fillRule="evenodd" /> </svg> ) ...

I am having trouble with Django Bootstrap when trying to center this container

I've been struggling to center this div container following my navbar in order to achieve equal spacing on both sides of the page. Unfortunately, my attempts have been unsuccessful. Could anyone provide assistance, please? The issue seems to be occurr ...

Struggling to connect HTML with CSS in Hugo

I am experimenting with a pre-fabricated theme in Hugo and incorporating some Bootstrap codes. I want to change the color of all links when displayed or hovered over. Despite searching extensively, I have been unable to achieve this through the use of CSS ...