中心背景图像与标头

时间:2016-07-12 12:34:51

标签: css

我在这里有一个JSFiddle - https://jsfiddle.net/ow1x3e0a/14/

简单的CSS问题。

我有一个标题,我需要在旁边有一个图像。我可以用背景图片做到这一点。

当窗口较小时,我需要将文本和图像居中,使其显示在中心,就像在左侧一样。

目前文本居中但图像保留在左侧

我在没有文字对齐的情况下尝试过,但无法将其与margin: auto:

对齐
.container{
    margin-top: 50px;
}
.header{
    font-size: 20px;
    padding-left: 110px;
    background-image: url(http://placehold.it/100x50);
    background-repeat: no-repeat;
}

@media only screen and (max-width:767px){
  .header{
      text-align: center;
  }
}

2 个答案:

答案 0 :(得分:1)

当您提供背景时,您需要使用:

func tapDetected() {
    print("Single Tap on imageview")
    print(param1) // print 120
    print(param2) // print hello
    print(param3) // print world
} 

background-position: center center; text-align: center仅适用于内容,不适用于背景。

答案 1 :(得分:0)

添加background-position: center center(或简写background: url(...) center center;)。调整填充以使其按照您想要的方式对齐。