简体   繁体   English

围绕中心图像的DIV

[英]DIV around a centered image

I am trying to display an image, centered vertically and horizontally on a screen (which in my case, is a modal screen). 我试图在屏幕上显示垂直和水平居中的图像(在我的例子中,是一个模态屏幕)。 The image must be a maximum of 80% high, and 80% wide. 图像必须最高为80%,宽度为80%。 It can be landscape, square or portrait. 它可以是风景,广场或肖像。

I have achieved that. 我已经实现了这一点。 But now I need to add a div, fitted tightly around the image, so that I can apply a text-over effect. 但是现在我需要在图像周围添加一个div,以便我可以应用文本效果。

This is what I have so far. 这就是我到目前为止所拥有的。

https://jsfiddle.net/Cralis/1w8d4zx5/ https://jsfiddle.net/Cralis/1w8d4zx5/

<div class="imageContainer">
    <img id="imgContainer" class="fullimage animated fadeIn" src="http://cameraflare.com/photostore/23d255d8-e259-47ee-be2d-fae68c730f6d/afda43dd-5a81-40b2-8161-d1f222689d3f.jpg">
    <h2>
              <span>The one and only:
                <span class='spacer'></span>
                <br />
                <span class='spacer'></span>
                The Hulk
                </span>
                </h2>
  </div>

I am trying to achieve what this tutorial shows, except, their demo is aligned left on the screen. 我试图实现本教程所展示的内容,除了他们的演示在屏幕上左对齐。 My image is centered. 我的图像居中。

https://css-tricks.com/text-blocks-over-image/ https://css-tricks.com/text-blocks-over-image/

How can I get the imageContainer div to 'hug' the image? 如何让imageContainer div“拥抱”图像?

Not pretty sure but i think what you are looking for is the following: 不太确定,但我认为您正在寻找的是以下内容:

  • Set your .imageContainer as display: inline-block and add it text-align: left .imageContainer设置为display: inline-block并将其添加到text-align: left
  • Set the parent of image container to text-align: center 将图像容器的父级设置为text-align: center

So you will get your div centered but you will also wrap/hug the image with the border. 因此,您将使div居中,但您也将使用边框包裹/拥抱图像。 Take a look here: 看看这里:

https://jsfiddle.net/0k3wrfxt/ https://jsfiddle.net/0k3wrfxt/

you can use css3 flexbox concept to achieve this. 你可以使用css3 flexbox概念来实现这一目标。 add the following styles to the parent container of your image 将以下样式添加到图像的父容器中

  • display:flex; 显示:弯曲;

  • align-items:center; 对齐项:中心;

  • justify-content:center; 证明内容:中心;

and remove the margin:100px; 并删除保证金:100px; on your .imageContainer ,it will work fine .I'm added the snippet below. .imageContainer上 ,它会正常工作。我添加了下面的代码段。

 html,body{ width:100%; height:100%; display:flex; align-items:center; justify-content:center; } .fullimage { vertical-align: top; display: block; margin: auto; border-radius: 7px; } .imageContainer { max-height: 80vh; max-width: 80vw; border: solid; /*margin: auto; margin-left:100px;*/ position: absolute; padding: 0; } h2 { position: absolute; top: 200px; left: 0; width: 100%; } h2 span { color: white; font: bold 24px/45px Helvetica, Sans-Serif; letter-spacing: -1px; background: rgb(0, 0, 0); /* fallback color */ background: rgba(0, 0, 0, 0.7); padding: 10px; } h2 span.spacer { padding: 0 5px; } 
 <div class="imageContainer"> <img id="imgContainer" class="fullimage animated fadeIn" src="http://cameraflare.com/photostore/23d255d8-e259-47ee-be2d-fae68c730f6d/afda43dd-5a81-40b2-8161-d1f222689d3f.jpg"> <h2> <span>The one and only: <span class='spacer'></span> <br /> <span class='spacer'></span> The Hulk </span> </h2> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM