How to Get a Flexbox to Have a Center Fixed and a Bottom Fixed Children Together

How do I get a flexbox to have a center fixed and a bottom fixed children together?

Update 1:

Center 'middle' div vertically of the whole 'wrapper'.

.wrapper {
height: 100px;
width: 100px;
display: flex;
flex-direction: column;
justify-content: center;
position: relative;
border: 1px solid red;
}

.middle,
.bottom {
background: aqua;
}

.bottom {
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
<div class="wrapper">
<div class="middle">middle</div>
<div class="bottom">bottom</div>
</div>

How can I have a position: fixed; behaviour for a flexbox sized element?

You can't.

As explained by the CSS2.1 spec:

Absolutely positioned boxes are taken out of the normal flow.

And the Flexible Box Layout spec confirms that:

An absolutely-positioned child of a flex container does not
participate in flex layout
. However, it does participate in the
reordering step (see order), which has an effect in their
painting order.

(Emphasis mine)

FlexBox: How can i keep boxes fixed one below the other

.wrapper {  display: flex;    flex-flow: row wrap;  font-weight: bold;  text-align: center;}
.wrapper > * { padding: 10px; flex: 1 100%;}
header{ background: tomato;}
section{ display: flex}.main { background: deepskyblue;}
.main img{ max-width: 100%;}
.full-width { background: lightgreen;}
.main { order: 2; flex: 3 0px; }.aside { flex: 1 auto; }.aside-1 { order: 1; background: gold; width: 10%} .aside-2 { order: 3; background: hotpink; position: relative; width: 10%}.aside-2 > span { position: absolute; bottom: 0; left: 0;}.vertical { width: 15%; /* Modify this value for different screen size*/}/* .full-width { order: 4; } *//*@media all and (min-width: 600px) { .aside { flex: 1 auto; }}
@media all and (min-width: 800px) { .main { flex: 3 0px; } .aside-1 { order: 1; } .main { order: 2; } .aside-2 { order: 3; } .footer { order: 4; }}*/body { padding: 2em; }
<div class="wrapper">    <header><img src="https://www.oilandgasreinvented.com/img/logo-placeholder.svg" width="80px"></header>    <section>        <div class="main">        <img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt="Sample Image">        </div>        <aside class="aside aside-1"></aside>        <aside class="aside aside-2"><span>Aside Right</span></aside>    </section>        <section>       <div class="main">          <img src="https://picsum.photos/200/300" alt="Sample Image">       </div>       <aside class="aside aside-1 vertical"></aside>       <aside class="aside aside-2 vertical"><span>Aside Right</span></aside>   </section>    <section>     <div class="main">          <img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt="Sample Image">        </div>        <aside class="aside aside-1"></aside>        <aside class="aside aside-2"><span>Aside Right</span></aside>    </section>
<section class="full-width">Full Width</section> <section> <div class="main"> <img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt="Sample Image"> </div> <aside class="aside aside-1"></aside> <aside class="aside aside-2"><span>Aside Right</span></aside> </section></div>

Align an element to bottom with flexbox

You can use auto margins

Prior to alignment via justify-content and align-self,
any positive free space is distributed to auto margins in that
dimension.

So you can use one of these (or both):

p { margin-bottom: auto; } /* Push following elements to the bottom */
a { margin-top: auto; } /* Push it and following elements to the bottom */

.content {  height: 200px;  border: 1px solid;  display: flex;  flex-direction: column;}h1, h2 {  margin: 0;}a {  margin-top: auto;}
<div class="content">  <h1>heading 1</h1>  <h2>heading 2</h2>  <p>Some text more or less</p>  <a href="/" class="button">Click me</a></div>

How to center vertically a specific child using flexbox?

with flexbox, you can do that:

only the child-3 will be vertically centered.

.parent {  height: 100px;  width: 100px;  display: flex;  border: 1px solid #ccc;}.child-3 {  align-self: center;}
<div class='parent'>  <div class="child-1">1</div>  <div class="child-2">2</div>  <div class="child-3">3</div></div>

How to make Flexbox items the same size

Set them so that their flex-basis is 0 (so all elements have the same starting point), and allow them to grow:

flex: 1 1 0px;

Your IDE or linter might mention that the unit of measure 'px' is redundant. If you leave it out (like: flex: 1 1 0), IE will not render this correctly. So the px is required to support Internet Explorer, as mentioned in the comments by @fabb;

How to use position fixed in flex layout?

If I understand your requirements, you want make the right scroll and the left be fixed. That can be done without the use of fixed position.

I would also personally recommend to not use fixed position, unless it is absolutely necessary, as it can have some unwanted behavior on mobile device, and you kind of loose the benefit non positioned solutions like flexbox offer.

html, body {  margin: 0;}#parent {  display: flex;  height: 100vh;}#left {  flex-grow: 1;  background: blue;}#right {  flex-grow: 5;  background: red;  overflow: auto;}#right div {  height: 300vh;}
<div id="parent">  <div class="child" id ="left">      ABC  </div>  <div class="child" id ="right">    <div>      DEF    </div>  </div></div>

Center one and right/left align other flexbox element

Below are five options for achieving this layout:

  • CSS Positioning
  • Flexbox with Invisible DOM Element
  • Flexbox with Invisible Pseudo-Element
  • Flexbox with flex: 1
  • CSS Grid Layout

Method #1: CSS Positioning Properties

Apply position: relative to the flex container.

Apply position: absolute to item D.

Now this item is absolutely positioned within the flex container.

More specifically, item D is removed from the document flow but stays within the bounds of the nearest positioned ancestor.

Use the CSS offset properties top and right to move this element into position.

li:last-child {  position: absolute;  top: 0;  right: 0;  background: #ddd;}ul {  position: relative;  padding: 0;  margin: 0;  display: flex;  flex-direction: row;  justify-content: center;  align-items: center;}li {  display: flex;  margin: 1px;  padding: 5px;  background: #aaa;}p {  text-align: center;  margin-top: 0;}span {  background-color: aqua;}
<ul>  <li>A</li>  <li>B</li>  <li>C</li>  <li>D</li></ul><p><span>true center</span></p>


Related Topics



Leave a reply



Submit