I have a div, and inside is a textarea and a div:
<div class="innotate">
<div class="innotate-form">
<div class="">
<textarea cols="30" rows="3" name="body"></textarea>
</div>
<div class="instructions">Some Text that is quite long but should wrap instead of lengthening the div</div>
</div>
</div>
I want my div.instructions to wrap around and not increase the width of my div.innotate.
I'm quite stumped on this. I know display: inline isn't what I need, and setting the width manually is not an option.
I want the textarea to decide the width. That means that the width of my parent div should be at most the width of my textarea.
http://jsfiddle.net/8fo6by8d/1/
I was searching for an answer for this question and found that another way to do this with out setting any min/max width to the container div is to add
max-width: fit-content
to the div around the text. And add
display: inline-block
to the container around the text and the textarea
<style type="text/css">
.innotate-form {
display: inline-block;
}
.instructions {
max-width: fit-content;
}
</style>
<div class="innotate">
<div class="innotate-form">
<textarea cols="30" rows="3" name="body"></textarea>
<div class="instructions">Some Text that is quite long but should wrap instead of lengthening the div</div>
</div>
</div>
Used flex box for this one, had to throw width into .innotate-form for good measure.
.innotate-form {
display: flex
flex-wrap: wrap;
width: 14em;
margin-top: 3px;
background-color: rgba(249,249,249,0.9);
border: 1px solid #eee;
padding: 12.5px;
z-index: 9999;
}
The fiddle
er. wait. You are physically setting the width of the text area at "cols" = 30. One good turn deserves another. When you set the width of the text area, set the width of .instructions to match. In fact, lets do it one better, all without JS.
see http://jsfiddle.net/8fo6by8d/5/
Lose the col= setting
<div class="innotate">
<div class="innotate-form">
<div class="">
<textarea rows="3" name="body"></textarea>
</div>
<div class="instructions">Some Text that is quite long but should wrap instead of lengthening the div</div>
</div>
</div>
CSS:
.innotate-form {
margin-top: 3px;
background-color: rgba(249,249,249,0.9);
border: 1px solid #eee;
padding: 12.5px;
position: absolute;
z-index: 9999;
}
.innotate-form .instructions {
font-size: 14px;
color: #f20;
width: 250px;
padding: 5px;
}
textarea {
width: 250px;
height: 120px;
border: 3px solid #cccccc;
padding: 5px;
}
And if you're upset at having to type in a width twice there is always
textarea, .innotate-form .instructions{
width: 250px;
padding: 5px;
}
use this CSS
.innotate-form .instructions {
font-size: 14px;
color: #f20;
min-width: 250px;
max-width: 250px;
padding: 5px;
word-break: break-all;
}
Also with flex, but German's didn't seem to work. This one uses a container instead. The text doesn't wrap, though. It just sizes with the container.
.container {
display: flex;
flex-wrap: wrap;
}
.innotate-form {
flex: 1;
margin-top: 3px;
background-color: rgba(249, 249, 249, 0.9);
border: 1px solid #eee;
padding: 12.5px;
}
.instructions {
font-size: 14px;
color: #f20;
}
textarea {
width: 100%;
}
JSFiddle
EDIT: I researched more and found CSS3's resize property. This will make any div behave like a text area, so it will squish the text. I used flex to make the text area occupy the entire area of the div, then disabled resizing on the actual textarea.
Can I Use?
JSFiddle
* {
box-sizing: border-box;
}
.container {
display: flex;
-webkit-resize: both;
-moz-resize: both;
resize: both;
overflow: auto;
width: 400px;
height:300px;
background: darkorange;
padding: 1em;
}
.innotate {
display: flex;
flex-direction: column;
flex: 1;
width: 100%;
padding: 1em;
background-color: rgba(255, 255, 255, 0.5);
border: 1px solid #FFF;
}
.instructions {
padding: 1em;
font-size: 16px;
color: red;
}
textarea {
flex: 1;
width: 100%;
resize: none;
background-color: rgba(255, 255, 255, 0.5);
border: 1px solid #FFF;
}
Related
my text is overflowing see the screenshot https://drive.google.com/file/d/1i_9VvP54CAJJSvtsArZiTMMfMzACDS11/view?usp=sharing
here is css:
.card_main {
border: 1px solid black;
margin-top: 30px;
border-radius: 5px;
height: 900px;
background: #ffffff;
width: 100%;
}
.blog_content__text {
width: 95%;
height: 320px;
border-bottom: 1.5px solid lightgray;
margin-left: 2.5%;
margin-top: 20px;
}
.blog_heading {
font-size: 24px;
color: black;
}
.blog_details {
font-size: 16px;
color: black;
opacity: 0.7;
margin-top: 20px;
}
my html
<div className="card_main">
<div className="blog_content__text">
<h1 className="blog_heading">{data.blog_title}</h1>
<p className="blog_details">{data.blog_body}</p>
</div>
<div/>
how to prevent overflowing my text and make the div responsive. I am not an CSS expert. I just start learning css
When using fixed height for a div, you also need to say how the scroll should work. In this case using overflow-y:auto makes sense. You may prefer overflow-y:hidden or always show scrollbars overflow-y:scroll;
If there is no serious limitation in terms of graphics, do not specify the height for a Div to make its height responsive to the content.
.blog_content__text {
width: 95%;
height: 320px;
overflow-y:auto;
border-bottom: 1.5px solid lightgray;
margin-left: 2.5%;
margin-top: 20px;
}
remove the height: 320px;
if you must, use it as min-height: 320px;
try setting a margin-bottom css attribute to the div that contains the text, the value of the margin should equal the height of that white footer that is hiding the text on the bottom.
You can also make use of the following property if you really want to set the height:
height: min-content;
I'm trying to implement the following design (For now I'm just worried about the text box):
When typing:
Maximum height:
Notice the top and bottom paddings were decreased.
Now, this is what I have so far:
.chat-wrapper {
width: 100%;
}
.message-text {
resize: none;
box-sizing: border-box;
height: auto;
min-height: 41px;
max-height: 97px;
width: 387px;
border: 1px solid #e4e7ec;
border-radius: 20px;
background-color: #f9fafb;
outline: none;
padding: 0 24px 0 24px;
overflow: hidden;
}
textarea {
resize: none;
box-sizing: border-box;
height: auto;
min-height: 41px;
max-height: 97px;
width: 387px;
border: 1px solid #e4e7ec;
border-radius: 20px;
background-color: #f9fafb;
outline: none;
padding: 0 24px 0 24px;
overflow: hidden;
}
<div class="chat-wrapper">
<p>
Using div with contentEditable:
</p>
<div class="message-text" contentEditable></div>
<br/>
<p>
Using regular textarea:
</p>
<textarea></textarea>
</div>
Now for the input text box, I have two solutions:
Using div with contentEditable attribute, it works and it is expandable to a certain height. But the text is not centered vertically (I'm trying to avoid using Flex, just to make sure old browsers are compatible, not very strict about that though)
Using textarea, it is more semantic IMHO, but it doesn't expand automatically.
I want also to detect the keypress event (I don't think it is a problem in both solutions).
Which solution do you think is the web standard? If both are good, how do make the div centers the text, and shrink the paddings when it grows? Or in the case of textarea, how do I make it expand without JS?
Also if you have any better suggestions, let me know.
UPDATE:
I just realized how messy is the option (div with contentEditable):
As you can see, first I can't wrap the text to lines when the text is more than the width.
Second, the text inside the div, is not clean ! Especially when copy-pasting. I need it to be pure text so when I use JS to get the content, I get just the text not the html tags.
I'm assuming that you want your padding preserved and it that case you could do something like this with contenteditable.
Add the wrapper around the .message-text:
<div class="chat-wrapper">
<p>
Using div with contentEditable:
</p>
<div class="message-wrapper">
<div class="message-text" contentEditable></div>
</div>
</div>
Update CSS:
.chat-wrapper {
width: 100%;
}
.message-text {
min-height: 1em; /* prevent height collapsing when there is no text */
max-height: 97px;
width: 100%;
align-content: center;
outline: none;
overflow: hidden;
}
.message-wrapper {
width: 387px;
border: 1px solid #e4e7ec;
border-radius: 20px;
background-color: #f9fafb;
padding: 24px; /* the container will keep the padding untouched */
max-height: 145px; /* added padding to the height of the .message-text */
}
Check the snippet:
.chat-wrapper {
width: 100%;
}
.message-text {
min-height: 1em; /* prevent height collapsing when there is no text */
max-height: 97px;
width: 100%;
align-content: center;
outline: none;
overflow: hidden;
}
.message-wrapper {
width: 387px;
border: 1px solid #e4e7ec;
border-radius: 20px;
background-color: #f9fafb;
padding: 24px; /* the container will keep the padding untouched */
max-height: 145px; /* added padding to the height of the .message-text */
}
textarea {
resize: none;
box-sizing: border-box;
height: auto;
min-height: 41px;
max-height: 97px;
width: 387px;
border: 1px solid #e4e7ec;
border-radius: 20px;
background-color: #f9fafb;
outline: none;
padding: 0 24px 0 24px;
overflow: hidden;
}
<div class="chat-wrapper">
<p>
Using div with contentEditable:
</p>
<div class="message-wrapper">
<div class="message-text" contentEditable></div>
</div>
<br/>
<p>
Using regular textarea:
</p>
<textarea></textarea>
</div>
max-height: 145px for the .message-wrapper is actually the height of content box, and that helps with pushing the .message-text with padding from the top and bottom.
I hope I got the right idea of what you want to achieve,let me know if this helps.
My second inner div position is weirdly adjusted when my first inner div have a long link text. How to fix it?
My html code:
<div class='div-wrapper'>
<div class='inner-div1'>
This is a long link
</div>
<div class='inner-div2'>
Link 2
</div>
</div>
My css code:
.div-wrapper {
border: 1px solid black;
width: 200px;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-block;
border: 1px solid black;
width: 90px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
link to the picture of the div:
https://www.dropbox.com/s/9zs4mgj7izuqsp1/question.png?dl=0
The problem is with your CSS. Particularly the .div-wrapper div
You need to change the display setting from inline-block to inline-table to get it inside the cell. You mentioned that you wanted the box inside the larger box, but you need to clarify how exactly you want the inner boxes to be placed inside the larger box (ex: small gap between the boxes, both perfectly fit inside the large box with equal sizes)
Just changed inline-block to inline-flex for your inner div and looks fine.
.div-wrapper {
border: 1px solid black;
width: 200px;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-flex;
border: 1px solid black;
width: 90px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
<div class='div-wrapper'>
<div class='inner-div1'>
This is a long link
</div>
<div class='inner-div2'>
Link 2
</div>
</div>
Just have to fix this, I don't think any solution here explains why the problem exists. Just to add up, the problem with this is because vertical-align is set to baseline by default.
What you have to do is set the vertical-align to top
Insert it in your CSS:
.div-wrapper div {
vertical-align: top;
}
Link to solution: https://jsfiddle.net/Lnvgkfz3/
Small changes in CSS
.div-wrapper {
border: 1px solid black;
width: auto;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-block;
border: 1px solid black;
width: 190px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
I'm trying to add an overlay box at the bottom of a textarea. Positioning the overlay box was easy, but now I want the textarea content to never overlap the overlay box.
My first approach was adding padding-bottom so that the text never reaches the bottom of the textarea, where the overlay box is placed. However, as I type, the text will go under it. Also, scrolling up will cause the same undesired behavior.
Edit:
In response to some of the answers that partially solve my issue. I'm trying to make the textarea look as native as possible, so border color changing on focus would be necessary as well.
.container {
position: relative;
width: 110px;
}
textarea {
width: 100%;
height: 50px;
resize: none;
}
texarea.with-padding {
padding-bottom: 1em;
}
span {
position: absolute;
bottom: 5px;
width: 100%;
height: 1em;
background: rgba(255,0,0,0.5);
}
<div class="container">
<textarea name="" id="">I want this to never go under the red box.</textarea>
<span></span>
</div>
<div class="container">
<textarea class="with-padding" name="" id="">I tried with padding-bottom, but it doesn't work either.</textarea>
<span></span>
</div>
You can use a <div> container (which holds your textarea and overlay) as a fake border and remove the border of textarea. Just as shown in the snippet below:
$('textarea').on('focus', function() {
$('.textarea-holder').css('border-color', 'rgba(255, 0, 0, 0.5)');
});
$('textarea').on('blur', function() {
$('.textarea-holder').css('border-color', '#333');
});
.textarea-holder {
border: 1px solid #333;
display: inline-block;
}
.textarea-holder textarea {
display: block;
resize: none;
border: none;
}
textarea:focus {
outline: none;
}
.textarea-holder .overlay {
width: 100%;
height: 20px;
background: rgba(255, 0, 0, 0.5);
}
body {
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="textarea-holder">
<textarea rows="6"></textarea>
<div class="overlay"></div>
</div>
Hope this helps!
You can simply add a bottom-border: 1em to the textarea to imitate the span element.
Here is a working example: http://codepen.io/anon/pen/woKyvy#anon-login
.container {
position: relative;
width: 200px;
}
textarea {
width: 100%;
height: 50px;
border-bottom: 1em solid rgba(255,0,0,0.5);
}
<div class="container">
<textarea>Try typing. The cursor will never end up under the red line.</textarea>
</div>
So I went ahead and wrote it down:
Removed the border and reset some styles of textarea
Added the fake border to the container and removed the positioning of the span and made it a block element.
See code below:
.container {
position: relative;
width: 110px;
border: 1px solid;
}
textarea {
width: 100%;
height: 50px;
resize: none;
border:none;
outline:none;
padding: 0;
}
.container span {
display:block;
width: 100%;
height: 1em;
background: rgba(255, 0, 0, 0.5);
}
<div class="container">
<textarea name="" id="">I want this to never go under the red box.</textarea>
<span></span>
</div>
I finally found a solution to this riddle thanks to Saurav Rastogi's and eyetea's answers. Both were almost perfect, but failed to make the textarea have its border highlighted on focus. I've managed to keep this behavior using outline.
I think both approaches are useful as they allow for two different border highlight on focus. One leaving the overlay outside, using a div wrapper strategy, and the one leaving it inside, using a very thick border-bottom.
/* Inner border on focus solution */
.textarea-wrapper {
border: 1px solid gray;
display: inline-block;
}
.textarea-wrapper textarea {
display: block;
border: none;
}
.textarea-wrapper textarea:focus {
outline: 1px solid green;
outline-offset: 0;
}
.textarea-wrapper .overlay {
width: 100%;
height: 20px;
background: rgba(255, 0, 0, 0.5);
}
/* Outer border on focus solution */
textarea.bottom-padded {
border-bottom: 21px solid rgba(255,0,0,0.5);
outline: 1px solid gray;
outline-offset: -1px;
}
textarea.bottom-padded:focus {
outline-color: green !important;
}
<div class="textarea-wrapper">
<textarea rows="3">Inner border on focus</textarea>
<div class="overlay"></div>
</div>
<textarea rows="3" class="bottom-padded">Outer border on focus</textarea>
Here's an image of what I'm referring to:
If you have some fixed height h from the baseline that the pin lies, and the green element is dynamically sized, how can you make the orange element take the space between the two?
Have exactly what you need in this case using a flexbox.
The pin approximately stays at the same height above the baseline give or take 1px.
How it works: When the green element grows say 10px the pin is elevated by 5px. But the flex setup means the dummy and the orange box reduces 5px each thus keeping the pin at a contant height.
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
}
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
height: 100px;
border-bottom: 1px solid black;
}
.dummy {
flex: 1;
}
.top {
height: 20px;
width: 20px;
border: 1px solid green;
position: relative;
}
.top div {
position: absolute;
height: 3px;
width: 3px;
background: #880015;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.bottom {
width: 50px;
border: 1px solid orange;
flex: 1;
}
<div class="wrapper">
<div class="dummy"></div>
<div class="top">
<div></div>
</div>
<div class="bottom"></div>
</div>
You can use table properties for this. Table cells fill its parent width. If one cell is short, the other one will expand to fill its parent. The trick here is to rotate 90ยบ your "table" and it's done. To change the 'height" of your pinned item you will actually be changing its width. The anchor element will resize accordingly.
Be aware of this though: http://caniuse.com/#search=transform
.baseline{
height: 100px;
width: 100px;
border: 3px solid black;
display: table;
transform: rotate(90deg);
}
.pinned,.anchored{
display: table-cell;
}
.pinned{
width: 30px;
border: 3px solid green;
}
.anchored{
border: 3px solid orange;
}
<div class="baseline">
<div class="pinned">
</div>
<div class="anchored">
</div>
</div>