Eliminate the spacing between elements when they are in close proximity

I've noticed there is a margin between the buttons as shown below:

.button + .button {
  margin-left : 1rem;
}

The buttons are contained within a

<div class="row">
, which will stack on top of each other on smaller screens: https://i.sstatic.net/yApXo.png

Is it feasible to remove the margin in the second line?

Answer №1

To apply custom styles to a button, utilize the following CSS snippet.

.btn {
    margin-left: 0.5rem;
    margin-right: 0.5rem;
}

Answer №2

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.

Answer №3

To maintain consistent spacing between buttons, try adding a right margin (margin-right: 1rem) instead of a left margin. This will ensure that the spacing is maintained after each button, preventing any additional space from appearing on a new line.

Answer №4

Uncertain about the structure of your HTML, but consider using a media query to target them

@media only screen and (min-width:adjust based on screen width issue in px) and (max-width:modify based on screen width issue in px) 
for example- 
@media only screen and (min-width:768px) and (max-width:1024px)
{
 .button + .button:nth-child(4){
  margin-left: 0px;
}

}

Answer №5

When the parent has the same margin but in negative:

section {
  border: 1px solid #f00;
  width: 300px;
  margin: auto;
}
div {
  font-size: 0;
  margin-left: -1rem;
}
button {
  margin-left: 1rem;
  font-size: 16px;
}
<section>
  <div>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
  </div>
</section>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Due to the feature in VISUAL STUDIO CODE that presents folders and subfolders at the same level

While working on my Angular project, I encountered an issue with creating a subfolder within a folder. Despite trying the same process in Windows Explorer, I faced the same problem of them appearing on the same level. What could be causing this discrepan ...

Can a hyperlink open multiple links at the same time when hovered over?

Can two links be opened in the same hyperlink when hovered over? ...

The horizontal rule element is not displaying

My <hr> element seems to be hidden. This is the HTML code: <div id="line"><hr /></div> And this is the CSS code: hr { border: 0; width: 96%; color: #FFFF00; height: 1px; } #line { float: left; width: 73 ...

Enhancing Inherited Django Templates with Custom Styles

In my Django project, I have created a central HTML template called base.html which contains a set of CSS styles. Various other templates in my project inherit from this base template. The issue I'm encountering is that some of these inherited templa ...

Rotate background image upon each visit

I have a collection of around 50 background images that I want to display randomly each time a user visits my website. The idea is to provide a unique background image for every visit, without saving any information about which image was previously display ...

Get rid of the rollover effect on the <a> tag when hovering over an image

I have a snippet of code that is fully functional. <div class="user-pic round-pic"> <a href="<?php echo $userLoggedIn; ?>"><img src="<?php echo $user['profile_pic']; ?>"></a> </div> The issue lies in i ...

Can someone please explain how to apply various fonts to the text in a `<p>` paragraph using CSS and HTML?

Take a look at this image Can you provide guidance on how to incorporate various fonts within a paragraph, like the example shown in the image above? I am experimenting with the "Creative Corner" section. ...

How can I add background color to WordPress mouse over text?

Hey there! I'm a beginner when it comes to WordPress and currently using version 4.2.2. I'm looking to display a mouse-over background color with text on an image. The text and images are being generated dynamically from the admin panel. Below ar ...

Safari not rendering SVG wave at full width reached

My SVG isn't scaling to 100% width in Safari like it is in IE, Chrome, and Firefox. Despite numerous attempts, I haven't been able to make my SVG stretch across the full width like it does in other browsers. The project is built using React. . ...

Red-colored text (as displayed in Firefox) is not recognized by JSoup when parsing HTML <img> tags

UPDATE: I was able to answer my own question. JSoup can indeed find all image tags. I've been attempting to scrape content from but encountered a problem. In the website's source code, the main images are displayed in red font, which seems to ...

Setting up automatic CSS linting in a Vue.js project for seamless correction

Is there a way to enforce linting on the <style> segment within a .vue file while coding? I have set up eslint (with airbnb ruleset)+prettier for the <template> and <script> sections, which includes some auto-correction upon saving, but I ...

Hovering over one element in Jquery triggers actions on multiple elements simultaneously

I'm working on a project where I have multiple cards, and I've implemented a hover effect using jQuery to make the images inside the cards bigger. However, I'm facing an issue where hovering over one card triggers the effect on all the other ...

How can I dynamically pass a background:url using JavaScript to CSS?

I have a CSS code snippet below that I would like to dynamically change the "background:url" part using Javascript. div{ background: url('pinkFlower.jpg') no-repeat fixed; -webkit-background-size: cover; -moz-background-size: cover; ...

Can you explain the significance of this HTML code?

While examining some source code, I came across the following: <div class="classname {height: 300px; width: 200px}"></div> I am aware that element styling can be done using the style="" attribute. Could you explain what this code snippet sig ...

What is the best way to increase the width of Bootstrap responsive tables?

When using Bootstrap 5 to create responsive tables, I encountered an issue where columns with a lot of text caused other columns to get squished, resulting in a weird appearance. I am looking for a way to prevent this by ensuring that columns with less tex ...

What are the methods to ascertain whether an HTML element possesses a pseudo element?

Is there a method to identify whether an HTML element has a pseudo element (::before / ::after) applied to it without invoking the function: window.getComputedStyle(element, ":before/:after"); MODIFIED: the response is NEGATIVE The issue with getCompute ...

Automatically expand all PrimeNG Accordion panels for easy printing purposes

I've implemented the PrimeNG library's accordion component in my angular project. You can find more information here. In my template, I have some custom css styling for printing the page that looks like this: @media print { .profile-progress ...

Tips for avoiding an automatic slide up in a CSS menu

Is there a way to disable the automatic slide-up effect on my CSS menu when clicking a list item? I have tried using stop().slideup() function in my JavaScript code, but it doesn't seem to work. Additionally, I would like to highlight the selected lis ...

Extract the raw text content from nested elements

Working with highlight.js to include a custom CSS code, however, this library automatically adds span tags around the desired text For example: <pre> <code class="language-css hljs" contenteditable="true" id="css-code&quo ...

Obtain a unique HTML ID dynamically with Selenium and VBA

I am currently working on automating a web form using Selenium and VBA. The form includes a search box that generates an autogenerated list when a value is entered. While I can successfully input a value into the search box and trigger the javascript to cr ...