When the window size is reduced (smaller than a page width), the layout of the page becomes distorted

Check out my cool header:

<head>
    <link href="/css/header.css" rel="stylesheet" type="text/css">
    </head>

    <body>

    <div id="background"><img src="/multi/background.jpg" width="100%" height="100%" alt=""></div>

    <div id="content">

    <div align="center" class="headtable">
        <table width="980" border="0" height="40">
            <tr>
                <td width="503" rowspan="2" height="35" align="left" valign="middle"><a href="/index.php"><img src="/multi/header.png" width="344" height="32" hspace="0" border="0"/></a></td>

                <td width="424" align="right" valign="top">
                    <a href="/index.php" class="headlink">Italiano</a>  
                    <span class="headlink">|</span>   
                    <a href="/ger/index.php" class="headlink">Deutsch</a>  
                    <span class="headlink">|</span>   
                    <a href="/fra/index.php" class="headlink">Français</a>  
                    <span class="headlink">|</span>   
                    <a href="/index.php" class="headlink">Home</a>
                    <span class="headlink">|</span>   
                    <a href="#" onClick="window.print();" class="headlink">Stampa Pagina</a>
                </tr>
            </table>
        </div>

        <div class="buttontable">
            <table width="1080" border="0" cellpadding="0" align="center">
                <tr>
                    <td height="20" align="center"> 

                    <a class="button" href="/o.php">o</a>
                    <a class="button" href="/p.php">p</a>
                    <a class="button" href="/a.php">a</a>
                    <a class="button" href="/s.php">s</a>
                    <a class="button" href="/st.php">st</a>
                    <a class="button" href="/p.php">p</a>
                    <a class="button" href="/t.php">t</a>
                    <a class="button" href="/c.php">c</a>
                </td>
            </tr>
            </table>
        </div>
        </div>
        </body>
    

Now take a look at the CSS file:

/* makes the page fill the entire viewing area */
    html {
        height:100%;
    }
    body {
        height:100%;
        margin:0;
        padding:0;
    }
    /* sets up the background image to cover the entire viewing area */
    #background {
        position:fixed;
        top:0;
        left:0;
        width:100%;
        height:100%;
    }
    /* positions the content on top of the background image */
    #content {
        position:relative; z-index:1;
    }

    /* specific styling for non-IE browsers */
    * html {
        background-color: #6f6;
    }
    * body {
        height:100%;
        margin:0;
        padding:0;
    }
    * #background {
        position:fixed;
        top:0;
        left:0;
        width:100%;
        height:100%;
    }
    * #content {
        position:relative; z-index:1;
    }
    /* END specific styling for non-IE browsers */

    .headtable {
        background-color:#02346F;
    }

    .headlink {
        font-family:Arial, Helvetica, sans-serif;
        font-size:12px;
        text-decoration:none;
        color:#d8dfea;
    }

    .buttontable {
        background-color:#F00;
    }

    a.button {
        display:inline-block;
        color:#FFF;
        font-family:Calibri;
        font-size:18px;
        font-weight:bold;
        padding:2px 16px;
        text-decoration:none;
        font-variant:small-caps;
    }a.button:hover {
        background-color:#F00;
        color:#02346F;
        padding:2px 16px;
        font-size:18px;
        font-weight:bolder;
    }

    a.buttonselect {
        background-color:#ffffff;
        display:inline-block;
        color:#02346F;
        font-family:Calibri;
        font-size:18px;
        font-weight:bold;
        padding:2px 16px;
        text-decoration:none;
        font-variant:small-caps;
    }
    

Having trouble with the table extending beyond the background when the window is smaller? One solution could be setting a fixed width for the body in the CSS, but keep in mind it might cause alignment issues. Need more guidance on this?

If the background image isn't the issue, have you considered other factors that might be causing the problem? It's a tricky one to troubleshoot, but with some adjustments, it can be fixed!

Answer №1

One possible reason for the jumping of your table is the fixed width of the table compared to the variable width of the container. To avoid this, consider using CSS to center the div with the table instead of an inline style.

.headtable
{
background-color:#02346F;
width:65% /* choose your width */
margin: 0 auto;
}

For navigation purposes, it may be better to use an unordered list instead of tables. This approach can offer more control and better semantics for your code.

Check out this site for examples of using lists for navigation menus, which can be more manageable with CSS than tables with fixed widths:

Answer №2

Your code shows potential, but there are a few areas where improvements can be made.

  • Avoid using tables for layout purposes. Instead, opt for semantic HTML tags. For instance, your navigation should be structured as a <ul> since it consists of a list of links.
  • Separating your content from your styling is recommended. This will make it easier to maintain your code and allow for quick style changes by simply loading a different stylesheet. It also simplifies the process of updating elements as you always know where to find them.

To illustrate, here is an example of how your HTML structure could be improved:

<body>

  <div id="wrapper">

    <div id="header">

       <a id="logo" href="/index.php" alt="company logo"><img src="/multi/header.png"/></a>

       <ul id="main-nav">
           <li><a href="/index.php">Italiano</a></li>    
           <li><a href="/ger/index.php" class="headlink">Deutsch</a></li>  
           ...    
       </ul>

       <div class="clear"></div>

    </div>
  </div>
  ...

You can view your current code in this fiddle: http://jsfiddle.net/ERghQ/
And here is an enhanced version in this fiddle: http://jsfiddle.net/JChZT/

I've also added some CSS to mimic the styles in your example. The width issue has been resolved by using floats and divs instead of tables and fixed widths. Try resizing the window to see how the content adapts.

