通过使用 d-none bootstrap 使元素消失来防止换行

prevent line wrap by making elements disappear with d-none bootstrap

我正在构建一个包含顶部栏菜单的响应式网站。这个菜单有很多选项,非常适合大屏幕显示。不幸的是,对于手机等较小的视口,所有这些选项都不适合,按钮最终会环绕。

普通视口:

手机:

这是代码片段:

<header class="header" id="header">

        <div class="header_toggle" id="toggle-up"> <i class="fas fa-bars secondary-bs-color" id="header-toggle"></i></div>

        <div id="nav-buttons" class="me-5 d-sm-flex">

            <div class="container me-2">
                <div class="row">
                    <span class="text-white text-end">Level 1</span>
                </div>
                <div class="progress" style="width: 100px; height: 5px;">
                    <div class="progress-bar bg-secondary" style="width: 70%;" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
            </div>
            
            

            <button class="btn btn-primary-outline me-2"><i class="far fa-envelope fa-lg text-field-grey"></i></button>

            <button class="btn btn-primary-outline me-2"><i class="fas fa-user fa-lg text-field-grey"></i></button>

            <div class="dropdown me-2">
                <button class="border border-field-grey btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
                  <span class="text-field-grey">TC</span>
                  <span>0.00</span>
                </button>
                <ul class="dropdown-menu bg-field-grey-transparency" aria-labelledby="dropdownMenuCoins">
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Action</a></li>
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Another action</a></li>
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Something else here</a></li>
                </ul>
            </div>

            <a type="button" class="white-bs-color fw-bold btn btn-secondary">Deposit</a>
        </div>
</header>

