105

I have a textarea which is contained in a div as I have jquery hint and wanted to use opacity without changing the border. There is a visible vertical scroll bar how I only want this displayed when I am typing in the text field and it goes beyond the container. I have tried overflow: auto; but does not work.

Textfield:

<label>
    <div id="name">
        <textarea name="message" type="text" id="message"
            title="Enter Message Here"
            rows=9 cols=60 maxlength="2000"></textarea>
    </div>
</label>

Styles:

#name { 
    border: 1px solid #c810ca;
    width: 270px;
    height:159px;
    overflow: hidden; 
    position: relative;
    }

#message {
    height: 400px;
    width: 235px;
    overflow: hidden;
    position: absolute;
}
Tuna
  • 2,937
  • 4
  • 37
  • 61
Lukus
  • 1,121
  • 3
  • 10
  • 10
  • Possible duplicate of [CSS hide scroll bar if not needed](https://stackoverflow.com/questions/18716863/css-hide-scroll-bar-if-not-needed) – Eren Tantekin Nov 21 '18 at 07:49

3 Answers3

222

overflow: auto (or overflow-y: auto) is the correct way to go.

The problem is that your text area is taller than your div. The div ends up cutting off the textbox, so even though it looks like it should start scrolling when the text is taller than 159px it won't start scrolling until the text is taller than 400px which is the height of the textbox.

Try this: http://jsfiddle.net/G9rfq/1/

I set overflow:auto on the text box, and made the textbox the same size as the div.

Also I don't believe it's valid to have a div inside a label, the browser will render it, but it might cause some funky stuff to happen. Also your div isn't closed.

Davy8
  • 30,868
  • 25
  • 115
  • 173
  • How to make this solution work when we have customized the scrollbar using -webkit-scrollbar psuedo element. ? because if overflow is auto psuedo elements arent working.... I want to customize my scrollbar as well as hide then when not needed ? – Kpatel1989 Apr 27 '15 at 08:37
3

overflow: auto; or overflow: hidden; should do it I think.

Boris Bachovski
  • 642
  • 2
  • 12
  • 22
2

Add this class in .css class

.scrol  { 
font: bold 14px Arial; 
border:1px solid black; 
width:100% ; 
color:#616D7E; 
height:20px; 
overflow:scroll; 
overflow-y:scroll;
overflow-x:hidden;
}

and use the class in div. like here.

<div> <p class = "scrol" id = "title">-</p></div>

I have attached image , you see the out put of the above code enter image description here

Ravish
  • 2,383
  • 4
  • 37
  • 55
DareDevil
  • 5,249
  • 6
  • 50
  • 88