Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make CSS pseudoelement :after same height as parent div

Tags:

html

css

sass

I have a parent div has to have a blue background on the margin of it. I have decided to use the :before psedoeelement to do that. I have set the position to be absolute and height to 100%. Now when I set the height of this to be 100%, I believe the the before is taking height of the page and not its div plus it almost seems as if the this psedoelement :before is a div that has position of fixed. Here is some code

<div contenteditable="true" class="editableDiv" style="heigth: 100px;">
<p><b>test </b><p>

  </p>
</div>

CSS

.editableDiv{
  background-color: #DEDEDE;

  min-height: 100px;
  max-height: 400px;
  overflow:scroll;
}
.editableDiv p{
    padding: 0px 0px 0px 43px;
}

.editableDiv:before{
 background-color:blue; 
 content: " ";
 position:absolute; 
 float:left;
 height: 100%;
 width: 40px;
 display:block;
}

How can I make it so the blue margin is scrolls with the div automatically adjusts height according to the height? Here this is the JSFiddle for the problem

Edit 1

The problem with adding just a position:relative to editabeDiv is that it works for as long as
don't make the div scroll but as soon as there is a scroll there is no longer a margin from that point onwards. You can try hitting enter for a while on jsfiddle to recreat this.

enter image description here

like image 942
Tushar Chutani Avatar asked May 24 '16 22:05

Tushar Chutani


People also ask

Can I have multiple before pseudo elements for the same element?

1, an element can only have at most one of any kind of pseudo-element at any time.

How do you align pseudo elements?

You need to position the :before pseudo-element absolutely by using the well known centering technique (use the top, left, and transform properties). Here, -25px will offset the text above the circle. Note that for the <span> element, we set the position property to its "relative" value and the display to "block".

How do I make my Div 100 height relative to parents?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

Do pseudo elements inherit?

The only way that pseudo-elements can inherit values from the parent of their generating element is when the generating element itself is also inheriting from its parent. This is because inheritance occurs from a parent to a child, one level at a time.


1 Answers

Your code works as expected: the blue pseudo-element covers all the editable element vertically.

The problem is that the contents of the editable div overflow it, and it seems you want the blue element to be as tall as the contents, not the editable element.

To achieve that, you should add another element.

.editableDiv-wrapper {
  max-height: 400px;
  overflow: scroll;
}
.editableDiv {
  background-color: #DEDEDE;
  position: relative;
  min-height: 100px;
  overflow: hidden; /* Prevent margin collapse */
}
.editableDiv p {
  padding: 0px 0px 0px 43px;
}
.editableDiv:before {
  content: " ";
  position: absolute;
  height: 100%;
  width: 40px;
  background-color: blue;
}
<div class="editableDiv-wrapper">
  <div contenteditable="true" class="editableDiv" style="heigth: 100px;">
    <p><b>test</b></p>
  </div>
</div>
<hr />
<div class="editableDiv-wrapper">
  <div contenteditable="true" class="editableDiv" style="heigth: 100px;">
    <p><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b><br /><b>test</b></p>
  </div>
</div>
like image 67
Oriol Avatar answered Sep 23 '22 06:09

Oriol