Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS add space between elements when one is below the other?

Tags:

html

css

I'm trying to figure out how to do this, basically I have two elements.

<div class="button">This is the first one</div>
<div class="button">This is the second one</div>

Sometimes both fit next to each other, in which case I'm ok with their styling.

enter image description here

However if the second one "falls down" due to the parent being too narrow I don't want the two elements to touch each other.

enter image description here

My problem is that adding a bottom-margin to the elements would also affect Case 1 (when they are next to each other) which I don't want to do. How can I separate the two elements when one is below the other?

Jsfiddle (try expanding and shrinking the result box)

like image 781
lisovaccaro Avatar asked May 25 '26 00:05

lisovaccaro


1 Answers

A very simple solution would be using negative padding values, but since it's impossible, we can use another simple way, even though it's a bit of a walk around. You can wrap the container with another wrapping element, and give the container a negative margin-top value.

jsFiddle Demo

HTML:

<div class="wrapper">
    <div class="container">
        <div class="button">This is the first button</div>
        <div class="button">This is the second button</div>
    </div>
</div>

CSS:

.wrapper {
    overflow: hidden;
}
.container {
    margin-top: -10px;
}
.button {
    margin-right: 10px;
    margin-top: 10px;
}
like image 94
Itay Avatar answered May 28 '26 00:05

Itay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!