Most textareas are typically rectangular or square in shape, similar to this:
However, I am interested in having a custom-shaped textarea, like the example below:
Is it feasible to achieve?
Most textareas are typically rectangular or square in shape, similar to this:
However, I am interested in having a custom-shaped textarea, like the example below:
Is it feasible to achieve?
There are several solutions that have been suggested in previous posts. In my opinion, the method outlined here is currently one of the most compatible with a wide range of browsers as it does not rely on CSS3 properties. However, please note that this approach may not work on browsers that do not support the contenteditable feature.
Following the suggestion by @Getz, you can utilize a div element with the contenteditable attribute and then style it using additional div elements placed on top. Below is an example showcasing two blocks floating at the top left and top right corners of the main div:
To achieve the desired layout described in your post, some adjustments need to be made especially with the borders. The main div has a blue border on all sides while the red blocks must be positioned to cover the top borders of the main div. Specific borders (bottom and left for the right block, bottom and right for the left) need to be applied to these red blocks accordingly.
Subsequently, you can extract the content via JavaScript when the "Submit" button is activated. Additionally, you should be able to modify other styling aspects such as font size and color through CSS :)
.block_left {
background-color: red;
height: 70px;
width: 100px;
float: left;
border-right: 2px solid blue;
border-bottom: 2px solid blue;
}
.block_right {
background-color: red;
height: 70px;
width: 100px;
float: right;
border-left: 2px solid blue;
border-bottom: 2px solid blue;
}
.div2 {
background-color: white;
font-size: 1.5em;
border: 2px solid blue;
}
.parent {
height: 300px;
width: 500px;
}
<div class="parent">
<div class="block_left"></div>
<div class="block_right"></div>
<div class="div2" contenteditable="true">
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut..."
</div>
</div>
Looking forward, we have the potential to utilize "css-shapes" in order to accomplish this task. By utilizing a div with the "contenteditable" attribute set to "true" along with "css-shapes," it becomes possible to shape a text area in any desired form.
At present, Chrome Canary has already extended its support for "css-shapes." An instance of the possibilities with css-shapes can be seen here:
The usage of a polygon shape is demonstrated to delineate the text-flow. It should be feasible to create two polygons that match the shape you aim for your textarea.
For further insights on "css-shapes," additional information can be found at:
To activate css-shapes in Chrome Canary:
- Copy and paste chrome://flags/#enable-experimental-web-platform-features into the address bar, then hit enter.
- Click the 'Enable' link within that section.
Press the 'Relaunch Now' button located at the bottom of the browser window.
from:
.container {
overflow: hidden;
shape-inside: polygon(200.67px 198.00px, 35.33px 198.47px, 34.67px 362.47px, 537.00px 362.74px, 535.67px 196.87px, 388.33px 197.00px, 386.67px 53.53px, 201.33px 53.53px);
font-size: 0.8em;
}
/** for red border **/
.container:before {
content: '';
position: absolute;
top: 8px;
left: 8px;
width: 190px;
height: 190px;
background-color: white;
border-right: 1px solid red;
border-bottom: 1px solid red;
}
.container:after {
content: '';
position: absolute;
top: 8px;
right: 8px;
width: 190px;
height: 190px;
background-color: white;
border-left: 1px solid red;
border-bottom: 1px solid red;
}
<div class="container" contenteditable="true">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque convallis diam lacus, id lacinia quam interdum quis. Ut vitae dignissim lorem, nec lobortis turpis. Fusce non fringilla nulla, eu blandit urna. Nulla facilisi. Nunc tristique, mauris vitae
tincidunt egestas, eros metus dapibus sapien, quis tincidunt sem dui ac purus. Morbi lobortis, quam sit amet consequat aliquam, elit mi rutrum erat, id tempus turpis turpis et sem. Vivamus tempor mollis porttitor. Sed elementum nisl sit amet sapien
auctor imperdiet. Sed suscipit convallis nisi, in dignissim risus placerat suscipit. Sed vel lorem eu massa vulputate pretium. Nulla eget dolor sed elit gravida condimentum non vel lorem. Vivamus pretium, augue sed aliquet ultricies, neque nibh porttitor
libero, a tristique elit mi eu nibh. Vestibulum erat arcu, condimentum eleifend adipiscing ut, euismod eu libero. In pharetra iaculis lorem, at consectetur nisi faucibus eu.
</div>
Polygon created using:
Outcome
Have you considered using Content Editable? It might be the solution you're looking for.
Even though it's not a textarea, creating a div with similar functionality could potentially achieve the desired outcome.
In my opinion, using just a textarea may not be sufficient...
Here's a small example to demonstrate: http://jsfiddle.net/qgfP6/5/
<div contenteditable="true">
</div>
To create a unique layout, consider using a contenteditable div with two corner divs:
<div style="border:1px blue solid ; width: 200px; height: 200px;" contenteditable="true">
<div style="float:left; width:50px; height: 50px; border: 1px solid blue" contenteditable="false"></div>
<div style="float:right; width:50px; height: 50px; border: 1px solid blue" contenteditable="false"></div>
hello world, hello worldsdf asdf asdf sdf asdf asdf
</div>
Another way to achieve this is by utilizing CSS Regions
CSS Regions allow you to flow content into styled containers, specifying the order of containers regardless of their position on the page.
(Web Platform)
[Supported in WebKit Nightly, Safari 6.1+, iOS7, Chrome and Opera with experimental features enabled - caniuse, Web Platform ]
You can create a textarea shape by flowing text through 2 regions and make it editable using contenteditable attribute.
<div id="box-a" class="region"></div>
<div id="box-b" class="region"></div>
<div id="content" contenteditable>text here</div>
#content {
-ms-flow-into: article;
-webkit-flow-into: article;
}
.region {
-ms-flow-from: article;
-webkit-flow-from: article;
box-sizing: border-box;
position: absolute;
width: 200px;
height: 200px;
padding: 0 1px;
margin: auto;
left:0;right:0;
border: 2px solid lightBlue;
}
#box-a {
top: 10px;
background: #fff;
z-index: 1;
border-bottom: none;
}
#box-b {
top: 210px;
width: 400px;
overflow: auto;
margin-top: -2px;
}
The end result:
To learn more about regions, check out this informative article: CSS3 regions: Rich page layout with HTML and CSS3
A lengthy text inside the box will cause the cursor to drop below the middle edges and I'm struggling to correct this issue.
**[Fiddle Diddle][1]**
#wrap {
overflow: hidden;
}
#inner {
height: 350px;
width: 500px;
border: 1px solid blue;
}
#textContent {
word-wrap: break-word;
word-break: break-all;
white-space: pre-line;
}
#left, #right {
height: 50%;
width: 25%;
margin-top: -1px;
padding: 0;
border: 1px solid blue;
border-top-color: white;
margin-bottom: 5px;
}
#right {
margin-left: 5px;
float: right;
margin-right: -1px;
border-right-color: white;
}
#left {
margin-right: 5px;
float: left;
margin-left: -1px;
border-left-color: white;
}
<div id="wrap">
<div id="inner">
<div id="left"></div>
<div id="right"></div>
<span id="textContent" contenteditable>The X Y Zs</span>
</div>
</div>
[1]: http://jsfiddle.net/yKSZV/
Sorry sire, it seems that creating a textarea in a different shape than the standard rectangle is not possible.
One workaround could be using 2 textareas with specified width and height. Alternatively, you can use an editable element like this:
http://jsfiddle.net/afzaal_ahmad_zeeshan/at2ke/
The code for an editable element:
<div contenteditable="true">
This text can be edited by the user.
</div>
By using this method, you can make any element editable and give it dimensions as needed, similar to how a textarea works.
For more information, refer to: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_Editable
By integrating CSS shapes with the contenteditable
attribute, you can achieve this on webkit browsers.
To start, you need to activate the flag: enable-experimental-web-platform-features
After that, restart your webkit browser and take a look at this FIDDLE!
This approach is effective even for non-standard shapes.
<div class="shape" contenteditable="true">
<p>
Text here
</p>
</div>
.shape{
-webkit-shape-inside: polygon(71.67px 204.00px,75.33px 316.47px,323.67px 315.47px,321.17px 196.00px,245.96px 197.88px,244.75px 87.76px,132.33px 87.53px,132.50px 202.26px);
shape-inside: polygon(71.67px 204.00px,75.33px 316.47px,323.67px 315.47px,321.17px 196.00px,245.96px 197.88px,244.75px 87.76px,132.33px 87.53px,132.50px 202.26px);
width: 400px;
height: 400px;
text-align: justify;
margin: 0 auto;
}
Wondering how I got that polygon shape?
Visit this website and create your very own custom shape!
Some tips on activating the flag: (from here)
To enable Shapes, Regions, and Blend Modes:
Copy and paste chrome://flags/#enable-experimental-web-platform-features into the address bar, then press enter. Click the 'Enable' link within that section. Click the 'Relaunch Now' button at the bottom of the browser window.
If you want to create intricate shapes using HTML5-canvas and CSS, consider utilizing the powerful Google web designer tool.
In addition to shape creation, you'll also have access to typing tools for adding text within the shapes.
To showcase the capabilities of the tool, check out a sample demo created with the Google Web Designer tool:
In situations where you find yourself needing to cater to browsers that lack support for the contenteditable
attribute, one possible solution could involve using JavaScript and events, although this approach can be quite messy.
Here is a rough outline of how this could work in pseudocode:
isFocused = false;
when user clicks on the div
{
isFocused = true;
}
when user clicks outside the div
{
isFocused = false;
}
when user presses a key
{
if (isFocused)
{
append character corresponding to the key pressed to div.innerHTML;
}
}
I'm currently working on a small CMS/Social network project for a school, which is quite complex and heavily relies on AJAX technology. Randomly, I encounter issues where calls are blocked and the browser displays an error message like net :: ERR_EMPT ...
I'm currently facing an issue where I want a divider to adjust its size when another divider changes size due to new content being loaded into it. However, even after binding the resize event handler to the divider that loads the content, the event do ...
I'm currently in the process of putting together a Video gallery playlist using HTML, CSS, and JavaScript. So far, I've set up the html and css files along with two js files. The first js file contains all the video information as shown here: ...
Sorry to bother you with this question, but I could really use some help. I'm facing an issue with updating the innerHTML or text of a specific HTML div based on data from an observable. When I try to access the element's content using .innerHTM ...
Looking to execute the function cookiefix() from main.js, which is linked at the bottom of my HTML file. echo '<body>'; if(!isset($_COOKIE['clicked'])) { if(!isset($_COOKIE['count'])) { echo '<script type="text/ ...
To upload a file to the server, I use the following code snippet: var fd = new FormData(); fd.append('folder', 'html'); fd.append('permission', 0); fd.append("file", file, file.name); After selecting the file from an input f ...
const [rowData, setRowData] = useState([]); const old = {id: 'stud1', name: 'jake', room: '2'}; const newData = {name: 'jake', room: '3A'}; useEffect(() => { let ignore = false; ...
I'm trying to implement a method similar to the one shown in this link on the link provided below. The first link works fine in IE, but for some reason my approach is failing in IE. It's loading the content, but the old content seems to still be ...
Here's the issue I'm facing: Whenever I make a json call using the code below var url="http://localhost:9000/json"; $.getJSON(url, function(data){ alert(data['yay']); }); It works perfectly fine. However, my localhost IP is ...
After making significant progress on my previous project, I found myself in need of some final assistance that has proven to be elusive. I successfully built my entire angular controller and populated all the necessary data for the page. However, I am str ...
Currently, I am working on a small project that involves two divs: one for English and another for Arabic. Despite creating the project, I am encountering an issue with getting the translation from English to Arabic. Below is the code I have attempted, but ...
One issue I am facing is taking an input from a search box and using that value as a parameter for another function. However, every time I click the button, the global variable resets itself when the page refreshes. In my javascript code, this is what I h ...
Just a quick question... Can a textbox have two carets simultaneously, or can we have two separate textboxes both focused at the same time? I am aware of simulating this using keydown listeners but I'm specifically looking for visible carets in both ...
I'm facing an issue with my code where the bot does not send a welcome message when a user joins, despite having everything set up correctly. Even when a user leaves, there is no farewell message being sent either. Here is the code I am using: bot.on ...
Currently, I am in the process of developing a Cordova application and require OAuth authentication with my Drupal backend. My main issue lies in obtaining a request token for this purpose. Despite receiving a 200 response indicating success, when inspecti ...
I need help figuring out how to display unique data dynamically for each tab within a table with tabs. Can anyone assist me with this? https://i.stack.imgur.com/63H3L.png Below is the code snippet for the tabs: <md-tab ng-repe ...
While working with a piece of JS code yesterday, I stumbled upon something peculiar. There was a div element that was initially hidden using display:none, and I was utilizing its height in some JavaScript calculations. Everything was functioning properly u ...
I'm struggling with displaying nested objects loaded from a JSON file in Angular. I've seen examples of using dot notations in HTML to access nested data, but I'm new to Angular and can't seem to get it right. The JSON is valid, but I j ...
Creating a Dynamic Div Layout with JavaScript I have successfully implemented functionality wherein clicking on + opens a div and clicking on - closes it. However, I am now faced with the challenge of handling multiple divs at runtime. How can this be ach ...
Although the model caching feature in Backbone-Relational is effective, loading a simple model securely requires considerable code. For example: // Attempt to locate a model in the cache this.model = MyModel.find({id:id}); if(this.model){ // Model re ...