When inserting html code from javascript, css fails

1

I have a code in javascript that accesses to firebase to obtain information and then inserts html code with this information. All this does but not as expected. For example, it uses images but instead of putting them with a specific size, it puts it to the maximum size, occupying the whole screen. Another thing would be the letas that instead of being written in white are put in a gray color. And of course all disorganized.

I was wondering if it was something I did not take into account, or just something I'm doing wrong, so I put the code here:

.
.
.
<div class="container-fluid">
<div class="card-group" id="ejemplo"> 







<script src="https://www.gstatic.com/firebasejs/5.7.2/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
.
.
.
.
};
firebase.initializeApp(config);

var database = firebase.database();
var numerojaja=-1;


var aaaa= firebase.database().ref("/peliculas/");          // hacemos una referencia al nodo de peliculas


aaaa.once("value",function(snapshot){            // accedemos a la base de datos 

var vector=[];                           // creamos un vector

snapshot.forEach(function(child){        // por cada hijo de "peliculas"

vector.push(child.key);          // pusheamos dicho hijo, ya que esta en orden ascendente

});      
// window.alert("hola");
var ejemplo = document.getElementById("ejemplo");                                            // una vez terminado la obtencion de los indices de los hijos de "peliculas"
for(var i = vector.length-1; i>=0;i--){       // hacemos un bucle para acceder al vector en orden inverso

aaaa.child(vector[i]).once('value').then(function(snapshot){     // accedemos al hijo que tiene el identificador de la ultima posicion del vector


ejemplo.insertAdjacentHTML("beforeend",'<div class="col-xs-12 col-sm-6  col-md-4  col-lg-3">');
ejemplo.insertAdjacentHTML("beforeend",'<div class="card">');
ejemplo.insertAdjacentHTML("beforeend",'<img class="card-img-top" src="'+snapshot.val().cartelera+'" alt="Card image cap" >');
ejemplo.insertAdjacentHTML("beforeend",'<div class="card-body">');
ejemplo.insertAdjacentHTML("beforeend",'<p class="card-title"> '+snapshot.val().nombre+'</p>');
ejemplo.insertAdjacentHTML("beforeend",'<i class="descripcion">'+snapshot.val().descripcion+'</i>');
ejemplo.insertAdjacentHTML("beforeend",'<br>');
ejemplo.insertAdjacentHTML("beforeend",'<i class="cocos">'+snapshot.val().reputacion+' cocos sobre 5</i>');
ejemplo.insertAdjacentHTML("beforeend",'</div>');
ejemplo.insertAdjacentHTML("beforeend",'</div>');
ejemplo.insertAdjacentHTML("beforeend",'</div>');





});                                                                                                  

}                                                                                                      
});                                                                                      




</script>

I also have how tags should be in html, but with the data locally (so if everything works well).

<div class="col-xs-12 col-sm-6  col-md-4  col-lg-3">
<div class="card">
<img class="card-img-top" src="imagenes/ep1.jpg" alt="Card image cap" >
<div class="card-body">
<p class="card-title"> Star Wars Episodio 1, la amenaza fantasma</p>
<i class="descripcion">Dual of fates.</i>
<br>
<i class="cocos">3 cocos sobre 5</i>
</div>
</div>
</div>

In the css file I have:

    body {

      background-color: #0F171E;
       min-height: 100%;
            min-width: 800px;

            /* Escalamiento proporcional */
            width: 100%;
            height: auto;



    }

    .navbar{
        background-color:rgba(000, 000, 000, 0.4);
        color:white;



    }



    .cocos{
        font-size:10px;

    }

    .descripcion{
        font-size:20px;

    }

    .card-title{
        font-size:25px;



    }

    .card{
        background-color:rgba(255, 255, 255, 0.0);
        width: 18rem;
        color:white;
        font-family:trebuchet-ms;
    }
    
asked by k1k4ss0 02.01.2019 в 23:32
source

1 answer

3

following your example:

