0

how can I make the dropdown menu to expand upwards instead of downwards?

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<br>
<div class="container"> 
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
    COLOR
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">red</a>
      <a class="dropdown-item" href="#">yellow</a>   
  </div>
</div>
</body>
</html>

I expect the dropdown menu to expand upwards instead of downwards but the actual output is, the dropdown menu shows downward.

Thanveer Shah
  • 3,250
  • 2
  • 15
  • 31
  • If you search for "dropup" you'll find a few examples like :https://www.w3schools.com/howto/howto_css_dropup.asp – Yilmaz Apr 17 '19 at 08:51
  • Possible duplicate of https://stackoverflow.com/questions/17581352/how-to-get-a-bootstrap-dropdown-submenu-to-dropup – 04FS Apr 17 '19 at 08:54
  • Possible duplicate of [Drop-down menu that opens up/upward with pure css](https://stackoverflow.com/questions/7814186/drop-down-menu-that-opens-up-upward-with-pure-css) – Tim T. Apr 17 '19 at 08:56
  • Bootstrap 4 also has a documentation for the "Dropup variation" https://getbootstrap.com/docs/4.0/components/dropdowns/#dropup-variation – Yilmaz Apr 17 '19 at 08:57

2 Answers2

1

if the button is the only thing on your page, and I suspect that from your code, the dropup wouldn't work... so check the code below and try removing the H1 and p tags...

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="container">
  <h1> This is demo text </h1>
  <p>There needs to be some text... else it won't open up</p>
  <div class="dropup">
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
    COLOR
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">red</a>
      <a class="dropdown-item" href="#">yellow</a>
    </div>
  </div>
</div>
Akber Iqbal
  • 14,487
  • 12
  • 48
  • 70
1

Here is a bootstrap solution

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<br>
<br>
<br>
<br>


<div class="container"> 
  <div class="btn-group dropup"> 
  <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    COLOR
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">red</a>
      <a class="dropdown-item" href="#">yellow</a>   
  </div>
  </div>
</div>

</body>
</html>
Thanveer Shah
  • 3,250
  • 2
  • 15
  • 31