Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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 将图像居中放置到div的中心_Html_Css - Fatal编程技术网

Html 将图像居中放置到div的中心

Html 将图像居中放置到div的中心,html,css,Html,Css,假设我有一个div和一个这样的图像 <div id="image_wrapper"> <img src="image.jpg" /> </div> 现在我的图像可以是任何大小,最大宽度或高度角色可以几乎完美地调整它的大小,但我需要图像完全填充image\u wrapper,现在我知道这听起来是不可能的,但我看到facebok对其图像所做的操作,它们只是剪切边缘,这样可以使所有图像看起来完美大小,那么他们是如何做到的呢 编辑Javascript或php

假设我有一个div和一个这样的图像

<div id="image_wrapper">
    <img src="image.jpg" />
</div>
现在我的图像可以是任何大小,最大宽度或高度角色可以几乎完美地调整它的大小,但我需要图像完全填充
image\u wrapper
,现在我知道这听起来是不可能的,但我看到facebok对其图像所做的操作,它们只是剪切边缘,这样可以使所有图像看起来完美大小,那么他们是如何做到的呢


编辑Javascript或php帮助完全可以,只要它能完成任务。

没有Javascript/jquery,就无法完成这项工作

更新,下面是一个示例:


没有javascript/jquery,您无法实现这一点

更新,下面是一个示例:

我无法改进。简而言之:使用表格。

我无法改进。简言之:使用表格。

如果您的
#image_wrapper
不需要特定的高度或纵横比,您可以从快速响应的网页设计运动中获得灵感,并允许图像将其宽度缩放到其包含元素的100%:

考虑这一点:

HTML:

<div id="image_wrapper">
    <img src="image.jpg" />
</div>
#image_wrapper {
    background-color: #000;
    padding: 5px;
    text-align: center;
    width: 500px;
}

img {
    width: 100%;
}
尝试更改
#image_wrapper
的宽度,并注意图像如何符合新的大小。

如果您的
#image_wrapper
不需要特定的高度或纵横比,您可以从快速响应的网页设计运动中获得灵感,并允许图像将其宽度缩放到其包含元素的100%:

考虑这一点:

HTML:

<div id="image_wrapper">
    <img src="image.jpg" />
</div>
#image_wrapper {
    background-color: #000;
    padding: 5px;
    text-align: center;
    width: 500px;
}

img {
    width: 100%;
}

尝试更改
#image_wrapper
的宽度,并注意图像如何符合新的大小。

如果您不想走js之路,有一种非常简单的使用php的方法。我建议您安装。它负责调整大小和过滤图像的缩略图(以及其他东西),这似乎是您在这里试图实现的。您可以将所有形式的参数传递给脚本(请参阅:)。它有一个叫做zoom crop(zc)的功能,可以将图像放大,使其符合您指定的大小,并在中心内进行裁剪。这完全符合你的描述。所以,当你想在你的div中使用图像时,只需使用这个URL

http://URLTOPHPTHUMB.PHP?src=SOURCEOFYOURIMAGE&w=120&h=100&zc=1

如果你不想走js之路,有一个非常简单的方法可以用php实现。我建议您安装。它负责调整大小和过滤图像的缩略图(以及其他东西),这似乎是您在这里试图实现的。您可以将所有形式的参数传递给脚本(请参阅:)。它有一个叫做zoom crop(zc)的功能,可以将图像放大,使其符合您指定的大小,并在中心内进行裁剪。这完全符合你的描述。所以,当你想在你的div中使用图像时,只需使用这个URL

http://URLTOPHPTHUMB.PHP?src=SOURCEOFYOURIMAGE&w=120&h=100&zc=1

使用CSS3
背景尺寸
无重复中心
()

如果要使用JavaScript,请尝试:

<style>
.image_wrapper {
    height:190px;
    width:1000px;
    border:1px solid;
    background:no-repeat center center;
    background-image:url(science.png);
    background-size:contain;
}
</style>

<script type="text/javascript">

function getStyle(el,cssAttribute){
    if(window.getComputedStyle)
        return window.getComputedStyle(el, "").getPropertyValue(cssAttribute);
    else if(document.getElementById(strID).currentStyle)
        return document.getElementById(strID).currentStyle.getAttribute(cssAttribute,false);
    else
        return el.style.getAttribute(cssAttribute,false);
}

window.addEventListener("DOMContentLoaded",function(){
    var w;
    for(var i = 0; w = document.querySelectorAll(".image_wrapper")[i];++i){
        var backgroundSize = '';
        if(w.firstChild.width < parseInt(getStyle(w,"width")))
            backgroundSize += 'auto ';
        if(w.firstChild.height < parseInt(getStyle(w,"height")))
            backgroundSize += 'auto ';
        if(backgroundSize === '')
            backgroundSize = 'contain';
        w.style.backgroundSize = backgroundSize;
        w.style.backgroundImage = 'url('+w.firstChild.src+')';
        w.firstChild.style.display = 'none';
    }
},true);
</script>

.image\u包装器{
高度:190px;
宽度:1000px;
边框:1px实心;
背景:无重复中心;
背景图片:url(science.png);
背景尺寸:包含;
}
函数getStyle(el、CSSATAttribute){
if(window.getComputedStyle)
返回窗口.getComputedStyle(el,“”).getPropertyValue(CSSATAttribute);
else if(document.getElementById(strID.currentStyle)
return document.getElementById(strID).currentStyle.getAttribute(cssAttribute,false);
其他的
返回el.style.getAttribute(cssAttribute,false);
}
addEventListener(“DOMContentLoaded”,function()){
var-w;
对于(var i=0;w=document.queryselectoral(“.image_wrapper”)[i];+i){
var backgroundSize=“”;
if(w.firstChild.width

这将使每个
都变成一个精美的图像包装。

使用CSS3
背景大小
无重复中心
()

如果要使用JavaScript,请尝试:

<style>
.image_wrapper {
    height:190px;
    width:1000px;
    border:1px solid;
    background:no-repeat center center;
    background-image:url(science.png);
    background-size:contain;
}
</style>

<script type="text/javascript">

function getStyle(el,cssAttribute){
    if(window.getComputedStyle)
        return window.getComputedStyle(el, "").getPropertyValue(cssAttribute);
    else if(document.getElementById(strID).currentStyle)
        return document.getElementById(strID).currentStyle.getAttribute(cssAttribute,false);
    else
        return el.style.getAttribute(cssAttribute,false);
}

window.addEventListener("DOMContentLoaded",function(){
    var w;
    for(var i = 0; w = document.querySelectorAll(".image_wrapper")[i];++i){
        var backgroundSize = '';
        if(w.firstChild.width < parseInt(getStyle(w,"width")))
            backgroundSize += 'auto ';
        if(w.firstChild.height < parseInt(getStyle(w,"height")))
            backgroundSize += 'auto ';
        if(backgroundSize === '')
            backgroundSize = 'contain';
        w.style.backgroundSize = backgroundSize;
        w.style.backgroundImage = 'url('+w.firstChild.src+')';
        w.firstChild.style.display = 'none';
    }
},true);
</script>

.image\u包装器{
高度:190px;
宽度:1000px;
边框:1px实心;
背景:无重复中心;
背景图片:url(science.png);
背景尺寸:包含;
}
函数getStyle(el、CSSATAttribute){
if(window.getComputedStyle)
返回窗口.getComputedStyle(el,“”).getPropertyValue(CSSATAttribute);
else if(document.getElementById(strID.currentStyle)
return document.getElementById(strID).currentStyle.getAttribute(cssAttribute,false);
其他的
返回el.style.getAttribute(cssAttribute,false);
}
addEventListener(“DOMContentLoaded”,function()){
var-w;
对于(var i=0;w=document.queryselectoral(“.image_wrapper”)[i];+i){
var backgroundSize=“”;
if(w.firstChild.width

这将把每个
都变成一个奇特的图像包装器。

@Linas-然后这样做,但在CSS中使用您的高度/宽度。它仍然有效。@Linas-然后这样做,但在CSS中使用您的高度/宽度。它仍然有效。我想不出比这更糟糕的主意了。@Rob:那你就是一个狂热者了。实用主义喜欢桌子。我只知道我在做什么。桌子是90年代的,适应变化!我想不出比这更糟糕的主意了。@Rob:那你就是个狂热者了。P