7

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

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • @RokoC.Buljan Your answer is exactly the same as [Set height 100% on absolute div](http://stackoverflow.com/q/14217783/1529630), and probably a gazillion of other posts. No need to have another opened duplicate question. – Oriol May 24 '16 at 22:37
  • @Oriol nope the question is more about the `pseudo` height 100%, which at some point ends and does not follow up with the increasing parent scrollHeight - where I presume background-image is the best solution (other than some variants using JS) – Roko C. Buljan May 24 '16 at 22:39
  • OK, after the edit it may not be a duplicate, but before it was. – Oriol May 24 '16 at 22:41
  • Sorry I edited after the comment – Tushar Chutani May 24 '16 at 22:42
  • 1
    @TusharChutani why don't you simply use a background image? or a CSS linear-gradient background-image? – Roko C. Buljan May 24 '16 at 22:45
  • @RokoC.Buljan thanks I think I am going to do it this way – Tushar Chutani May 25 '16 at 18:27
  • @RokoC.Buljan this doesn't work with IE9 or lower. Do you have any other suggestions? – Tushar Chutani May 26 '16 at 05:11

3 Answers3

2

Here is a second answer without an additional DIV. It's similar to your original code and uses a :before pseudo-element. No float, but position absolute in relation to a relative positioned element, which makes the 100% height setting work (in conjunction with height settings on the main element).

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

<div contenteditable="true" class="editableDiv" style="heigth: 100px;">

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>

<p>Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</p>

<p>In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,</p>
  </p>
</div>

 

editableDiv {
  position: relative;
  background-color: #DEDEDE;
  min-height: 100px;
  max-height: 400px;
  overflow: scroll;
}

.editableDiv p {
  padding: 0px 0px 0px 45px;
}

.editableDiv:before {
  content: ' ';
  position: absolute;
  width: 40px;
  height: 100%;
  z-index: 1;
  background: #00f;
  background-size: 40px 100px;
  background-repeat: no-repeat;
}
Johannes
  • 64,305
  • 18
  • 73
  • 130
  • but now you took away the "correct answer" confirmation again? Is it because you I have used the `:before` pseudo selector instead of your original `.after` ? If yes: It also works with `.after`, you just have to add `top: 0` , see here: http://codepen.io/anon/pen/QELdWZ – Johannes May 25 '16 at 14:37
  • Sorry I thought I made a comment but if you try to hit enter a couple of times the bug persists – Tushar Chutani May 25 '16 at 18:22
  • that's because of your max-height setting - just erase that and it will work. – Johannes May 25 '16 at 18:26
  • if you absolutely need that max-height, erase it from `.editableDiv`, put a wrapper around `.editableDiv` and give the max-height to that wrapper. – Johannes May 25 '16 at 18:30
  • I am constrained with not using a wrapper – Tushar Chutani May 25 '16 at 19:55
1

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>
Oriol
  • 274,082
  • 63
  • 437
  • 513
1

define the margin as an own div with position: absolute and height: 100%;, followed by the p tag/s. .editableDiv needs a height definition for the 100% to work. .editableDiv or (as in my codepen) the p tags inside it get a left padding that lets the text start right of the margin.

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

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

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>

<p>Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</p>

<p>In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,</p>
  </p>
</div>


.editableDiv{
  position: relative;
  background-color: #DEDEDE;
  min-height: 100px;
  max-height: 400px;
  overflow:scroll;
}
.editableDiv p {
    padding: 0px 0px 0px 45px;
}

.bluemargin {
  position: absolute;
  background-color: #00f;
  width: 40px;
  height: 100%;
}
Johannes
  • 64,305
  • 18
  • 73
  • 130