HTML/CSS Background-position CSS confusion with percentage values?

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confusion Css
AI Thread Summary
The discussion centers around the use of the CSS property background-position for setting a background image. The user initially sets background-position to 50% 50% but does not achieve the expected centered output. Clarifications are provided regarding how background-position works, with examples showing that the specified percentages place a point on the image at certain coordinates relative to the element. To center the background image, it is suggested to use background-position: center; or background-position: 50% 50%;. A key point raised is that the body element may not have a defined height, which affects the output. The solution offered is to add height: 100vh; to the body, which resolves the issue and allows the background image to display as intended. The discussion concludes with an acknowledgment that the body element requires a specified height to display properly, similar to other block elements.
shivajikobardan
Messages
637
Reaction score
54
TL;DR Summary
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 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.
 
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
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
3
Views
3K
Back
Top