Centering a background image, using CSS
background-image: url(path-to-file/img.jpg);
background-repeat:no-repeat;
background-position: center center;
That should work.
If not, why not make a div
with the image and use z-index
to make it the background? This would be much easier to center than a background image on the body.
Other than that try:
background-position: 0 100px;/*use a pixel value that will center it*/
Or I think you can use 50% if you have set your body min-height
to 100%.
body{
background-repeat:no-repeat;
background-position: center center;
background-image:url(../images/images2.jpg);
color:#FFF;
font-family:Arial, Helvetica, sans-serif;
min-height:100%;
}
Large centred background image
One way you could do this is with background-position: center top;
, put the 2000px image as a background to a 960px div like this:
DEMO
As far as I'm aware this is supported on IE6+
Center one background image over the other in CSS
You need to correct your JS, you are specifying background
which will override all the background properties specfied in the CSS. So instead use background-image
to keep the CSS properties:
var divElem = document.createElement("div");
divElem .setAttribute( 'style', 'background-image: url( "caution.png" ), url("background.png")' );
divElem .className = "imgPin";
Then you may need to adjust the CSS if you still have alignment issue:
- Use
background-size:cover
orbackground-size:contain
to avoid the icon to stretch or simply don't specify any size to keep the original ones. - Add
background-position:center
in order to center both icons. You can also specify values in order to control position like thisbackground-position: 5px 10px,10px 5px
(works also with % values). - Then specify
background-repeat:no-repeat
(only once and it will affect both images)
How can I position a background-image an absolute distance from the right of its container?
Depending on your situation and what browsers you want to support, this works (tested on IE7-8, Firefox):
background: url(...) no-repeat right 50%; border-right: 4px solid transparent;
Of course, if you are already putting a border on the right, this will not help you at all.
Added on edit: If the above doesn't work because your are using the border, and you don't care about IE7 (not sure we are quite at that point yet), and your "icon" width is known, then you could do:
.yourContainer {
position: relative;
}
.yourContainer:after {
content: ' ';
display: block;
position: absolute;
top: 0;
bottom: 0;
right: 4px;
width: 10px; //icon width
z-index: -1; //makes it act sort of like a background
background: url(...) no-repeat right 50%;
}
Android: how to have a centered background image in a view
You have two choices:
Make the image bigger. Specifically, add a single black line all around its edges, then use the tools/draw9patch tool to make that black line the 'stretchable' part. When the image is scaled up to the background, your original icon will not be stretched.
Give up on the android:background and use a top level RelativeLayout instead. This should work
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher
android:id="@+id/ImageSwitcher01"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:background="@drawable/icon">
</ImageSwitcher>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView
android:text="Your old top level here"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</TextView>
</LinearLayout>
</RelativeLayout>
Hope this helps
Related Topics
How to Use a Material Theme Colors in My CSS in Angular2
Succinct Way of Specifying Two or More Values for an Attribute in CSS Selector
Gotham Book' Font-Family Not Working in Safari Browser and iPhone Devices
Flex-Basis for Wrapping Columns
Change Text Color for Gtktoggletoolbutton in C Code (Gtk+3)
Placing The: Before-Value in The Same Way as a List-Item Is Placed
CSS Can't Seem to Set Height to 100% of Parent Container
How Would You Do This: Tables or CSS
How to Import CSS from Node_Modules in Webpack Angular2 App
D3Js Svg Open Lines Display a Fill Artifact, How to Fix It
In HTML Table How to Force Cell Text to Truncate and Not Increase The Width of The Cell or Wrap
Issue with Embedded Svg Images in Dark Mode
Child Div Is Bigger Than Parent Div
Site Doesn't Look Right in Mobile Browser