html - Need help getting a button with a drop-down menucontent in it to be centered and to push content down - Stack Overflow

时间: 2025-01-06 admin 业界

I am trying to get a clickable button with a drop-down menu to be centered horizontally on my page and also to have it push down content below it when my webpage is in mobile view. When I try display: flex;, flex-direction: column; the "table of contents" button is centered, but there is a space where the content would fill up even though the button is in a closed state with none of the content showing. When I look at the dev tools, the div that the button is in takes up the space of the content regardless if it is closed or not. The only way I figured out to get the button to take up the appropriate space when it is closed is to give it a position: absolute; but the content now appears over my other content rather than pushing the content down. Any help or suggestions?

.main-article {
  align-items: start;
  display: grid;
  gap: 1rem;
  grid-template-columns: 1fr 2fr 1fr;
}

.button-link {
  background: none;
  border: none;
  text-decoration: none;
  color: var(--clr-neutral-400);
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
  padding: 0;
  text-align: center;
}

.dropdown.active>.menu-link,
.menu-link:hover {
  color: var(--clr-primary-400);
}

.dropdown {
  position: relative;
}

.dropdown-menu {
  position: absolute;
  left: 0;
  top: calc(100% + .25rem);
  background-color: var(--clr-neutral-100);
  padding: .75rem;
  border-radius: .25rem;
  box-shadow: 0 2px 5px 0 rgb(0, 0, 0, .1);
  opacity: 0;
  pointer-events: none;
  transform: translateY(-10px);
  transition: opacity 150ms ease-in-out, transform 150ms ease-in-out;
}

.dropdown.active>.button-link+.dropdown-menu {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

@media (max-width: 48rem) {
  .main-article {
    display: grid;
    gap: 0.5rem;
    grid-template-columns: 1fr;

  }
}

@media (max-width: 48rem) {
  .dropdown {
    display: flex;
    flex-direction: column;
  }
}

My website with the issue that I am having

最新文章