October 8, 201313 yr I came across this problem and dont know why is it happening and hoping that someone can explain ti to me I have this two elements <div class"row"> <div class"box"> </div> </div> .row { margin:0 auto; width:960px; } .box { width:928px; margin:0 16px 0 16px; } ok so now i have two elements and i want "box" to have top margin 50px so i did next .topmargin{ margin-top:50px; } (i did it this way because im using this element called "box" several times and i want a margin top only on some) so here im calling two classes in one div: <div class"row"> <div class"box topmargin"> </div> </div> NOT WORKING!!! but if i do it this way: <div class"row"> <div class"box" style="margin-top:50px;"> </div> </div> WORKING can someone explain to me why isnt first way working, its the same thing i dont understand TY in advance
October 8, 201313 yr Maybe because you haven't wrote the markup correctly: <div class"row"> <div class"box"> </div> </div> should be <div class="row"> <div class="box"> </div> </div> You were missing the '=' sign that tells the DIVs to add the necessary class info Edited October 8, 201313 yr by Lyndsey
October 8, 201313 yr Was just about to say that Lyndsey. What are you seeing on your screen paco? I've pretty much copied your code (edited what Lyndsey has posted above) and added height to both divs and it works fine.
October 8, 201313 yr Author no its not "=" i just forgot to write it here, it was just an example, dummy divs
October 8, 201313 yr Author Was just about to say that Lyndsey. What are you seeing on your screen paco? I've pretty much copied your code (edited what Lyndsey has posted above) and added height to both divs and it works fine. when i put <div class="row"> <div class="box topmargin"> </div> </div> margin top is not applied but when i add style to <div> then its applied
October 8, 201313 yr Have you got a link to it? Have you taken note of what D4Y0 said? Ok sorry, mine was working because I added float: left; to .box - you can see here Here is my code: <!DOCTYPE html> <html lang="en"> <head> <title>Div Top Margin</title> <style> body { margin: 0; padding: 50px 0 0 0; } .row { margin: 0 auto; width: 960px; background: #ccc; height: 150px; } .box { width: 928px; margin: 0 16px; background: #333; height: 50px; float: left; } .topmargin { margin-top: 50px; } </style> </head> <body> <div class="row"> <div class="box topmargin"> </div> </div> </body> </html> Edited October 8, 201313 yr by mrludable
October 8, 201313 yr Its margin collapse add padding will fix .row { margin: 0 auto; width: 960px; background: #ccc; height: 149px; padding:1px 0 0; } .box { width: 928px; margin: 0 16px; background: #333; height: 50px; } .topmargin { margin-top: 50px; }
October 9, 201313 yr Collapsing margins can be a nuisance and difficult to understand. Here's a tutorial: http://reference.sitepoint.com/css/collapsingmargins Padding in lieu of margins, as mteam says, can be a fix if you can't sort it out.
October 13, 201313 yr the properties specified in style attribute always overwrite the styles inherited from specific class (if you dont use !important) as for the classes, the definitions further in the code overwrite those specified before them. so, if you write .topmargin { margin-top: 50px; } .box { margin: 0 16px; } the margin-top property from the class .topmargin will be overwritten by margin property specified in .box class. if you still need to have .topmargin before .box in the CSS code, you can make a new definition for .box.topmargin: .box.topmargin { margin-top: 50px; } however, if you need to use .box with .topmargin, you maybe arent using it in the right context and better defining a new class for the boxes with larger top margin
Create an account or sign in to comment