如何在CSS中实现鼠标悬停时防止元素变大时影响其他元素的位置?

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

How can I prevent other elements from moving when an element grows in size on hover in CSS?

问题

  1. <!-- 开始代码片段:js 隐藏:false 控制台:true Babelfalse -->
  2. <!-- 语言:lang-css -->
  3. .aboutme {
  4. padding-top: 15%;
  5. display: flex;
  6. justify-content: space-around;
  7. align-items: flex-end;
  8. }
  9. .box {
  10. position: relative;
  11. width: 300px;
  12. height: 300px;
  13. display: flex;
  14. justify-content: center;
  15. align-items: center;
  16. margin-top: 500px;
  17. margin-left: 40px;
  18. background: #141414;
  19. transition: 0.5s;
  20. font-family: consolas;
  21. }
  22. .box:hover {
  23. height: 400px;
  24. }
  25. .box .imgBx {
  26. position: absolute;
  27. top: 0;
  28. left: 0;
  29. width: 100%;
  30. height: 100%;
  31. padding: 10px;
  32. box-sizing: border-box;
  33. }
  34. .bio {
  35. background: linear-gradient(235deg, #0c0c0c, #161616);
  36. border-radius: 10px;
  37. padding: 20px;
  38. color: #fff;
  39. font-size: 20px;
  40. font-family: consolas;
  41. max-width: 40%;
  42. }
  43. <!-- 语言:lang-html -->
  44. <div class="aboutme">
  45. <div class="box">
  46. <div class="imgBx" id="aboutme">
  47. <img src="me.jpg">
  48. </div>
  49. <div class="content">
  50. <h2>Raul Souza<br><span>计算机科学家</span></h2>
  51. </div>
  52. </div>
  53. <section class="bio">
  54. ..通用文本和图像..
  55. </section>
  56. </div>
  57. <!-- 结束代码片段 -->
英文:

I have this box with a image and when mouse hover, the box grows to show a text. I'm having troubles with all other page elements moving together. position: absolute or fixed don't work. Is it possible to not move the page using only CSS? Sorry for english troubles.

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

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

  1. .aboutme {
  2. padding-top: 15%;
  3. display: flex;
  4. justify-content: space-around;
  5. align-items: flex-end;
  6. }
  7. .box {
  8. position: relative;
  9. width: 300px;
  10. height: 300px;
  11. display: flex;
  12. justify-content: center;
  13. align-items: center;
  14. margin-top: 500px;
  15. margin-left: 40px;
  16. background: #141414;
  17. transition: 0.5s;
  18. font-family: consolas;
  19. }
  20. .box:hover {
  21. height: 400px;
  22. }
  23. .box .imgBx {
  24. position: absolute;
  25. top: 0;
  26. left: 0;
  27. width: 100%;
  28. height: 100%;
  29. padding: 10px;
  30. box-sizing: border-box;
  31. }
  32. .bio {
  33. background: linear-gradient(235deg, #0c0c0c, #161616);
  34. border-radius: 10px;
  35. padding: 20px;
  36. color: #fff;
  37. font-size: 20px;
  38. font-family: consolas;
  39. max-width: 40%;
  40. }

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

  1. &lt;div class=&quot;aboutme&quot;&gt;
  2. &lt;div class=&quot;box&quot;&gt;
  3. &lt;div class=&quot;imgBx&quot; id=&quot;aboutme&quot;&gt;
  4. &lt;img src=&quot;me.jpg&quot;&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;content&quot;&gt;
  7. &lt;h2&gt;Raul Souza&lt;br&gt;&lt;span&gt;Computer Scientist&lt;/span&gt;&lt;/h2&gt;
  8. &lt;/div&gt;
  9. &lt;/div&gt;
  10. &lt;section class=&quot;bio&quot;&gt;
  11. ..generic text and images..
  12. &lt;/section&gt;
  13. &lt;/div&gt;

<!-- end snippet -->

prints:
before move
and
after move

答案1

得分: 0

给.aboutme div固定高度并添加了悬停时显示文本的代码。

  1. .aboutme {
  2. height: 350px;
  3. }
  4. .box:hover {
  5. height: 400px;
  6. }
  1. <div class="aboutme">
  2. <div class="box">
  3. <div class="imgBx" id="aboutme">
  4. <img src="me.jpg">
  5. </div>
  6. <div class="content">
  7. <h2>Raul Souza<br><span>Computer Scientist</span></h2>
  8. </div>
  9. </div>
  10. <section class="bio">
  11. ..通用文本和图像..
  12. </section>
  13. </div>
英文:

Give a fixed height to the .aboutme div and I added code to show the text on hover.

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

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

  1. .aboutme {
  2. padding-top: 15%;
  3. display: flex;
  4. justify-content: space-between;
  5. align-items: center;
  6. height:350px;
  7. background-color:#fff;
  8. padding:25px;
  9. }
  10. .box {
  11. position: relative;
  12. width: 300px;
  13. height: 300px;
  14. display: flex;
  15. justify-content: center;
  16. align-items: center;
  17. background: #141414;
  18. transition: 0.5s;
  19. font-family: consolas;
  20. }
  21. .box:hover {
  22. height: 400px;
  23. }
  24. .box .imgBx {
  25. position: absolute;
  26. top: 0;
  27. left: 0;
  28. width: 100%;
  29. height: 100%;
  30. padding: 10px;
  31. box-sizing: border-box;
  32. }
  33. .content{
  34. display:none;
  35. color:white;
  36. }
  37. .box:hover .content{
  38. display:block;
  39. }
  40. .bio {
  41. background: linear-gradient(235deg, #0c0c0c, #161616);
  42. border-radius: 10px;
  43. padding: 20px;
  44. color: #fff;
  45. font-size: 20px;
  46. font-family: consolas;
  47. max-width: 40%;
  48. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Page Title&lt;/title&gt;
  5. &lt;/head&gt;
  6. &lt;body&gt;
  7. &lt;div class=&quot;aboutme&quot;&gt;
  8. &lt;div class=&quot;box&quot;&gt;
  9. &lt;div class=&quot;imgBx&quot; id=&quot;aboutme&quot;&gt;
  10. &lt;img src=&quot;me.jpg&quot;&gt;
  11. &lt;/div&gt;
  12. &lt;div class=&quot;content&quot;&gt;
  13. &lt;h2&gt;Raul Souza&lt;br&gt;&lt;span&gt;Computer Scientist&lt;/span&gt;&lt;/h2&gt;
  14. &lt;/div&gt;
  15. &lt;/div&gt;
  16. &lt;section class=&quot;bio&quot;&gt;
  17. ..generic text and images..
  18. &lt;/section&gt;
  19. &lt;/div&gt;
  20. &lt;/body&gt;
  21. &lt;/html&gt;

<!-- end snippet -->

答案2

得分: 0

You can use transform: scaleY(...); instead of changing the height value on hover, in your case (300 to 400px height) that would be transform: scaleY(1.33);:

  1. .aboutme {
  2. padding-top: 15%;
  3. display: flex;
  4. justify-content: space-around;
  5. align-items: flex-end;
  6. }
  7. .box {
  8. position: relative;
  9. width: 300px;
  10. height: 300px;
  11. display: flex;
  12. justify-content: center;
  13. align-items: center;
  14. margin-top: 500px;
  15. margin-left: 40px;
  16. background: #141414;
  17. transition: 0.5s;
  18. font-family: consolas;
  19. }
  20. .box:hover {
  21. transform: scaleY(1.33);
  22. }
  23. .box .imgBx {
  24. position: absolute;
  25. top: 0;
  26. left: 0;
  27. width: 100%;
  28. height: 100%;
  29. padding: 10px;
  30. box-sizing: border-box;
  31. }
  32. .bio {
  33. background: linear-gradient(235deg, #0c0c0c, #161616);
  34. border-radius: 10px;
  35. padding: 20px;
  36. color: #fff;
  37. font-size: 20px;
  38. font-family: consolas;
  39. max-width: 40%;
  40. }
  1. <div class="aboutme">
  2. <div class="box">
  3. <div class="imgBx" id="aboutme">
  4. <img src="me.jpg">
  5. </div>
  6. <div class="content">
  7. <h2>Raul Souza<br><span>Computer Scientist</span></h2>
  8. </div>
  9. </div>
  10. <section class="bio">
  11. ..generic text and images..
  12. </section>
  13. </div>
英文:

You can use transform: scaleY(...); instead of changing the height value on hover, in your case (300 to 400px height) that would be transform: scaleY(1.33);:

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

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

  1. .aboutme {
  2. padding-top: 15%;
  3. display: flex;
  4. justify-content: space-around;
  5. align-items: flex-end;
  6. }
  7. .box {
  8. position: relative;
  9. width: 300px;
  10. height: 300px;
  11. display: flex;
  12. justify-content: center;
  13. align-items: center;
  14. margin-top: 500px;
  15. margin-left: 40px;
  16. background: #141414;
  17. transition: 0.5s;
  18. font-family: consolas;
  19. }
  20. .box:hover {
  21. transform: scaleY(1.33);
  22. }
  23. .box .imgBx {
  24. position: absolute;
  25. top: 0;
  26. left: 0;
  27. width: 100%;
  28. height: 100%;
  29. padding: 10px;
  30. box-sizing: border-box;
  31. }
  32. .bio {
  33. background: linear-gradient(235deg, #0c0c0c, #161616);
  34. border-radius: 10px;
  35. padding: 20px;
  36. color: #fff;
  37. font-size: 20px;
  38. font-family: consolas;
  39. max-width: 40%;
  40. }

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

  1. &lt;div class=&quot;aboutme&quot;&gt;
  2. &lt;div class=&quot;box&quot;&gt;
  3. &lt;div class=&quot;imgBx&quot; id=&quot;aboutme&quot;&gt;
  4. &lt;img src=&quot;me.jpg&quot;&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;content&quot;&gt;
  7. &lt;h2&gt;Raul Souza&lt;br&gt;&lt;span&gt;Computer Scientist&lt;/span&gt;&lt;/h2&gt;
  8. &lt;/div&gt;
  9. &lt;/div&gt;
  10. &lt;section class=&quot;bio&quot;&gt;
  11. ..generic text and images..
  12. &lt;/section&gt;
  13. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月25日 23:23:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333930.html
匿名

发表评论

匿名网友

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

确定