简体   繁体   English

对齐项目时出现问题

[英]Issue with aligning the items

I have to list 3 items in each row, which is working fine for the desktop version, but it's not much responsive on some devices. 我必须在每行中列出3个项目,这对于台式机版本来说还算不错,但是在某些设备上却反应不佳。 The items overlap on iPad so when I click on the item link it doesn't open. 这些项目在iPad上重叠,因此当我单击项目链接时,它不会打开。 And the text inside some of the items are longer, so I feel even this is the issue 而且某些项目中的文字较长,所以我觉得这是问题所在

I have tried using display :Flex and Grid but all went in vain. 我曾尝试使用display:Flex和Grid,但是都徒劳无功。

Gift item 礼品

gift = [{ image_url ,Price of the item, Name of the gift }]

 giftResults { margin: 0 -5px; } .giftResults:after { content: ""; display: table; clear: both; } .error { color: red; } .giftResult { float: left; width: 25%; padding: 0 10px; } .gift-container { width: 940px; max-width: 100%; min-width: 768px; margin: 0 auto; padding: 0 0; padding-bottom: 3em; } .card { background-color: #e5474b; display: inline-block; /* width: 25%; */ height: 50%; margin-top: 30px; width: 33.33333%; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); padding: 16px; text-align: center; } .card a { display: block; } .card h2 { padding-bottom: 25px; font-size: 20px; height: 35px; } .card:hover { height: 55%; width: 39%; } @media screen and (max-width: 600px) { .giftResult { width: 100%; display: block; margin-bottom: 20px; } .card { background-color: #e5474b; max-width: 400px; display: block; height: 50%; margin-top: 30px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); padding: 16px; text-align: center; width: fit-content; } } 
 <div className="giftResults"> <ul>{ giftDetails } <li className="giftResult"> <div className="gift-container"> <div className="card"> <img src={props.imageUrl} alt="Item" /> <p>{`$ ${props.price}`}</p> <a href={props.url} target="_blank" rel="noopener noreferrer"> {props.name} </a> </div> </div> </li> </ul> </div> 

If you cannot use bootstrap check out below ... 如果您不能使用引导程序,请在下面查看...

/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {...} 

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
     /*Define the new css rules 
} 

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {...} 

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {...} 

/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {...}

Im not exactly sure what you want to happen, there could be multiple outcomes, for example: 我不确定要发生什么,可能会有多种结果,例如:

  1. You have 3 items in a row, when the screen is smaller the items stack on top of each other 您连续有3个项目,当屏幕较小时,这些项目会彼此堆叠
  2. The items shrink in size and stay on the same row 物品尺寸缩小并排在同一行

If you want the items to stack on top of each other, this may help you (its very basic so you would have to update it to match your needs): 如果您希望这些项目相互堆叠,那么这可能会对您有所帮助(它非常基础,因此您必须对其进行更新以满足您的需求):

 .singleItemContainer { display: inline-block; width: 200px; height: 200px; background-color: #e6e6e6; margin: 4px; } 
 <div class="itemsContainer"> <div class="singleItemContainer"> <div class=""> Title </div> <div class=""> Text </div> <div class=""> Image </div> </div> <div class="singleItemContainer"> <div class=""> Title </div> <div class=""> Text </div> <div class=""> Image </div> </div> <div class="singleItemContainer"> <div class=""> Title </div> <div class=""> Text </div> <div class=""> Image </div> </div> </div> 

If you want the items to shrink in size (width specifically), then you would change the width to be 33.33% (with no margin in between, if you want to have space between them then you would just have to adjust the width of the elements based on how much space/padding you give the items). 如果您要缩小尺寸(具体来说是宽度),则可以将宽度更改为33.33%(之间没有空白,如果要在它们之间留出空间,则只需调整宽度)即可。元素(取决于您为物品分配多少空间/填充)。

For mobile sizes you have the option of using media queries, you could combines both solutions here and add media queries, the logic would be that, for example, from the screen size 320px until 600px the elements will be stacked on top of each other, after 600px the elements will have the 33.33% width. 对于移动尺寸,您可以选择使用媒体查询,您可以在此处结合两种解决方案并添加媒体查询,其逻辑是,例如,从屏幕尺寸320像素到600像素,元素将相互堆叠, 600px之后,元素的宽度为33.33%。

To do this you would need to use a media query: 为此,您需要使用媒体查询:

 .singleItemContainer{
    display: inline-block;
    width: 200px;
    height: 200px;
    background-color: #e6e6e6;
    margin: 4px;
}
 @media only screen and (min-width: 320px) { //This one is probably not needed since the width: 200px; is the default one, but just threw this in here to help understand the example
    .singleItemContainer{
       width: 200px;
    }
 } 
 @media only screen and (min-width: 600px) {
    .singleItemContainer{
       width: 31.33%; //31 to allow the margins in between
    }
 }

Media queries can be added and adjusted to fit your needs, everything here can really. 可以添加和调整媒体查询以适合您的需求,这里的一切都可以做到。 Hope this helps! 希望这可以帮助!

I even deleted the extra div 我什至删除了多余的div

  <div className="gift-container"> 

which was causing the issue and even added media queries as per the above suggestions 导致问题的原因,甚至根据上述建议添加了媒体查询

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

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