html 如何让item自动继承container的padding?

gzszwxb4  于 2023-11-15  发布在  其他
关注(0)|答案(1)|浏览(134)

我试着做一个这样的按钮:


的数据
悬停时:



到目前为止,它已经在桌面上工作,代码如下:

  1. .white-button-arrow {
  2. width: 70%;
  3. height: 75px;
  4. font-size: 16px;
  5. border-radius: 10px;
  6. border: solid 3px #000000;
  7. cursor: pointer;
  8. position: relative;
  9. background: white;
  10. display: flex;
  11. justify-content: space-between;
  12. align-items: center;
  13. padding: 0;
  14. transition: all 0.3s ease;
  15. }
  16. .white-button-arrow-text {
  17. padding-left: 10px;
  18. font-family: Friends-UltraBold !important;
  19. font-size: 200%;
  20. }
  21. .white-button-arrow-icon {
  22. font-size: 200%;
  23. position: relative;
  24. border-left: 1px solid #ffffff29;
  25. border-radius: 0 10px 10px 0;
  26. padding: 20px 20px 22px 20px;
  27. background: #000000;
  28. color: #fff;
  29. transition: all 0.3s ease;
  30. transform-origin: right;
  31. }
  32. .white-button-arrow:hover .white-button-arrow-icon {
  33. color: #ff5a45;
  34. padding-left: 10%;
  35. text-indent: -60%;
  36. }
  37. .white-button-arrow:hover {
  38. background: #ff5a45;
  39. }

个字符
正如你所看到的,这不是一个很好的解决方案,因为我仍然是手动放入填充。当我试图缩小它以用于移动的版本时,它变成这样:



然后我意识到,它可能无法正常工作,因为有这么多不同的屏幕尺寸,这导致了一个问题:有没有一种方法可以让右边箭头部分的黑框自动填充容器的任何尺寸?

oewdyzsn

oewdyzsn1#

您可以设置height: 100%,而不是使用padding将.white-button-arrow-icon的文本内容居中,以便它填充其容器内的空间。
在这里,我稍微简化了场景,删除了不需要的样式,并添加了一个div容器,将>文本 Package 在其中。

  1. .white-button-arrow {
  2. display: flex;
  3. justify-content: space-between;
  4. align-items: center;
  5. cursor: pointer;
  6. padding: 0;
  7. width: 70%;
  8. height: 75px;
  9. background: white;
  10. font-size: 16px;
  11. border: solid 3px black;
  12. border-radius: 10px;
  13. transition: all 0.3s ease;
  14. }
  15. .white-button-arrow-text {
  16. padding-left: .5em;
  17. font-family: Friends-UltraBold;
  18. font-size: 2em;
  19. }
  20. .white-button-arrow-icon {
  21. display: flex;
  22. align-items: center;
  23. height: 100%;
  24. font-size: 2em;
  25. padding: 0 0.5em;
  26. background: black;
  27. color: white;
  28. transition: all 0.3s ease;
  29. transform-origin: right;
  30. }
  31. .white-button-arrow:hover .white-button-arrow-icon {
  32. color: #ff5a45;
  33. padding-left: 10%;
  34. text-indent: -60%;
  35. }
  36. .white-button-arrow:hover {
  37. background: #ff5a45;
  38. }

个字符
因为你在你的pitures中显示了两次箭头看起来像一个实际的箭头,而不是我在上面的例子中改为>>,这里我展示了如何使用一个fontawesome arrow-right

  1. .white-button-arrow {
  2. cursor: pointer;
  3. width: 70%;
  4. height: 75px;
  5. display: flex;
  6. justify-content: space-between;
  7. align-items: center;
  8. padding: 0;
  9. background: white;
  10. font-size: 16px;
  11. border: solid 3px black;
  12. border-radius: 10px;
  13. transition: all 0.3s ease;
  14. }
  15. .white-button-arrow-text {
  16. padding-left: .5em;
  17. font-family: Friends-UltraBold;
  18. font-size: 2em;
  19. }
  20. .white-button-arrow-icon {
  21. display: flex;
  22. align-items: center;
  23. height: 100%;
  24. font-size: 2em;
  25. padding: 0 .5em;
  26. background: black;
  27. color: white;
  28. transition: all 0.3s ease;
  29. }
  30. .white-button-arrow:hover .white-button-arrow-icon {
  31. color: #ff5a45;
  32. padding-left: 10%;
  33. text-indent: -60%;
  34. }
  35. .white-button-arrow:hover {
  36. background: #ff5a45;
  37. }
  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  2. <button class="white-button-arrow">
  3. <span class="white-button-arrow-text">
  4. <b>GOOGLE-MAPS</b>
  5. </span>
  6. <span class="white-button-arrow-icon">
  7. <i class="fa-solid fa-arrow-right"></i>
  8. </span>
  9. </button>
展开查看全部

相关问题