textbox tailored for slim parents width minus paddings

This may be a common query, but I have yet to come across a solution that fits my specific scenario...

Inside a div, there is a textarea

Picture the div with a width of 90% relative to its container, and I want the textarea to span the entire width of the div .notes, with a left and right margin of 10px

Something like 100% of div - 10px from the left margin and 10px from the right margin")

<div class="notes">
  <h2>Title</h2>
  <textarea></textarea>
  <button>Save</button>
</div>

This solution could also prove beneficial for any other div within a div where a similar width adjustment is desired.... How can this be achieved?

Answer №1

One way to achieve this is by utilizing the calc() function

textarea {
    width: calc(100% - 20px); /* 20px = 10px for left and 10px for right */
    margin: auto; /* To center your textarea horizontally */
    display: block; /* Necessary for horizontal centering as well*/
}

Check out the demo


Please keep in mind that if you care about supporting older browsers, you should also consider using -webkit-calc and -moz-calc.

There are some JavaScript polyfills available, such as:

Answer №2

Instead of using margin, you can also utilize box-sizing and padding:

div {
  width:80%;
  margin:auto;
  padding:15px;
  box-sizing:border-box;/*includes border & padding */
  background:lightgray;
}
textarea {
  display:block;
  width:100%;
  box-sizing:border-box;/*includes border & padding */
}

Live demonstration here: http://codepen.io/anon/pen/uegjo

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The block tag on line 117 is incorrect: 'endblock' was found instead of 'empty' or 'endfor'. Perhaps the tag was not registered or loaded properly?

{% extends 'shop/basic.html' %} {% block css %} .col-md-3 { display: inline-block; margin-left: -4px; } .col-md-3 { width: 100%; height: auto; } body .no-padding { padding-left: 0; padding-right: 0; } .car ...

Difficulty encountered when modifying a form field in Javascript

Just wanted to say thanks in advance. I've been trying to modify the value of the input field "total" within a form based on the selection made in the "opciones" dropdown. I attempted using onchange() and document.getElementById("").value, but unfort ...

Embedding the @media tag directly into a class for simplification and to eliminate redundancy

For the complete css/html code sample, please visit: https://jsfiddle.net/menelaosbgr/jbnsd9hc/1/ Here are two specific definitions I am working with: /* Dropdown Content (Hidden by Default) */ .dropdown-content { display: none; position: absolut ...

A guide to revealing/hiding a connected radio button using javascript or jquery

Despite the confusing title of my question, my goal is clear: When I select the radio button for text1, the corresponding subtext1 radio button should appear. Subsequently, when I click on text2's radio button, the subtext2 radio button will be displ ...

What are the implications of using CSS classes for JavaScript markup?

The circumstances: An PHP-generated homepage using a template engine is currently undergoing a redesign. The new design is centered around jQuery UI elements. The current CMS utilizes various templates such as page, article details, and comments. The i ...

Transforming a traditional HTML/CSS/JS website into a cutting-edge React application using Tailwind CSS styling

I have a unique website template complete with HTML, CSS, and all necessary assets. Now, I am looking to develop a React JS application using this template. Firstly, I am wondering: Since I typically use Tailwind CSS for my other projects, would it be mo ...

Add the div element to a different div in the HTML code

I'm struggling with my HTML layout. I have a <div> ... </div> section positioned in a fixed location. My goal is to be able to use that <div></div> multiple times within the body tag. I attempted various methods using jQuery a ...

Transforming the style of a particular element within React using Array().fill().map's method

Looking for a way to dynamically change the color of an array in React + styled jsx? Let's say you want to change the color when the number of elements in the array is a multiple of 4 (e.g. the index is 4, 8, etc.) Is there a clever solution to achi ...

Fetching JSON data using Javascript/JQuery

My goal is to access JSON data from an online web service that queries a MySQL database for a specific string and use Javascript to present it on an HTML page. My challenge lies in effectively displaying the retrieved information. The relevant part of my ...

"What is the process for creating a single line border around a pandas DataFrame using df.to_html method

After exporting a pandas dataframe as an HTML table to Outlook, I noticed that the double cell borders are not aesthetically pleasing. I am seeking assistance in changing them to a single line border. Please refer to the attached screenshot and code snip ...

accommodating multiple background images in CSS

I am currently working on a WordPress website using the Twenty Twelve theme. On one of the pages, I am trying to incorporate three background images - one at the top, the next one right below the top image, and the third one at the very bottom... Is there ...

Implementing Jquery to Repurpose a Function Numerous Times Using a Single Dynamic Value

Important: I have provided a functional example of the page on my website, currently only functioning for the DayZ section. Check it out here Update: Below is the HTML code for the redditHeader click event <div class="redditHeader grey3"> & ...

Using Previous and Next Buttons in Bootstrap Tabs for Form Navigation

I am currently facing a challenge in splitting a form into multiple tabs and encountering issues with the next and previous buttons on the second and third tabs. My goal is for the tabs at the top to change state as you cycle through them by clicking the ...

Can a Django form be configured to hide submitted values using display:none?

As I was working on submitting a form in Django with multiple details, I came up with the idea to split it into sections. The first set of details would be hidden initially, and upon clicking the next button, the next set of details would be displayed fo ...

Choosing Text with JavaScript

I am looking to enhance the appearance of text on an HTML page by making it bold. I have implemented the following code: <script type="text/javascript" > function getSelectedText(){ if(window.getSelection){ ; return window.getSelect ...

Issues with Read More/Less functionality on a website - Troubleshooting with JavaScript, jQuery, and Bootstrap 4

I'm looking to create a simple, lightweight feature where clicking on a "read more" link will reveal a paragraph, and then clicking on a "read less" link will hide it again. My knowledge of JS and JQuery is quite limited. I'm currently utilizing ...

Utilize Flexbox to create ample space on the right side

Hi there, I've noticed that when I use display: flex, the right side of my row seems to have a slight gap. How can I go about fixing this issue? It's not very obvious in jsfiddle, but you can spot it using Firebug or a similar tool. Check out th ...

Having trouble getting the Lightbox video Gallery to function properly in Bootstrap

When building my website with bootstrap 4, I incorporated image and video galleries. To create a lightbox effect, I referenced . My CSS and jQuery script are implemented at the end of the body. The image gallery functions perfectly, indicating that the im ...

Could the issue lie with PHP or the caching system?

I'm encountering a frustrating issue with my simple test that I just can't seem to resolve. It seems like there's some sort of glitch involving images and PHP, as I can only display one image at a time. The script looks correct to me, so I&a ...

Where within Video.js can I modify the color of the large play button when the cursor hovers over the video?

After successfully changing the SCSS $primary-background-color to orange using the video.js default skin editor (CodePen), I encountered an issue. Whenever I hover my mouse cursor over the video, the big play button background reverts to its default grayis ...