无法使用 flexbox 裁剪图像。

huangapple go评论60阅读模式
英文:

Unable to crop image with flexbox

问题

我有一张尺寸完全相同的图像,但无法垂直裁剪它。结果是图像溢出,出现空白空间,或者 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?

无法使用 flexbox 裁剪图像。


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(&quot;../img/sideImage2.png&quot;); */
/* max-height: 100vh; */
max-height: 100%;
width: 400px;
margin: 0;
padding: 0;
object-fit: cover;
object-position: top left;
}
.leftBanner&gt;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 -->

&lt;!-- Add your site or application content here --&gt;
&lt;div class=&quot;mainContainer&quot;&gt;
&lt;div class=&quot;leftBanner fixed&quot;&gt;
&lt;img src=&quot;./img/sideImage2.png&quot; alt=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;bodyContainer&quot;&gt;
&lt;div class=&quot;formContainer&quot;&gt;
&lt;h4 class=&quot;formHeading fontstyle1&quot;&gt; 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?&lt;/h4&gt;
&lt;div class=&quot;buttonContainer&quot;&gt;&lt;button class=&quot;styled&quot; form=&quot;signUp&quot; type=&quot;submit&quot;&gt;Create Account&lt;/button&gt;
&lt;div&gt;Already Have an account? &lt;a href=&quot;&quot;&gt;Log in&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

有两种方法可以实现这种裁剪,首先,您可以将.leftBanner的高度设置为与.bodyContainer相同。这样做没问题,但它不够响应,并且我想象.bodyContainer中可能会有更多内容,因此裁剪效果可能会被移除。我建议做以下修改:

将横幅图像放在表单内,这样它的最大高度将始终是表单的高度,从而实现正确的裁剪。

这在使用.leftBannerbackground-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(&quot;https://media.istockphoto.com/id/1167895713/photo/abstract-galaxy-watercolor-backgrounds.jpg?b=1&amp;s=170667a&amp;w=0&amp;k=20&amp;c=ivNkiy9Bq_CMEAEoGrDNQuilHll53fhAn-BKTgB9dqU=&quot;);
background-color: green;
display: flex;
width: 400px;
}
.leftBanner &gt; 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 -->

&lt;body&gt;
&lt;div class=&quot;mainContainer&quot;&gt;
&lt;div class=&quot;bodyContainer&quot;&gt;
&lt;div class=&quot;formContainer&quot;&gt;
&lt;!--Place the Banner inside the form this will restrict its max height to the height of the form--&gt;
&lt;div class=&quot;leftBanner fixed&quot;&gt;
&lt;img src=&quot;./img/sideImage2.png&quot; alt=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div class=&#39;form&#39;&gt;
&lt;h4 class=&quot;formHeading fontstyle1&quot;&gt; 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?&lt;/h4&gt;
&lt;div class=&quot;buttonContainer&quot;&gt;&lt;button class=&quot;styled&quot; form=&quot;signUp&quot; type=&quot;submit&quot;&gt;Create Account&lt;/button&gt;
&lt;div&gt;Already Have an account? &lt;a href=&quot;&quot;&gt;Log in&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;

<!-- 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 &lt;img&gt;&lt;/img&gt; 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%;
}

无法使用 flexbox 裁剪图像。

huangapple
  • 本文由 发表于 2023年3月21日 03:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定