Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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 使用URL将图像居中_Html_Css_Twitter Bootstrap - Fatal编程技术网

Html 使用URL将图像居中

Html 使用URL将图像居中,html,css,twitter-bootstrap,Html,Css,Twitter Bootstrap,我使用bootstrap的中心块类将图像居中 <section class="container"> <a href="www.apple.com"> <img class="center-block" src="https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201605191653" width="150px;" height="100px;"/>

我使用bootstrap的中心块类将图像居中

<section class="container">
    <a href="www.apple.com">
    <img class="center-block" src="https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201605191653" width="150px;" height="100px;"/>
    </a>
</section>

唯一的问题是链接跨越了容器的宽度。如何将URL限制为图像


工作

如果您指的是链接,如将鼠标悬停在容器的任何部分上。容器将激活链接,而不仅仅是图像,以下是解决方案:

.container {
    text-align: center;
}

.container a {
    display: inline-block;
}

这基本上为链接提供了自己的尺寸,因此它不会拉伸以填充父链接。

如果您指的是链接,就像在容器的任何部分上悬停一样。容器将激活链接,而不仅仅是图像,以下是解决方案:

.container {
    text-align: center;
}

.container a {
    display: inline-block;
}

这基本上为链接提供了自己的维度,因此它不会拉伸以填充父元素。

尝试在

示例


居中的苹果标志

尝试在您的

示例


居中的苹果标志

除了一个分区标签,您还必须为图像添加一个容器,并在其中添加链接。这样,您就不必修改容器类,并且可以引入一个div来按照您的意愿组织和设置样式。以下是工作代码:

<section class="container">
  <!-- You can use Bootstrap's text-center class a style="text-align: center;" here -->
  <div class='center-block text-center'>
    <a href="www.apple.com">
      <img src="https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201605191653" width="150px;" height="100px;"/>
    </a>
  </div>
</section>

除了一个分区标签,您还必须为图像添加一个容器,并在其中添加链接。这样,您就不必修改容器类,并且可以引入一个div来按照您的意愿组织和设置样式。以下是工作代码:

<section class="container">
  <!-- You can use Bootstrap's text-center class a style="text-align: center;" here -->
  <div class='center-block text-center'>
    <a href="www.apple.com">
      <img src="https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201605191653" width="150px;" height="100px;"/>
    </a>
  </div>
</section>

链接是一个内联元素,因此只需在容器上使用
文本中心即可

<section class="container">
<div style="width:150px;margin-left:auto;margin-right:auto;">
  <a class="center-block"href="www.apple.com">
    <img class="center-block" src="https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201605191653" width="150px;" height="100px;"/>
  </a>
  </div>
</section>


链接是一个内联元素,因此只需在容器上使用
文本中心即可

<section class="container">
<div style="width:150px;margin-left:auto;margin-right:auto;">
  <a class="center-block"href="www.apple.com">
    <img class="center-block" src="https://www.apple.com/ac/structured-data/images/open_graph_logo.png?201605191653" width="150px;" height="100px;"/>
  </a>
  </div>
</section>


我在您的FIDLE扩展中没有看到任何url。url跨越容器的宽度。我想将其限制为图像本身。我不明白URL与宽度有什么关系。我希望有人能够单击图像,通常这样做:。这会将URL限制为图像。但是,如果使用中心块类将图像居中,则可以从容器内的任何位置单击URL。我希望它可以从图像中单击。与答案无关,但请尝试使用
href=”http://www.apple.com“
而不是
href=“www.apple.com”
。这样,单击不会将您带到图像源,也不会将您带到服务器中的url。我在您的FIDLE扩展中没有看到任何url。url跨越容器的宽度。我想将其限制为图像本身。我不明白URL与宽度有什么关系。我希望有人能够单击图像,通常这样做:。这会将URL限制为图像。但是,如果使用中心块类将图像居中,则可以从容器内的任何位置单击URL。我希望它可以从图像中单击。与答案无关,但请尝试使用
href=”http://www.apple.com“
而不是
href=“www.apple.com”
。通过这种方式,单击不会将您带到图像源或服务器中的url。非常感谢!我没想到只把链接和图像包装在中间块和文本中心类中。我如何用img响应标签获得同样的效果?非常感谢!我没有想到仅仅将链接和图像包装在中间块和文本中心类中。如何使用img响应标签获得相同的效果?