Centered background with a chrome bug pattern

My background image appears centered, but Chrome displays it offset by one pixel.

CSS

#container { 
    background: url("images/header.jpg") no-repeat scroll 50% transparent;
    width: 100%
}
#header {
    width: 986px;
    margin: 100px auto 0 auto;
}

HTML

<html>
<body>
    <div id="container">
        <div id="header">centered content</div>
    </div>
</body>
</html>

It seems to be related to how different browsers handle the center or 50% property of the background in CSS:

Are there any known (simple) hacks or alternative methods to fix this? The background container must remain 100% wide.

Answer №1

To resolve the issue, try expanding the width of your image beyond the browser window.

I came across a helpful solution on this site -

Answer №2

This solution worked perfectly for me:

@media screen and (-webkit-min-device-pixel-ratio:0) { 

html {
    margin-left: 1px;
}

}

Once I track down the source where I found this tip a few days ago, I will share the link. The individual who shared it also mentioned that the issue was related to odd or even container widths. Regardless, this resolved the issue in my specific situation.

Answer №3

For a consistent positioning experience across all browsers, try setting the background image width to an odd number like 987px. This might seem strange at first, but it has always worked for me without needing any CSS tricks.

Answer №4

Is the picture truly 986 pixels wide? To address this issue, I discovered that adjusting the width of the image to an even number is the simplest solution.

Alternatively, you can include a 2 pixel buffer in the background image to maintain an even width and prevent any shifting. By adding one pixel to each side, the appearance of your image in the browser should remain unchanged.

Answer №5

Experiment with adjusting the size of your browser to observe its functionality. We are discussing pixels in this context, and if the window's width is not perfectly even, a pixel will need to be sacrificed somewhere.

Answer №6

Could it be that the background image also has a width of 986 pixels? In that case, this effect would need to be reflected on the left side, albeit in a reversed manner.

My suggestion is to take out the background image from the container and place it in the header instead:

#container {
    width: 100%;
}
#header {
    width: 986px;
    background: url("images/header.jpg") no-repeat scroll 50% transparent;
    margin: 100px auto 0 auto;
}

Answer №7

To address the issue in webkit, I utilized the following snippet of CSS. In case JavaScript is not enabled, the code assumes that the browser will likely be in full screen mode, resulting in an odd viewport width on webkit browsers.

CSS Code

@media screen and (-webkit-min-device-pixel-ratio:0) { 
    html {
        margin-left: 1px;
    }
    html.evenWidth {
        margin-left: 0px;
    }
}

JavaScript using jQuery

$(document).ready(function() {
    var oWindow = $(window),
    htmlEl = $('html');

    function window_width() {
        if(oWindow.width() % 2 == 0) {
            htmlEl.addClass('evenWidth');
        } else {
            htmlEl.removeClass('evenWidth');
        }
    }

    $(document).ready(function(){
        window_width();
        $(window).resize(window_width);
    });
});

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

Positioning icon/image beside the input box

Currently, I have a form with two buttons in a row and an icon (paper clip) that I am attempting to align next to the buttons using UIKit. However, I am facing challenges in getting the alignment right without affecting the size of the elements. Below is a ...

Divide the toggle buttons into separate rows for better organization

Snippet: <mat-button-toggle-group value="0" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group2="matButtonToggleGroup"> <div *ngFor="let item of test; let i = index;"> ...

Retrieving Basic HTML Source Code in UITextView Without CSS Styling Using Objective-C

I am looking to make changes to HTML text within a UITextView. The original text was created using an HTML editor in the web version of the app and needs to be edited with a default font on iOS. Here is the simple HTML code for the text that needs editing ...

Unable to Render CSS After Submitting a Post with Express

Upon initially loading the page, I was successful in getting the CSS to load. However, when I submit data using POST with a button, the post functionality works fine but the CSS fails to load. As a result, the screen appears plain with only the post inform ...

Ways to transition to the subsequent page in Selenium WebDriver following the click of the submit button

https://i.sstatic.net/QWcHm.jpg After successfully automating the process of filling in a textbox and clicking the "proceed with booking" button using a submit() command, I encountered an issue. The HTML code does not provide a URL that can be used in the ...

Unable to scroll within ion-view component

I recently started working with the ionic framework and I am currently struggling with a scrolling issue in my page design. Below is the code from my index.html file: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

What could be the reason for the presence of three pixels of white space below this particular table

My table is supposed to have a fixed height of 146px, but there seems to be an extra row of three pixels at the bottom. I've tried various methods to remove any unwanted spacing or padding, but nothing has worked so far. The only solution I found was ...

How to align two divs in CSS when they are not always both present

Currently working on CSS styling for a store. I have a div that aligns the buy button to the left, and Prev and View Next images to the right. The issue arises when the "buy" button is occasionally not present due to PHP functionality. When the buy butto ...

Adjusting the height of a textarea in React mui to match its parent element height

Utilizing the react mui library version 3.9.3 for UI components in my current project, I am facing an issue with a textarea field: <div className={classes.fieldWrappper}> <TextField id="filled-multiline-static" label="Oppgavetekst ...

Adjusting the size of MaterialUI SVG icon: a simple guide

Hey everyone, I'm having some trouble trying to make this MaterialUI icon larger. I've attempted changes with the viewbox and inline styling, but no luck so far. The current size of the icon isn't cutting it for me, I need it to be bigger. ...

What causes a semiopaque background to show through in an adjacent div?

My webpage consists of three elements. The main element is centered with a beautiful background image, while overlayed on top of it is a second div with a black semi-transparent background to enhance text readability. In addition, there is a floating div ...

Ways to maintain image size while zooming out a webpage with CSS

Is there a way to maintain image dimensions when zooming out a page using CSS? Take, for example, fiverr.com. When the page is zoomed out by 25%, And then, when the page is zoomed back to 100% (normal), How can I fix the image dimensions in this situat ...

Tips for minimizing the width of a form in CSS/HTML

I created an HTML page with the following code: <div class="panel panel-default"> <div class="panel-heading"> <h1>Users</h1> </div> <div class="panel-body"> <table class="table table-strip ...

Modify the text field's color when it is in a disabled state

When the text field is disabled, I want to change its color. Below is the code I have written for enabled and disabled states. The style works correctly when the text field is enabled. const StyledTextField = styled(({ dir, ...other }) => <TextFiel ...

I am interested in developing a select box with autocomplete functionality

Here is the HTML code I have: <select class="form-control" id="city" style="width:100%; margin-left:2%;"> <option>Sonal</option> <option>abcd</option> <option>3</option> <option& ...

Utilizing Bootstrap to arrange table cells in a shifted manner

I'm new to utilizing bootstrap in web development. I've been exploring various examples of bootstrap templates that include tables, and I noticed that when I resize my browser window, the table adjusts accordingly. Now, I'm curious to know ...

Maintaining the initial value of a textbox in jQuery across various focusin events

Struggling to accurately explain the process, so feel free to check out this helpful fiddle. In my form, there are several text input fields and a single submit button. The submit button becomes enabled once any of the form fields have been altered. If a ...

Bootstrap 4 utilized for creating a dynamic carousel of quotes that adjusts to

I am in the process of recreating a captivating Quote carousel from Bootsnipp, showcasing: An image on the left Text on the right Dark chevrons that stand out against a white backdrop The original design was achieved using Bootstrap 3, and my goal is to ...

Guide on linking an id with a trigger function in HTML and JavaScript

In the snippet below, I aim to create a responsive feature based on user input of 'mute' and 'muteon'. Whenever one of these is entered into the textbox, it will change the color of linked elements with the IDs "text" and "text1" to red ...

Blurred border encircling a CSS circle styled with overflow: hidden;

Take a look at the JSFIDDLE of my cat animation without any drop-shadows to highlight the issue clearly. The problem appears to be related to the use of border-radius and possibly due to overflow: hidden. Just to clarify, the owl is not the focus of this ...