The presentation changes when styling is added through the <link> element

I've encountered a strange issue. Here's the HTML I'm currently using:

<!DOCTYPE HTML>
<html>
    <head>
        <style type='text/css'>
        <title>Site name</title>
            *{
                padding: 0px;
                margin: 0px;
                
                font-family: sans-serif;
            }
            
            html{
                background: #BED4EB;
            }
            
            body{
                background: #FFFFFF;
                width: 1000px;
                margin: 0px auto;
                padding-bottom: 25px;
            }
            
            header{
                background-image: linear-gradient(to bottom, #004F9E 0%, #00284F 100%);
                
                padding: 10px 0px;
            }
                header h1{
                    color: #FFFFFF;
                    display: inline;
                    font-size: 2.5em;
                }
                header nav{
                    display: inline-block;
                    float: right;
                }
                    header nav ul li{
                        margin: 0px 20px;
                        
                        display: inline-block;
                    }
                        header nav ul li a{
                            color: #FFFFFF;
                            
                            text-decoration: none;
                            
                            font-size: 1.4em;
                        }
                        header nav ul li a:hover{
                            color: #FFA54D;
                        }
            
            section#quick_login{
                width: 20%;
                float: right;
                background: #91A9FA;
                border: 1px solid #5D82FC;
                margin: 0px 0px 5px 5px;
                padding: 5px;
                text-align: center;
            }
                section#quick_login input{
                    padding: 3px;
                    border-radius: 3px;
                    border: 1px solid #BDBDBD;
                }
            
            section#notices{
                display: inline-block;
                width: 75%;
            }
                section#notices .alert, section#notices .notice{
                    padding: 5px;
                    display: inline-block;
                }
                section#notices .alert{
                    background: #FA9191;
                    border: 1px solid #FC5D5D;
                }
                section#notices .notice{
                    background: #91A9FA;
                    border: 1px solid #5D82FC;
                }
                    
            section#main h2{
                margin-top: 25px;
            }
            section#main p{
                margin-top: 5px;
            }
        </style>
    </head>
    <body>
        <header>
            <h1>Site name</h1>
            <nav>
                <ul>
                    <li>
                        <a href='#'>Link 1</a>
                    </li>
                    <li>
                        <a href='#'>Link 2</a>
                    </li>
                    <li>
                        <a href='#'>Link 3</a>
                    </li>
                    <li>
                        <a href='#'>Link 4</a>
                    </li>
                    <li>
                        <a href='#'>Link 5</a>
                    </li>
                </ul>
            </nav>
        </header>
        
        <section id='quick_login'>
            <h2>Quick login</h2>
            <form action='login.php'>
                Username: <input type='text' placeholder='Your username' name='username' />
                <br />Password: <input type='password' placeholder='Your password' name='password' />
                <br /><input type='submit' value='Go' />
            </form>
        </section>
        
        <section id='notices'>
            <p class='alert'>
                Your email address hasn't been verified; check your email for a link to verify it.
            </p>
            <p class='notice'>
                We received a record 553 visitors yesterday!
            </p>
        </section>
        
        <section id='main'>
            <h2>Subtitle 1</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nisl risus, viverra eu sollicitudin volutpat, ultricies vel sapien. Cras in felis eu justo mattis convallis vel et orci. Maecenas consequat fermentum magna, sit amet dapibus nulla pulvinar venenatis. Nullam nulla mi, consectetur id diam at, pulvinar tempus diam. Morbi suscipit odio nec arcu ultricies dignissim. Curabitur consectetur libero lectus, ornare sodales tellus congue nec. Nam id tellus id nulla eleifend condimentum.</p>
            
            <h2>Subtitle 2</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lacus nisi, mattis vitae volutpat tempus, suscipit nec lorem. Quisque posuere lectus odio, porttitor sagittis ligula ullamcorper quis. Vivamus ornare ut sapien in rutrum. Nullam et porttitor metus. Curabitur ornare orci sit amet aliquam rutrum. Phasellus scelerisque dignissim tellus in dictum. Pellentesque eu nibh purus. Nulla scelerisque sodales urna in feugiat. Maecenas et eleifend neque. Sed dolor turpis, congue et nisl quis, euismod bibendum magna. Sed eget laoreet urna.</p>
        </section>
    </body>
</html>

It appears properly when directly written in, except for the menu alignment issue that I am addressing:

https://i.sstatic.net/H10Xk.png
(source: gyazo.com)

However, once I switch the content of the <style> tag with

<link rel='stylesheet' type='text/css' href='style.css' />
and move everything within the <style> tags to style.css, the display changes to this:

https://i.sstatic.net/rBsd5.png
(source: gyazo.com)

Why is this happening? How can I resolve it? It seems to be browser-specific since Firefox shows it correctly, while Chrome displays it incorrectly.

Answer №1

The issue with the header display is due to an error in the title tag placement within the style section. Here is a corrected version: http://jsfiddle.net/Paul_Geronca/2ZCVw/4/

Here is the fixed code:

<!DOCTYPE HTML>
<html>
    <head>
        <style type='text/css'>
            <title>Site name</title>

            *{
                padding: 0px;
                margin: 0px;
                font-family: sans-serif;
            }

            html{
                background: #BED4EB;
            }

            body{
                background: #FFFFFF;
                width: 1000px;
                margin: 0px auto;
                padding-bottom: 25px;
            }

            header{
                background-image: linear-gradient(to bottom, #004F9E 0%, #00284F 100%);
                padding: 10px 0px;
            }
            
            header h1{
                color: #FFFFFF;
                display: inline;
                font-size: 2.5em;
            }

            header nav{
                display: inline-block;
                float: right;
            }

            header nav ul li{
                margin: 0px 20px;
                display: inline-block;
            }

            header nav ul li a{
                color: #FFFFFF;
                text-decoration: none;
                font-size: 1.4em;
            }

            header nav ul li a:hover{
                color: #FFA54D;
            }

            section#quick_login{
                width: 20%;
                float: right;
                background: #91A9FA;
                border: 1px solid #5D82FC;
                margin: 0px 0px 5px 5px;
                padding: 5px;
                text-align: center;
            }

            section#quick_login input{
                padding: 3px;
                border-radius: 3px;
                border: 1px solid #BDBDBD;
            }

            section#notices{
                display: inline-block;
                width: 75%;
            }

            section#notices .alert, section#notices .notice{
                padding: 5px;
                display: inline-block;
            }

            section#notices .alert{
                background: #FA9191;
                border: 1px solid #FC5D5D;
            }

            section#notices .notice{
                background: #91A9FA;
                border: 1px solid #5D82FC;
            }

            section#main h2{
                margin-top: 25px;
            }

            section#main p{
                margin-top: 5px;
            }
        </style>
    </head>

    <body>
        <header>
            <h1>Site name</h1>
            <nav>
                <ul>
                    <li><a href='#'>Link 1</a></li>
                    <li><a href='#'>Link 2</a></li>
                    <li><a href='#'>Link 3</a></li>
                    <li><a href='#'>Link 4</a></li>
                    <li><a href='#'>Link 5</a></li>
                </ul>
            </nav>
        </header>

        <section id='quick_login'>
            <h2>Quick login</h2>
            <form action='login.php'>
                Username: <input type='text' placeholder='Your username' name='username' />
                <br />Password: <input type='password' placeholder='Your password' name='password' />
                <br /><input type='submit' value='Go' />
            </form>
        </section>

        <section id='notices'>
            <p class='alert'>Your email address hasn't been verified; check your email for a link to verify it.</p>
            <p class='notice'>We received a record 553 visitors yesterday!</p>
        </section>

        <section id='main'>
            <h2>Subtitle 1</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit....congue nec. Nam id tellus id nulla eleifend condimentum.</p>
            <h2>Subtitle 2</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit... laoreet urna.</p>
        </section>
    </body>
</html>

Answer №2

The reason for the issue might be due to the header h1 { display: inline; } rule not being properly adhered to.

If you choose to disable this particular rule while the CSS is embedded within the page, it could lead to the rendering problem you are currently facing with the stylesheet linked via a tag.

Moreover, it's important that the title tag is placed directly under the head and not within the style section.

Answer №3

Have you considered using the W3C validator to check your HTML or CSS files? Uploading the style.css file can help identify any errors. If the validator doesn't produce results, feel free to share your HTML code or provide a web address for further assistance.

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

How can I use Selenium in combination with Python to retrieve the visible text of a selected radio button or checkbox on a webpage?

I am currently working on processing a form where I need to extract the text values of all checked radio buttons. Each line item will have one radio button checked. Here is an example of the HTML structure for a checked radio button: <div style="white ...

A guide to modifying the color of the Menu component in material-ui

Currently, I am utilizing the Menu component from material-ui and facing an issue while trying to modify the background color. The problem arises when I attempt to apply a color to the Menu, as it ends up altering the entire page's background once the ...

How can I stop a hyperlink from activating Jquery?

