Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Margin top of first span not working [duplicate]

Tags:

html

css

margin

<div class ="cards">
  <span id="cardtitle">
    Chance 
  </span>
  <span id="cardinfo">
    Your ass is going to jail.
  </span>
</div>
.cards{
  background: #F8F8F8 ;
  height: 100px;
  margin: 0 auto;
  width: 200px;
}
#cardtitle, #cardinfo{
  background: #ffcc00;
  display: block;
  margin-top: 10px;
  width: 100px;
}

Okay the margin-top on #cardinfo works but #cardtitle doesn't. The problem seems to be the first element, as the problem reverses if i reverese the 2 spans.

like image 228
Elton Frederik Avatar asked Oct 27 '25 12:10

Elton Frederik


1 Answers

The issue you are facing, is called collapsed margin, hence either you can use position: relative; with top set to 10px or use overflow: auto; on the parent element.

Demo (Using overflow: auto;)

Demo 2 (Using position: relative; + top: 10px; and top: 20px;)

position: relative; method will require you to set the top separately as it will move your element, though it physically reserves the space, hence you will have to double up for the second one..

like image 105
Mr. Alien Avatar answered Oct 29 '25 02:10

Mr. Alien



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!