【问题标题】:Fieldset like div container with headers/subheaderFieldset 类似于带有标题/子标题的 div 容器
【发布时间】:2023-03-12 16:17:01
【问题描述】:

我正在尝试在 React 中构建一个字段集,这将允许我在外部边框内添加任何元素(例如按钮等)。 我正在寻找一种方法使边框内的元素透明(其中显示“访问我们”),以便页面的真实背景很好地可见。

使用如下实现,背景被明确设置为黑色。我想避免它,并且没有背景。但是,边界线显示出来了。

有没有巧妙的办法解决这个问题?

谢谢!

function ThankYou(){
  return(
    <div className="thankYouParent">
      <div className="thankYouBox">
        <label className="borderText"> For visiting us </label>
        <label className="thankYouText">Thank You</label>
      </div>
    </div>
  )
}

ReactDOM.render(
  <ThankYou />,
  root
)
.thankYouParent {
  display: flex;
  border: 1px dotted red;
  flex-direction: column;
  align-items: center;
  height: 100vh;
  justify-content: space-between;
  justify-content: center;
}

.thankYouBox {
  display: flex;
  position: relative;
  text-align: center;
  border: 5px solid #ffffff;
  text-transform: uppercase;
  height: 40vh;
  width: 75vw;
  justify-content: center;
}

.thankYouText {
  font-family: interstateBold;
  font-size: 10vw;
  color: white;
  align-self: center;
}

.borderText {
  font-family: interstateRegular;
  position: absolute;
  color: white;
  font-size: 3vh;
  background-color: black;
  padding-left: 1vw;
  padding-right: 1vw;
  bottom: -1.5vh;
  margin-bottom: -3px;
}

body {
  background: black;
}

* {
  box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>

【问题讨论】:

    标签: javascript css reactjs


    【解决方案1】:

    我确实使用了fieldset。当然这个标签基本上用于分组表格,但在你的情况下很有用。

    function ThankYou() {
      return (
           <div className="thankYouParent">
          <div className="thankYouBox">
            <div className="wrapper">
              <span className="gap" />
              <div className="borderText">
                <p>For visiting us</p>
              </div>
              <span className="gap" />
              <div className="borderText">
                <p>For visiting us</p>
              </div>
              <span className="gap" />
              <button type="button" id="borderButton">
                <p>Click me</p>
              </button>
              <span className="gap" />
            </div>
            <h2 className="thankYouText">Thank You</h2>
          </div>
        </div>
      );
    }
    
    ReactDOM.render(
      <ThankYou />,
      root
    )
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: interstateBold;
      color: white;
      text-transform: uppercase;
    }
    body {
      background: green;
    }
    
    .thankYouParent {
      display: flex;
      border: 1px dotted red;
      flex-direction: column;
      align-items: center;
      height: 100vh;
      justify-content: space-between;
      justify-content: center;
    }
    
    .thankYouBox {
      height: 40vh;
      width: 75vw;
      display: flex;
      justify-content: center;
      position: relative;
      border: 5px solid white;
      border-bottom-color: transparent;
    }
    
    .thankYouText {
      font-size: 10vw;
      align-self: center;
    }
    
    .wrapper {
      display: grid;
      grid-template-columns: auto max-content auto max-content auto max-content auto;
      align-items: flex-end;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 1px;
    }
    
    .wrapper::before,
    .wrapper::after {
      content: '';
      width: 6px;
      height: 5px;
      position: absolute;
      bottom: -6px;
      background-color: white;
    }
    .wrapper::before {
      left: -4px;
    }
    .wrapper::after {
      right: -4px;
    }
    
    .gap,
    .borderText,
    #borderButton {
      display: flex;
      align-items: center;
    }
    .gap {
      width: 100%;
      height: 5px;
      background-color: white;
      position: relative;
      bottom: -6px;
    }
    
    .borderText,
    #borderButton {
      height: 50px;
      border: 5px solid transparent;
      background-color: transparent;
      padding-left: 1vw;
      padding-right: 1vw;
      position: relative;
      bottom: -28px;
      font-size: 1.5vh;
    }
    
    #borderButton {
      transition: all 0.3s ease-in-out;
      cursor: pointer;
    }
    
    #borderButton:hover {
      border: 5px solid white;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    
    <div id="root"></div>

    【讨论】:

    • 谢谢! Fieldset 有一个限制,据我所知,它不能显示两个或多个图例。对于我的用例,我正在寻找可以让我完全做到这一点的东西(例如,左边一个,右边一个)。
    • 我确实在没有 fieldset 的情况下更新了解决方案。
    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 2015-10-25
    • 2018-12-29
    • 1970-01-01
    • 2016-08-04
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多