Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Clear understanding

I have defined a simple form to learn about clear. I am surprised the 'Submit' button is not going to the next line. My understanding of clear:both is that there should be no floated element to the left or right of the element to which clear is applied. Given this definition, I was expecting Submit to move the last line since I have applied clear to input and label.

can someone pls explain why this is not working? Pls note my goal is to understand where my understanding is flawed and not how to bring the Submit button to the next linec

label {
  color: blue;
  float: left;
  margin-right: 2px;
  clear: left;
  width: 3em;
}

input {
  border: 2px black solid;
  float: left;
  width: 10em;
}

button {
  clear: both;
  margin-left: auto;
  margin-right: auto;
}
<form action="#">
  <fieldset>
    <label>Name </label>
    <input type="text" value="Enter name" />

    <label>Phone </label>
    <input type="text" value="Enter phone" />

    <button type="button">Submit </button>
  </fieldset>
</form>
like image 221
TimeToCodeTheRoad Avatar asked Sep 26 '26 18:09

TimeToCodeTheRoad


1 Answers

9.5.2 Controlling flow next to floats: the 'clear' property

Applies to: block-level elements

Button, is by default, an inline level element, not a block level element. To make clear apply, give it display:block;

label {

    color: blue;
    float: left;
    margin-right: 2px;
    clear: left;
    width: 3em;
}

input {

    border: 2px black solid;
    float: left;
    width: 10em;
}

button {
    display:block;
    clear: both;
    margin-left: auto;
    margin-right: auto;
}
<form action="#">
        <fieldset>
                <label>Name </label>
                <input type="text" value="Enter name"/>
                <label>Phone </label>
                <input type="text" value="Enter phone"/>
                <button type="button">Submit </button>
       </fieldset>
</form>
like image 150
Alohci Avatar answered Sep 28 '26 07:09

Alohci



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!