简体   繁体   English

CSS 子组件的边框在父组件的边框之上

[英]CSS child component's border on top of parent component's border

How do I make a child component's border go on top (higher z-index) of its parent component's border?如何使子组件的边框 go 在其父组件边框的顶部(更高的 z-index)? Z-index didn't work for me. Z-index 对我不起作用。

Wanted behavior: clicking on the tab enables green border-bottom to show that the tab is clicked.想要的行为:单击选项卡会启用绿色边框底部以显示已单击选项卡。 This green border overlaps on top of the default gray border that is set throughout all the tabs.此绿色边框与在所有选项卡中设置的默认灰色边框顶部重叠。

https://codesandbox.io/s/navbar-component-d5f1c?file=/Navbar.js https://codesandbox.io/s/navbar-component-d5f1c?file=/Navbar.js

import React, { useState } from 'react';
import styled from "styled-components";

const Navbar = ({ value, children, tabFilter, contentFilter }) => {
  const [activeTab, setActiveTab] = useState(value[0].title);

  const onClickTabItem = tab => {
    setActiveTab(tab);
  }

  return (
    <React.Fragment>
      <NavbarOutline id="main">
        <ol>
          {value.map(child => {
            const { title } = child;
            return <Tab activeTab={activeTab} key={title} title={title} handleClick={onClickTabItem} />;
          })}
        </ol>
      </NavbarOutline>
      <div>
        {value.map(child => {
          if (child.title !== activeTab) return undefined;
          return <StyledTabs className="content">{child.title}</StyledTabs>
        })}
      </div>
    </React.Fragment>
  );
}

const Tab = props => {
  const { activeTab, title, handleClick } = props;
  let className = 'not-active';

  const onClick = () => {
    handleClick(title);
  };

  if (activeTab === title) {
    className = 'active';
  }

  return (
    <StyledTabs className={className} onClick={onClick}>
      {title}
    </StyledTabs>
  );
};

// eslint-disable-next-line import/prefer-default-export
export const NavbarOutline = styled.div`
  margin-left: 35px;
  margin-right: 35px;
  overflow-x: auto;
  white-space: nowrap;
  border-bottom: 2px solid #e3e3e3;
  top: 0px;
  -ms-overflow-style: none; /* Internet Explorer 10+ */
  scrollbar-width: none; /* Firefox */
  &::-webkit-scrollbar {
    display: none; /* Safari and Chrome */
  }
`;

const StyledTabs = styled.button.attrs(props => ({
  className: props.className
}))`
  &.not-active {
    font-style: normal;
    font-weight: normal;
    font-size: 16px;
    line-height: 20px;
    padding: 16px 31px 16px 31px;
    background: none;
    border: none;
    position: relative;
    bottom: -29px;
  }
  &.active {
    font-style: normal;
    font-weight: normal;
    font-size: 16px;
    line-height: 20px;
    position: relative;
    bottom: -29px;
    z-index: 3;
    background: none;
    color: #2b8000;
    border: none;
    border-bottom: 3px solid #2b8000;
  }
  &.content {
    background: none;
    border: none;
  }
`;

The parent div with id="main" has overflow-x: auto , which is causing your child tab element to disappear when it goes outside of it. id="main"的父 div 具有overflow-x: auto ,这会导致您的子选项卡元素在超出它时消失。 It's probably what you want, but that's why you can't see the green border.这可能是您想要的,但这就是您看不到绿色边框的原因。

Open the devtools and untick overflow-x: auto to see for yourself.打开 devtools 并取消勾选overflow-x: auto自己看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM