inserting a line break following a div tag

I have a parent div element with several child div elements, each containing a button. I need to insert a line break after every 4th child div element.

<div class="btn-container">
    <div class="btn-div"><button class="btn">+</button></div>
    <div class="btn-div"><button class="btn">-</button></div>
    <div class="btn-div"><button class="btn">*</button></div>
    <div class="btn-div"><button class="btn">/</button></div>
    <div class="btn-div"><button class="btn">7</button></div>
    <div class="btn-div"><button class="btn">8</button></div>
    <div class="btn-div"><button class="btn">9</button></div>
    <div class="btn-div"><button class="btn">C</button></div>
    <div class="btn-div"><button class="btn">4</button></div>
    <div class="btn-div"><button class="btn">5</button></div>
    <div class="btn-div"><button class="btn">6</button></div>
    <div class="btn-div"><button class="btn">%</button></div>
    <div class="btn-div"><button class="btn">1</button></div>
    <div class="btn-div"><button class="btn">2</button></div>
    <div class="btn-div"><button class="btn">3</button></div>
    <div class="btn-div"><button class="btn">=</button></div>
</div>

What's the CSS solution for this issue? Here is what I tried:

.btn{
  height: 5em;
  width: 5em;

}

.btn-div{
  display: inline-block;

}

.btn-container div:nth-of-type(4n)::after{
  content: '\A';
  display: block;
  height: 2em;
  width: 2em;

}

Answer №1

Use float property in this CSS code snippet

.btn-container div{
  float:left;
}
.btn-container div:nth-child(4n){
    float:none;
    height:2em;
}
<div class="btn-container">
    <div class="btn-div"><button class="btn">+</button></div>
    <div class="btn-div"><button class="btn">-</button></div>
    <div class="btn-div"><button class="btn">*</button></div>
    <div class="btn-div"><button class="btn">/</button></div>
    <div class="btn-div"><button class="btn">7</button></div>
    <div class="btn-div"><button class="btn">8</button></div>
    <div class="btn-div"><button class="btn">9</button></div>
    <div class="btn-div"><button class="btn">C</button></div>
    <div class="btn-div"><button class="btn">4</button></div>
    <div class="btn-div"><button class="btn">5</button></div>
    <div class="btn-div"><button class="btn">6</button></div>
    <div class="btn-div"><button class="btn">%</button></div>
    <div class="btn-div"><button class="btn">1</button></div>
    <div class="btn-div"><button class="btn">2</button></div>
    <div class="btn-div"><button class="btn">3</button></div>
    <div class="btn-div"><button class="btn">=</button></div>
</div>

Answer №2

Your code snippet appears to be functioning correctly when tested on JSFiddle

.btn-container div:nth-of-type(4n)::after{
    content: '\A';
    display: block;
    height: 2em;
    width: 2em;
}

Check it out on JSFiddle

What are the outcomes you're observing? Is the CSS being interpreted as expected?

Answer №3

The most efficient method to achieve this task is by utilizing the <br> tag, which will create a line break/add a line before the next div element. Here's an example:

<div class="btn-div"><button class="btn">+</button></div>
 <br>
 <div class="btn-div"><button class="btn">-</button></div>

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

Implementing login functionality in an Angular application with the help of Kinvey.Social and utilizing the Promise API

I have been utilizing the Kinvey HTML5 library in an attempt to create a client-side application that integrates Google identities for Login. While the Oauth transaction appears to be functioning properly, I am encountering an issue with toggling the visib ...

In JavaScript, unchecking a radio button results in all options becoming uncheckable

I have a pure CSS accordion and I want to enhance it with some JavaScript functionality for users who have JavaScript enabled. The CSS accordion currently utilizes the :checked pseudo-class. The new feature I am looking to add is: if a button that is alre ...

Issue with CSS pseudo-elements :before and :after in Internet Explorer

I am attempting to create a heading with lines on both sides, but I am facing an issue where it only seems to work on the left side in IE. Check out the code here h2 { position: relative; overflow: hidden; text-align: center; } h2:before, h2:afte ...

Unable to access external API through Jquery due to CORS error when making multipart formposts

