I cannot create a Modal with html

Asked

Viewed 183 times

1

I’m trying to open a simple modal using HTML:

.modal:target {
  opacity: 1;
  pointer-events: auto;
}

.modal > div {
  width: 400px;
  position: relative;
  margin: 10% auto;
  padding: 15px 20px;
  background: #fff;
}
<html>
    <head></head>
    <body>
    
    <a href="#Modal">Visualizar tabelas usadas</a>

    <div id="Modal" class="modal">
    <a href="#fechar" title="Fechar" class="fechar">x</a>
    <h2>Janela Modal</h2>
    <p>Só teste top</p>
  
    </div>
    </body>
    </html>

But the same only returns me the text, and not a fashion.

  • That stretch .modal > div is applying the style in a div within the div.modal. This code doesn’t need, just .modal { /* Code Here */ }

  • Even so the text remains fixed on the page

  • It is necessary to modify the css of modal. Take a look at this example, maybe you’ll find out https://answall.com/a/268025/99718

2 answers

3

In this code the problem is in the class declaration

.modal:target {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  width: 400px;
  position: relative;
  margin: 10% auto;
  padding: 15px 20px;
  background: #fff;
}
<html>
    
    <head></head>
    <body>
    
    <a href="#Modal">Visualizar tabelas usadas</a>

    <div id="Modal" class="modal">
    <a href="#fechar" title="Fechar" id="fechar" class="fechar">x</a>
    <h2>Janela Modal</h2>
    <p>Só teste top</p>
  
    </div>
    </body>
    </html>

Here an example of modal made with bootstrap

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Exemplo
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
           // aqui você irá colocar os códigos html que vão preencher o corpo do seu modal
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

  • Good kk, I never used bootstrap, I should insert some files in my directory before correct?

  • if Voce is going to choose to use bootstrap it’s just you copy the tags scritps and link that I put at the beginning of that code

2


Here is an example of Modal only with CSS and HTML, is very simple, but can give you a light. Gives a studied in the code:

body {
  color: #333;
  font-family: 'Helvetica', arial;
}
.wrap {
  padding: 40px;
  text-align: center;
}

.btn {
  background: #428bca;
  border: #357ebd solid 1px;
  border-radius: 3px;
  color: #fff;
  display: inline-block;
  font-size: 14px;
  padding: 8px 15px;
  text-decoration: none;
  text-align: center;
  min-width: 60px;
  position: relative;
  transition: color .1s ease;
}
.btn:hover {
  background: #357ebd;
}
.btn.btn-big {
  font-size: 18px;
  padding: 15px 20px;
  min-width: 100px;
}
.btn-close {
  color: #aaa;
  font-size: 30px;
  text-decoration: none;
  position: absolute;
  right: 5px;
  top: 0;
}
.btn-close:hover {
  color: #919191;
}
.modal:before {
  content: "";
  display: none;
  background: rgba(0, 0, 0, 0.6);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
}
.modal:target:before {
  display: block;
}
.modal:target .modal-dialog {
  -webkit-transform: translate(0, 0);
  -ms-transform: translate(0, 0);
  transform: translate(0, 0);
  top: 20%;
}
.modal-dialog {
  background: #fefefe;
  border: #333 solid 1px;
  border-radius: 5px;
  margin-left: -200px;
  position: fixed;
  left: 50%;
  top: -100%;
  z-index: 11;
  width: 360px;
  -webkit-transform: translate(0, -500%);
  -ms-transform: translate(0, -500%);
  transform: translate(0, -500%);
  -webkit-transition: -webkit-transform 0.3s ease-out;
  -moz-transition: -moz-transform 0.3s ease-out;
  -o-transition: -o-transform 0.3s ease-out;
  transition: transform 0.3s ease-out;
}
.modal-body {
  padding: 20px;
}
.modal-header,
.modal-footer {
  padding: 10px 20px;
}
.modal-header {
  border-bottom: #eee solid 1px;
}
.modal-header h2 {
  font-size: 20px;
}
.modal-footer {
  border-top: #eee solid 1px;
  text-align: right;
}
<!-- Btn abre Modal -->
<div class="wrap">
  <a href="#modal-one" class="btn btn-big">Modal!</a>
</div>
 
<!-- Janela Modal -->
<div class="modal" id="modal-one" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-header">
      <h2>Modal in CSS?</h2>
      <a href="#" class="btn-close" aria-hidden="true">×</a>
    </div>
    <div class="modal-body">
      <p>One modal example here! :D</p>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn">Nice!</a>
    </div>
  </div>
</div>

Source: https://codepen.io/LFeh/pen/oEula

Browser other questions tagged

You are not signed in. Login or sign up in order to post.