水平屬性

7大屬性:margin-left、border-left、padding-left、width、padding-right、border-right、margin-right 這7個屬性的值加在一塊兒必須是元素包含塊的寬度,也就是元素父級元素的widthide

這7個屬性中,只有3個屬性能夠設置爲auto:元素內容width,以及左、右外邊距,其他屬性必須設定爲特定的值,或者默認寬度爲0code

width必須設置爲auto或者某種類型的非負值。圖片

假設:父元素的寬是400pxit

一、那麼左外邊距(margin-left)就是200pxim

p{margin-left:auto; margin-right:100px; width:100px;}

二、那麼margin-right會強制爲auto(只對從左向右讀的語言),使其總和加起來爲400pxmargin

p{margin-left:100px; margin-right:100px; width:100px;}

三、自動寬度img

p{margin-left:100px; margin-right:100px; width:auto;}

也就等價於下面語言

p{margin-left:100px; margin-right:100px;}

四、會自動給左右邊距設置相等的長度,將元素在其父元素中居中di

p{margin-left:auto; margin-right:auto; width:100px;}

五、若是width爲auto,那麼設爲auto的外邊距會減爲0co

p{margin-left:auto; margin-right:100px; width:auto;}

六、外邊距都減爲0

p{margin-left:auto; margin-right:auto; width:auto;}

七、負外邊距

div{width:400px; border:3px solid black;}
p.wide{margin-left:10px; width:auto; margin-right:-50px;}

輸入圖片說明

相關文章
相關標籤/搜索