The alignment of the body will vary depending on the screen resolution

I am facing an issue with the alignment of my website when viewed on a different resolution. It shifts everything to the right instead of staying centered. I tried setting the overflow to 0% but it didn't solve the problem. Can anyone help me understand why this is happening?

Below is the HTML file for reference:

<html>
    <head>
        <script type="text/javascript">
        </script>
        <link rel="stylesheet" type="text/css" href="stylesheet.css" media="screen"/>   
    <body>
        <div id="banner">
        <h2 id="bannertext"> Websites4u </h2>
        </div>

        <div id="buttonbar">
            <a id="homeb" href="webpageone.html">Home</a>
            <a id="aboutb" href="fake.html">About</a>
            <a id="contactb" href="webpage2.html">Contact Us!</a>

        </div>

        <div id="mainbody">

        <p id="radio">
        3gb:   <input type="radio" name="age" value ="<3gb"> <br> <br>
        4gb:   <input type="radio" name="age" value ="4gb"> <br> <br>
        8gb:   <input type="radio" name="age" value ="8gb"> <br> <br>
        16gb: <input type="radio" name="age" value ="16gb"> <br>
        </p>

        <h4 id="bodytext"> Please Select Your Hardware </h4>

        <h3 id="Ram"> Ram </h3>

        </div>
    </body>
</html> 

And here is the corresponding CSS code:

  *{
        margin:0;
        padding:0;
    }

    h1 {
        color:blue;
    }
    body{
        width:1280px;
        height:720px;
        background-image:url("background colour.jpg");
        overflow:hidden;
    }
    #banner{
        position: relative;
        height: 50px;
        width: 148%;
        border: medium solid BFBDBA;
        background-color:F1C43E;
        margin:0 auto;
    }

    #bannertext{
        color:white;
        text-align:center;
        font-family:Comic Sans MS, cursive, sans-serif;
        margin:0 auto;
    }

    #buttonbar {
        position: relative;
        height: 30px;
        width: 148% ;
        border: medium solid BFBDBA;
        background-color:lightgrey;
        color:white;
        margin:0 auto;
        font-family:Comic Sans MS, cursive, sans-serif;
        font-style:bold;
    }
    #homeb {
        position: relative;
        left: 450px;
    }

    #aboutb{
        position: relative;
        left: 500px;
    }

    #contactb{
        position: relative;
        left: 550px;
    }

    a { 
        color: white; 
        font-weight:bold;
    }
    a:hover {
        COLOR: orange;
        font-weight:bold;
    }

    #bodytext{
        position: relative;
        top:50px;
        left:50px;
        color:red;
        font-size:35px;
        margin:0 auto;
        font-family:Comic Sans MS, cursive, sans-serif;
    }

    #mainbody{
        position: relative;
        background-color: white;
        height: 1000px;
        width: 80%;
        left: 30%;
        top: 5px;
        border:medium solid F1C43E;
    }

    #radio{
        position: absolute;
        top: 240px;
        left:100px;
        font-size: 18px;
        margin:0 auto;
    }

    #Ram{
        position: absolute;
        top: 176px;
        left: 100px;
        font-size: 30px;
        color: Green;
        margin:0 auto;
        font-family:Comic Sans MS, cursive, sans-serif;
    }

Answer №1

It can be challenging to address all of your issues efficiently. A key error you are making is using absolute values for every element.

Take this snippet for example:

 body {
    width: 1280px;
    height: 720px;
    background-image: url("background colour.jpg");
    overflow: hidden;
 }

By setting the width to 1280px, your content will always display at that width regardless of the screen size. This can cause alignment problems and make elements appear shifted on smaller screens due to the overflow:hidden property. Remember, a computer will follow your instructions precisely, not necessarily what you intend.

Regarding #buttonbar:

 #buttonbar {
    position: relative;
    height: 30px;
    width: 148%;
    border: medium solid BFBDBA;
    background-color: lightgrey;
    color: white;
    margin: 0 auto;
    font-family: Comic Sans MS, cursive, sans-serif;
    font-style: bold;
    }

Questionably, why set width: 148% here? Additionally, positioning buttons inside with absolute values like #homeb { left: 450px; } might lead to misalignment issues rather than centering them as desired.

To address these concerns, you can simplify your styling by using text-align:center; within #buttonbar to ensure proper centering without the need for individual classes for each button.

Fixing these issues may seem overwhelming, but taking the time to understand how each line of code affects browser rendering is crucial. It's a learning process of reading, experimenting, and seeking guidance to achieve the desired results. Here is a demo showing some fixes made: http://jsfiddle.net/2rM6K/7/

Answer №2

website
{
Margin-right:auto;
margin-left:auto;
width:800px;
}

Feel free to adjust the width to suit your needs.

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

If the current text field is populated, the cursor will move to the newly added text field when the Enter key is pressed on the keyboard

Creating a Dynamic Table <table> <thead> <th>Item</th> <th>Cost</th> </thead> <tbody id="tbody"> <tr> <td>Item 1</td> <td> <input type="text" class="elm" /> ...