There exists an external REST API used for uploading resources to a repository, which can be accessed via the curl command: curl -k -X POST -u username:password \ -F package_id="1234" \ -F file=@"path_to_a_file" \ https:/ ...

How to properly center and align the width of a figcaption element within an image placed in a figure element

I've been struggling for two days now trying to fix an issue with the alignment of images and captions on my Django application. In my app, users can upload images, and I'm using figure and figcaption tags to display the image with a caption. Ho ...

Creating a Unique CSS Grid Border with Custom Images

I have a client project using nextjs and the design calls for a component with a custom border on a responsive CSS grid. I've successfully created the CSS grid with all the necessary content, but I'm struggling to add the required border as per t ...

Adding a plethora of HTML using jQuery

Struggling to create a single-page website, I've found myself relying heavily on jQuery's .append function. But surely, there has to be a more efficient method for appending large chunks of code (like tables, graphs, etc.) onto a page. Take this ...

Triggering modal activation upon clicking on the SVG map

Seeking advice on the best way to implement a new feature. I have an SVG map with tiny blue icons that users can click on. You can refer to the image below for clarity: https://i.sstatic.net/XHry9.png Whenever a user clicks on any of these blue icons, I ...

Acquire the content of a nested element using jQuery

I have a navigation list with separate headlines and text for each item. The goal is to switch the main headline and paragraph of text when hovering over a navigation item. CodePen Example Currently, my code displays all text. I only want to display the ...

Creating a perfectly centered card using Bootstrap 4, both horizontally and vertically

I'm struggling to vertically center a card (or container). Although I can center it horizontally, the vertical alignment is proving to be difficult. Despite trying out various code snippets from Stack Overflow and other websites, none of them seem to ...

Using Javascript to display or conceal a specific child element within a loop, based on which parent element has been clicked

I need assistance with creating a grid of projects where clicking on a specific 'project' in the loop will display the corresponding 'project_expanded' div, while hiding all other 'project_expanded' divs. My initial plan was ...

Styling a CSS dropdown menu: tips and tricks

I came across this example and tried to implement it, but I'm facing some difficulties. Any assistance would be greatly appreciated. UPDATE: I managed to position my submenu better with the help of yourkishore and figured out how to deal with nested ...

Utilizing the bootstrap table-striped design to enhance the appearance of a table with empty cells

While utilizing Bootstrap for table displays, I've encountered an issue where the striped style is not being applied to rows with missing cells, resulting in an odd appearance. How can this be resolved? https://i.sstatic.net/1oKKO.png ...

Implementing text formatting for user comments within my NodeJS application

Is there a way to incorporate an interactive text formatting feature into the comment form on my nodejs app? After searching for options online, I couldn't find exactly what I was looking for. Can anyone point me in the right direction? I'm refe ...

A guide to crafting a cross design using only CSS

Is it possible to create a cross design in pure CSS that overlaps an image inside a div? Or would I need to create a new image with the cross included? ...

Is it possible to persist CSS/JS modifications made to a remote resource when the page is reloaded, or to link a remote resource in DevTools to a local

Although I am aware of the Workspaces feature recently added to DevTools, it does not address my specific needs. For instance, when a page utilizes jQuery from a CDN and I make modifications to the library code, saving them by pressing ctrl-s only to find ...

AngularJS - Enhance table row visibility with ngRepeat for targeted highlighting

Example Data <table class="table table-condensed table-striped table-responsive"> <thead> <tr> <th>Sender</th> <th>Subject</th> <th>Received</th> <th>Actions&l ...

Storing form data in temporary local storage using a JSON object array within a PhoneGap project

I am currently developing a data acquisition system using PhoneGap and facing an issue with storing form data temporarily on local storage using JSON. The data should remain visible even after closing and reopening the application by pressing the Get Data ...

Modify the JS/JQuery variable by utilizing the data entered into an HTML input field

I have implemented a javascript/ajax request on my advanced search page. <script> // window.setInterval(function() // { $(function () { var SearchTerm = '<?php echo $_POST['Search']; ...

Expanding the input range slider to fill the entire available space

https://jsfiddle.net/a5gdhmfn/1/ <div> <i>O</i> <input type="range" /> <button/> <div>...</div> </div> Inside a div, there is a range slider along with other fixed-width elements. The issue ...