简体   繁体   English

如何在图像下方放置轮廓矩形,如下图所示

[英]How can I put an outline rectangle underneath an image as shown in the following image

Probably my question is fairly simple and I apologize for this but started shortly with html and css and I still do not have much practice. 也许我的问题很简单,对此我深表歉意,但不久之后就开始使用html和css,但我仍然没有太多练习。 I would like to know how I can put a rectangle underneath an image (jpeg or css) as shown in the following image: 我想知道如何在图像(jpeg或css)下面放置一个矩形,如下图所示:

我的问题

Wrap img in a div and add a psuedoelement to that div . imgdiv和添加psuedoelementdiv

Using a pseudoelement means you don't need to declare the height and width of the img container. 使用pseudoelement意味着您无需声明img容器的高度和宽度。

 div { position: relative; display: inline-block; } img { vertical-align: bottom; } div:after { content: ''; position: absolute; left: 3rem; top: 3rem; border: 2px solid black; width: 100%; height: 100%; z-index: -1; } 
 <div><img src="https://unsplash.it/400"></div> 

A more complete layout example... 更完整的布局示例...

 section { padding: 3rem 4.5rem; } figure { position: relative; float: left; margin: 0 3rem 1rem 0; } img { vertical-align: bottom; } figure:after { content: ''; position: absolute; left: 1.5rem; bottom: 1.5rem; border: 2px solid black; width: 100%; height: 100%; z-index: -1; } h3 { margin-top: 0; } 
 <section> <figure><img src="https://unsplash.it/260"></figure> <h3>A Title</h3> <h5>Subtitle subtitle subtitle subtitle</h5> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </section> 

This is a very basic example on how you can add a rectangle underneath an image using css. 这是一个非常基本的示例,说明如何使用css在图像下方添加矩形。 Check the snippet. 检查代码段。

 div { position: absolute; width: 350px; height: 230px; border: 2px solid grey; top: 30px; left: 30px; z-index: -10; } img { width: 350px; } 
 <img src="https://static.boredpanda.com/blog/wp-content/uuuploads/landscape-photography/landscape-photography-23.jpg" /> <div></div> 

One way to do this, is by using a separate div for the border: Setting position: absolute; 一种方法是,为边框使用单独的div :设置position: absolute; to the image and the div for the border, and using z-index to put the image on top of the border div. 图像和边框的div,然后使用z-index将图像放在边框div的顶部。

You have to use a container div to embrace both, the image and the border div. 您必须使用容器div来包含图像和边框div。 This container gets position: relative (to set the position: absolute of the child elements into effect) and needs to have the dimensions of the entire cluster of child elements. 此容器获取position: relative (以设置position: absolute子元素的position: absolute生效),并且需要具有整个子元素簇的尺寸。

 .container { position: relative; width: 240px; height: 120px; } .container div { width: 200px; height: 100px; } .image { z-index: 20; position: absolute; top: 0px; background-color: lightblue; } .rect { z-index: 10; position: absolute; top: 20px; left: 40px; border: 1px solid #666; } 
 <p>Text</p> <p>Text</p> <div class="container"> <div class="image"> Image </div> <div class="rect"> </div> </div> <p>Text</p> <p>Text</p> 

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

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