centered element in background doesn't match centered element in foreground

center''> <div vertically and horizontally responsive
center div horizontally and vertically
center a div horizontally
css center form vertically
vertically center div inside div
vertically center text in div
html5 center div
div float center

I'm working on an Android application where I want to create a windowBackground with a centered element and a layout also with a centered element. I want these elements to be in the exact same position, with the layout overlapping the background. The problem I'm having is that the layout and the background seem to be calculating center differently (see image). Why is this happening, and what can I do to line the elements up?

This is what I see right now. The red box is created by the background and the green box is created by the foreground. Screenshot was created with a Nexus 5X API 26 emulator.

Foreground layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:background="@color/foreground_box"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_centerInParent="true" />
</RelativeLayout>

Background Drawable (applied via android:windowBackground in my theme)

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background" />
    <item android:gravity="center">
        <shape
            android:gravity="center"
            android:shape="rectangle">
            <solid android:color="@color/background_box" />
            <size android:width="10dp"
                android:height="10dp" />
        </shape>
    </item>

</layer-list>

For clarity, my colors file is

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    ...
    <color name="background">#ffffff</color>
    <color name="background_box">#AAFF0000</color>
    <color name="foreground_box">#AA00FF00</color>
</resources>

Full source for this sample project is available at https://github.com/HofmaDresu/AndroidCenteredTest

The reason windowBackground includes both the heights 1) statusBar and 2) actionBar

Modify below line in your background.xml

<item android:gravity="center" android:top="80dp"> // 56 actionBarSize + 24 statusBarHeight

You may need to manage this programatically as statusBarHeight and actionBarSize varies based on device API/resolution.

Here is the result. For testing, have resized background size bit bigger so that overlapping between views and background become visible.

CSS: centering things, CSS level 2 doesn't have a property for centering things vertically. position: relative } div.container4 p { margin: 0; background: yellow; position: absolute; top:​  To make a long story short, for the content to center properly in IE use either auto margins or justify-content: center. Using both breaks the centering in IE because of faulty implementation of CSS priority.

It is probably because of the extra space taken up by the ActionBar in the foreground.

To fix this, you can add a margin to your View in the foreground layout as:

android:layout_marginBottom="?android:attr/actionBarSize"

The Complete Guide to Centering a Div, When you get to centering a div vertically, you can end up in a world of CSS hurt. With this method the inner div doesn't require a set width. width: 100%; } .​inner-div { margin: 0 auto; width: 100px; height: 100px; background-color: #ccc; }. Tip: By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally. yes. Read about animatable Try it. JavaScript syntax: object .style.backgroundPosition="center" Try it. Browser Support. The numbers in the table specify the first browser version that fully supports the property.

After test it in AS, I can say you that the right code for you background_drawable is this:

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/background" />

    <item android:gravity="center" android:bottom="48dp">
    <shape
        android:gravity="center"
        android:shape="rectangle">
        <solid android:color="@color/background_box" />
        <size
            android:width="10dp"
            android:height="10dp" />
    </shape>
</item>

using android:top, the red square go more down than center. Need to use android:bottom instead to center background. By my tests results that 48dp is the right value.

Center fixed div with dynamic width (CSS), For kicks, alter the background-position values so that the purple dots are along the bottom edge of the header div (center bottom). (Doesn't look so good; I'm  I recently needed to make a placeholder page for a site. I wanted the logo image to be centered exactly in the middle of the screen, that is, both vertically and horizontally centered. Quickly, I thought I'd just give the image element a class of "centered" and then style that class: But as I'm sure you are thinking, this doesn't quite work.

10 Ways to Center a Div Horizontally and Vertically in CSS, .element{ display: table-cell; text-align: center; vertical-align: middle; background: tomato; height: 200px; width:200px; color: white; font-size:  The lines in this paragraph are all centered between the paragraph's margins, thanks to the value 'center' of the CSS property 'text-align'. Centering a block or image Sometimes it is not the text that needs to be centered, but the block as a whole.

How to align text vertically center in a DIV element using CSS, Figure 5-9: The Background category in the CSS Rule Definition dialog box. In all cases, the image is cropped if it doesn't fit behind the element. Horizontal Position: Allows you to align the image left, center, or right, or to set a numeric  Setting the width of the element will prevent it from stretching out to the edges of its container. The element will then take up the specified width, and the remaining space will be split equally between the two margins:

Learning Web Design: A Beginner's Guide to HTML, CSS, JavaScript, , The background-position CSS property sets the initial position for each 0 0, center; /* Edge offsets values */ background-position: bottom 10px right x/y coordinate, to place an item relative to the edges of an element's box. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it. The obsolete HTML Center Element (<center>) is a block-level element that displays its block-level or inline contents centered horizontally within its containing element.

Comments
  • background_box color is green ?
  • It seems like you have added this extra line <item android:drawable="@color/background" /> I am checking it.
  • background_box color is red
  • The first <item> is to set an overall background color (white in this case)
  • Note that neither the status bar height nor the action bar height are consistent across devices/configurations, and so hardcoding 80dp may work well on many devices but will not on others.
  • @BenP. Agreed !! thats what I mentioned in answer post to manage the heights pro-grammatically.
  • That pushes the boxes apart, forcing the green 'foreground' box further down on the page. The issue occurs even if I adjust my theme to remove the action bar, though the green foreground box is above the red background box in that case
  • @HofmaDresu My bad. I've edited the answer to correct it. Basically since there is an ActionBar on top, we're creating an equal margin on the bottom so that the center is not disturbed.
  • This is still a problem even if I remove the action bar. My real-life situation doesn't have one
  • I also tested this code with marginBottom and the boxes don't line up