How to right align grid elements
P粉296080076
2023-08-13 11:27:01
<p>I have a fixed number of columns in a CSS Grid layout and have elements to be placed in these columns. The number of elements to be placed is less than the number of columns. </p>
<p>I want the columns to be aligned to the right, in the order the elements were added. In the following code, there are 4 elements aligned to the left with 6 empty elements to the right: </p>
<p><br /></p>
<pre class="snippet-code-css lang-css prettyprint-override"><code>.hello {
border-style: solid;
border-color: blue;
}
.container {
display: grid;
grid-gap: 5px;
grid-template-columns: repeat(10, 5rem);
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="container">
<div class="hello">1</div>
<div class="hello">2</div>
<div class="hello">3</div>
<div class="hello">4</div>
</div></code></pre>
<p><br /></p>
<p>I want to change the layout above to: </p>
<pre class="brush:php;toolbar:false;">empty empty empty empty empty empty 1 2 3 4</pre>
<p>I tried various combinations of <code>[align,justify]-[content,items]</code>, but none of them achieved the effect of right alignment. Can CSS Grid achieve this? </p>
Due to the limited number, you can define them manually
But it’s better to use flexbox: