Set CSS 100% Width Minus Padding And Margin
来源:互联网 发布:android 淘宝首页 编辑:程序博客网 时间:2024/05/05 23:49
Set CSS 100% Width Minus Padding And Margin
Styling elements to fluid layout when its parent element has fixed padding or margin always prompts the question: How do I set the width of the child element to 100% minus its parent’s padding and margin?
You can solve it with CSS box-sizing
or with play with parent/child padding without having to add extra wrapping elements. The latter approach is preferable since not all current popular browsers support it.
Set Child Elements To 100%
In this example, a parent form has a fixed margin of .6em
, we want the form’s child elements (such as label or input fields) to be fluid. So we set the width of these elements to be 100%
.
Add Padding to Parent
Yikes, the input boxes with 100%
width are now overflowing the parent form container. We want the form child elements’ width to be 100% width minus 1.2em (two .6em)
paddings. We could use box-sizing
, but that would complicate things with browser support. The easiest way is to add the 1.2em
padding value of to the parent element.
This makes all the child elements’ width 100% minus padding and margin.
Add Padding to Some Child Elements
Not all child elements want to have the padding, such as our form heading. It now has 1.2em
of white space on the right. No worries, we can add .6em
padding on the heading element since CSS box model’s visual width is width + padding + border
. This brings us to our final result:
参考:http://www.ikelin.com/set-css-width-100-percent-minus-padding-and-margin/
- Set CSS 100% Width Minus Padding And Margin
- CSS 之 width padding margin border
- css中行内元素padding,margin,width,height说明
- Fuck CSS (2) Width, Height, Padding, Border, Margin关系
- css中行内元素padding,margin,width,height说明
- 【CSS】padding,border,margin与width宽度的关系
- 【CSS】padding,border,margin与width宽度的关系
- CSS 100% height with padding/margin
- CSS: margin,padding详解
- css padding margin初始化
- CSS Border & Padding & Margin
- margin-padding-css
- CSS padding margin border
- css margin,padding 百分比
- CSS:Margin和Padding
- CSS 百分比 margin & padding
- CSS margin和padding
- CSS-border,padding,margin
- C语言的const关键字与指针搭配使用
- 关于android内的invalidate()函数
- Eclipse Cocos2d-x 开发自动管理
- Linux学习总结(2)
- 基于C#的序列分类器:Part I:隐马尔科夫模型
- Set CSS 100% Width Minus Padding And Margin
- 转帖:AI会议的总结(by南大周志华)
- Design Pattern: Abstract Factory 模式
- cocos2d-x读文件
- java的定时器使用方法
- URL中文传值出现乱码处理方法
- 数组和链表的优缺点
- 面向对象(中)
- 2013-7-1 产品经理笔记