Flexbox 最簡單的表單

彈性布局(Flexbox)逐漸流行,越來越多的人開始使用,因為它寫Css布局真是太簡單了一

一、<form>元素

表單使用<form>元素

<form></form>復制代碼

上面是一個空的表單,根據HTML標準,它是一個塊級元素,默認將占據整個寬度。高度為0,因為內部沒有任何的內容。

二、表單控件

現在加入兩個表單控件

<form><input type="email" name="email"/><button type="submit">Send</button>
<form>復制代碼

上面代碼中,表單包含一個輸入框(input)和一個按鈕(button)

根據HTML標準,這兩個都是行內塊級元素(inline-block),也就是說它們默認是在一行顯示的。


上圖是瀏覽器對這個表單默認渲染(顏色除外),可以看到,這個兩個控件之間有3像素~4像素的間隔,這是瀏覽器的內置樣式指定的。

三、指定的Flexbox布局

接著,指定表單使用Flexbox布局

form {display: flex;
}復制代碼

這時在瀏覽器中可以看到兩個控件之間的間距消失了。因為彈性布局的項目(item)默認沒有間距。

四、flex-grow屬性

兩個地方值得注意:

1.兩個控件元素的寬度沒有發生變化,因為彈性布局不會改變項目的寬度。

2.彈性布局默認左對齊,所以兩個控件會從行首開始排列。

如果我們希望,輸入框占據當前行剩余的寬度,值需要指定輸入框的flex-grow屬性為1。

input {flex-grow: 1;
}復制代碼

這時在瀏覽器中可以看到,按鈕的寬度沒有發生變化,但是輸入框變寬了,等于當前行的寬度減去按鈕的寬度。

flex-grow屬性默認值是0,即使用本來的寬度不拉伸。等于1時,就表示該項目寬度拉伸,占據當前行剩余的寬度。

五、align-self 屬性和 align-items 屬性

我們做一點改變,在按鈕里面插入一張圖片。

<form action="#"><input type="email" name="email" /><button type="submit"><svg>  <!-- a smiley icon -->  </svg></button>
</form>復制代碼

按鈕插入圖片后,它的高度發生了變化,變得更高了,這時候發生了一個奇妙的事情。


上圖中按鈕變高了,輸入框也變得一樣高!

前面說過,彈性布局默認不會改變項目的寬高,但是它默認改變項目的高度,如果項目沒有顯示指定高度,就將占據容器的所有高度,本例中,按鈕變高了,導致表單元素也變高了,使得輸入框的高度自動拉伸了。

align-self屬性可以改變這個行為。

input {flex-grow: 1;align-self: center;
}復制代碼


align-self 屬性可取的四個值:

1.flex-start:頂邊對齊,高度不拉伸

2.flex-end:底邊對齊,高度不拉伸

3.center:居中,高度不拉伸

4.stretch:默認值,高度自動拉伸

如果項目很多,一個個地設置align-self屬性很麻煩,這時可以在容器元素(本例是表單)設置align-items屬性,它的值被所有子項目的align-self屬性繼承。

form {display: flex;align-items: center;
}復制代碼

上面代碼中,<form>元素設置了align-items以后,就不用在控件上設置align-self,除非希望兩者的值不一樣。

轉載:http://www.ruanyifeng.com/blog/2018/10/flexbox-form.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389428.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389428.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389428.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CSS中的盒子模型

一.為什么使用CSS 1.有效的傳遞頁面信息 2.使用CSS美化過的頁面文本&#xff0c;使頁面漂亮、美觀&#xff0c;吸引用戶 3.可以很好的突出頁面的主題內容&#xff0c;使用戶第一眼可以看到頁面主要內容 4.具有良好的用戶體驗 二.字體樣式屬性 1.font-family:英…

jdk重啟后步行_向后介紹步行以一種新穎的方式來預測未來

jdk重啟后步行“永遠不要做出預測&#xff0c;尤其是關于未來的預測。” (KK Steincke) (“Never Make Predictions, Especially About the Future.” (K. K. Steincke)) Does this picture portray a horse or a car? 這張照片描繪的是馬還是汽車&#xff1f; How likely is …

PyTorch官方教程中文版:入門強化教程代碼學習

PyTorch之數據加載和處理 from __future__ import print_function, division import os import torch import pandas as pd #用于更容易地進行csv解析 from skimage import io, transform #用于圖像的IO和變換 import numpy as np import matplotlib.pyplot a…

css3-2 CSS3選擇器和文本字體樣式

css3-2 CSS3選擇器和文本字體樣式 一、總結 一句話總結&#xff1a;是要記下來的&#xff0c;記下來可以省很多事。 1、css的基本選擇器中的:first-letter和:first-line是什么意思&#xff1f; :first-letter選擇第一個單詞&#xff0c;:first-line選擇第一行 2、css的偽類選…

mongodb仲裁者_真理的仲裁者

mongodb仲裁者Coming out of college with a background in mathematics, I fell upward into the rapidly growing field of data analytics. It wasn’t until years later that I realized the incredible power that comes with the position. As Uncle Ben told Peter Par…

優化 回歸_使用回歸優化產品價格

優化 回歸應用數據科學 (Applied data science) Price and quantity are two fundamental measures that determine the bottom line of every business, and setting the right price is one of the most important decisions a company can make. Under-pricing hurts the co…

Node.js——異步上傳文件

前臺代碼 submit() {var file this.$refs.fileUpload.files[0];var formData new FormData();formData.append("file", file);formData.append("username", this.username);formData.append("password", this.password);axios.post("http…

用 JavaScript 的方式理解遞歸

原文地址 1. 遞歸是啥? 遞歸概念很簡單&#xff0c;“自己調用自己”&#xff08;下面以函數為例&#xff09;。 在分析遞歸之前&#xff0c;需要了解下 JavaScript 中“壓棧”&#xff08;call stack&#xff09; 概念。 2. 壓棧與出棧 棧是什么&#xff1f;可以理解是在內存…

PyTorch官方教程中文版:Pytorch之圖像篇

微調基于 torchvision 0.3的目標檢測模型 """ 為數據集編寫類 """ import os import numpy as np import torch from PIL import Imageclass PennFudanDataset(object):def __init__(self, root, transforms):self.root rootself.transforms …

大數據數據科學家常用面試題_進行數據科學工作面試

大數據數據科學家常用面試題During my time as a Data Scientist, I had the chance to interview my fair share of candidates for data-related roles. While doing this, I started noticing a pattern: some kinds of (simple) mistakes were overwhelmingly frequent amo…

scrapy模擬模擬點擊_模擬大流行

scrapy模擬模擬點擊復雜系統 (Complex Systems) In our daily life, we encounter many complex systems where individuals are interacting with each other such as the stock market or rush hour traffic. Finding appropriate models for these complex systems may give…

公司想申請網易企業電子郵箱,怎么樣?

不論公司屬于哪個行業&#xff0c;選擇企業郵箱&#xff0c;交互界面友好度、穩定性、安全性都是選擇郵箱所必須考慮的因素。網易企業郵箱郵箱方面已有21年的運營經驗&#xff0c;是國內資歷最高的電子郵箱&#xff0c;在各個方面都非常成熟完善。 從交互界面友好度來看&#x…

莫煩Matplotlib可視化第二章基本使用代碼學習

基本用法 import matplotlib.pyplot as plt import numpy as np""" 2.1基本用法 """ # x np.linspace(-1,1,50) #[-1,1]50個點 # #y 2*x 1 # # y x**2 # plt.plot(x,y) #注意&#xff1a;x,y順序不能反 # plt.show()"""…

vue.js python_使用Python和Vue.js自動化報告過程

vue.js pythonIf your organization does not have a data visualization solution like Tableau or PowerBI nor means to host a server to deploy open source solutions like Dash then you are probably stuck doing reports with Excel or exporting your notebooks.如果…

plsql中導入csvs_在命令行中使用sql分析csvs

plsql中導入csvsIf you are familiar with coding in SQL, there is a strong chance you do it in PgAdmin, MySQL, BigQuery, SQL Server, etc. But there are times you just want to use your SQL skills for quick analysis on a small/medium sized dataset.如果您熟悉SQ…

第十八篇 Linux環境下常用軟件安裝和使用指南

提醒&#xff1a;如果之后要安裝virtualenvwrapper的話&#xff0c;可以直接跳到安裝virtualenvwrapper的方法&#xff0c;而不需要先安裝好virtualenv安裝virtualenv和生成虛擬環境安裝virtualenv&#xff1a;yum -y install python-virtualenv生成虛擬環境&#xff1a;先切換…

莫煩Matplotlib可視化第三章畫圖種類代碼學習

3.1散點圖 import matplotlib.pyplot as plt import numpy as npn 1024 X np.random.normal(0,1,n) Y np.random.normal(0,1,n) T np.arctan2(Y,X) #用于計算顏色plt.scatter(X,Y,s75,cT,alpha0.5)#alpha是透明度 #plt.scatter(np.arange(5),np.arange(5)) #一條線的散點…

計算機科學必讀書籍_5篇關于數據科學家的產品分類必讀文章

計算機科學必讀書籍Product categorization/product classification is the organization of products into their respective departments or categories. As well, a large part of the process is the design of the product taxonomy as a whole.產品分類/產品分類是將產品…

es6解決回調地獄問題

本文摘抄自阮一峰老師的 http://es6.ruanyifeng.com/#docs/generator-async 異步 所謂"異步"&#xff0c;簡單說就是一個任務不是連續完成的&#xff0c;可以理解成該任務被人為分成兩段&#xff0c;先執行第一段&#xff0c;然后轉而執行其他任務&#xff0c;等做好…

交替最小二乘矩陣分解_使用交替最小二乘矩陣分解與pyspark建立推薦系統

交替最小二乘矩陣分解pyspark上的動手推薦系統 (Hands-on recommender system on pyspark) Recommender System is an information filtering tool that seeks to predict which product a user will like, and based on that, recommends a few products to the users. For ex…