A script was created to highlight any <TR> element when a user clicks on it, and it is functioning correctly. <script> $(document).ready(function () { $(document).on('click', 'tbody tr', function (e) { ...

"Unlocking the Power of mediaElementjs: Easy Steps to Accessing the Player Instance

I'm facing a small issue with the MediaElement.js player. To access the player instance, I usually use the following code (which works in HTML5 compatible browsers): // Retrieve player this.playerId = $('div#shotlist-player video').att ...

Issue with Animation Pause Functionality

I'm currently working on a music loader feature and I'm trying to create a toggle switch to pause and play the music. So far, I've been struggling with this functionality but managed to get it partially working on a simpler project, which yo ...

I would like to add a border at the bottom of each item in a ul list

My current focus is on making my website responsive with a breakpoint set at 576px I am aiming to achieve the design shown in this image without any space in the border-bottom and have both elements expand to full width: menu li hover However, I'm c ...

Struggling to center items on a page with Bootstrap 5

I'm attempting to center an item on the page using Bootstrap flex boxes in HTML. However, I am having trouble with the align-items-center attribute not working. <!DOCTYPE html> <head> <link href="https://cdn.jsdelivr.net/npm/& ...

Modifying the .textcontent attribute to showcase an image using JavaScript

I am working on a website and I want to change editButton.textContent = 'Edit'; so that it displays an image instead of text. var editButton = document.createElement('button'); editButton.textContent = 'Edit'; After exploring ...

Set a class for the nth table row

I'm trying to find a solution using jQuery where I can assign different classes to each <tr> in a table. For the first row, I want to add class='alt', for the second row class='alt-2', and for the third row class='alt-3& ...

What is the best way to calculate the cumulative score from the 24 values provided? Should I include an additional SELECT statement or simply sum up the individual scores?

I am trying to generate a query that displays scores for a user across 24 different questions. I have managed to load these scores into a table, but now I want to also include the total score for the same user. Adding a second SELECT statement with SUM() ...

How can the backgroundImage css in react admin <Login /> be replaced using Material-UI?

I have been refering to the following resources: Learn about customizing login page background in React-Admin: Explore themes customization in Material-UI: https://material-ui.com/customization/components/ Instead of using a preset background, I want to ...

The HTML5 datalist feature is only capable of displaying the first suggestion from the filtered array for autocomplete

I can't seem to display all the elements suggested in the datalist except for the first one. I've double-checked and confirmed that my array contains more than one element. It's puzzling why the other elements are not showing up in the sugge ...

Is it possible to utilize a single template and dynamically fill it with content as needed?

Is there a method to utilize a single template and dynamically insert content into it? http://jsfiddle.net/cmckeachie/mtV62/light/ var routingExample = angular.module('FunnyAnt.Examples.Routing', []); routingExample.controller('HomeControl ...

Update the logo for mobile, desktop, and tablet display using Bootstrap 4alpha

I am currently working on customizing a header with Bootstrap 4 alpha. <div class="container"> <div class="row"> <div class="col-md-6 text-md-left text-center"> <div class="navbar-brand"><img src="/wp-con ...

Align elements to the left and right using Flexbox

I am having trouble arranging a list of buttons horizontally. My goal is to have each row display two buttons - one on the left side and one on the right side. Occasionally, the left button needs to be hidden using display: none, but I still want the right ...

Issue with innerHTML functionality within a div element

I'm currently troubleshooting an issue with a website that has suddenly stopped functioning properly. Unfortunately, I don't have access to the server at the moment, and I need to quickly understand how the system works. Essentially, there is a ...

Responsive CSS design that adjusts to fit the content

Struggling to make the wrapper expand with its content... This is the structure: * { padding: 0; margin: 0; } body { background-color: #ccc; background-repeat:repeat; font: Tahoma, Geneva, sans-serif; color: #FFF; } .wrapper { width: 95%; margin: 0 au ...

Overflow of nested item within flex container

I'm trying to make the content under "foo" fully scrollable, but I've been unsuccessful using flexbox. I attempted it with min-height, yet it didn't work out. Check out this JSFiddle for reference $('.ui.dropdown') .dropd ...

Utilizing HTML and jQuery to load a dynamic URL within a div placeholder

I'm experiencing some difficulty with loading a custom URL. Essentially, the user will click on a series of navigation links that will dynamically load the relevant content in a tabbed bootstrap jumbotron. The navigation links vary based on data store ...

Customize Material UI components by incorporating SASS classes

Currently, I am using Material UI components but I want to streamline my styles by moving them all into a .scss file. Right now, I have a significant styles object within the same JavaScript file where I am utilizing the Material UI components. This styles ...