[RUBY] [No need for Gem! !! HTML & CSS only] Easy way for beginners to create drop-down menus

Introduction

I want to create a hamburger menu or drop-down menu, but I don't know how to install *** Gem *** · *** JavaScript *** doesn't work as I expected. This article is recommended for such beginners.

Development environment

・ Rails 6.0.0 ・ MySQL 5.6.50

Drop-down menu implementation

I will explain the flow of how to implement the drop-down menu immediately. By the way, the drop-down menu is the following menu.

f15d1dda5826200510a6a8d6eabad639.png

Press "..." to display the menu.

I often see it, but it is troublesome to do it with JavaScript, and I think that introducing Gem and jQuery for Bootstrap introduction is a high hurdle for beginners.

Then I will explain.

1) Initial setting in application.html.erb

First, load the necessary items such as Bootstrap in app/views/layouts/application.html.erb.

Below is the HTML code I used when I created the dropdown menu.

ruby:views/layouts/application.html.erb


<!DOCTYPE html>
<html>
  <head>
    <title>PlansApp</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css">
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    <link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet">

    #I'm loading bootstrap here
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>

  <body>
    <%= yield %>
  #The following 3 lines of script tags are added so that they can be used in HTML.
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

This completes the initial settings.

2) Write HTML

After making the initial settings, we will write the HTML of the drop-down menu immediately. Specifically, it is best to have them look at the formula and arrange it, but I will introduce the ones that I referred to and the ones that I created.

Reference article example


<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <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>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <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">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

In my case, there was a problem such as the layout being broken because there was a list tag. So I arranged it as follows.

Arrangement example


<a class="nav-link" href="#" id="navbarDropdown" role="button" datatoggle="dropdown" aria-haspopup="true" aria-expanded="false">
   <i class="fas fa-ellipsis-h"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
   <a class="dropdown-item" href="#">Comment history</a>
   <div class="dropdown-divider"></div>
   <a class="dropdown-item" href="#">Users who are following</a>
   <div class="dropdown-divider"></div>
  <%= link_to 'Log out', destroy_user_session_path, method: :delete, class: "dropdown-item" %>
</div>

In my case, I used icons etc. with fontAwesome, and because it is an application under development, there are sparse places where there is a path specification, but I arranged it like this.

Official Document

3) Apply CSS

At this point, all you have to do is write the CSS as you like. For reference, I will introduce a description example of the reference article below.

*** Apply to the following classes in HTML 7th line *** of the reference article.

 <div class="collapse navbar-collapse" id="navbarSupportedContent">

For a detailed explanation of why it is applied to this class, please see the reference article, so please take a look.

[Beginner] I tried to decorate the bar after displaying the details of the hamburger menu

Reference article example


@media screen and (max-width: 992px) {
.collapse.navbar-collapse{
    padding: 10%;
    border-radius: 10px 10px 10px 10px;
    background: linear-gradient(white, #cccccc) !important;
    margin: 15%;
    font-size: 1.7rem;
  }
}

@media screen and (max-width: 992px) {
  //For smartphones
  .collapsing.navbar-collapse{
    padding: 10%;
    border-radius: 10px 10px 10px 10px;
    background: linear-gradient(white, #cccccc) !important;
    margin: 15%;
    font-size: 1.7rem;

  }
}

This completes the implementation of the drop-down menu. With this in mind, why not try to find some of your UI/UX skills?

References

Official Document

[Beginner] I tried to decorate the bar after displaying the details of the hamburger menu

Recommended Posts

[No need for Gem! !! HTML & CSS only] Easy way for beginners to create drop-down menus
Easy way to create an original logo for your application (easy with your smartphone)
Easy way to create JSP custom tags
A memo to simply create a form using only HTML and CSS in Rails 6
Easy way to create an implementation of java.util.stream.Stream
[Spring Boot] How to create a project (for beginners)