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

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

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

问题

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-css -->

    .aboutme {
      padding-top: 15%;
      display: flex;
      justify-content: space-around;
      align-items: flex-end;
    }

    .box {
      position: relative;
      width: 300px;
      height: 300px;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 500px;
      margin-left: 40px;
      background: #141414;
      transition: 0.5s;
      font-family: consolas;
    }

    .box:hover {
      height: 400px;
    }

    .box .imgBx {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      padding: 10px;
      box-sizing: border-box;
    }

    .bio {
      background: linear-gradient(235deg, #0c0c0c, #161616);
      border-radius: 10px;
      padding: 20px;
      color: #fff;
      font-size: 20px;
      font-family: consolas;
      max-width: 40%;
    }

<!-- 语言:lang-html -->

    <div class="aboutme">
      <div class="box">
        <div class="imgBx" id="aboutme">
          <img src="me.jpg">
        </div>
        <div class="content">
          <h2>Raul Souza<br><span>计算机科学家</span></h2>
        </div>
      </div>
      <section class="bio">
        ..通用文本和图像..
      </section>
    </div>

<!-- 结束代码片段 -->
英文:

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

.aboutme {
  padding-top: 15%;
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
}

.box {
  position: relative;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 500px;
  margin-left: 40px;
  background: #141414;
  transition: 0.5s;
  font-family: consolas;
}

.box:hover {
  height: 400px;
}

.box .imgBx {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
}

.bio {
  background: linear-gradient(235deg, #0c0c0c, #161616);
  border-radius: 10px;
  padding: 20px;
  color: #fff;
  font-size: 20px;
  font-family: consolas;
  max-width: 40%;
}

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

&lt;div class=&quot;aboutme&quot;&gt;
  &lt;div class=&quot;box&quot;&gt;
    &lt;div class=&quot;imgBx&quot; id=&quot;aboutme&quot;&gt;
      &lt;img src=&quot;me.jpg&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;content&quot;&gt;
      &lt;h2&gt;Raul Souza&lt;br&gt;&lt;span&gt;Computer Scientist&lt;/span&gt;&lt;/h2&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;section class=&quot;bio&quot;&gt;
    ..generic text and images..
  &lt;/section&gt;
&lt;/div&gt;

<!-- end snippet -->

prints:
before move
and
after move

答案1

得分: 0

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

    .aboutme {
        height: 350px;
    }

    .box:hover {
        height: 400px;
    }
            <div class="aboutme">
                <div class="box">
                    <div class="imgBx" id="aboutme">
                        <img src="me.jpg">
                    </div>
                    <div class="content">
                        <h2>Raul Souza<br><span>Computer Scientist</span></h2>
                    </div>
                </div>
                <section class="bio">
                    ..通用文本和图像..
                </section>
            </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 -->

.aboutme {
    padding-top: 15%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height:350px;
    background-color:#fff;
    padding:25px;
}

.box {
    position: relative;
    width: 300px;
    height: 300px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #141414;
    transition: 0.5s;
    font-family: consolas;
}

.box:hover {
    height: 400px;
}

.box .imgBx {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
}

.content{
    display:none;
    color:white;
}

.box:hover .content{
    display:block;
}

.bio {
    background: linear-gradient(235deg, #0c0c0c, #161616);
    border-radius: 10px;
    padding: 20px;
    color: #fff;
    font-size: 20px;
    font-family: consolas;
    max-width: 40%;
}

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Page Title&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div class=&quot;aboutme&quot;&gt;
        &lt;div class=&quot;box&quot;&gt;
            &lt;div class=&quot;imgBx&quot; id=&quot;aboutme&quot;&gt;
                &lt;img src=&quot;me.jpg&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;content&quot;&gt;
                &lt;h2&gt;Raul Souza&lt;br&gt;&lt;span&gt;Computer Scientist&lt;/span&gt;&lt;/h2&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;section class=&quot;bio&quot;&gt;
            ..generic text and images..
        &lt;/section&gt;
    &lt;/div&gt;
    &lt;/body&gt;
&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);:

.aboutme {
  padding-top: 15%;
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
}

.box {
  position: relative;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 500px;
  margin-left: 40px;
  background: #141414;
  transition: 0.5s;
  font-family: consolas;
}

.box:hover {
  transform: scaleY(1.33);
}

.box .imgBx {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
}

.bio {
  background: linear-gradient(235deg, #0c0c0c, #161616);
  border-radius: 10px;
  padding: 20px;
  color: #fff;
  font-size: 20px;
  font-family: consolas;
  max-width: 40%;
}
<div class="aboutme">
  <div class="box">
    <div class="imgBx" id="aboutme">
      <img src="me.jpg">
    </div>
    <div class="content">
      <h2>Raul Souza<br><span>Computer Scientist</span></h2>
    </div>
  </div>
  <section class="bio">
    ..generic text and images..
  </section>
</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 -->

.aboutme {
  padding-top: 15%;
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
}

.box {
  position: relative;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 500px;
  margin-left: 40px;
  background: #141414;
  transition: 0.5s;
  font-family: consolas;
}

.box:hover {
  transform: scaleY(1.33);
}

.box .imgBx {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
}

.bio {
  background: linear-gradient(235deg, #0c0c0c, #161616);
  border-radius: 10px;
  padding: 20px;
  color: #fff;
  font-size: 20px;
  font-family: consolas;
  max-width: 40%;
}

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

&lt;div class=&quot;aboutme&quot;&gt;
  &lt;div class=&quot;box&quot;&gt;
    &lt;div class=&quot;imgBx&quot; id=&quot;aboutme&quot;&gt;
      &lt;img src=&quot;me.jpg&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;content&quot;&gt;
      &lt;h2&gt;Raul Souza&lt;br&gt;&lt;span&gt;Computer Scientist&lt;/span&gt;&lt;/h2&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;section class=&quot;bio&quot;&gt;
    ..generic text and images..
  &lt;/section&gt;
&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:

确定