Background-position CSS confusion with percentage values?

  • Context: HTML/CSS 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confusion Css
Click For Summary

Discussion Overview

The discussion revolves around the use of the CSS property background-position, specifically with percentage values, and the challenges participants face in achieving the desired positioning of background images. The scope includes technical explanations and troubleshooting related to CSS styling.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their use of background-position: 50% 50% and expresses confusion about the output they receive.
  • Another participant suggests that using background-position: center is a simpler alternative to achieve centering.
  • There is a claim that background-position: 50% 50% should work, but some participants report it does not produce the expected results.
  • One participant proposes that the issue may be due to the body element lacking height, recommending the addition of height: 100vh;.
  • Another participant confirms that adding height resolves the issue but expresses confusion about why the height is necessary.
  • It is noted that the body element will only have height if it contains content or if height is explicitly set in CSS.

Areas of Agreement / Disagreement

Participants generally agree that the body element's height affects the background positioning, but there is no consensus on why the initial output was unexpected or how to best achieve the desired result.

Contextual Notes

Some participants reference specific CSS properties and values, but there is uncertainty regarding the behavior of the background-position property in relation to the body element's height.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
percentage value in background-position property. What happens there?
This is my background image.
6kJvOSHo0HzdBhjItUn2qTSQnNd1ZUfRLg9FkAWFQ4vajkNVCQ.png

I set:
background-position:50% 50%;

I get this as my output:
XS5OEIF_2UMs3d-xZKfKN3-6dctsWBHr3VcoqqlOGxVh5rp4hg.png

But I don’t understand the output.
So, I read little bit about background-position again.A value such as 20% 65% specifies that the point 20% across and 65% down the image be placed at the point 20% across and 65% down the element.
https://www.htmlhelp.com/reference/css/color-background/background-position.html

With a value pair of '14% 84%', the point 14% across and 84% down the image is to be placed at the point 14% across and 84% down the padding area.
https://wiki.cdot.senecacollege.ca/wiki/CSS_GUIDE_BACKGROUND_POSITION

A horizontal value of 25% positions the point 25% from the left edge of the image at a point that is 25% from the left edge of the background positioning area.

“Learning web design” by Jenniver Nierdest Robbins

If I wanted to put the background image at the center using the “percentage percentage”, how’d I do it? (50% 0% doesn’t work).

My HTML Code:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
my name is tommy
</body>
</html>

My CSS code:
Code:
body{
    background-color:yellowgreen;
    background-repeat: no-repeat;
    background-image: url(../images/circle.png);
    background-position:50% 0%;
  
}
CSS Code is tamed CSS code in background position just for reference.
 
Technology news on Phys.org
shivajikobardan said:
I set:
background-position:50% 50%;

I get this as my output:
View attachment 315888
Are you sure? That looks to me like background-position: 50% 0%;

shivajikobardan said:
If I wanted to put the background image at the center
Then use background-position: center;

shivajikobardan said:
using the “percentage percentage”, how’d I do it? (50% 0% doesn’t work).
No, this should be obvious. You want background-position: 50% 50%;, but why make life hard for yourself? background-position: 50% 50%; = background-position: center center; = background-position: center;.

shivajikobardan said:
CSS Code is tamed CSS code in background position just for reference.
What does this mean?
 
background-position: 50% 50%;
isn't producing expected results like you said.
 
shivajikobardan said:
background-position: 50% 50%;
isn't producing expected results like you said.
Perhaps the body doesn't have any height. Try adding height: 100vh;.
 
now, I'm getting different output when I try the same code with the image posted here.
 
pbuk said:
Try adding height: 100vh;.

shivajikobardan said:
now, I'm getting different output when I try the same code with the image
And why is that unexpected after changing the code?
 
  • Like
Likes   Reactions: shivajikobardan
pbuk said:
Perhaps the body doesn't have any height. Try adding height: 100vh;.
wow. this fixed. I didn't get the reason why this fixed? Body doesn't have height without us specifying?
 
Tom.G said:
And why is that unexpected after changing the code?
I'd missed that update of pbuk.
 
  • Haha
Likes   Reactions: Tom.G
shivajikobardan said:
Body doesn't have height without us specifying?
Like any block element, the <body> will only have (inner) height if it has content, or if it is set as an attribute or in CSS.
 
Last edited:
  • #10
pbuk said:
Like any block element, the <body> will only have (inner) height if it has content, or if it is set as an attribute or in CSS.
thank you.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K