Languages
[Edit]
EN

CSS - how to align element to left and right with flex?

13 points
Created by:
Jacob
532

Using Flexbox it is possible to align child elements in few ways.

1. Spacer element between example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .parent {
      	border: 1px solid red;
    	display: flex;
    }
    
    .child {
    	padding: 10px;
    }
    
    .left-child {
    	background: lightgreen;
      	flex: 0;
    }
    
    .spacer {
      	background: ivory;
    	flex: auto;
    }
    
    .right-child {
    	background: lightblue;
    	flex: 0;
    }
    
  </style>
</head>
<body>
  <div class="parent">
    <div class="child left-child">LEFT</div>
    <div class="child spacer">SPACER</div>
    <div class="child right-child">RIGHT</div>
  </div>
</body>
</html>

2. space-beetwen container style example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .parent {
      	border: 1px solid red;
    	display: flex;
      	justify-content: space-between;
    }
    
    .child {
    	padding: 10px;
    }
    
    .left-child {
    	background: lightgreen;
      	flex: 0;
    }
    
    .right-child {
    	background: lightblue;
    	flex: 0;
    }
    
  </style>
</head>
<body>
  <div class="parent">
    <div class="child left-child">LEFT</div>
    <div class="child right-child">RIGHT</div>
  </div>
</body>
</html>

3. auto margin example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .parent {
      	border: 1px solid red;
    	display: flex;
    }
    
    .child {
    	padding: 10px;
    }
    
    .left-child {
      	margin-right: auto;
    	background: lightgreen;
      	flex: 0;
    }
    
    .right-child {
      	/* margin-left: auto; */
    	background: lightblue;
    	flex: 0;
    }
    
  </style>
</head>
<body>
  <div class="parent">
    <div class="child left-child">LEFT</div>
    <div class="child right-child">RIGHT</div>
  </div>
</body>
</html>

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

CSS

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join