CSS中如何使用負margin值來調整置中位置

來源:互聯網
上載者:User
這篇文章主要介紹了CSS中使用負margin值來調整置中位置的方法,文中同時提到了這種常用方法的一些值得注意的地方,需要的朋友可以參考下

這或許是最常用的置中方法。如果知道了各個元素的大小,設定等於寬高一半大小的負margin值(如果沒有使用box-sizing: border-box樣式,還需要加上padding值),再配合top: 50%; left: 50%;樣式就會使塊元素置中。

需要注意的是,這是按照預想情況也能在工作在IE6-7下的唯一方法。

.is-Negative {               width: 300px;               height: 200px;               padding: 20px;               position: absolute;               top: 50%; left: 50%;               margin-left: -170px; /* (width + padding)/2 */        margin-top: -120px; /* (height + padding)/2 */}

  好處:

瀏覽器安全色性非常好,甚至支援IE6-7
需要的編碼量很少

  同時注意:

這是個非響應式的方法,不能使用百分比的大小,也不能設定min-/max-的最大值最小值。
內容可能會超出容器
需要為padding預留空間,或者需要使用box-sizing: border-box樣式。

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.