Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why css margins don't work?

Tags:

html

css

I have been struggling with this annoying piece of code. You'd think I'd had enough practice with css, but as always, it is temperamental with me.

My problem is as follows, I have the following css:

.FORM ul li label {
    margin-top: 50px;    //<--------------THE PROBLEM
    height: 20px;
    max-height: 20px;
    width: 100px;
    min-width: 100px;
}
.FORM ul li {
    list-style: none;
    width: 500px;
    height: 100px;
    min-width: 500px;
    min-height: 100px;
    background: #ddd;
    border-top: #eee 1px solid;
    border-bottom: #bbb 1px solid;
    padding: 10px 10px;
    margin: auto;
}
ul {
    background: #ccc;
    padding: 10px 10px 10px 10px;
    width: 530px;
    margin: auto;
}
body {
    background: #cfc;
    padding: 0px;
    margin: 0px;
}
.FORM {
    background: #fcc;
}

the html it controls is:

<form class="FORM">
    <ul>
        <li>
            <label for="workersAddr">Worker's Address:</label>
            <input type='text' id='workersAddr' class='validate[required,minSize[5]]'/>
        </li>
    </ul>
</form>

notice how in the image below the margin-top: 50px; have no effect at all?

enter image description here

how do I solve this issue?

like image 774
vvMINOvv Avatar asked Apr 04 '11 10:04

vvMINOvv


People also ask

Why is margin not working in CSS?

This happens because the div has no border, and no padding defined, and the CSS specification states to collapse the two top margins into a single one of the parent. (The same happens with bottom margins.)

Why is margin not collapsing?

The margin never collapses when the elements are aligned in flexbox or grid box, or they are not in-flow e.g. absolutely positioned or floated.

Why margin-right is not working on table?

margin-right will not work if you set width to 100%. What you could do is : wrap table in a div tag.


2 Answers

Vertical margins and paddings only have effect in block-level elements and <label> is an inline element. You can either emulate it with other properties or convert into an inline-block:

.FORM ul li label {
    display: inline-block;
}
like image 163
Álvaro González Avatar answered Oct 21 '22 01:10

Álvaro González


Use the line-height css attribute on the label. This will not increase the height of any visible background on the label, but will allow you to effectively add a margin.

like image 28
JSideris Avatar answered Oct 21 '22 01:10

JSideris