为了避免这个问题,我决定使用 bootstrp5 组合 d-none d-flex-sm 类 (https://getbootstrap.com/docs/5.0/utilities/display/)

<div id="nav-buttons" class="me-5 d-sm-flex">

            <div class="container me-2 d-none d-sm-block">
                <div class="row">
                    <span class="text-white text-end">Level 1</span>
                </div>
                <div class="progress" style="width: 100px; height: 5px;">
                    <div class="progress-bar bg-secondary" style="width: 70%;" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
            </div>
            
            

            <button class="btn btn-primary-outline me-2"><i class="far fa-envelope fa-lg text-field-grey"></i></button>

            <button class="btn btn-primary-outline me-2"><i class="fas fa-user fa-lg text-field-grey"></i></button>

            <div class="dropdown me-2">
                <button class="border border-field-grey btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
                  <span class="text-field-grey">TC</span>
                  <span>0.00</span>
                </button>
                <ul class="dropdown-menu bg-field-grey-transparency" aria-labelledby="dropdownMenuCoins">
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Action</a></li>
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Another action</a></li>
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Something else here</a></li>
                </ul>
            </div>

            <a type="button" class="white-bs-color fw-bold btn btn-secondary d-none d-sm-flex">Deposit</a>
        </div>
</header>

但它们最终还是会换行,尽管小屏幕中的元素有很多 space:

有没有人知道我们如何解决这个问题?

完整代码:

HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <link href="./css/custom.css" rel="stylesheet">
    <link href="./css/navlayout.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css" rel="stylesheet">

    <script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <script defer src="./js/navlayout.js"></script>
    <script defer src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js" crossorigin="anonymous"></script>

    <link rel="icon" href="./img/icon.png" />

    <title>Vegaz.bet</title>
</head>

<body id="body-pd" style="background: url('./img/Home e Roleta.png') no-repeat center center fixed; background-size: cover;">
    <header class="top-header align-middle">
        <div class="mx-auto"></div>
    </header>
    <header class="header" id="header">

        <div class="header_toggle" id="toggle-up"> <i class="fas fa-bars secondary-bs-color" id="header-toggle"></i></div>

        <div id="nav-buttons" class="me-5 d-sm-flex">

            <div class="container me-2 d-none d-sm-block">
                <div class="row">
                    <span class="text-white text-end">Level 1</span>
                </div>
                <div class="progress" style="width: 100px; height: 5px;">
                    <div class="progress-bar bg-secondary" style="width: 70%;" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
            </div>
            
            

            <button class="btn btn-primary-outline me-2"><i class="far fa-envelope fa-lg text-field-grey"></i></button>

            <button class="btn btn-primary-outline me-2"><i class="fas fa-user fa-lg text-field-grey"></i></button>

            <div class="dropdown me-2">
                <button class="border border-field-grey btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
                  <span class="text-field-grey">TC</span>
                  <span>0.00</span>
                </button>
                <ul class="dropdown-menu bg-field-grey-transparency" aria-labelledby="dropdownMenuCoins">
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Action</a></li>
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Another action</a></li>
                  <li><a class="coin-dropdown-item dropdown-item" href="#">Something else here</a></li>
                </ul>
            </div>

            <a type="button" class="white-bs-color fw-bold btn btn-secondary d-none d-sm-flex">Deposit</a>
        </div>
    </header>
    <div class="l-navbar" id="nav-bar">
        <nav class="nav">
            <div>
                <div class="nav_list">
                    <a href="#" class="nav_link active"> <i class="fas fa-chart-line nav_icon secondary-bs-color"></i> <span class="nav_name">Crash</span> </a>
                    <a href="#" class="nav_link"> <i class="far fa-life-ring nav_icon secondary-bs-color"></i> <span class="nav_name">Roullete</span> </a>
                    <a href="#" class="nav_link"> <i class="fas fa-dice nav_icon secondary-bs-color"></i> <span class="nav_name">Dice</span> </a>
                    <a href="#" class="nav_link"> <i class="fas fa-coins nav_icon secondary-bs-color"></i> <span class="nav_name">Heads or Tails</span> </a>
                    <hr class="bg-white border-2 border-top border-white">
                    <a href="#" class="nav_link"> <i class="fas fa-hand-holding-usd nav_icon white-bs-color"></i> <span class="nav_name">Deposit /<br> Withdrawal</span> </a>
                    <a href="#" class="nav_link"> <i class="far fa-question-circle nav_icon white-bs-color"></i> <span class="nav_name">Help</span> </a>
                </div>
            </div>
            
        </nav>
    </div>
    <div class="r-navbar invisible" id="nav-bar-chat">

        <div id="invisible-toggle-div" class="container pb-5 invisible">

            <div class="row h-20 mb-1">
                <button id="close-chat" type="button" class="position-absolute top-8 start-10 btn-close btn-close-white" aria-label="Close"></button>
                <button type="button" class="w-50 position-absolute top-7 end-10 white-bs-color fw-bold btn btn-primary"><span class="text-nowrap">Vegaz Chat</span></button>
            </div>

            <div class="row h-60">
                <div id="chatContainer" class="chat-font d-flex flex-column bd-highlight position-absolute top-15 end-5 w-90 h-75">
                    
                    <span class="time-span-chat ms-2">11:29</span>
                    <div class="card bg-field-grey text-white mb-1">
                        <span class="pt-1 pb-1 ps-1 pe-1"><span class="fw-bold" style="color: yellow">youranus420:</span> oi putões</span>
                    </div>
                    
                    <span class="time-span-chat ms-2">11:29</span>
                    <div class="card bg-field-grey text-white mb-1">
                        <span class="pt-1 pb-1 ps-1 pe-1"><span class="fw-bold" style="color: yellow">youranus420:</span> bora crash?</span>
                    </div>
                    
                    <span class="time-span-chat ms-2">11:30</span>
                    <div class="card bg-field-grey text-white mb-1">
                        <span class="pt-1 pb-1 ps-1 pe-1"><span class="fw-bold" style="color: purple">elon musk:</span> Olha o nome do cara kkkkk</span>
                    </div>
                        
                </div>
            </div>

            <div class="row h-10">
                <div class="bg-primary text-center position-absolute end-3 bottom-6 w-94 h-5">
                    <div class="input-group mb-3">
                        <input type="text" class="chat-font form-control" placeholder="Your message" aria-label="Message">
                        <button class="chat-font btn btn-outline-secondary" type="button" id="button-addon2">Send</button>
                    </div>
                </div>
            </div>

        </div>

    </div>
    <!--Container Main start-->
    <div class="height-100">
        <h4>Main Components</h4>

        <a class="position-absolute bottom-5 end-5 float-icon-chat" id="chatButton" role="button">
            <span class="position-absolute top-15 start-20"><i class="far fa-comment-dots" style="font-size: 40px;"></i></span>
        </a>
    </div>
    <!--Container Main end-->

</body>

</html>

CSS:

@import url("https://fonts.googleapis.com/css2?family=Montserrat&display=swap");
:root {
    --top-header-top-position: 5%;
    --header-height: 3rem;
    --nav-width: 68px;
    --chat-width: 100px;
    --primary-color: var(--bs-primary);
    --secondary-color: var(--bs-secondary);
    --field-gray: var(--bs-field-grey);
    --field-grey-transparency: var(--bs-field-grey-transparency);
    --white-color: var(--bs-white);
    --body-font: 'Montserrat', sans-serif;
    --normal-font-size: 1rem;
    --z-fixed: 100;
    --z-topbar: 101;
}

*,
::before,
::after {
    box-sizing: border-box
}

.top-header {
    width: 100%;
    height: var(--top-header-top-position);
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    background-color: var(--secondary-color);
    z-index: var(--z-topbar);
    transition: .5s;
    text-align: center !important;
}

.header {
    width: 100%;
    height: var(--header-height);
    position: fixed;
    top: var(--top-header-top-position);
    left: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    background-color: var(--primary-color);
    z-index: var(--z-fixed);
    transition: .5s
}

.header_toggle {
    color: var(--primary-color);
    font-size: 1.5rem;
    cursor: pointer
}

.header_img {
    width: 35px;
    height: 35px;
    display: flex;
    justify-content: center;
    border-radius: 50%;
    overflow: hidden
}

.header_img img {
    width: 40px
}

.l-navbar {
    position: fixed;
    top: 0;
    left: -30%;
    width: var(--nav-width);
    height: 100vh;
    background-color: var(--primary-color);
    padding: .5rem 1rem 0 0;
    transition: .5s;
    z-index: var(--z-fixed)
}

.r-navbar {
    position: fixed;
    top: 0;
    right: 0;
    width: 0;
    height: 100vh;
    background-color: var(--primary-color);
    padding: 1rem 0 0 1rem;
    transition: .5s;
    z-index: var(--z-fixed);
    padding: 1rem 0 0 1rem;
}

.nav {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow: hidden;
    margin-top: 5rem;
}

.nav_logo,
.nav_link {
    display: grid;
    grid-template-columns: max-content max-content;
    align-items: center;
    column-gap: 1rem;
    padding: .5rem 0 .5rem 1.5rem;
    text-decoration: none;
}

.nav_logo {
    margin-bottom: 2rem
}

.nav_logo-icon {
    font-size: 1.25rem;
    color: var(--white-color)
}

.nav_logo-name {
    color: var(--white-color);
    font-weight: 700
}

.nav_link {
    position: relative;
    color: var(--white-color);
    margin-bottom: 1.5rem;
    transition: .3s
}

.nav_link:hover {
    color: var(--white-color)
}

.nav_icon {
    font-size: 1.25rem;
}

.secondary-bs-color {
    color: var(--bs-secondary)
}

.white-bs-color {
    color: var(--white-color)
}

.show-left-nav {
    left: 0;
    width: calc(var(--nav-width) + 156px);
}

.show-right-nav {
    width: 100%;
}

.body-menu-pd {
    padding-left: calc(var(--nav-width) + 188px); 
}

.body-chat-pd {
    padding-right: calc(var(--chat-width) + 1rem);
}

.chat-font {
    font-size: small;
}

.time-span-chat {
    font-size: smaller;
    color: var(--white-color);
}

#chatContainer {
    -ms-overflow-style: none; /* for Internet Explorer, Edge */
    scrollbar-width: none; /* for Firefox */
    overflow-y: scroll; 
}

