简体   繁体   English

如何将图片放在div的顶部(水平居中,垂直居中,居中)

[英]How to put an image on top of a div (center horizontally but top vertically, in the middle)

I am writing a blog post (blogger) in which I wanna share a letter. 我正在写一篇博客文章(博客),我想分享一封信。 The text of this letter I have put it in a div with a light grey color and a shadow on the right and bottom so it looks like a piece of paper. 我将这封信的文本放在一个div中,该文本的底部和右侧分别是浅灰色和阴影,因此看起来像张纸。

My HTML says something like: 我的HTML内容如下:

This is my post. Blah blah blah<br />
<br />
Here is more text, blah bla blah. And this is the letter:<br />
<br />
<div style="background-color: #f7f7f7; box-shadow: 3px 3px 8px #DBDBDB; padding-bottom: 20px; padding-left: 40px; padding-right: 20px; padding-top: 35px;">
28 August 2015<br />
<br />
This is the letter, blah blah blah.<br />
<br />
Here mor parragraphs of the letter, bla bla bla. <br />
<br />

This makes my post look like this: http://i.stack.imgur.com/xUNNL.png 这使我的帖子看起来像这样: http : //i.stack.imgur.com/xUNNL.png

Now, I want to add a digital washi tape (like a piece of cellotape) on top of my grey div, so it looks like the letter has been stuck there with the bit of cellotape. 现在,我想在我的灰色div上添加一个数字纸胶带(像一块大纸胶带),所以看起来这封信已经被那块大纸胶带粘住了。

Like this: http://i.stack.imgur.com/78kyz.jpg 像这样: http : //i.stack.imgur.com/78kyz.jpg

How could I do this in the HTML? 如何在HTML中执行此操作? (I am writing directly in just one blogger blogspot) (我直接在一个博客博客中直接写作)

Thanks. 谢谢。

You can add the image in the div, give it an id or class, and give it the properties: z-index:10; 您可以将图像添加到div中,为其提供ID或类,并为其提供属性:z-index:10; //or whatever number you want. //或您想要的任何数字。 What this does, it will bring this image in front of all other position:absolute; 这将使此图像处于所有其他位置的前面:绝对; left: 50%; 左:50%; // it wil bring it in the middle. //它会把它放在中间。

<img src="image-src" style="z-index:10; position:absolute; left:50%">

Simplified version. 简化版。

I suppose that your letter div is positioned relative . 我想您的来信div是relative To implement this digital wash tape just put another div in your letter div and position it absolute . 要实现此数字清洗带,只需在字母div中放入另一个div并将其absolute放置即可。 Something like that: 像这样:
.cellotape { height:100px; width:400px; position:absolute; top:-20px; left:500px; }

Here is a fiddle example 这是一个小提琴的例子

add this .img class to your image: 将此.img类添加到您的图像中:

.img{
    position: relative;
    top: 0;
    margin-top: -60px;
    left: calc(50% - 50px);
    width: 100px;
    height: 50px;
    background: lightcoral;
}

This will set your image position to relative, at the top of the letter, into its center. 这会将您的图片位置设置为相对于字母顶部相对于其中心的位置。

You will have to add the image inside your letter like this: 您必须像这样在信件中添加图片:

<div class='letter'>
    <div class='img'></div>
    28 August 2015<br />
    <br />
    This is the letter, blah blah blah.<br />
    <br />
    Here mor parragraphs of the letter, bla bla bla. <br />
    <br />

</div>

Notice that I have div just to show you how to get things done. 请注意,我的div只是向您展示如何完成任务。

You may need to adjust the left percent, height and width to suit your needs, but this should work for you 您可能需要调整左百分比,高度和宽度以适合您的需求,但这应该对您有用

<style>
.topImg {
    position: absolute;
    left: 25%;
    z-index: 10;
    width: 500px;
    height: 335px;
}
</style>



<img src="" class="topImg">

you could try something like the following if you only can aply inline styles... 如果您只能使用内联样式,则可以尝试以下操作...

 This is my post. Blah blah blah<br /> <br /> Here is more text, blah bla blah. And this is the letter:<br /> <br /><br /><br /> <div style="background-color: #f7f7f7; box-shadow: 3px 3px 8px #DBDBDB; padding-bottom: 20px; padding-left: 40px; padding-right: 20px; padding-top: 35px;"> <div style="width:50px; margin:0 auto"> <img style="width:50px; position:absolute; margin:-60px auto 0;" src="https://i.stack.imgur.com/ue96u.jpg?s=328&g=1" /> </div> 28 August 2015<br /> <br /> This is the letter, blah blah blah.<br /> <br /> Here mor parragraphs of the letter, bla bla bla. <br /> <br /> 

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

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