在屏幕上居中横幅图像调整大小

时间:2017-08-17 19:06:52

标签: html css

我遇到一个问题,在尝试调整屏幕尺寸时,横幅图像("评论我们"文字和线条)没有正确居中。这是我的问题的前后。后图像也在移动屏幕上显示相同的结果 before after

该图片的HTML如下:

<div class="rowBanner">
     <img src="images/reviewus.png" alth="review">
</div>

唯一有效的CSS是

div.rowBanner{
    padding-top: 40px;
    text-align:center;
}

我希望&#34;回顾我们&#34;无论屏幕宽度如何,图像的文本部分都要居中(从左到右)

4 个答案:

答案 0 :(得分:1)

只需将此css添加到图片中:

div.rowBanner > img {
    display: block; 
    margin: 0 auto;
    max-width: 100vw;
}
  

已编辑:为img添加了最大值,100vw表示100个视口宽度单位,请参阅:http://caniuse.com/#feat=viewport-units

答案 1 :(得分:1)

您遇到的问题可能是从桌面移动到移动设备,汉堡包菜单会移动您的徽标。使用transform: translate(-50%, 0%);的绝对位置会使你的img按照自己的宽度居中。

.rowBanner {
  position: absolute;
  left: 50%;
  display: block;
  transform: translate(-50%, 0%);
}
  

居中百分比宽度/高度元素

     

参考:Redirect

.navbar {
  position: relative;
}

.rowBanner {
  position: absolute;
  left: 50%;
  display: block;
  transform: translate(-50%, 0%);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="rowBanner">
      <img src="http://via.placeholder.com/100x50" alth="review">
    </div>
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

答案 2 :(得分:0)

您可以尝试关注CSS:

div.rowBanner {
margin-left:auto;
margin-right:auto;
width:80%;
}

答案 3 :(得分:0)

如何使用CCS background-image 来设置图像。然后,您可以通过 background-position

控制中心点在不同分辨率上的位置

function resize(isDesktop) {
  if (isDesktop) {
    $('.banner').css('width', '100%');
  } else {
    $('.banner').css('width', '200px');
  }
}
.banner {
  background-image: url('https://i.ytimg.com/vi/qh7LLydY8eo/maxresdefault.jpg');
  background-size: cover;
  background-position: center;
  height: 175px;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="resize(true)">Desktop</button>
<button onclick="resize()">Mobile</button>

<div class="page">
  <div class="banner">
  </div>
</div>