1

I am trying to build a responsive website. and I want all of my divs to always be in the middle.

for some reason when I use flex attribute in my container the bootstrap card go offscreen: any suggestions on how to handle it right?

Normal Screen Normal

Moblie Screen :( Mobile

My HTML CODE

<body>
    <div class="container">
        <div class="d-flex justify-content-center align-items-center">
            <form action="" method="POST">
                {% csrf_token %}
                <div id="smain">
                    <h1> Lets Start</h1>

                    <div class="card-deck">
                        <div class="card" style="width: 18rem;">
                            <img class="card-img-top" src="{% static "/images/pc.jpg" %}" alt="Card image cap">
                            <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">Some quick example text to build on the card title bulk of the
                                    card's content.</p>
                                <a href="#" class="btn btn-primary">Go somewhere</a>
                            </div>
                        </div>
                        <div class="card" style="width: 18rem;">
                            <img class="card-img-top" src="{% static "/images/dance.png" %}" alt="Card image cap">
                            <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">Some quick example
                                    text to build on the card title and makecontent.
                                </p>
                                <a href="#" class="btn btn-primary">Go
                                    somewhere</a>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>

CSS

body {
    background-image: url("{% static "images/unnamed1.jpg" %}");
    background-size: cover;
    background-repeat: no-repeat;
}

html,
body {
    height: 100%;
}
.container {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}
Aymen TAGHLISSIA
  • 1,675
  • 13
  • 30

1 Answers1

2

Use margin on the children instead of justify-content: center; on the parent.

html,
body {
    height: 100%;
}
.container {
    height: 100%;
    display: flex;
}
.container>div{
  margin:auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<div class="container">
  <div class="d-flex justify-content-center align-items-center">
    <form action="" method="POST">
      <div id="smain">
        <h1> Lets Start</h1>

        <div class="card-deck">
          <div class="card" style="width: 18rem;">
            <img class="card-img-top" src="https://picsum.photos/300/500" images="" pc.jpg "=" " %}"="" alt="Card image cap">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title bulk of the card's content.</p>
              <a href="#" class="btn btn-primary">Go somewhere</a>
            </div>
          </div>
          <div class="card" style="width: 18rem;">
            <img class="card-img-top" src="https://picsum.photos/300/500" images="" dance.png "=" " %}"="" alt="Card image cap">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and makecontent.
              </p>
              <a href="#" class="btn btn-primary">Go
                                    somewhere</a>
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
</div>
Rainbow
  • 6,772
  • 3
  • 11
  • 28
  • thank you it worked!, can you explain to me what happens? why did it go offscreen? – Snir Ben Yosef Apr 23 '20 at 06:28
  • 1
    @SnirBenYosef Flexbox does true centering, it doesn't care if the content is taller than the parent it will stay centered, `margin:auto;` however will consume the left over space from the edges of the element up to the edges of the parent. [**Full Explanation**](https://stackoverflow.com/a/33455342/7148391) – Rainbow Apr 23 '20 at 10:44