Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Before Position Fixed

Tags:

css

I'm trying to create a very simple modal in CSS and wanted to use a single DOM element for the 'vignette' and the main content. Thus far I have:

<div class="modal"></div>
.modal {
  position: fixed;
  top: 20%;
  left: 50%;
  right: 50%;
  width: 620px;
  margin: 0 -320px;
  height: 540px;
  background: $white;
  z-index: 4000;
}

/* Vignette */
.modal:before {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 100%;
    background: $black;
    z-index: 2000
}

Unfortunately this is leading to the vignette being displayed over top of the modal (not behind it as desired). How can it be forced underneath without creating a new div for it?

like image 467
Kevin Sylvestre Avatar asked Jul 04 '13 21:07

Kevin Sylvestre


People also ask

What is the opposite of position fixed in CSS?

position:static; , position:absolute; , and position:relative; are the alternatives to position:fixed; . There isn't a definitive opposite, because relative , absolute , static , and fixed have different properties to behave differently.

How do I move a fixed position in CSS?

Fixed 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.

What is the difference between position static and position fixed in CSS?

Fixed: The position of the element will be positioned relative to the viewport. Static: The elements will be positioned according to the normal flow of the page. Relative: The element remains in the normal flow of the document but left, right, top, and bottom affects.


1 Answers

You cannot do this, at least not with cross-browser code. Pseudo-elements as :before and :after are on another stack started from the parent element.

This is stated in the CSS specification files:

The :before and :after pseudo-elements elements interact with other boxes, such as run-in boxes, as if they were real elements inserted just inside their associated element

http://www.w3.org/TR/CSS21/generate.html#before-after-content

like image 162
BebliucGeorge Avatar answered Oct 08 '22 11:10

BebliucGeorge