Side by side in Bootstrap, jumping line

Asked

Viewed 2,028 times

4

I have a simple problem with Bootstrap v4.0.0-alpha.5.

I’ll run it through the database. It lines side by side but in some cases a line is skipped, and there is only one item for the bottom line.

<div class="row">
                <div class="col-md-12">
                    <h2 style="text-align:left; color:white; margin:-top:1%;">Ação/Aventura</h2>

                    <?php

                    require 'conexao.php';

                    $consulta = $PDO->query("SELECT * FROM filmes WHERE categoria1 = 'acaoaventura' ORDER BY id ASC;");

                    while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) { ?>

                    <div class="col-md-2" style="margin:0;margin-bottom:3%;margin-top:2%;padding:0.1%;">

                        <div class="col-md-12" style="margin:0;padding:0;">
                            <a href="filme.php?id=<?php echo "$linha[id]";?>">
                                <img src="<?php echo "$linha[fotodacapa]"; ?>" class="img-fluid">                               
                                <p style="color:yellow; text-align:center; margin:0; padding:0;"><?php echo "$linha[nome]"; ?></p>
                                <div class="col-md-6">
                                    <p style="color:white; text-align:center; margin:0; padding:0;">16 visitas</p></a>
                                </div>

                                <div class="col-md-6">
                                    <a class="venobox_custom" data-type="youtube" href="https://youtu.be/<?php echo "$linha[trailer]"; ?>?autoplay=1"><p style="color:white; text-align:center; margin:0; padding:0;"><i class="fa fa-film" aria-hidden="true"></i> Trailer</p></a>
                                </div>
                            </div>
                        </div>

                        <?php } ?>

                    </div>
                </div>

In this code snippet, I do the search to show on the screen what comes from the bank, but get the following result:

inserir a descrição da imagem aqui

I’ve tried to move a few items around, but to no avail. In another job, I managed to take the "container", something not recommended, but it worked for me. This time, did not solve.

Some idea of friends?

Thank you in advance!

Note: Do not notice the settings of Div’s, as I am still testing, I have not organized the classes.

4 answers

7

That’s not how bootstrap grids work, the bootstrap solution @inkeliz worked more for coincidence, but it’s not the right solution, especially when it comes to the way you applied it.

As I explained in Prevent line breaking (encavalating) in col-Md bootstrap, then the steps you should follow to use Grids are basically:

  1. Do not use col- inside col-
  2. The "parent" element of col- should always be a row
  3. The sum of all cols should always be 12, for example:

    • If you have 4 Divs with the class .col-*-3 then the sum will be 12 (3+3+3+3 = 12)
    • If you have 3 Divs with the class .col-*-4 then the sum will be 12 (4+4+4 = 12)
    • Can also do .col-*-6+.col-*-3+.col-*-3 for example (6+3+3=12)

First you explain where the mistakes are

Follow the problems in your HTML:

  1. You <div class="col-md-2" style="margin:0;margin-bottom:3%;margin-top:2%;padding:0.1%;"> within a col-md-12, like I said, col- should always go inside row, then you’re wrong.

  2. You have a <div class="col-md-12" style="margin:0;padding:0;"> within a col-md-2

  3. You have two <div class="col-md-6"> inside col-md-12

  4. The tag <a> is poorly closed: <a href="filme.php?id=<?php echo "$linha[id]";?>">, you put the </a> within another tag, probably not to conflict with the link to youtube. But this is wrong.

  5. Has two col mixed with other HTML elements that are not part of the grid.

  6. Never changes the margins of a col- using margin: or margin-left or margin-right, I think top and bottom is fine.

  7. Extra: Although not affecting the grids has an error in your CSS is wrong h2 style="text-align:left; color:white; margin:-top:1%;, has a margin:-top, the correct is margin-top:1%;


How to solve

As I explained here /a/180194/3635 being PHP you can use something like $i % (numero de colunas desejada), no seu caso você usou "col-md-2" then I believe you want to 6 columns per rows

Should stay like this:

<div class="row">
    <div class="col-md-12"> <!-- aqui está certo total é igual 12 -->
        <h2 style="text-align:left; color:white; margin:-top:1%;">Ação/Aventura</h2>

        <?php
        require 'conexao.php';

        $consulta = $PDO->query("SELECT * FROM filmes WHERE categoria1 = 'acaoaventura' ORDER BY id ASC;");

        while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
        $i = 0;

        while ($i < 20) {

        $needarow = !($i % 6);
        $i++;//Deve vir depois
        ?>

        <?php if ($needarow) { ?>
        <?php if ($i > 1) { ?>

        </div> <!-- //fecha .row -->

        <?php } ?>

        <div class="row">

        <?php }?>

            <div class="col-md-2" style="margin-bottom:3%;margin-top:2%;padding:0.1%;">

                <!--//acho que nem precisa de col aqui -->
                <a href="filme.php?id=<?php echo $linha['id'];?>">

                    <img src="" class="img-fluid">
                    <p style="color:yellow; text-align:center; margin:0; padding:0;"><?php echo $linha['nome']; ?></p>
                </a> <!-- Link fechado -->

                <div class="row"> <!--// Row necessário para adicionar -->
                    <div class="col-md-6">
                        <p>16 visitas</p>
                    </div>

                    <div class="col-md-6">
                        <a class="venobox_custom" data-type="youtube" href="https://youtu.be/<?php echo $linha[trailer]; ?>?autoplay=1"><p><i class="fa fa-film" aria-hidden="true"></i> Trailer</p></a>
                    </div>
                </div>

            </div>

        <?php
        }   //Termina o loop
        ?>

        </div> <!-- //fecha o ultimo .row -->
    </div>
</div>
  • 1

    A super class, thank you for your attention. I left the "gambiarra" for now, because of the rush of my friend to finish the project, but I will seek to study much more to be applying correctly. I will take your reply as future references. Again, thank you for the clarifications.

  • @Hebertrichard thank you :D

4


/!\ This is a gambit!

You can use the matchHeight so that all elements stay the same size.

Get in http://brm.io/jquery-match-height/, can use:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.matchHeight/0.7.0/jquery.matchHeight-min.js"></script>

Then use:

$('.col-md-2').matchHeight();

This will make all the elements .col-md-2 have the same height, correcting the problem.

This is the simplest way to solve this if you are using a single row with elements exceeding their width row.

  • Interesting solution, I didn’t know her.

  • Neither do I. I’m trying to deploy here. It will solve a lot of my problems! Thanks, @Inkeliz. I’ve answered if I can deploy.

  • 1

    Dude, this is just perfect! You solved it here! It’s all set up! Really cool solution. Thanks for the support, @Inkeliz. Stay as a reference here at SOPT, I gave a searched and found no solution to this problem!

  • Sorry for the honesty, but you didn’t jump the line because of the height, what you did was practically a gambit to avoid the problem, the system of col-*-* (bootstrap grids) don’t work like that, you’re having trouble with the way AP used, the cols can only go within row, but he put col inside col, the sum of the cols within a row always should be 12.

  • @Guilhermenascimento, I would add the solution similar to this one (http://answall.com/questions/179798/gerar-um-row-depois-de-2-colunas-no-bootstrap/179999#179999), add row every 6 elements, summing 12 in total at md. However, as there was something similar, I decided to add another option. In my opinion the problem remains the height, when you have different heights in the same row the whole row will be the same height, so the problem will not exist. I can edit by adding this other correction to add.

  • @Inkeliz has a lot more problem than just making the sum.

  • @Guilhermenascimento I will put my fashion stamp of gambiarra in this answer.

  • I suggest to exchange the '.col-Md-2' for a valid class name. The ideal is to create a class for these display frames and call it as Inkeliz said: $('.class-block'). matchHeight();

  • @Inkeliz I know you got it, it’s just to show you the problem, see I listed 6 problems in his HTML http://answall.com/a/180205/3635 (7 is extra)

  • @Guilhermenascimento I will try to slow down in the gambis, I had already given +1. I didn’t even know I needed the row always, because for me already in some situations the margin of the col could be useful, for example, inside another col.

Show 5 more comments

1

Put a height EQUAL for all items marked with col-Md-2.

PS: using styling in HTML is highly not advisable, that’s why Bootstrap is there, so you don’t have to do this.

  • Thank you for the prompt response! Well, regarding the style, I like to create a file "Styles.css" to leave the changes I want, the code gets cleaner to be read. Regarding col-Md-2, it is only 1 that you have on the page, searching for all id'ss that are in the bank. I don’t have different divs, just this one, on the whole page.

  • Yes, but the while is repeating it according to the number of results. This problem happens because each col-Md-2 has a different height, so when it jumps a line and is left with only one col-Md-2, it is tried to "fit" where it fits. So go on the col-Md-2 you have and put a height: 300px (estimated value). This should surely solve.

  • I would not like to work with fixed size, afraid of losing responsiveness.

1

At first, the problem is when your query results in a title with 2 lines and another with 1 only, it causes misalignment and ends up making you skip a line.

You have some options to solve:

Create a larger width so that the text is not in 2 lines, or create a default margin at the height of the photo and title squares.

to keep them aligned.

  • Thank you for the prompt response. Regarding the text, I totally agree with you. The problem is that nobody wants this limitation, which makes it difficult to solve it. I had a similar problem when I was developing a newspaper. I already modified the margins, even so, the problem persists. Suddenly, I’m not understanding its placement.

  • yes I expressed myself badly, but think about it, it would be possible to create a coma block s Divs uniting image and text in a single group, as if it were the block, then you would set the size of the block in css and all would have the same sizes, like those with smaller titles would have a little more space left and those with larger titles would fill the whole space. You can understand my idea?

  • I understood yes, my friend. I will study a little more, to avoid such disorders. I made mistakes on this site, but for the customer it’s okay, he doesn’t see the tricks at the moment. Anyway, I will improve the code in 1 or 2 weeks, after finishing other work already underway. Thank you for your attention!

Browser other questions tagged

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