드롭 다운 항목이 페이지를 벗어나지 않는 것 같습니다. BS3에서 몇 가지를 시도했지만 작동하지 않는 것 같습니다. ml-auto 때문인지 잘 모르겠습니다. (if-else 문 무시)
CodePen 은 다음과 같습니다 .
<div class="container-fluid">
<nav class="navbar fixed-top navbar-toggleable-md navbar-inverse" style="background-color: #3c763d!important;">
<button class="navbar-toggler navbar-toggler-right navbar-toggler-custom" type="button" data-toggle="collapse" data-target="#searchNav" aria-controls="searchav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button> -->
<div id="logo">
<a class="navbar-brand" href="/">Company</a>
</div>
<div class="collapse navbar-collapse" id="searchNav">
<ul class="navbar-nav mx-auto">
<form action="/search" class="form-inline">
<input class="form-control mr-sm-2" id="keyword" name="keyword" :value='keyword' type="text" placeholder="Search for jobs" />
<!-- <div class="input-group"> -->
<input class="form-control mr-sm-2" id="location" name="location" :value='location' type="text" placeholder="Location" />
<!-- <button class="btn btn-secondary" type="button">Find Jobs</button> -->
<!-- </span> -->
<!-- </div> -->
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Find Jobs</button>
</form>
</ul>
</div>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a href="/post" class="nav-link"><strong>Post Job</strong></a></li>
<div class="hidden-lg-up">
<li v-if="!user_logged" class="nav-item">
<a v-if="!user_logged" href="/signup" class="nav-link"><strong>Register</strong></a>
</li>
<li v-if="user_logged && user_type === 'user'" class="nav-item">
<a v-if="user_logged && user_type === 'user'" href="/profile" class="nav-link"><strong>Profile</strong></a>
</li>
<li v-if="user_logged && user_type === 'user'" class="nav-item">
<a v-if="user_logged && user_type === 'user'" href="/applied" class="nav-link"><strong>My Jobs</strong></a>
</li>
<li v-if="user_logged && user_type === 'user'" class="nav-item">
<a v-if="user_logged && user_type === 'user'" href="/searches" class="nav-link"><strong>My Searches</strong></a>
</li>
<li v-if="user_logged && user_type === 'company'" class="nav-item">
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="nav-link"><strong>Employer Dashboard</strong></a>
</li>
<li v-if="!user_logged" class="nav-item">
<a v-if="!user_logged" href="/login" class="nav-link"><strong>Login</strong></a>
</li>
<li v-if="user_logged" class="nav-item">
<a v-if="user_logged" href="/logout" class="nav-link"><strong>Logout</strong></a>
</li>
</div>
<div class="hidden-md-down">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="/" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/signup" class="dropdown-item"><strong>Register</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged && user_type === 'user'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'user'" href="/profile" class="dropdown-item"><strong>Profile</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/applied" class="dropdown-item"><strong>My Jobs</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/searches" class="dropdown-item"><strong>My Searches</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged && user_type === 'company'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="dropdown-item"><strong>Employer Dashboard</strong></a>
<!-- </li> -->
<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/login" class="dropdown-item"><strong>Login</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged" class="nav-item"> -->
<a v-if="user_logged" href="/logout" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
<!-- </li> -->
</div>
</li>
</div>
</ul>
</div>
</nav>
</div>
답변
Bootstrap에는 이미 내장되어 있습니다. 메뉴 정렬을 참조하십시오 . div에 dropdown-menu-right
클래스를 추가하기 만하면 dropdown-menu
됩니다.
<div class="dropdown-menu dropdown-menu-right">
작업 예 : (전체 페이지 열기)
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-xl navbar-dark bg-primary">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Company</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<form action="#" class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" id="keyword" name="keyword" :value='keyword' type="text" placeholder="Search for jobs" />
<input class="form-control mr-sm-2" id="location" name="location" :value='location' type="text" placeholder="Location" />
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Find Jobs</button>
</form>
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a href="#" class="nav-link active">
<strong>Post Job</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Register</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Profile</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>My Jobs</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>My Searches</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Employer Dashboard</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Login</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Logout</strong>
</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item">
<strong>Register</strong>
</a>
<a href="#" class="dropdown-item">
<strong>Profile</strong>
</a>
<a href="#" class="dropdown-item">
<strong>My Jobs</strong>
</a>
<a href="#" class="dropdown-item">
<strong>My Searches</strong>
</a>
<a href="#" class="dropdown-item">
<strong>Employer Dashboard</strong>
</a>
<a href="#" class="dropdown-item">
<strong>Login</strong>
</a>
<a href="#" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
</div>
</li>
</ul>
</div>
</nav>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.4.2/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
답변
에 대한 스타일 시트에서는 .dropdown-menu
변화 left: 0;
에 right: 0;
.
답변
인터넷 검색을 할 때이 문제를 우연히 발견했지만 react를 사용하는 사람들의 이익을 위해 (정확하지만) 수락 된 답변으로 문제가 해결되지 않습니다. React는 className에 수동으로 추가 할 때 ‘dropdown-menu-right’를 무시합니다. 대신 코드를 아래로 업데이트하면 동일한 클래스가 올바르게 설정됩니다.
자바 스크립트의 경우
<DropdownMenu right>
타이프 스크립트 용
<DropdownMenu right={true}>
이 스레드가 문제를 검색 할 때 나타나고 반응 (reactstrap)에서 부트 스트랩 4에 대한 올바른 솔루션을 알아내는 데 꽤 오랜 시간이 걸렸습니다.
답변
귀하의 코드를 살펴보고 수용된 답변이 다른 문제를 해결한다고 생각합니다. 메뉴가 페이지의 왼쪽이나 오른쪽으로 이동하고 버튼 아래에 있지 않은 경우 부모 클래스가 누락되어 있고 클래스가 잘못된 요소에있는 .btn-group
것처럼 보입니다 . 다음은 Bootstrap 4 문서의 가장 기본적인 예입니다..dropdown-toggle
.dropdown-menu
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Right-aligned menu
</button>
<div class="dropdown-menu dropdown-menu-right">
<button class="dropdown-item" type="button">Action</button>
<button class="dropdown-item" type="button">Another action</button>
<button class="dropdown-item" type="button">Something else here</button>
</div>
</div>
코드의 작은 블록 만 살펴 보았지만 마지막에는 큰 화면에 드롭 다운이 표시되는 부분에 집중했습니다.
<li class="nav-item dropdown"> <!--- change this line --->
<a class="nav-link dropdown-toggle" href="/" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/signup" class="dropdown-item"><strong>Register</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged && user_type === 'user'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'user'" href="/profile" class="dropdown-item"><strong>Profile</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/applied" class="dropdown-item"><strong>My Jobs</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/searches" class="dropdown-item"><strong>My Searches</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged && user_type === 'company'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="dropdown-item"><strong>Employer Dashboard</strong></a>
<!-- </li> -->
<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/login" class="dropdown-item"><strong>Login</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged" class="nav-item"> -->
<a v-if="user_logged" href="/logout" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
<!-- </li> -->
</div>
</li>
에
<li class="nav-item btn-group">
<a class="nav-link dropdown-toggle" href="/" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/signup" class="dropdown-item"><strong>Register</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged && user_type === 'user'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'user'" href="/profile" class="dropdown-item"><strong>Profile</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/applied" class="dropdown-item"><strong>My Jobs</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/searches" class="dropdown-item"><strong>My Searches</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged && user_type === 'company'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="dropdown-item"><strong>Employer Dashboard</strong></a>
<!-- </li> -->
<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/login" class="dropdown-item"><strong>Login</strong></a>
<!-- </li> -->
<!-- <li v-if="user_logged" class="nav-item"> -->
<a v-if="user_logged" href="/logout" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
<!-- </li> -->
</div>
</li>