css3的盒子模型布局


写在前面的话:

css3盒子布局,可以更简单直观的均分页面某一个div,并且还可以达到均分后的每一个元素中的内容上下居中、左右居中,但是该模型对浏览器的版本有要求:

{
display: -webkit-box; /* Firefox 30+,Chrome 4+, Safari 3.1, iOS Safari 3.2+ */
display: -moz-box; /* Firefox 17- */
display: -webkit-flex; /* Chrome 21+, Safari 6.1+, iOS Safari 7+, Opera 15/16 */
display: -moz-flex; /* Firefox 18+ */
display: -ms-flexbox; /* IE 10 */
display: flex; /* Chrome 29+, Firefox 22+, IE 11+, Opera 12.1/17/18, Android 4.4+ */
}
建议使用display: flex;因为display: -webkit-box;除了ie外都支持,但是在火狐和chrome上对该样式的解析又不一样,容易导致火狐和chrome样式混乱,并且火狐和chorme都出现盒子中的元素会改变盒子布局的长宽比,要不然就需要写两种样式display: -webkit-flexdisplay: -moz-flex;但这会导致写很多无意义的代码,所以也不建议使用,如果不针对低版本的ie开发,那么display: flex;无疑是更好的选择
效果1:
效果2:
代码:
  1 <style>
  2         #Settings-div {
  3             width: 100%;
  4             height: 500px;/*开启盒子模型的框必须写长宽*/
  5             background-color: #fff;
  6             display: flex;/*开启盒子布局*/
  7             flex-flow: column;/*竖向布局,指width不变height改变*/
  8             /* display: -webkit-box;
  9             -webkit-box-orient: vertical//竖向布局*/
 10         }
 11 
 12         #Settings-header {
 13             width: 100%;
 14             height: 2.5em;/*如果不想按比例分配长宽可以自己写固定样式*/
 15             line-height: 2.5em;
 16             text-align: center;
 17             font-size: 1.5em;
 18             border-bottom: 1px solid #e5e5e5;
 19             background-image: -moz-linear-gradient(top,#ffffff,#f7f7f7);
 20         }
 21 
 22         #Content {
 23             flex: 1;
 24             display: flex;
 25             flex-flow: row;/*横向布局,如果是display: -webkit-box;则不用写布局方式,因为浏览器均默认为横向*/
 26         }
 27 
 28         #Settings-list {
 29             flex: 1;
 30             display: flex;
 31             height: 100%;
 32             flex-flow: column;
 33         }
 34 
 35         #Settings-content {
 36             flex: 3;
 37             height: 100%;
 38         }
 39 
 40         #Settings-list div {
 41             /*display: -webkit-box;//声明盒子模型下的所有div都按盒子模型布局
 42             -webkit-box-align: center;//上下居中
 43             -webkit-box-pack: center;//左右居中*/
 44             display: flex;
 45             justify-content: center;/*左右居中*/
 46             align-items: center;/*上下居中*/
 47         }
 48 
 49         .list-title {
 50             flex: 1;/*不单单是id可以写class也可以*/
 51             background-image: -moz-linear-gradient(top,#ffffff,#f7f7f7);/*页面元素背景从上到下颜色渐变,怕你们看不懂,虽然与主题无关-moz是只能火狐识别的单独样式,-webkit是chrome、Safari、Opera为主的浏览器识别的样式  */
 52             font-size: 1.2em;
 53             border-right: 1px solid #e5e5e5;
 54         }
 55 
 56         .list-content {
 57             flex: 1;
 58             background-color: #f7f7f7;
 59             border-top: 1px #e5e5e5 solid;
 60             cursor: pointer;
 61             border-right: 1px solid #e5e5e5;
 62         }
 63 
 64         .action {
 65             border-left: 5px solid #0094ff;
 66         }
 67 
 68         .select {
 69             background-color: #fff;
 70             border-right: 0px;
 71         }
 72 
 73         #Content-one {
 74             width: 100%;
 75             height: 100%;
 76         }
 77 
 78         #Content-two {
 79             width: 100%;
 80             height: 100%;
 81         }
 82 
 83         #Content-twodiv {
 84             width: 100%;
 85             height: 100%;
 86             display: flex;
 87             flex-flow: column;
 88         }
 89      /*由于没有给#Content-twodiv底下的div写布局模式,所以底下的div中的元素没用居中*/
 90         #Content-two1 {
 91             flex: 9;
 92             width: 100%;
 93             display: flex;
 94             flex-flow: row;
 95         }
 96 
 97         #Content-two2 {
 98             flex: 1;
 99         }
100 
101         #Content-three {
102             display: none;
103         }
104 
105         #form1 {
106             width: 100%;
107             height: 100%;
108             display: -webkit-box;/*另一种要写很多无意义的代码的写法*/
109             display: -moz-box;
110             -webkit-box-orient: vertical;
111             -moz-box-orient: vertical;
112         }
113      /*完全无意的浪费时间写的样式,实在是不推荐*/ 
114             #form1 div {
115                 display: -webkit-box;
116                 -webkit-box-pack: center;
117                 -webkit-box-align: center;
118                 display: -moz-box;
119                 -moz-box-align: center;
120                 -moz-box-pack: center;
121             }
122 
123         #form1-header {
124             width: 100%;
125             height: 50px;
126             font-size: 1.3em;
127             border-bottom: 1px solid #e5e5e5;
128         }
129 
130         #table {
131             width: 100%;
132             table-layout: auto;
133             margin-top: 10px;
134         }
135 
136             #table td {
137                 width: 50%;
138                 height: 40px;
139                 line-height: 40px;
140             }
141 
142         .table-left {
143             text-align: right;
144             padding-right: 20px;
145         }
146 
147         .table-right {
148             padding-left: 20px;
149         }
150 
151         #save1 {
152             background-color: #aa56ff;
153             color: #ffffff;
154             width: 80px;
155             height: 35px;
156             border: 0px;
157             box-shadow: 2px 4px 6px #000;
158             cursor: pointer;
159         }
160 
161         #save2 {
162             background-color: #aa56ff;
163             color: #ffffff;
164             width: 80px;
165             height: 35px;
166             border: 0px;
167             box-shadow: 2px 4px 6px #000;
168             cursor: pointer;
169         }
170 
171         #age {
172             width: 25px;
173             border-color: #d8d8d8 #e5e5e5 #e5e5e5 #d8d8d8;
174             border-style: solid;
175             border-width: 1px;
176         }
177 
178         .table-right select {
179             width: 100px;
180             height: 30px;
181             border-color: #d8d8d8 #e5e5e5 #e5e5e5 #d8d8d8;
182             border-style: solid;
183             border-width: 1px;
184         }
185     </style>
186 <div id="Settings-div">
187     <div id="Settings-header">
188         <span>个人设置</span>
189     </div>
190     <div id="Content">
191         <div id="Settings-list">
192             <div class="list-title action">
193                 <img src="~/images/user.png" />
194                 <span>个人资料</span>
195             </div>
196             <div class="list-content select" data-leve="1">
197                 <span>基本资料</span>
198             </div>
199             <div class="list-content" data-leve="2" style="border-bottom:1px #e5e5e5 solid ;">
200                 <span>头像设置</span>
201             </div>
202             <div class="list-title">
203                 <img src="~/images/mi.png" />
204                 <span>隐私设置</span>
205             </div>
206             <div class="list-content" style="border-top:1px #e5e5e5 solid ;" data-leve="3">
207                 <span>修改密码</span>
208             </div>
209         </div>
210         <div id="Settings-content">
211             <div id="Content-one" class="content">
212                 <form id="form1">
213                     <div id="form1-header">
214                         <span>基本资料</span>
215                     </div>
216                     <table id="table">
217                         <tr>
218                             <td class="table-left">性别:</td>
219                             <td class="table-right">
220                                 <input type="radio" name="sex" value="" checked />221                                 <input type="radio" name="sex" value="" />222                             </td>
223                         </tr>
224                         <tr>
225                             <td class="table-left">年龄:</td>
226                             <td class="table-right"><input type="text" name="age" id="age" value="" />岁</td>
227                         </tr>
228                         <tr>
229                             <td class="table-left">居住地:</td>
230                             <td class="table-right">
231                                 <select>
232                                     <option value="0">请选择</option>
233                                 </select>
234                                 <select>
235                                     <option value="0">请选择</option>
236                                 </select>
237                             </td>
238                         </tr>
239                     </table>
240                     <div>
241                         <input type="button" id="save1" value="保存" />
242                     </div>
243                 </form>
244             </div>
245             <div id="Content-two" class="content" style="display:none;">
246                 <div id="Content-twodiv">
247                     <div id="Content-two1">
248                         <div style="flex:2;background-color:#e2e2e2;">
249 
250                         </div>
251                         <div style="width:290px;"></div>
252                     </div>
253                     <div id="Content-two2">
254                         <form id="upload-imgs" action="/FileUpload/FileImages" method="post" enctype="multipart/form-data">
255                             <input type="button" value="上传图片" style="" />
256                             <input type="file" name="img" value="" style="opacity:0; margin-left:-80px;width:80px;cursor:pointer;" onchange="uploadFile(this)" />
257                         </form>
258                     </div>
259                 </div>
260             </div>
261             <div id="Content-three" class="content" style="display:none;">
262 
263             </div>
264         </div>
265     </div>
266 
267 </div>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM