简体   繁体   English

弹性盒问题

[英]Issues with flex-box

So I have a question about flexbox. 所以我有一个关于flexbox的问题。 Basically, I'm creating a card component. 基本上,我正在创建卡组件。 I'd like the image on the left and the card body on the right. 我想要左边的图像,右边的卡体。 I have it.. Sort of. 我有..有点。

The problem is the card is bigger than the image. 问题是卡大于图像。 I'd like the card to stay the height of the image. 我希望卡片保持图像的高度。

Here is my code: 这是我的代码:

 .card-horizontal { display: flex; flex-direction: row; width: 100%; } .card-horizontal__image { width: 100%; max-width: 100%; background-position: center; } .card-horizonal__body{ display: flex; flex-direction: column; background: white; box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23); padding: 15px 20px; } 
 <div class='st-grid-12'> <div class='card-horizontal'> <div class='card-horizontal__imgage'> <img alt='' src='https://source.unsplash.com/random/400x400'> </div> <div class='card-horizonal__body'> <h2>This is a title</h2> <p class='card__copy'>Lorem ipsum dolor sit amet consectetur adipisicing elit. Blanditiis velit accusamus non numquam assumenda quaerat, consequatur recusandae facilis inventore dicta, distinctio magni obcaecati vel aliquid, fugit maxime. Voluptas, ullam officiis?</p> <div class='btn btn--secondary'>Read More</div> </div> </div> </div> 

First, you had a typo in your class, And second you had a misuse of your display: flex 首先,您的课堂上有一个错字,其次,您滥用了显示器:flex

Here is your css now 这是你的CSS

  .card-horizontal {
    display: flex;
    flex-direction: row;
    width: 100%;
    border: 1px solid red;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  }

  .card-horizontal__imgage {
    max-width: 100%;
    background-position: center;  
    display: flex;  
  }

  .card-horizonal__body {
    display: flex;
    flex-direction: column;
    background: white;
    padding: 15px 20px;
  }

I'm sure you can tell why I did that border and commented the shadow. 我敢肯定,你可以说出我为什么做那个边框并评论了阴影。 To show you they're truly equal in size. 为了向您展示它们的大小真正相等。

here is the code working 这是代码的工作

  .card-horizontal { display: flex; flex-direction: row; width: 100%; border: 1px solid red; } .card-horizontal__imgage { max-width: 100%; background-position: center; display: flex; } .card-horizonal__body { display: flex; flex-direction: column; background: white; /* box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23); */ padding: 15px 20px; } 
 <div class='st-grid-12'> <div class='card-horizontal'> <div class='card-horizontal__imgage'> <img alt='' src='https://source.unsplash.com/random/400x400'> </div> <div class='card-horizonal__body'> <h2>This is a title</h2> <p class='card__copy'>Lorem ipsum dolor sit amet consectetur adipisicing elit. Blanditiis velit accusamus non numquam assumenda quaerat, consequatur recusandae facilis inventore dicta, distinctio magni obcaecati vel aliquid, fugit maxime. Voluptas, ullam officiis?</p> <div class='btn btn--secondary'>Read More</div> </div> </div> </div> 

EDIT 编辑

To ease the worries about the shadow, I added the shadow to the entire card, which is how it should be done anyway. 为了减轻对阴影的担心,我将阴影添加到整个卡中,无论如何应该这样做。 You can't count the shadow as height. 您不能将阴影视为高度。 Shadows are CSS embellishments, like an outline for example. 阴影是CSS点缀,例如轮廓。 It doesn't add size to the item even if it appears that way....Checking the browser dev tool will answer some of these questions. 即使以这种方式出现,它也不会增加项目的大小。...检查浏览器开发工具将回答其中的一些问题。

First, as I mentioned in the comments, in your HTML, you have a typo on the class='card-horizontal__imgage' code. 首先,正如我在评论中提到的那样,在您的HTML中,您在class='card-horizontal__imgage'代码上有错字。

The issue is that the height of the overall card is causing the image div to stretch to be taller than the image by 4 pixels. 问题是整个卡的高度导致图像div拉伸到比图像高4像素。 Setting a height on the card itself solves the problem. 在卡本身上设置height可以解决此问题。

You have background-position: center; 您有background-position: center; in your CSS, but that does nothing if the element you apply it to doesn't have a background. 在您的CSS中,但是如果您将其应用到的元素没有背景,则不会执行任何操作。 Another way to solve the issue (and that's what I'm showing below) is to not use an image element at all and just set the image as the background-image for the div element that is supposed to hold it. 解决此问题的另一种方法(这就是我在下面显示的内容)是根本不使用图像元素,而只是将图像设置为应该保留它的div元素的背景图像。

Also, applying the box-shadow to the entire card, instead of just the text area on the right, makes for a better looking result. 此外,将box-shadow应用于整个卡片,而不只是右侧的文本区域,可以使效果更好。 Remember, the shadow is applied outside of the element's height. 请记住,阴影是在元素高度之外应用的。

 .card-horizontal { display: flex; width: 100%; height:25%; box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23); } .card-horizontal__image { width: 100%; max-width: 100%; background-image:url("https://source.unsplash.com/random/400x400"); background-size:cover; } .card-horizonal__body{ background: white; padding:15px 20px; } 
 <div class='st-grid-12'> <div class='card-horizontal'> <div class='card-horizontal__image'></div> <div class='card-horizonal__body'> <h2>This is a title</h2> <p class='card__copy'>Lorem ipsum dolor sit amet consectetur adipisicing elit. Blanditiis velit accusamus non numquam assumenda quaerat, consequatur recusandae facilis inventore dicta, distinctio magni obcaecati vel aliquid, fugit maxime. Voluptas, ullam officiis?</p> <div class='btn btn--secondary'>Read More</div> </div> </div> </div> 

When I look, the card is only slightly larger than the image. 当我看时,该卡仅比图像稍大。 This seems to be due to the extra space that images have below them by default, as they are display:inline; 这似乎是由于图像默认情况下在其下方具有多余的空间所致,因为它们分别是display:inline;

To remove this small space, and make the image column the same height as your card, make the image element display:block; 要删除此小空间,并使图像列与卡的高度相同,请使图像元素显示:

.card-horizontal__imgage img {
    display: block;
}

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

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