Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSS hover margin-top change does not work when the parent container contains more than one element

I am trying to create a menu bar with elements which move upwards when hovered, but it only works when the parent container contains exactly one element (like in title-bar-right, unlike in title-bar-left). Other properties (the font family in this example) change as expected. I’ve tried removing the transition duration, but it does not fix the issue. Below is what I believe to be the minimum reproducible example.

#title-bar-left {
  justify-content: left;
  align-items: left;
  float: left;
  height: inherit;
}

#title-bar-right {
  justify-content: right;
  align-items: right;
  float: right;
  height: inherit;
}

.title-bar-element-container {
  height: 100%;
  margin-top: 15px;
  margin-left: 20px;
  margin-right: 20px;
  display: inline-block;
  transition-duration: 0.3s;
}

.title-bar-element-container:hover {
  margin-top: 10px;
  height: 100%;
  font-family: monospace;
  transition-duration: 0.3s;
}
<div id="title-bar">
  <flex id="title-bar-left">
    <flex class="title-bar-element-container">
      <a class="title-bar-element bold-text" href="/main.html">main</a>
    </flex>
    <flex class="title-bar-element-container">
      <a class="title-bar-element" href="/category1.html">Category 1</a>
    </flex>
  </flex>
  <flex id="title-bar-right">
    <flex class="title-bar-element-container">
      <a class="title-bar-element" href="/category2.html">Category 2</a>
    </flex>
  </flex>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should try using the "top" property and make the position of the element relative.

In your css file change this:

.title-bar-element-container:hover {
    position: relative;
    top: 10px;
    height: 100%;
    font-family: monospace;
    transition-duration: 0.3s;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading