*新闻详情页*/>
1. 父级器皿设定成报表,子级设为行内元素。
合适子级內容为文字展现。
<!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; display: table-cell; /* 变化成报表 */ text-align: center; /* 水平 */ vertical-align: middle; /* 竖直 */ } #child { background-color: blue; color: white; display: inline; /* 子元素设定为行内或行内块 */ } </style> <!-- html --> <div id="parent"> <div id="child">內容</div> </div>
2. 父级器皿设定相对性精准定位,子级设定肯定精准定位后根据外边距垂直居中。
<!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; position: relative; /* 设定相对性精准定位 */ } #child { height: 50px; width: 50px; color: white; background-color: blue; /* 肯定精准定位,4 个方位设定为 0 后,margin 设为 auto */ position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div>
3. 父级器皿设定为延展性盒,子级设定外边距。
<!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; display: flex; /* 父元素变换为延展性盒 */ display: -webkit-flex; /* Safari */ } #child { height: 50px; width: 50px; background-color: blue; margin: auto; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div>
4. 父级器皿设定相对性精准定位,子级设定肯定精准定位,左侧距和上边距设定负1半宽度。
合适子级的宽高固定不动的状况。
<!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; position: relative; /* 设定相对性精准定位 */ } #child { /* 子元素已知本身宽高的状况下 */ background-color: blue; width: 50px; height: 50px; margin-top: ⑵5px; margin-left: ⑵5px; position: absolute; left: 50%; top: 50%; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div>
5. 父级器皿设定相对性精准定位,子级设定肯定精准定位,根据形变特性设定水平静竖直方位负1半。
合适子级的宽高不固定不动的状况。
<!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; position: relative; /* 设定相对性精准定位 */ } #child { /* 子元素未知自身的宽高,应用 transform 的 translate */ border: 1px solid blue; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(⑸0%,⑸0%); -ms-transform: translate(⑸0%,⑸0%); -o-transform: translate(⑸0%,⑸0%); transform: translate(⑸0%,⑸0%); } </style> <!-- html --> <div id="parent"> <div id="child"> <div id="content"> 內容1 <br/> 內容2 </div> </div> </div>
6. 父级设定为延展性盒,设定对齐特性。
<!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; display: flex; /* 父元素变换为延展性盒 */ display: -webkit-flex; /* Safari */ justify-content: center;/* 水平 */ align-items: center; /* 竖直 */ } #child { height: 50px; width: 50px; background-color: blue; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div>
以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。
Copyright © 2002-2020 html网页制作_php网页制作_网页设计稿_网页编辑工具_学生网页设计模板 版权所有 (网站地图) 粤ICP备10235580号