0

How to bring two elements to the center and right edge by using flex? I want to set A to the center like the image.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

enter image description here

CSS

.flex{
  display: flex;
  justify-content: space-between;
  align-items: center;
  background:lightblue; 
}

HTML

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="main.css" />
    <title>sample</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
      <div class="row mx-auto text-center">
            <div class="col-12">
              <div class="flex">
                <p class="">A</p>
                <p class="">B</p>
              </div>
            </div>
      </div>
  </div>
</body>
</html>
  • Also don't forget to use bootstrap classes instead of custom css when you can (`d-flex`, `justify-content-between`, `align-items-center`) – Cédric Sep 28 '22 at 07:36

1 Answers1

-1

There are many ways of doing it. the easiest way in my opinion would be :

setting justify-content: flex-end; on container.which will push everything to right.

then using margin-left: auto; on both Childs to equally distribute the space to their left side

.flex {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: lightblue;
}

.flex>p {
  margin-left: auto;
}
<div class="container">
  <div class="row mx-auto text-center">
    <div class="col-12">
      <div class="flex">
        <p class="">A</p>
        <p class="">B</p>
      </div>
    </div>
  </div>
</div>
UmairFarooq
  • 1,269
  • 1
  • 3
  • 8