英文:
Unable to crop image with flexbox
问题
我有一张尺寸完全相同的图像,但无法垂直裁剪它。结果是图像溢出,出现空白空间,或者 flexbox 扩展到图像范围。如何解决这个问题,以使图像以响应的方式裁剪到与其他元素的高度相匹配?
白色框是我想要去除的区域,以便图像裁剪到与其他元素的高度相匹配,而不会缩放图像。然而,我无法弄清如何实现这一点(假设我可以使用原始的 CSS)。
<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->
<!-- 语言:lang-css -->
body {
max-width: 100vw;
max-height: 100vh;
font-family: var(--fontStyleStandard);
}
.mainContainer {
display: flex;
flex-direction: row;
justify-content: left;
width: 100vw;
height: 100vh;
margin: 0;
background-color: var(--mainBgColor);
}
.formContainer {
display: flex;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
align-items: flex-start;
flex-direction: column;
}
.innerContainer {
display: flex;
width: auto;
height: auto;
background-color: var(--modulecolor1);
position: relative;
align-items: flex-start;
flex-direction: column;
}
.bodyContainer {
display: inherit;
flex-direction: column;
justify-content: space-between;
margin: 0px;
padding: 0px;
width: auto;
height: auto;
}
.fieldContainer {
padding: 0 0 0 3rem;
background-color: var(--modulecolor1);
position: relative;
box-shadow: 5px 2px 5px rgb(24, 12, 27);
width: 100%;
}
.leftBanner.fixed {
display: flex;
/* 背景:边框框无重复左/包含固定的 URL("../img/sideImage2.png"); */
/* 最大高度:100vh; */
最大高度:100%;
宽度:400px;
边距:0;
填充:0;
对象适合:封面;
对象位置:左上;
}
.leftBanner>img {
display: inherit;
位置:相对;
上:0;
左:0;
右:0;
底部:0;
边距:自动;
最小宽度:45%;
最小高度:45%;
}
.formHeading {
background-color: var(--modulecolor2);
}
<!-- 语言:lang-html -->
<!-- 在此添加您的站点或应用程序内容 -->
<div class="mainContainer">
<div class="leftBanner fixed">
<img src="./img/sideImage2.png" alt="">
</div>
<div class="bodyContainer">
<div class="formContainer">
<h4 class="formHeading fontstyle1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus, totamimpeditcupiditate cumque atque aperiam delectus mollitia rem error incidunt consequuntur reiciendis asperiores id eveniet. In nemo alias debitis unde?</h4>
<div class="buttonContainer"><button class="styled" form="signUp" type="submit">Create Account</button>
<div>Already Have an account? <a href="">Log in</a></div>
</div>
</div>
</div>
</div>
<!-- 结束代码片段 -->
英文:
I have an image sized exactly, but I can't get it to crop vertically. I end up with empty white space with image overflow, or the flexbox extends itself to the image extents. How can I resolve this so the image will crop to match the height of the rest of my elements in a responsive way?
The white box is the area I would like to remove so that the image gets cropped to match the other elements without scaling the image. However I can't figure out how to achieve this. (assuming I can with vanilla CSS)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
max-width: 100vw;
max-height: 100vh;
font-family: var(--fontStyleStandard);
}
.mainContainer {
display: flex;
flex-direction: row;
justify-content: left;
width: 100vw;
height: 100vh;
margin: 0;
background-color: var(--mainBgColor);
}
.formContainer {
display: flex;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
align-items: flex-start;
flex-direction: column;
}
.innerContainer {
display: flex;
width: auto;
height: auto;
background-color: var(--modulecolor1);
position: relative;
align-items: flex-start;
flex-direction: column;
}
.bodyContainer {
display: inherit;
flex-direction: column;
justify-content: space-between;
margin: 0px;
padding: 0px;
width: auto;
height: auto;
}
.fieldContainer {
padding: 0 0 0 3rem;
background-color: var(--modulecolor1);
position: relative;
box-shadow: 5px 2px 5px rgb(24, 12, 27);
width: 100%;
}
.leftBanner.fixed {
display: flex;
/* background:border-box no-repeat left / contain fixed url("../img/sideImage2.png"); */
/* max-height: 100vh; */
max-height: 100%;
width: 400px;
margin: 0;
padding: 0;
object-fit: cover;
object-position: top left;
}
.leftBanner>img {
display: inherit;
position: relative;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 45%;
min-height: 45%;
}
.formHeading {
background-color: var(--modulecolor2);
}
<!-- language: lang-html -->
<!-- Add your site or application content here -->
<div class="mainContainer">
<div class="leftBanner fixed">
<img src="./img/sideImage2.png" alt="">
</div>
<div class="bodyContainer">
<div class="formContainer">
<h4 class="formHeading fontstyle1"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus, totamimpeditcupiditate cumque atque aperiam delectus mollitia rem error incidunt consequuntur reiciendis asperiores id eveniet. In nemo alias debitis unde?</h4>
<div class="buttonContainer"><button class="styled" form="signUp" type="submit">Create Account</button>
<div>Already Have an account? <a href="">Log in</a></div>
</div>
</div>
</div>
</div>
<!-- end snippet -->
答案1
得分: 1
有两种方法可以实现这种裁剪,首先,您可以将.leftBanner
的高度设置为与.bodyContainer
相同。这样做没问题,但它不够响应,并且我想象.bodyContainer
中可能会有更多内容,因此裁剪效果可能会被移除。我建议做以下修改:
将横幅图像放在表单内,这样它的最大高度将始终是表单的高度,从而实现正确的裁剪。
这在使用.leftBanner
的background-image
设置时会有问题,因为background-images
没有设置的高度,所以您需要在该类中输入一个高度/最小高度,以便它被视为具有尺寸的活动div。或者使用<img></img>
来设置背景图像,并给它一个object-fit: cover
。
上述解决方案有效,只要您不介意为表单设置最小高度。
英文:
There are two ways to achieve this cropping,
First, you could set the height of your .leftBanner
to be the same as your .bodyContainer
This is fine, though it will not be as responsive, and I also imagine your .bodyContainer
will have more content in it, thus removing the set crop.
Instead I'd do the following
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
max-width: 100vw;
max-height: 100vh;
font-family: var(--fontStyleStandard);
}
.mainContainer {
background-color: var(--mainBgColor);
background-color: red;
display: flex;
flex-direction: row;
justify-content: left;
width: 100vw;
height: 100vh;
margin: 0;
}
.bodyContainer {
display: inherit;
justify-content: space-between;
margin: 0px;
padding: 0px;
width: auto;
}
.formContainer {
/*Change the FormContainer to a flexbox and set to your intended Height/Max-Height */
display: flex;
/*This will be a flex Row */
flex-direction: row;
height:fit-content;
max-height: 400px;
width: 100%;
margin: 0;
background-color: blue;
padding: 0;
align-items: flex-start;
}
.form{
background-color:green;
height:fit-content;
}
.leftBanner.fixed {
/*set this image Height to 100% filling the intended height of the formContainer give it a min-height so it will always have a size*/
height:100%;
min-height:200px;
background: border-box no-repeat left / contain fixed
url("https://media.istockphoto.com/id/1167895713/photo/abstract-galaxy-watercolor-backgrounds.jpg?b=1&s=170667a&w=0&k=20&c=ivNkiy9Bq_CMEAEoGrDNQuilHll53fhAn-BKTgB9dqU=");
background-color: green;
display: flex;
width: 400px;
}
.leftBanner > img {
display: inherit;
position: relative;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 45%;
min-height: 45%;
}
.formHeading {
background-color: var(--modulecolor2);
}
<!-- language: lang-html -->
<body>
<div class="mainContainer">
<div class="bodyContainer">
<div class="formContainer">
<!--Place the Banner inside the form this will restrict its max height to the height of the form-->
<div class="leftBanner fixed">
<img src="./img/sideImage2.png" alt="">
</div>
<div class='form'>
<h4 class="formHeading fontstyle1"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus, totamimpeditcupiditate cumque atque aperiam delectus mollitia rem error incidunt consequuntur reiciendis asperiores id eveniet. In nemo alias debitis unde?</h4>
<div class="buttonContainer"><button class="styled" form="signUp" type="submit">Create Account</button>
<div>Already Have an account? <a href="">Log in</a></div>
</div>
</div>
</div>
</div>
</div>
</body>
<!-- end snippet -->
By Placing your banner image inside the form, it's max-height will always be the height of the form. Thus cropping it to the correct height.
This becomes problematic with using the background-image
setting of the .leftBanner
because background-images
do not have set heights, so you will need to input a height/min-height to that class in order for it to register as an active div with dimensions. Or use the <img></img>
to set your background image, and give it an object-fit:cover
The above solution will work, so long as you don't mind setting a min-height to the form.
答案2
得分: 1
代码部分不翻译,以下是翻译好的部分:
"A simple construct with position: absolute;
and overflow: hidden;
should do the task, if I understood what you're trying to do correctly. It's absolutely responsive."
英文:
A simple construct with position: absolute;
and overflow: hidden;
should do the task, if I understood what you're trying to do correctly. It's absolutely responsive:
.mainContainer{
display: flex;
justify-content: left;
}
.leftBanner.fixed {
width: 200px;
position: relative;
overflow: hidden;
}
.leftBanner img{
position: absolute;
width: 100%;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论