Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using position: relative inside a flex container

I am making preview of post like its text and image as a background with some filters.

The problem is that I want to have the whole div not 1300px, but only 650px.

However, this way I will not be able to use display: flex and will not have div with img the same height as the div with text.

Is there any possible way to solve this problem only with css (without js)?

Here is the code: http://codepen.io/anon/pen/RGwOgN?editors=1111

.post {
  width: 650px;
  padding: 25px 25px;
  z-index: 10;
  position: relative;
  color: white;
}
.flex-row {
  display: flex;
  width: 1300px; /* here is a problem */
}
.back-img {
  width: 650px;
  background-size: cover;
  position: relative;
  z-index: 0;
  left: -650px;
  filter: blur(3px);
  -webkit-filter: blur(3px);
  -moz-filter: blur(3px);
  -o-filter: blur(3px);
  -ms-filter: blur(3px);
  filter: url(#blur);
  overflow: hidden;
}
<div class="flex-row">
  <div class="post">
    <h1>Lorem ipsum</h1>
    <h2>text here</h2>
    <p class="lead">text hete</p>
  </div>
  <div class="back-img" style="background-image: linear-gradient(
      rgba(0, 0, 0, 0.3),
      rgba(0, 0, 0, 0.3)
    ),url(http://unsplash.com/photos/g-AklIvI1aI/download)">
    <div></div>
  </div>
</div>

So as you see it works fine, but only if parent element (flex-row) is set to size*2, because another way the size of children elements will be automatically reduced.

like image 532
Oleksii Kryzh Avatar asked Sep 03 '16 11:09

Oleksii Kryzh


People also ask

Can I use position absolute with Flex?

If you make the child position: absolute; but don't apply any top/right/bottom/left properties, then flexbox alignment will still apply to it.

How do you position elements in Flex?

Justify ContentFlex Start positions element at the start of the page. Flex End sets the element to the end of the page. Space Around arranges the items evenly but with spaces between them. The spaces will be equal among all the elements inside a flex-box container, but not outside them.

How do you position a relative element?

In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.

How do you move an element with a relative position?

Relative Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.


1 Answers

Relative positioning keeps elements in the normal flow. That means that after you position them, their original location is still occupying space.

Absolute positioning removes elements from the normal flow. These elements do not take up space, so they can be aligned without disturbing the surrounding layout.

This should work for you:

.flex-row {
  display: flex;
  width: 650px;                       /* 1 */
  position: relative;                 /* 2 */
}
.post {
  width: 100%;
  padding: 25px 25px;
  z-index: 10;
  color: white;
}
.back-img {
  position: absolute;                 /* 3 */
  height: 100%;
  width: 100%;
  background-size: cover;
  z-index: 0;
  filter: blur(3px);
  -webkit-filter: blur(3px);
  -moz-filter: blur(3px);
  -o-filter: blur(3px);
  -ms-filter: blur(3px);
  filter: url(#blur);
  overflow: hidden;
}
<div class="flex-row">
  <div class="post">
    <h1>Lorem ipsum dolor</h1>
    <h2>Lorem ipsum dolor sit amet, cu melius feugiat delenit mei. Sea cu tale etiam definitiones. An pro scribentur omittantur, ei sea utinam hendrerit. Has ad dico illud fierent. Vis tale modus ridens te.

Vix viris zril forensibus eu, dolor expetendis dissentiunt duo in. Vis id ludus theophrastus. Debitis tibique necessitatibus quo ea. At movet bonorum intellegat eos. Nec ne ubique erroribus. Rebum accusata est ad.</h2>
    <p class="lead">{{post.summary}}</p>
  </div>
  <div class="back-img" style="background-image: linear-gradient(
      rgba(0, 0, 0, 0.3),
      rgba(0, 0, 0, 0.3)
    ),url(http://unsplash.com/photos/g-AklIvI1aI/download)">
    <div></div>
  </div>
</div>

http://codepen.io/anon/pen/XjWQxo

Notes:

  1. primary container width changed from 1300px to 650px
  2. make primary container the bounding box (nearest positioned ancestor) for absolute positioning of children
  3. image is taken out-of-flow and occupies full width and height of parent div
like image 61
Michael Benjamin Avatar answered Sep 21 '22 19:09

Michael Benjamin