The text outside of the button is overridden by my CSS styling

I've been working on a simple card matching game and I decided to style the buttons to enhance their appearance. However, I'm facing an issue where the styling code has caused the text to appear outside of the button. Whenever I try to adjust the ...

Upon clicking a button, I aim to retrieve the data from the rows that the user has checked. I am currently unsure of how to iterate through the rows in my mat-table

My goal is to iterate through the column of my mat-table to identify the rows that have been checked, and then store the data of those rows in an array. <td mat-cell *matCellDef="let row"> <mat-checkbox (click)="$event.stopPropagation()" (c ...

Using Vue.js to create a table with dynamically colored rows in a v-for loop

I'm struggling to apply different colors to my table rows in a v-for loop. Currently, I'm developing a basic soccer app that displays recent match outcomes for a specific team. The issue I'm facing is related to changing the background c ...

Adjust the size of items using a background image that covers the element

I'm struggling with a seemingly simple task here. I've been experimenting with different methods, but I just can't seem to grasp it. On the front page of my website, there is a cover section that contains a logo and a button: <section i ...

Creating dynamic animations with CSS3 to make circles grow in size

I'm currently working on a project where I want circles to grow outwards from the center when the page loads, rather than from the top left corner. Here is the link to see what I have so far: My goal is to achieve this effect using CSS, but I'm ...

The perplexing illusion of the undecipherable table! Numerous mergers fail to

Attempting to combine cells 1, 2, and 3. The current code successfully merged cells 1 and 2: <td rowspan="2" colspan="2"> &nbsp;1</td> I left the second row empty to accommodate this change. <tr> </tr> However, the mergin ...

Attempting to get the boxes in the final row to show up

Exploring new territories in web design, I'm currently working on achieving the layout shown below: Box Layout I've encountered an issue where the last row of 4 boxes in the middle section is not appearing as intended. Once this row is successfu ...

Ways to include a right arrow symbol for sub-child items and a downward arrow for child menu options

I have a php form that generates a drop-down menu using php mysql. The current menu is simple, but I would like to enhance it by adding a right arrow for sub-children and a down arrow for child menus using css. Unfortunately, I am unable to modify the css ...

Woocommerce archive and single product pages appear unformatted due to the failure of stylesheets to load properly

Recently, a perplexing issue has arisen on my client's Woocommerce archive and single product pages. Strangely, the stylesheets are failing to load on these pages, resulting in an unformatted appearance. Curiously, the cart and checkout pages seem un ...

Discovering a class within a div element using Selenium

Currently, I am attempting to utilize Selenium in Python to locate and click on the 'X' button. This action will redirect me to the next page where I can extract some crucial information. However, I am encountering difficulties in finding the ele ...

We encountered an issue loading the resource: the server has returned a 404 (Not Found) error message and a 403 Forbidden error message. However, the resource functions properly on Codepen and localhost

I have a CodePen demo that works fine. However, when I upload it to the server, it stops working properly and most external references show 404 errors. Initially, there were no problems, but now it seems to be an issue like this: !--> http://sachin.ipov ...

"Delving deep into the realm of CSS3 with

Is there a way to use the CSS3 selector to target an element under a specific class? <div> <span>Example Text</span> </div> I attempted using div:not(span) but it didn't work. (I believe the :not pseudo-class ...

Challenge with maintaining tab view data in Openui5

I am facing an issue with my application's tabs. Each tab renders data retrieved through ajax calls from the database. However, whenever I switch between tabs, the data gets refreshed each time. Is there a way to prevent this refreshing behavior and i ...

Disable onclick action for programmatically created buttons

I'm facing an issue with the code I'm using to delete messages on my website. The problem is that while newly added messages are being dynamically loaded, the delete button (which links to a message deletion function) does not seem to be working. ...

Creating dynamic PDFs with automatically adjusting heights in DOMPDF Learn how to make

It seems like this question may not have a straightforward answer, but let's give it a shot. My goal is to collect various information from users on my website and display it in an HTML template file that will later be converted into a PDF. With user ...

Utilizing Django's numeric variable within CSS styling

Currently experiencing an issue with a bootstrap progress bar in my project. I am attempting to retrieve the progress bar width from a Django variable using template language, but encountered a problem. The float variable gets changed to a string by HTML ( ...

How can I align 2 ul-navbar items in the right navbar using Bootstrap?

Attempting to create an additional menu on top of the right menu using Bootstrap 4.6 My approach is to have: 2 navbar-nav elements with classes w-100 and float-right, so they would be 100% width and floating to the right. However, this is not working as ...

Tips for achieving a blurred background image effect when hovering, while keeping the text unaffected

Hey there, I've recently started my journey into web development and have encountered a roadblock. I'm trying to blur the background image of a div on hover without affecting the text inside. I have an id called c1 where I used a javascript func ...

The third error states that "scene" is not a valid function

Getting an error while using three.js and the code snippet below is throwing the following error message: Uncaught TypeError: THREE.Scene is not a function init @ new 1.html:18 This error occurred on this specific line of code: scene=new THREE.Scene(); ...