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;
}
}
After retrieving values from a database, I am using them in JSON/javascript as an array. For example, see The issue arises when trying to calculate the sum of the array elements. I attempted to solve it with this code: var obj1 = JSON.parse(data); var m ...
I am currently using the open source edition of Kendo Web with the Kendo UI Web on ASP.NET MVC 4. My Kendo grid contains the following JavaScript code: <script> $(document).ready(function () { $("#grid").kendoGrid({ dataSource: ...
I am trying to achieve a functionality where clicking on an element will add a class, but if the parent of this element already has the class, it should remove it first. However, the current code is not working as expected. It only adds new classes without ...
On my website, there is a "share on Facebook" button that, when clicked, opens a popup for users to share a picture on their profile. While this feature works, I am encountering an issue with the callback function. I need to determine if the user actually ...
I want to incorporate Animista animations to make a button scale in from the left when hovering over a div block, and then scale out to the left when the mouse moves out of the div block. Check out my code snippet below: http://jsfiddle.net/30sfzy6n/3/. ...
I came across this code snippet: $(element).on('click', function() { $this.closest('div').before('<span id="message" style="display:none"></span>'); $('#message').fadeIn().delay(5000).fadeOut(); ...
As per the guidelines provided in the react documentation, this code snippet is expected to function properly. <div tabIndex="0"></div> However, upon testing it myself, I encountered an issue where the input was not working as intended and ...
When working on an animation in javascript, I encountered a problem that I can't seem to identify. I am attempting to make the pan function work with the "mover" function, but it seems like either I am not using the properties correctly within the "tr ...
When attempting to use sendkeys on an input field, I encountered a warning that puzzled me: org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only (WARNING: The server did not provide any stacktrace informati ...
I'm attempting to fadeOut a variable that includes the classes I provided: var elementsToFadeOut = "'.slider-bg , .inner-content , .about-app , .contact-us'" $('.menu-li.a').click(function() { $(elementsToFadeOut).fadeOut(300) ...
Just starting out with Jquery mobile and I'm having trouble binding data to a listview. Here is the code I've written, but when I run the page, the listview doesn't show up. Can someone please help me? My service Method [WebMethod] pub ...
Is there a way to make the drop-down from the select tag open to the right side(drop-right)? <label for="ExpLabel">Select Message Expiry:</label> <select name="ExpSelect" id="Expiry"> <option value="ExpiryDate">1 Day</opt ...
On my webpage, I display data that is inserted into a MySQL database. I am interested in having the view page automatically update whenever a new record is added to the database. It's worth noting that the data may be inserted from a different user an ...
I have a straightforward website where I need to implement the ability to drag and drop styled DIV elements between two containers. Utilizing JQueryUI's sortable function, this behavior was successfully achieved with the following code: $("#active-co ...
Consider the following scenario. If I want to extract the name of the country chosen using JavaScript, how can this be achieved? <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label getmdl-select getmdl-select__fullwidth"> ...
I'm currently in the process of retrieving property details from a specific website (Find Properties For Sale). Initially, I successfully scraped the information from the initial page. However, upon attempting to gather data from subsequent pages, my ...
My current code is functioning correctly, but I am facing an issue where if an error occurs, I want it to halt all other promises in the chain. Specifically, when chi.getCommand(val1, val2) returns a reject and triggers the exception catch block, I need to ...
Currently, I am in the process of learning how to utilize Angular (1.3.10). My objective is to create two input fields that will specify the suit and value for a hand of playing cards. In my attempts to achieve this, I have encountered an issue. When I man ...
I'm struggling to understand why I can't get the image in this HTML snippet to appear anywhere other than on top. Despite my attempts to adjust z-index, use !important overrides, and try other methods for hours, I haven't been successful. My ...
I am currently working on designing a HTML/CSS layout for a Kiosk-style website. However, I am encountering some challenges in getting the alignment of images and text to appear exactly as desired. The goal is to have the logo and header remain fixed in ...