My intention is not to criticize your work, but rather to offer guidance on modern practices. It may take some time to adjust, but the benefits will soon become apparent. Feel free to reach out if you need further clarification on the changes. Hopefully, this points you in the right direction!

edit:
The empty div with the .clear class is a common solution for the clearfix problem. This ensures proper height for elements within a container when all children are floating. While it may seem contradictory to the principle of separating content and styling, it is a quick fix. I recommend exploring a proper .clearfix class for a more elegant solution.

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

Unable to append HTML table row to designated table

I am trying to populate an HTML table with the JSON string data I receive. The object data seems correct, but for some reason, it is not getting appended to the table. Can you help me identify what's wrong with my jQuery append statement? functio ...

Designing Post Archive with Flexbox to create dynamic layouts

I am currently exploring different techniques to style my columns, and I have encountered an issue where if there are less than 4 posts in a row, there is extra space between each post. While I understand that floats can resolve this problem, I am searchin ...

Upon clicking a link, the webpage will automatically scroll to a specific point

As I work on creating my website, I have come across a specific need: I currently have a menubar with a dropdown menu. Inside this dropdown, there are 4 different options. These options are all located on the same page. What I would like to achieve is tha ...

Looking for a solution to resolve the error in this SQL example?

I'm looking to extract node "-all_models=1-4.htm" in SQL using the example provided. Here is my current code: <div class="models_selector_block" > <DIV class="msb_item"> <a class="ser_active" hr ...

Personalize Drop-Down Selection

Currently implementing bootstrap on a site and seeking to enhance the dropdown menu customization. The default navbar appearance is present, with dropdown elements appearing upon hover. My goal is to include an icon next to each item in the navbar and ha ...

Having trouble accessing a fully loaded Wordpress remotely through Wamp

After installing WAMP Developer Pro on my Windows 7 computer, a testing website was set up locally. Now, I am trying to test it on the LAN, but when accessing the page, only text appears without any images, themes, or styling. Apache is running on port 80 ...

Issue with Bootstrap 5 dropdown menu extending beyond the viewport on the right side

The user dropdown in my code is cut off, even though I'm using Bootstrap 5. I came across an older article on stackoverflow suggesting to add .dropdown-menu-left or .dropdown-menu-right to the </ul>, but that didn't solve the issue for me. ...

Unexpected behavior with first-child selector in a simple CSS code

It seemed so clear to me, but it turns out I was mistaken. Check out this link $('p:first-child').css({color:'red'}); The color of all text is not being altered. <div class="post text"> <a href="#">this is a url</a> ...

What is the best way to ensure these elements are neatly stacked one on top of the other?

Is there a way to center this text div while keeping it behind a fading image on the page? I've tried using margin-left: auto; and margin-right: auto; but it doesn't seem to work. Am I missing something? I'm testing everything in Chrome, ju ...

Entering text into the input field with the string "Foo"<[email protected]> is not functioning correctly

The text in my input is initially filled with this string "foo foo"<<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2920200f29202061263b">[email protected]</a>>. However, after an insert, the string now l ...

Is there a way to adjust the input type date format to show mm/dd/yy?

My input type is set to "date" with the default format of mm/dd/yyyy. However, I would like to change it to mm/dd/yy. Is there a way to adjust the format of a date input? Date: <input type="date" id="date" name="date" value="mm/dd/yy" required/> ...

The CSS styles are not being rendered on the page

My simple static html/css project seems to be having trouble applying styles to the html elements in stackblitz. You can view the project here. I'm wondering if there might be an issue with linking the css file using the link tag, or perhaps it's ...

Hide the border on a table with CSS

I recently installed a Wordpress Template and customized the table's background and text colors. However, I am struggling to remove the border around the table. Despite searching for solutions, my limited CSS knowledge has not led me to a fix that wor ...

Tips on how to stop a webpage from scrolling and instead allow only the inner content to scroll

Check out my plunker for reference. The goal I am aiming for is as follows: The webpage should stay fixed and not scroll in any direction If the content overflows its container, a scroll bar should appear strictly within that container I have a main co ...

Creating a React Swiper that dynamically centers a sliding element of varying width

I am currently working on a carousel where the center slide needs to expand to display additional content. I have set up a working example in this codesandbox. The main criteria for this project are: Non-centered slides will be minimized to show only the ...

The right side alignment is malfunctioning

I need assistance with creating a section on a webpage that resembles a piece of paper, where there is content displayed on white background against a grey background. Here's what my CSS currently looks like: body { background:grey; } .page { ...

CSS Trouble with Stacking Order

Seeking assistance with my learning/practice journey, any help is greatly appreciated. Please feel free to point out errors or inefficiencies. I currently have four layers in place: .body .main .main-side .sidebar Here is the design I'm trying to r ...

Issues with HTML keycodes not functioning properly in Firefox

Here is the code I am using: function restrictInput(e) { var charCode = (e.which) ? e.which : ((e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : 0)); if((charCode < 48 || charCode > 57) && ( ...

The content section sits discreetly behind the sidebar

Upon loading my Bootstrap 5 webpage, the toggle button successfully moves the navbar and body section to show or hide the full sidebar. However, an issue arises where the body section goes behind the sidebar when using the toggle button. Below is a portio ...

"Wordpress Pluto theme: A stellar choice for your

I'm trying to add a link in my main template.index file that will redirect to one of my pages when clicked on the JavaScript button at the top in Pluto theme. Currently, the text is there but clicking on it opens something I don't want. Here&apo ...