Flexible layout (Flexbox) is becoming increasingly popular and more and more people are using it because it is so convenient to write CSS layouts. Three years ago, I wrote an introduction to Flexbox (above, below), but it was a little unclear. Today, I saw a tutorial and realized that a simple form can explain Flexbox, and the content is very practical. Below, you only need 10 minutes to learn simple form layout. 1. <form> element Forms use <form></form> Above is an empty form. According to the HTML standard, it is a block-level element and will take up the full width by default, but will have a height of 0 since it has no content. 2. Form Controls Now, add the two most commonly used form controls. <form> <input type="email" name="email"> <button type="submit">Send</button> </form> In the code above, the form contains an input box ( According to the standard, both controls are inline-block elements, that is, they are placed side by side on a line by default. The above picture shows the default rendering of this form by the browser (except for the color). You can see that there is a 3-4 pixel interval between the two controls, which is specified by the browser's built-in style. 3. Specifying Flexbox layout Next, specify that the form should use Flexbox layout. form { display: flex; } As you can see, the space between the two controls disappears because elastic layout items have no space by default. 4. flex-grow attribute Two places are worth noting. (1) The width of the two control elements did not change because the elastic layout does not change the width of the items by default. (2) The elastic layout is left-aligned by default, so the two controls will be arranged from the beginning of the row. If we want the input box to occupy all the remaining width of the current line, we only need to specify input { flex-grow: 1; } In the image above, the button width has not changed, but the input box has become wider, equal to the width of the current row minus the width of the button. 5. align-items property Let's make a slight change and insert an image into the button. <form action="#"> <input type="email" placeholder="Enter your email"> <button type="button"><svg> <!-- a smiley icon --> </svg></button> </form> After the button was inserted into the image, its height changed and became taller. At this time, a very wonderful thing happened. In the picture above, the button becomes taller, and the input box automatically becomes the same height! As mentioned earlier, the elastic layout does not change the width of the item by default, but it changes the height of the item by default. If an item does not explicitly specify a height, it will take up the entire height of the container. In this example, the button becomes taller, causing the form element to also become taller, causing the input box to automatically stretch in height. input { flex-grow: 1; align-self: center; }
If there are many items, it is troublesome to set form { display: flex; align-items: center; } In the above code, after setting The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: SQL group by to remove duplicates and sort by other fields
>>: How to deploy Oracle using Docker on Mac
This article shares with you the specific code of...
Golden Rules of Performance: Only 10% to 20% of e...
Table of contents Preface: 1. Default value relat...
<br />In the first section of this series, w...
Table of contents nextTick v-model syntax sugar ....
Preface: I used the official nginx proxy_cache as...
Navigation and other things are often used in dai...
I believe that many users who make websites will ...
Experimental environment: MYSQL 5.7.22 Enable bin...
<br />For every ten thousand people who answ...
Table of contents Preface Related Materials Vue p...
The tutorial for installing OpenStack Ussuri with...
Table of contents 1. The simplest example 2. Cust...
Yesterday, I helped someone install a system and ...
Table of contents 1. View the tables in the curre...