Creating space between elements in CSS div elements

I've come across numerous resources on this topic that have already been answered, but none of them have provided a satisfactory solution.

Here's the scenario:

I have created a basic fiddle showcasing the desired behavior:

@ See fidlle

If I have 3 cells, I want spacing only between each div. Is it possible to achieve this using just CSS?

[HTML]

<div class="cel-1-2">
   Max Size of Objects in Cache (Shared Memory)
    <div class="cel-1-2">
        <input type="text" placeholder="foo" />
    </div>
    <div class="cel-1-2">
        <select>
            <option value="foo">bar</option>
        </select>
    </div>
</div>
<div class="cel-1-2">
    foobar
</div>

[CSS]

[class*=cel-] {
   padding-right:10px;
   -moz-box-sizing: border-box; 
   -webkit-box-sizing: border-box;     
   box-sizing: border-box;
    background: blue;
}

.cel-1-2 {
    width: 50%;
    float: left;
}

input, select {
    width: 100%;
    float: left;
}

To clarify further:

This is what I am looking for

==>Not this!

Answer №1

Not sure what the problem is here. You can easily achieve this using CSS:

.container {
   width: 100%;
   background-color: blue;
    padding: 2em; 
    height: 600px;
}
.item {
    width: 40%;
    height: 300px;
    background-color: pink;
    float: left;
}
.item + .item {
    margin-left: 10% /* Pro Tip */
}

Check out a live demo here: http://jsfiddle.net/abc123/

Answer №2

In my opinion, a good way to handle this issue is by adding a margin-left to all child elements and then specifically resetting the margin for the first child.
http://jsfiddle.net/abc123/4/

.child {    
    display: inline-block;
    margin-left: 10px;
}

.child:first-child {
    margin-left: 0;
}

Answer №3

Just dipping my toes into the world of web design, but if I understood your question correctly, here are a few suggestions:

1) Utilize bootstrap to align those divs on the same line - bootstrap will take care of the spacing automatically.

2) Avoid manually adjusting divs with the margin property, as it's not the best practice.

P.S.: I wanted to ask for some clarification before responding, but I don't quite have enough reputation yet. If this isn't relevant, feel free to disregard and I'll work on providing a more fitting answer.

Answer №4

If you're looking to build a website, my strong suggestion is to utilize twitter bootstrap. Not only is it simple and robust, but it is also responsive. I've tried implementing it into your code like so:

<html>
    <head>
        <title>Student Program Management</title>
        <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css" rel="stylesheet">
    </head>
    <body>
        <div class="container">
            <div class="row">
                <p>Max Size of Objects in Cache (Shared Memory)</p>
                <div class="col-md-4 ">
                    <input type="text" placeholder="foo" />
                </div>
                <div class="col-md-4">
                    <select>
                        <option value="foo">bar</option>
                    </select>
                </div>
                <div class="col-md-4">
                    foobar
                </div>
            </div>
        </div>
    </body>
</html>

While there are other methods using simple CSS, they can often be more cumbersome than this approach. Bootstrap relies on a Grid Layout system, where the layout of your site is structured into rows (using the "row" class attribute in div elements) as desired. Within each row, columns can be added (div with the class attribute "col-md-4", for example). Each row consists of 12 columns, so in this case, I have divided it into 3 rows with one column each, resulting in every div having the class "col-md-4" because 12/3=4. This ensures uniform width for all divs. While this may not be exactly what you had in mind, utilizing frameworks like bootstrap proves to be more efficient compared to plain CSS. I highly recommend considering this shift from plain CSS when possible. For more information, visit: http://getbootstrap.com/css/#grid

Answer №5

Assign a fresh class to the center div and apply appropriate styles.

Here's a sample:

.CenteredBox{
    padding: 20px;
    background-color: #f0f0f0;
}

I trust this meets your query, although I may not have grasped it entirely.

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

Unexpected behavior observed with LitHTML when binding value to input type range

Currently, I am working on an implementation that involves using range inputs. Specifically, I have two range inputs and I am trying to create a 'double range' functionality with them. The challenge I am facing is related to preventing one slider ...

Setting Image Height with CSS

I have a div with a height of 105px. The image inside the div is 359px tall. How can I adjust the div's size to prevent cutting off the image and ensure it displays in full height? Thank you for your help! ...

Tips on choosing additional (sibling) elements with CSS?

