简体   繁体   English

左侧为文本,右侧为溢出图像的 div 容器

[英]Div container with text on the left side and overflow image on the right

I want to recreate the following structure:我想重新创建以下结构: 在此处输入图像描述

With black is div container and inside the container on the left there will be text and on the right i need an image bigger than the container.黑色是 div 容器,左边的容器内会有文本,右边我需要一个比容器大的图像。

I tried to do this by grids but things got funky real quick.我试图通过网格来做到这一点,但事情变得很时髦。

As it seems to be important that the containing div maintains the dimensions (as shown by its border), this snippet adds in the actual image as a background on a pseudo element that is absolutely positioned.由于包含 div 保持尺寸(如其边框所示)似乎很重要,因此此代码段将实际图像添加为绝对定位的伪元素的背景。

That way the protruding bit of image does not alter the container div dimensions.这样,图像的突出部分不会改变容器 div 的尺寸。

Here's a simple snippet using a grid to position the left and right sides.这是一个使用网格来定位左侧和右侧的简单片段。 Of course you will want to alter proportions to suit your particular case, add styling to the leftside and so on:当然你会想要改变比例以适应你的特殊情况,在左侧添加样式等等:

 .container { display: grid; grid-template-columns: 3fr 2fr; width: 50vw; height: auto; margin-top: 10vh; border: solid 2px black; } .leftside { padding: 1vw; } .rightside { position: relative; width: 100%; height: 100%; } .rightside::before { content: ''; background-color: pink; background-image: url(https://picsum.photos/id/1015/500/200); background-size: cover; background-position: center center; background-repeat: no-repeat; width: 50%; height: 140%; bottom: 0; left: 25%; position: absolute; }
 <div class="container"> <div class="leftside"> <h2>Heading</h2> <div>text1</div> <div>text2</div> </div> <div class="rightside"></div> </div>

Here you go:干得好:

 .background { padding: 25px; display: flex; border: 1px solid black; height: 150px; position: relative; margin-top: 50px; } .text { border: 1px solid green; width: 50%; padding: 10px; } .img { text-align: center; width: 50%; display: flex; justify-content: center; } .img>div { border: 1px solid blue; width: fit-content; padding: 10px; height: 200px; position: absolute; bottom: 25px; }
 <div class="background"> <div class="text"> <p> text1 </p> <p> text2 </p> <button> Click me </button> </div> <div class="img"> <div> me img </div> </div> </div>

Hope this helps希望这可以帮助

go with the flexbox.使用 flexbox。

 .main-container{ display:flex; display: flex; justify-content: space-evenly; border:1px solid black; margin:30px; height:300px; padding:10px; } .image{ width:50vw; position:relative; } img{ width:100%; height:150%; width: 100%; height: 150%; top: -50%; position: absolute; } .text{ display:flex; align-items:center; }
 <div class="main-container"> <div class="text"> <p>Somthing Somthing</p> </div> <div class="image"> <img src="https://loremflickr.com/640/360" /> </div> </div>

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

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