It seems like you are looking to align the second row completely to the left. Dealing with different button arrangements when the screen size changes can be tricky, but using flex display makes it much simpler as you don't have to worry about margins at all.
Take a look at this example:
.buttons {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
button {
padding: 10px 35px;
}
<body>
<div class="buttons">
<button>Button 1</button>
<button>Button 2</button>
<button>Button 3</button>
<button>Button 4</button>
<button>Button 5</button>
<button>Button 6</button>
<button>Button 7</button>
</div>
</body>
Try resizing your screen and observe how the buttons remain aligned. The display: flex
property is ideal for this purpose. Simply create a container element (like the div
in the provided code) and set it to display: flex
. You can also utilize the flex-wrap
property to allow overflow to wrap onto the next line, and adjust the space between elements using the gap
property.
If you prefer not to use flex, another option is to set equal left and right margins instead of only setting the left margin. Setting margin: 0.5rem
, for instance, will apply 0.5rem margins on all sides and ensure alignment. However, this approach may not achieve the desired flush alignment against the container edges, which can be easily achieved with flex display. Alternatively, working with negative margins without flex can introduce complexities, especially if the container has borders.