#chatContainer::-webkit-scrollbar {
    display: none; /* for Chrome, Safari, and Opera */
}

.active {
    color: var(--white-color)
}

.active::before {
    content: '';
    position: absolute;
    left: 0;
    width: 2px;
    height: 32px;
    background-color: var(--white-color)
}

.height-100 {
    height: 100vh
}

.float-icon-chat {
    width: 60px;
    height: 60px;
    bottom: 40px;
    right: 40px;
    background-color: var(--secondary-color);
    color: var(--white-color);
    border-radius: 50px;
    text-align: center;
    vertical-align: middle;
}

.coin-dropdown-item {
    color: var(--white-color);
}

.coin-dropdown-item:hover {
    color: var(--secondary-color);
    background-color: var(--field-grey-transparency);
    font-weight: bold;
}

@media screen and (min-width: 768px) {
    .header {
        height: calc(var(--header-height) + 1rem);
        padding: 0 2rem 0 calc(var(--nav-width) + 2rem)
    }
    .header_img {
        width: 40px;
        height: 40px
    }
    .header_img img {
        width: 45px
    }
    .l-navbar {
        left: 0;
        padding: 1rem 1rem 0 0
    }
    .r-navbar {
        right: 0;
        padding: 1rem 0 0 1rem;
        width: 0;
    }
    .show-left-nav {
        width: calc(var(--nav-width) + 156px)
    }
    .show-right-nav {
        width: calc(var(--chat-width) + 156px)
    }
    .body-menu-pd {
        padding-left: calc(var(--nav-width) + 188px);   
    }
    .body-chat-pd {
        padding-right: calc(var(--chat-width) + 156px)
    }
}

JS:

document.addEventListener("DOMContentLoaded", function(event) {

    const showNavbar = (toggleId, navId, bodyId, headerId) => {
        const toggle = document.getElementById(toggleId),
            nav = document.getElementById(navId),
            bodypd = document.getElementById(bodyId),
            headerpd = document.getElementById(headerId),
            toggleup = document.getElementById("toggle-up")

        // Validate that all variables exist
        if (toggle && nav && bodypd && headerpd) {
            toggleup.addEventListener('click', () => {
                // show navbar
                nav.classList.toggle('show-left-nav');
                //hide navbar buttons on sm
                document.getElementById("nav-buttons").classList.toggle('d-none');
                // change icon
                toggle.classList.toggle('fa-times');
                // add padding to body
                bodypd.classList.toggle('body-menu-pd');
                // add padding to header
                headerpd.classList.toggle('body-menu-pd');
            });
        }
        
    }

    showNavbar('header-toggle', 'nav-bar', 'body-pd', 'header');


    /*===== LINK ACTIVE =====*/
    const linkColor = document.querySelectorAll('.nav_link');

    function colorLink() {
        if (linkColor) {
            linkColor.forEach(l => l.classList.remove('active'));
            this.classList.add('active');
        }
    }
    linkColor.forEach(l => l.addEventListener('click', colorLink));

    // show chat
    const showChat = (toggleId, navId, bodyId, headerId) => {
        const toggle = document.getElementById(toggleId),
            nav = document.getElementById(navId),
            bodypd = document.getElementById(bodyId),
            headerpd = document.getElementById(headerId),
            closeChat = document.getElementById("close-chat")

        // Validate that all variables exist
        if (toggle && nav && bodypd && headerpd) {
            const toggleFunc = () => {
                // show navbar
                document.getElementById("invisible-toggle-div").classList.toggle('invisible');
                nav.classList.toggle('invisible');
                nav.classList.toggle('show-right-nav');
                
                // change icon
                toggle.classList.toggle('invisible');
                // add padding to body
                bodypd.classList.toggle('body-chat-pd');
                // add padding to header
                headerpd.classList.toggle('body-chat-pd');
            }
            toggle.addEventListener('click', toggleFunc);
            closeChat.addEventListener('click', toggleFunc);
        }
        
    }

    showChat('chatButton', 'nav-bar-chat', 'body-pd', 'header');
    
});

变化:

<div id="nav-buttons" class="me-5 d-sm-flex">

至:

<div id="nav-buttons" class="me-5 d-flex">