flex布局中某个行内元素居右并垂直居中

flex布局中某个行内元素居右并垂直居中

  1. 首先父盒子设置成display: flex
  2. 子元素(行内元素)设flex: 1text-align: right
<div class="flex-box">
    <div>1div>
    <div>2div>
    <div>3div>
    <span class="item-right">水平居右并垂直居中span>
  div>
.flex-box {
  display: flex;
  flex-direction: row;
}

.flex-box div {
  width: 100px;
  height: 100px;
  background: #f9f9f9;
  margin-right: 10px;
}

.item-right {
  flex: 1;
  text-align: right;
  align-self: center;
}

完整的HTML如下


<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Documenttitle>
  <style>
    .flex-box {
      display: flex;
      flex-direction: row;
    }

    .flex-box div {
      width: 100px;
      height: 100px;
      background: #f9f9f9;
      margin-right: 10px;
    }

    .item-right {
      flex: 1;
      text-align: right;
      align-self: center;
    }
  style>
head>

<body>
  <div class="flex-box">
    <div>1div>
    <div>2div>
    <div>3div>
    <span class="item-right">水平居右并垂直居中span>
  div>
body>

html>

你可能感兴趣的:(+Frontend,Practices/前端实战)