Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I position my CSS background to "bottom minus x pixels"?

I need to position my background at the bottom of the screen. But on mobile device resolution I want to display it 100px lower than that, like "bottom minus 100px". Is it possible to achieve such a thing in CSS?

like image 746
drake035 Avatar asked Jan 08 '16 13:01

drake035


People also ask

Which of these is the correct CSS property to position the background image?

The background-position-x CSS property sets the initial horizontal position for each background image.

How does CSS background-position work?

Definition and Usage. The background-position property sets the starting position of a background image. Tip: By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

How do you make a background image fit your screen in CSS?

Using CSS, you can set the background-size property for the image to fit the screen (viewport). The background-size property has a value of cover . It instructs browsers to automatically scale the width and height of a responsive background image to be the same or bigger than the viewport.

What this means background-position 100px 200px?

This style rule places a background-image at a position that's 100 pixels from the left and 200 pixels from the top of the element with ID “example” : #example{ background-position: 100px 200px; } This property accepts one or two length values, percentages, or keywords.


2 Answers

Yes, with the four-value syntax of the background-position property.

Presentation

// This two-value declaration
background-position: center bottom
// Is equivalent of this four-value declaration
background-position: center 0px bottom 0px

Disclaimer: the support is Chrome 25+, Firefox 13+, IE9+, Safari 7+

In your case

So you can position your background to "bottom minus 100px":

background-position: center bottom -100px

Example:

.container {
  height: 190px;
  padding: 1em;
  margin: 0 auto 1em;
  border: 1px solid #333;
  text-align: center;
  
  background-image: url('http://lorempixel.com/200/140/city/4');
  background-repeat: no-repeat;
  background-position: center bottom;
}
.plus {
  background-position: center bottom 20px;
}
.minus {
  background-position: center bottom -20px;
}
<div class="container">
    background-position:<br>
    center bottom
</div>
<div class="container plus">
    background-position:<br>
    center bottom 20px
</div>
<div class="container minus">
    background-position:<br>
    center bottom -20px
</div>

See the MDN documentation

like image 132
tzi Avatar answered Sep 20 '22 18:09

tzi


If you are using the background property use:

.background{
    background-position: center bottom -100px
}

If you want to position a tag that resembles a background try using:

.background{
    margin-bottom -100px;
}

or

.background{
    bottom: -100px;
    position: absolute;
}
like image 27
Clemens Himmer Avatar answered Sep 23 '22 18:09

Clemens Himmer