【问题标题】:Issue with aligning the items对齐项目的问题
【发布时间】:2019-02-05 10:09:57
【问题描述】:

我必须在每一行中列出 3 个项目,这对于桌面版本来说效果很好,但在某些设备上响应速度并不快。这些项目在 iPad 上重叠,所以当我单击项目链接时它不会打开。而且有些项目里面的文字比较长,所以我觉得这就是问题

我尝试过使用 display :Flex 和 Grid 但都失败了。

礼品

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>

【问题讨论】:

  • 我建议您考虑使用引导程序来解决您的问题。 Bootstrap 是一种行业标准,它易于使用来对齐项目以获得响应式外观
  • 这是我的顶点项目,所以我不应该使用 Bootstrap 或任何库
  • 乍一看您的代码,您只有 1 个 css 媒体查询,您应该使用多个来捕获多种设备尺寸
  • 对于 Ipad 我不确定宽度

标签: javascript html css flexbox css-grid


【解决方案1】:

如果您不能使用引导程序,请查看下面...

/* 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) {...}

【讨论】:

    【解决方案2】:

    我不确定你想要发生什么,可能有多种结果,例如:

    1. 您有 3 个连续的项目,当屏幕较小时,这些项目会堆叠在一起
    2. 项目缩小并保持在同一行

    如果您希望项目彼此堆叠,这可能会对您有所帮助(它非常基本,因此您必须对其进行更新以满足您的需求):

    .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>

    如果您希望项目的尺寸缩小(特别是宽度),那么您可以将宽度更改为 33.33%(中间没有边距,如果您想在它们之间留出空间,那么您只需调整元素的宽度取决于您为项目提供多少空间/填充)。

    对于移动设备尺寸,您可以选择使用媒体查询,您可以在此处结合两种解决方案并添加媒体查询,例如,从屏幕尺寸 320 像素到 600 像素,元素将堆叠在其顶部彼此,在 600px 之后元素将具有 33.33% 的宽度。

    为此,您需要使用媒体查询:

     .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
        }
     }
    

    可以添加和调整媒体查询以满足您的需求,这里的一切都可以。 希望这会有所帮助!

    【讨论】:

    • 这很有帮助
    【解决方案3】:

    我什至删除了多余的 div

      <div className="gift-container">
    

    这是导致问题的原因,甚至根据上述建议添加了媒体查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      相关资源
      最近更新 更多