CSS卡片在文本增加时不会调整大小

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

Css card not resizing when text increases

问题

我使用flex使卡片居中,并尝试增加文本的大小,但发现卡片不会调整大小以适应文本,而是保持不变。

英文:

I am designing a pricing page.

If I increase the text the card will not expand, making it look like this:

<a href="https://i.stack.imgur.com/87O1h.png"><img src="https://i.stack.imgur.com/87O1h.png"></a>

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.pricing-box {
  margin: 5rem 0rem;
}

.pricing-box header {
  margin-bottom: 2rem;
  text-align: center;
}

.pricing-box .card-container {
  display: flex;
  justify-content: center;
  width: 100%;
  flex-wrap: wrap;
  align-items: center;
}

.pricing-box .card-container .card:nth-of-type(2) {
  margin-left: 0px;
  box-shadow: none;
  transform: scale(1.1);
  z-index: 2;
}

.pricing-box .card-container .card {
  width: 100%;
}

.pricing-box .card-container ul {
  margin: 2.6px;
}

.pricing-box .card-container ul li {
  list-style: none;
  margin-top: 1rem;
}

.pricing-box .card-container ul li.price {
  font-size: 3rem;
}

<!-- language: lang-html -->

&lt;div class=&quot;pricing-box&quot;&gt;
  &lt;header&gt;
    &lt;h1&gt;Our Pricing&lt;/h1&gt;
  &lt;/header&gt;


  &lt;div class=&quot;row&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;card-container&quot;&gt;
    &lt;div class=&quot;card shadow&quot;&gt;
      &lt;ul&gt;
        &lt;li class=&quot;pack&quot;&gt;Basic&lt;/li&gt;
        &lt;li id=&quot;basic&quot; class=&quot;price bottom-bar&quot;&gt;
          $dollar; 199.99
        &lt;/li&gt;
        &lt;li class=&quot;bottom bar&quot;&gt;5600 GB Storage&lt;/li&gt;
        &lt;li class=&quot;bottom bar&quot;&gt;2 Users Allowed&lt;/li&gt;
        &lt;li class=&quot;bottom bar&quot;&gt;Send up to 3 GB&lt;/li&gt;
        &lt;li&gt;
          &lt;button class=&quot;btn&quot;&gt;Learn More&lt;/button&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I centered the card using flex, and I tried to increase the size of the text and found that the card does not resize to accommodate the text while I tried to increase the size of the text.

答案1

得分: 1

看起来价格卡已经设置为100%宽度,因此增加文本不应该导致卡片扩展。但是,如果文本变得太长,可能会溢出并引起布局问题。

为了避免这种情况,您可以为价格卡设置最大宽度,并在价格元素上添加"text-overflow"和"overflow"属性来处理较长的价格。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<!DOCTYPE html>
<html>

<head>
  <title>Pricing Page</title>
  <style>
    .pricing-box {
      margin: 5rem 0rem;
    }
    
    .pricing-box header {
      margin-bottom: 2rem;
      text-align: center;
    }
    
    .pricing-box .card-container {
      display: flex;
      justify-content: center;
      width: 100%;
      flex-wrap: wrap;
      align-items: center;
    }
    
    .pricing-box .card-container .card:nth-of-type(2) {
      margin-left: 0px;
      box-shadow: none;
      transform: scale(1.1);
      z-index: 2;
    }
    
    .pricing-box .card-container .card {
      width: 100%;
      max-width: 400px;
    }
    
    .pricing-box .card-container ul {
      margin: 2.6px;
    }
    
    .pricing-box .card-container ul li {
      list-style: none;
      margin-top: 1rem;
    }
    
    .pricing-box .card-container ul li.price {
      font-size: 3rem;
      max-width: 100%;
      /* allow the price element to take up the full width of the card */
      text-overflow: ellipsis;
      /* add an ellipsis if the price overflows */
      overflow: hidden;
      /* hide the overflow of the price element */
    }
    
    .btn {
      background-color: #4CAF50;
      border: none;
      color: white;
      padding: 12px 30px;
      cursor: pointer;
      font-size: 16px;
    }
    
    .btn:hover {
      background-color: #3e8e41;
    }
  </style>
</head>

<body>
  <div class="pricing-box">
    <header>
      <h1>Our Pricing</h1>
    </header>

    <div class="row"></div>
    <div class="card-container">
      <div class="card shadow">
        <ul>
          <li class="pack">Basic</li>
          <li id="basic" class="price bottom-bar">
            $dollar; 199.99 per month
          </li>
          <li class="bottom bar">5600 GB Storage</li>
          <li class="bottom bar">2 Users Allowed</li>
          <li class="bottom bar">Send up to 3 GB</li>
          <li>
            <button class="btn">Learn More</button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</body>

</html>

<!-- end snippet -->

希望这个答案对您有帮助。

英文:

It seems that the pricing card is already set to be 100% width, so increasing the text should not cause the card to expand. However, if the text becomes too long, it might overflow and cause layout issues.

To avoid this, you can set a maximum width for the pricing card and add text-overflow and overflow properties to the price element to handle long prices.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Pricing Page&lt;/title&gt;
&lt;style&gt;
.pricing-box {
margin: 5rem 0rem;
}
.pricing-box header {
margin-bottom: 2rem;
text-align: center;
}
.pricing-box .card-container {
display: flex;
justify-content: center;
width: 100%;
flex-wrap: wrap;
align-items: center;
}
.pricing-box .card-container .card:nth-of-type(2) {
margin-left: 0px;
box-shadow: none;
transform: scale(1.1);
z-index: 2;
}
.pricing-box .card-container .card {
width: 100%;
max-width: 400px;
}
.pricing-box .card-container ul {
margin: 2.6px;
}
.pricing-box .card-container ul li {
list-style: none;
margin-top: 1rem;
}
.pricing-box .card-container ul li.price {
font-size: 3rem;
max-width: 100%;
/* allow the price element to take up the full width of the card */
text-overflow: ellipsis;
/* add an ellipsis if the price overflows */
overflow: hidden;
/* hide the overflow of the price element */
}
.btn {
background-color: #4CAF50;
border: none;
color: white;
padding: 12px 30px;
cursor: pointer;
font-size: 16px;
}
.btn:hover {
background-color: #3e8e41;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;pricing-box&quot;&gt;
&lt;header&gt;
&lt;h1&gt;Our Pricing&lt;/h1&gt;
&lt;/header&gt;
&lt;div class=&quot;row&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;card-container&quot;&gt;
&lt;div class=&quot;card shadow&quot;&gt;
&lt;ul&gt;
&lt;li class=&quot;pack&quot;&gt;Basic&lt;/li&gt;
&lt;li id=&quot;basic&quot; class=&quot;price bottom-bar&quot;&gt;
$dollar; 199.99 per month
&lt;/li&gt;
&lt;li class=&quot;bottom bar&quot;&gt;5600 GB Storage&lt;/li&gt;
&lt;li class=&quot;bottom bar&quot;&gt;2 Users Allowed&lt;/li&gt;
&lt;li class=&quot;bottom bar&quot;&gt;Send up to 3 GB&lt;/li&gt;
&lt;li&gt;
&lt;button class=&quot;btn&quot;&gt;Learn More&lt;/button&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

I hope this answer is helpful to you.

huangapple
  • 本文由 发表于 2023年3月8日 19:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672453.html
匿名

发表评论

匿名网友

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

确定