Flexbox(只有chrome和火狐兼容)详解 里的两个eg:

本文介绍了使用Flexbox实现水平竖直居中布局及如何通过flex-flow属性控制子元素的布局方式。通过实例展示了如何利用justify-content属性使子元素均匀分布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flexbox详解 https://segmentfault.com/a/1190000002910324里的两个eg:

eg1:水平竖直居中

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>flexbox</title>
	<style type="text/css">
body{
    padding: 0;
    margin: 0;
}

.parent {
  display: flex;
  height: 300px; /* Or whatever */
  background-color: black;
}

.child {
  width: 100px;  /* Or whatever */
  height: 100px; /* Or whatever */
  margin: auto;  /* Magic! */
  background-color: white;
}
	</style>
</head>
<body>
	<div class="parent"><div class="child"></div></div>
</body>
</html>

095221_e4mc_2444023.png

eg2:新增子元素布局

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>flexbox2</title>
	<style type="text/css">
body{
    margin: 0;
    padding: 0;
}

ul {
    margin: 0;
    padding: 0;
}

li{
    list-style: none;
}

.flex-container {
    /* We first create a flex layout context */
    display: flex;

    /* Then we define the flow direction and if we allow the items to wrap 
    * Remember this is the same as:
    * flex-direction: row;
    * flex-wrap: wrap;
    */
    flex-flow: row wrap;
    /*
flex-flow 属性用于设置或检索弹性盒模型对象的子元素排列方式。
	    flex-flow:<' flex-direction '> || <' flex-wrap '>
		默认值:看各分拆属性
		适用于:flex容器
		继承性:无
		动画性:否
		计算值:指定值

		flex-direction:row;:默认值。灵活的项目将水平显示,正如一个行一样。
		flex-wrap:wrap;规定灵活的项目在必要的时候拆行或拆列。
    _________________________
    <' flex-direction '>:
定义弹性盒子元素的排列方向。
<' flex-wrap '>:
控制flex容器是单行或者多行。

     */
    /* Then we define how is distributed the remaining space */
    justify-content: space-around;
    /*
    justify-content 用于设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式。
    space-around:项目位于各行之前、之间、之后都留有空白的容器内。
     */
    
      display: -webkit-flex; /* Safari */
    -webkit-justify-content: space-around; /* Safari 6.1+ */
}
.flex-item {
    background: tomato;
    padding: 5px;
    width: 200px;
    height: 150px;
    margin-top: 10px;

    line-height: 150px;
    color: white;
    font-weight: bold;
    font-size: 3em;
    text-align: center;
}
	</style>
</head>
<body>
	 <ul class="flex-container">
        <li class="flex-item">1</li>
        <li class="flex-item">2</li>
        <li class="flex-item">3</li>
        <li class="flex-item">4</li>
        <li class="flex-item">5</li>
        <li class="flex-item">6</li>
        <li class="flex-item">5</li>
        <li class="flex-item">6</li>
    </ul>
</body>
</html>

095120_Dmmv_2444023.png

转载于:https://my.oschina.net/u/2444023/blog/741356

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值