D T
D T

Reputation: 3756

Why align items center not working?

This is my HTML code attempt to create an image grid:

Source at: https://www.w3schools.com/howto/howto_css_image_grid_responsive.asp

I want set image display center of screen but align-items or align-self not woking both:

<div style="align-items: center;align-content: center;align-self: center;">
  <%foreach $datalist as $row%>
    <div class="row">
      <div class="column">
        <img src="shared/img/<%$row.image_name%>">
        <p><%$row.title%></p>
      </div>
      <div class="column">
        <img src="shared/img/<%$row.image_name%>">
        <p><%$row.title%></p>
      </div>
      <div class="column">
        <img src="shared/img/<%$row.image_name%>">
        <p><%$row.title%></p>
      </div>
      <div class="column">
        <img src="shared/img/<%$row.image_name%>">
        <p><%$row.title%></p>
      </div>
    </div>
  <%/foreach%>
</div>

Result:

enter image description here

Why align items center not working?

How can display to center of screen?

Upvotes: 2

Views: 6555

Answers (3)

root
root

Reputation: 582

This will work:

<div>
    <%foreach $datalist as $row%>
    <div class="row" style="style="display: flex; justify-content: center; align-items: center;">
        <div class="column">
            <img src="shared/img/<%$row.image_name%>">
            <p><%$row.title%></p>
        </div>
        <div class="column">
            <img src="shared/img/<%$row.image_name%>">
            <p><%$row.title%></p>
        </div>
        <div class="column">
            <img src="shared/img/<%$row.image_name%>">
            <p><%$row.title%></p>
        </div>
        <div class="column">
            <img src="shared/img/<%$row.image_name%>">
            <p><%$row.title%></p>
        </div>
    </div>

    <%/foreach%>

</div>

Upvotes: 2

לבני מלכה
לבני מלכה

Reputation: 16261

First align the content of warp div and not the warp div

.row{
  display: flex;
  justify-content: center;
  align-items: center;
}

or try align-items with setting width

Upvotes: 3

Quentin
Quentin

Reputation: 944321

See MDN:

The CSS align-items property defines how the browser distributes space between and around flex items along the cross-axis of their container.

Since the div is not display: flex, the align-items property does not apply.

Upvotes: 0

Related Questions