programing tip

CSS를 사용하여 배경 이미지 중심 맞추기

itbloger 2020. 6. 27. 11:44
반응형

CSS를 사용하여 배경 이미지 중심 맞추기


배경 이미지를 중앙에 배치하고 싶습니다. 사용 된 div가 없으며 CSS 스타일입니다.

body{
    background-position:center;
    background-image:url(../images/images2.jpg) no-repeat;
}

위의 CSS는 모든 타일과 중앙에 위치하지만 이미지의 절반은 보이지 않으며 위로 이동합니다. 내가하고 싶은 것은 이미지의 중앙에 있습니다. 21 인치 화면에서도 이미지를 볼 수 있습니까?


background-image: url(path-to-file/img.jpg);
background-repeat:no-repeat;
background-position: center center;

작동합니다.

그렇지 않다면 div이미지 z-index를 만들어 배경으로 사용 하는 것이 어떻습니까? 이것은 몸의 배경 이미지보다 중앙에 놓기가 훨씬 쉽습니다.

그 이외의 시도 :

background-position: 0 100px;/*use a pixel value that will center it*/또는 몸 min-height을 100 %로 설정하면 50 %를 사용할 수 있다고 생각합니다 .

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%;
}

이 코드를 사용하면 잘 작동합니다.

html, body {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  background: black url(back2.png) center center no-repeat;;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

나는 이것이 원하는 것이라고 생각 합니다 .

body
{
    background-image:url('smiley.gif');
    background-repeat:no-repeat;
    background-attachment:fixed;
    background-position:center;
} 

시험

background-position: center center;

이처럼 :

background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;

html{
height:100%
}

body{
background-image:url(https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Dore_woodcut_Divine_Comedy_01.jpg/481px-Dore_woodcut_Divine_Comedy_01.jpg);
background-repeat:no-repeat;
background-position: center;
background-size: cover;
}


There's an error in your code. You're using a mix of full syntax and shorthand notation on the background-image property.This is causing the no-repeat to be ignored, since it's not a valid value for the background-image property.

body{   
    background-position:center;
    background-image:url(../images/images2.jpg) no-repeat;
}

Should become one of the following:

body{
    background:url(../images/images2.jpg) center center no-repeat;
}

or

body
{
    background-image: url(path-to-file/img.jpg);
    background-repeat:no-repeat;
    background-position: center center;
}

EDIT: For your image 'scaling' issue, you might want to look at this question's answer.


Try this background-position: center top;

This will do the trick for you.


background-repeat:no-repeat;
background-position:center center;

Does not vertically center the background image when using a html 4.01 'STRICT' doctype.

Adding:

background-attachment: fixed;

Should fix the problem

(So Alexander is right)


if your not satisfied with the answers above then use this

    * {margin: 0;padding: 0;}

    html
    {
        height: 100%;
    }

    body
    {
        background-image: url("background.png");
        background-repeat: no-repeat;
        background-position: center center;
        background-size: 80%;
    }

Had the same problem. Used display and margin properties and it worked.

.background-image {
  background: url('yourimage.jpg') no-repeat;
  display: block;
  margin-left: auto;
  margin-right: auto;
  height: whateveryouwantpx;
  width: whateveryouwantpx;
}

simply replace

background-image:url(../images/images2.jpg) no-repeat;

with

background:url(../images/images2.jpg)  center;

The only thing that worked for me is..

margin: 0 auto

참고URL : https://stackoverflow.com/questions/2643305/centering-a-background-image-using-css

반응형