My navbar has the following structure: <ul class="nav navbar-nav"> <li class="nav-item"><a href="#services">Services</a></li> <li class="nav-item"><a href="#work">Work</a></li> <li class ...

The expansion animation for the Nextjs/React accordion box did not work as expected when utilizing tailwindcss

I am currently working on creating an animation for a collapsible box (accordion). My goal is to have the child component initially hidden with display:none. When I hover over the parent component, the child should be revealed and the dimensions of the pa ...

Is there a way to implement a sticky footer on just a single webpage if that's all I need?

On my main page, I have an empty div with only a background image. To prevent it from collapsing, I created a sticky footer using the following code: * { margin:0; padding:0; } html, body, #wrapper{ height: 100%; } body &g ...

Is there a way for me to determine the total duration of a testcase's execution?

What is the most useful command/target to use from beginning to end of a testcase in Selenium IDE - HTML? Could you provide an example? ...

I would appreciate your assistance in the modification of the input type from text to radio within the PHP code

I've been struggling to update the input type from text to a radio button for Gender selection in my Wordpress theme's user profile settings. I can't seem to get it right in the code and I'm not sure how to make the change. I tried usi ...

Tips for maintaining the particles.js interaction while ensuring it stays under other elements

I have incorporated particle.js as a background element. By adjusting the z-index, I successfully positioned it in the background. While exploring solutions on the Github issues page, I came across a suggestion involving the z-index and this code snippet: ...

Tips for choosing and deselecting data using jQuery

Is there a way to toggle the selection of data in my code? Currently, when I click on the data it gets selected and a tick image appears. However, I want it so that when I click again on the same data, the tick will disappear. How can I achieve this func ...

Using HTML5 to display the {{data}} extracted from a JSON file right in the center of the text

Can someone help me with a question about working with html and angular5? <span [innerHTML]="'client.acceptance.explanation'| translate"></span> <span><b>{{data}}</b></span> I have text stored in a json file ...

Attempting to arrange all the images in a horizontal display rather than a vertical one

This is my first ever question on the forum sorry if it is unprofessional hopefully i can learn from my mistakes on this post!! I put the question in the title but if you have any questions feel free to ask below THANK YOU! for the help! My Code ...

Is it possible to display or hide a spinner within a span using Javascript and AJAX?

When submitting a contact form, I want to display and hide a spinner on the submit button. The spinner should be visible while the form is being submitted and hidden once the submission is complete. Below is the code for my submit button with the spinner: ...

"Seamless responsiveness in design with jQuery, but encountering compatibility issues

My usage of jQuery is quite basic to ensure that elements are properly positioned when they are moved: function ipad() { var width = jQuery(window).width(); if (width < 1200 && width >= 768){ //if tablet jQuery('.mobbut').css(&apo ...

Tips for choosing elements in JavaScript using querySelector even after they've been included in the code using innerHTML

Within the scenario below, a parent element is present in the HTML code and the span element with a class of 'child' is nested within the parent element using the createChild function. Subsequently, the content of the child element is modified el ...

Clicking within the text activates the dropdown menu, but clicking outside the text does not

My custom drop down menu is not functioning properly. When I click on the text, it successfully links to another place, but when I click beside the text, it does not link. Can you please help me identify what's wrong here? Your assistance would be gre ...

The sup tag does not function properly when used within the title tag

I added a sup tag inside a title tag but it doesn't seem to work. Surprisingly, the same sup tag works perfectly fine within li tags. Can anyone explain why this is happening? I tried changing the title tags to H class tags but I'm still curious ...

Strange behaviors when using setinterval with classes

Currently working on enhancing a slider functionality, and I have observed that everything is functioning smoothly - function Slider(element) { this.i = 0; this.element = element; var self = this; this.timer = window.setInterval(functi ...

Achieving a seamless scrolling effect using CSS

I recently completed the design of a basic website that includes both mobile and desktop versions. However, I am looking to incorporate some animation into the site to enhance its appearance. One specific feature is a "go up" link at the bottom of the mob ...

Develop a dynamic progress bar using jQuery

I am having trouble creating a percentage bar using jquery and css. The code I have written is not working as expected, particularly the use of jquery css({}). Here is the code snippet: *'width' : width - seems to be causing issues for me / ...

The CSS3 Animation remains stationary

I've been attempting to animate my block element moving to the left using CSS animation shorthand property, but unfortunately, it's not working as expected. Here is my HTML: <div id="game"> <div id="block">&l ...