var ejemplo = document.getElementById("ejemplo");
ejemplo.insertAdjacentHTML("beforeend",'<div class="col-xs-12 col-sm-6  col-md-4  col-lg-3">');
ejemplo.insertAdjacentHTML("beforeend",'<div class="card">');
ejemplo.insertAdjacentHTML("beforeend",'<img class="card-img-top" src="https://static.comicvine.com/uploads/original/11132/111322790/6171776-8796081884-v69lH.jpg" alt="Card image cap" >');
ejemplo.insertAdjacentHTML("beforeend",'<div class="card-body">');
ejemplo.insertAdjacentHTML("beforeend",'<p class="card-title">Hackers</p>');
ejemplo.insertAdjacentHTML("beforeend",'<i class="descripcion">Hackers movie</i>');
ejemplo.insertAdjacentHTML("beforeend",'<br>');
ejemplo.insertAdjacentHTML("beforeend",'<i class="cocos">5 cocos sobre 5</i>');
ejemplo.insertAdjacentHTML("beforeend",'</div>');
ejemplo.insertAdjacentHTML("beforeend",'</div>');
ejemplo.insertAdjacentHTML("beforeend",'</div>');
body {

      background-color: #0F171E;
       min-height: 100%;
            min-width: 800px;

            /* Escalamiento proporcional */
            width: 100%;
            height: auto;



    }

    .navbar{
        background-color:rgba(000, 000, 000, 0.4);
        color:white;



    }



    .cocos{
        font-size:10px;

    }

    .descripcion{
        font-size:20px;

    }

    .card-title{
        font-size:25px;



    }

    .card{
        background-color:rgba(255, 255, 255, 0.0);
        width: 20px;
        color:white;
        font-family:trebuchet-ms;
    }
<div id="ejemplo">

</div>

If we execute and go to the inspection of the elements to see how the html is being created (F12, then position with the mouse in the image and with the right mouse button select "inspect item") we will see that the resulting code is:

<div id="ejemplo">
<div class="col-xs-12 col-sm-6  col-md-4  col-lg-3"></div>
<div class="card"></div> 
<img class="card-img-top" 
 src="https://static.comicvine.com/uploads/original/11132/111322790/6171776- 
 8796081884-v69lH.jpg" alt="Card image cap"><div class="card-body"></div>
<p class="card-title">Hackers</p>
<i class="descripcion">Hackers movie</i><br>
<i class="cocos">5 cocos sobre 5</i>
</div>

If you notice you are not creating as you define in the code, that at least in this line you indicate that the layer opening is created:

ejemplo.insertAdjacentHTML("beforeend",'<div class="card">');

But the closing layer is also created:

<div class="card"></div>

What happens then? The answer is that the function insertAdjacentHTML () "analyzes the string of text entered as HTML or XML string and inserts the nodes resulting from that analysis into the DOM tree at the specified position". insertAdjacentHTML Doc , in other words the function will determine based on the string passed by parameter what element to add to the DOM, therefore you see that you want to add a div and generate that NODE, when generating the same it does it in a valid way that is to say without errors and that means that the label has a beginning and an end.

What you must do in this specific case is to have a variable that contains all the text of the html and then insert it.

var ejemplo = document.getElementById("ejemplo");
var card = '<div class="col-xs-12 col-sm-6  col-md-4  col-lg-3"><div class="card"><img class="card-img-top" src="https://static.comicvine.com/uploads/original/11132/111322790/6171776-8796081884-v69lH.jpg" alt="Card image cap" ><div class="card-body"><p class="card-title">Hackers</p><i class="descripcion">Hackers movie</i><br><i class="cocos">5 cocos sobre 5</i></div></div></div>';



ejemplo.insertAdjacentHTML("beforeend",card);
body {

      background-color: #0F171E;
       min-height: 100%;
            min-width: 800px;

            /* Escalamiento proporcional */
            width: 100%;
            height: auto;



    }

    .navbar{
        background-color:rgba(000, 000, 000, 0.4);
        color:white;



    }



    .cocos{
        font-size:10px;

    }

    .descripcion{
        font-size:20px;

    }

    .card-title{
        font-size:25px;



    }

    .card{
        background-color:rgba(255, 255, 255, 0.0);
        width: 10rem;
        color:white;
        font-family:trebuchet-ms;
    }
    
    .card img{
      width: 100%;
    }
<div id="ejemplo">

</div>

Of good rest, you have an extra detail with the image since it does not take the size of its container by default so you have to indicate it, we do that by placing in the css the witdh attribute of the image in 100% , in this way it will occupy 100% of the element in which it is contained.

.card img{
  width: 100%;
}

If card measures 10mm width then the image will measure 10 wide and so with the other sizes. Notice also that since we have a main element called card we can take it in the css as the father of others and manipulate others based on it, that is why I put .card img instead of creating a class specifically for the image with card-img-top.

I leave you a game that helps to understand the selectors of css, it's very good: Css Dinner

    
answered by 03.01.2019 / 14:53
source