Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何进行img缩放和居中?_Html_Css_Twitter Bootstrap - Fatal编程技术网

Html 如何进行img缩放和居中?

Html 如何进行img缩放和居中?,html,css,twitter-bootstrap,Html,Css,Twitter Bootstrap,我有一个问题,使图像缩放和居中。我想让图像(带有文本设计和代码)像这样:我在网上查找这个,我举了很多例子,但我不能这样做。我的上一个版本是当我更改浏览器视图时,因为最小的图像没有像saaleedesign.com上那样居中。在浏览器调整大小后,如何使div中的img居中?所以基本上你应该将背景位置设置为居中 html: 这就是如何真正地将其居中: 演示- 下面是一个关于如何在body标签上使用的非常粗糙、极简主义的示例 HTML JS //此部分检测窗口宽度对于当前图像纵横比是否太窄,并将其调整

我有一个问题,使图像缩放和居中。我想让图像(带有文本设计和代码)像这样:我在网上查找这个,我举了很多例子,但我不能这样做。我的上一个版本是当我更改浏览器视图时,因为最小的图像没有像saaleedesign.com上那样居中。在浏览器调整大小后,如何使div中的img居中?

所以基本上你应该将背景位置设置为居中

html:


这就是如何真正地将其居中:

演示- 下面是一个关于如何在body标签上使用的非常粗糙、极简主义的示例

HTML JS
//此部分检测窗口宽度对于当前图像纵横比是否太窄,并将其调整为高度100%,而不是宽度100%。
var photo=document.images[0],
容器=document.body;
$(window).on('resize.coverPhoto',function(){
requestAnimationFrame(检查比率);
});
函数校验比(){
var state=photo.clientHeight=container.clientWidth;
类列表[状态?'add':'remove']('max');
}

您可以为图像指定所需的任何宽度

整个技巧基于使用百分比度量

这是一个非常简单的解决方案,适用于所有浏览器,无论其版本如何

例如:
html,正文{
身高:100%;
宽度:100%
显示:块;
}
img{
位置:绝对位置;
保证金:自动;
排名:0;
左:0;
右:0;
底部:0;
}


您尝试了哪些代码?如何使这两个按钮显示并按此工作?我以前从来没见过这样的!检查此链接
<div class="container">Text</div>
#container {
   background-image: url("img.jpg");
   height: [some-height]px;
   width: 100%;
   background-position: center center;
   background-size: cover;
}
<img src="image.jpg">
img{
    position:absolute;
    z-index:-1;
    top:50%;
    left:50%;
    width:100%;
    transform:translate(-50%, -50%); 
}

img.max{ width:auto; height:100%; }

/* RESET & stuff */
html, body{ height:100%; overflow:hidden; }
*{ margin:0; padding:0; }
// this part detects when the window width is too narrow for the current image aspect-ratio, and fits it to height 100% instead of width 100%.
var photo     = document.images[0],
    container = document.body;

$(window).on('resize.coverPhoto', function(){
  requestAnimationFrame(checkRatio);
});

function checkRatio(){
  var state = photo.clientHeight <= container.clientHeight && 
              photo.clientWidth >= container.clientWidth;
  
  photo.classList[state ? 'add' : 'remove']('max');
}