簡體   English   中英

即使使用react和css更改div中的內容,如何始終使div中的分隔符與div的頂部箭頭保持同步?

[英]How to keep the divider in a div in sync with the top arrow of the div always even if the content changes in the div using react and css?

我希望 div 內的分隔線正好位於 div 的頂部箭頭下方。 現在我有如下內容:

示例圖像

下面是我的代碼,

    function Parent() {
        return (
            <div className="wrapper">
                <div className="flex_wrapper">
                    <button> click </button>
                    <div className="box">
                        <BoxContent />
                    </div>
                </div>
            </div>
        );
    }

    function BoxContent() {
        return (
            <div className="box_content_wrapper">
                {true && (
                    <>
                        <Available />
                        <div className="spacing" />
                        <span>
                            Available <br />
                            <span className="sub_text">some text</span>
                        </span>
                        <div className="divider" />
                    </>
                )}
                <Available />
                <div className="spacing" />
                <span>
                    Available <br />
                    <span className="sub_text">some text</span>
                </span>
            </div>
        );
    }

    function Available() {
        return (
            <div className="view">
                <span>10</span> //when i change these values to any big number also i want the divider 
                 //and the arrow to be 
                //in just below each other
                <span>/100</span> //when i change these values to any big number also i want the divider 
                //and the arrow to be in just below each other
            </div>
        ); 
    }

下面是css

    .wrapper {
        height: 40px;
        display: flex;
        z-index: 4;
        background-color: #fff;
        border-bottom: 1px solid #cccccc;
        position: relative;
    }

    .flex_wrapper {
        flex: 1;
        display: flex;
        justify-content: flex-start;
        align-items: center;
    }

    .box {
        padding: 16px;
        height: 61px;
        border-radius: 8px;
        border: 1px solid black;
        background-color: white;
        position: absolute;
        display: flex;
        justify-content: center;
        align-items: center;
        top: 72px;
        left: 35px;
    }

    .box::after {
        content: "";
        width: 0px;
        height: 0px;
        position: absolute;
        background-color: black;
        top: -4px;
        left: 141px;
        border-left: 8px solid;
        border-left-color: #fff;
        border-bottom: 8px solid transparent;
        border-top: 8px solid transparent;
        transform: translateY(-50%) rotate(-90deg);
    }

    .box_content_wrapper {
        display: flex;
        flex-direction: row;
        align-items: center;
    }

    .view {
        display: flex;
        flex-direction: row;
        align-items: baseline;
        margin-top: 8px;
    }

    .sub_text {
        white-space: nowrap;
    }

    .spacing {
        width: 8px;
    }

    .divider {
        height: 37px;
        margin-left: 16px;
        margin-right: 16px;
        border: 1px solid grey;
     }

您可以在此處找到工作示例: https : //codesandbox.io/s/blissful-bush-mj7lz?file=/src/App.js

我不確定如何保持分隔符和箭頭同步,即使可用組件中的數據含義,即使我將跨度內容更改為一些較大的數字,分隔符和箭頭也應位於彼此的下方。

我該怎么做?

在這里編輯了您的沙箱。

唯一應用的更改如下:

  1. 添加CSS規則position: relative; 在元素.divider
  2. 更改了偽選擇器的父級:after (與頂部箭頭相關的那個)。 現在箭頭將元素.divider作為父元素。 這樣,無論內容里面的值有多長,箭頭都會一直停留在分隔線的上方(請查看下圖)。
  3. 最后,我調整了箭頭元素的頂部和左側位置( .divider:after ),因此它將始終停留在容器的頂部,並且關於分隔線居中對齊( left值來自計算0 - width of the arrow / 2 )。 新值如下: top: -32px; left: -4px; top: -32px; left: -4px;

代碼沙盒截圖

您可以在下面找到所有修改后的 CSS:

 .App { font-family: sans-serif; text-align: center; } .wrapper { height: 40px; display: flex; z-index: 4; background-color: #fff; border-bottom: 1px solid #cccccc; position: relative; } .flex_wrapper { flex: 1; display: flex; justify-content: flex-start; align-items: center; } .box { padding: 16px; height: 61px; border-radius: 8px; border: 1px solid black; background-color: white; position: absolute; display: flex; justify-content: center; align-items: center; top: 72px; left: 35px; } .box_content_wrapper { display: flex; flex-direction: row; align-items: center; } .view { display: flex; flex-direction: row; align-items: baseline; margin-top: 8px; } .sub_text { white-space: nowrap; } .spacing { width: 8px; } .divider { height: 37px; margin-left: 16px; margin-right: 16px; border: 1px solid grey; position: relative; } .divider::after { content: ""; width: 0px; height: 0px; position: absolute; background-color: black; top: -32px; left: -4px; border-left: 8px solid; border-left-color: #fff; border-bottom: 8px solid transparent; border-top: 8px solid transparent; transform: translateY(-50%) rotate(-90deg); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM