Is it possible to achieve HTML padding with full width and height?

Is there a way to make a <div> completely fill up a parent div, while still maintaining a 5% gap around its content? Applying the rules

width: 100%; height: 100%; padding: 5%;
achieves the desired effect for the <div>, but causes it to exceed its parent element's bounds. How can this be done without overflowing?

I'm seeking a solution that ensures the <div>'s bounds take up 100% of its parent's and retains a 5% padding. While hacky solutions may exist, I am interested in an elegant and future-proof approach. Modern CSS technologies like Grid and Flexbox could potentially provide a cleaner solution to positioning and sizing issues with individual divs.

Answer №1

To ensure that padding and border are included in the total width and height of elements, you can use box-sizing:border-box:

For example:

*{
   box-sizing: border-box;
   -webkit-box-sizing: border-box;
   -ms-box-sizing: border-box;
   -moz-box-sizing: border-box;
}

Make sure to check this code snippet for reference:

body {
  margin:0;
  padding:0;
}
.demo {
  width:100%;
  height:100vh;
  float:left;
  background:#000;
  padding:5%;
  box-sizing:border-box;
}
<div class="demo"></div>

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

What is the best way to overlay text onto a background image using Material UI's Card and CardMedia components?

Using the card component, I have successfully added a background image. However, I am facing difficulty in figuring out how to overlay text on top of this image. If anyone has suggestions or alternative methods, please feel free to share! <Card> ...

Hiding an image when pressing on a navbar icon: Tips and tricks

How can I toggle the visibility of an image when the bars icon is pressed on smaller screens, using CSS without JQuery or JavaScript? I have attempted to hide the image with visibility: hidden; in CSS but it does not work as intended. Please run this cod ...

Differences in color rendering between XAML WPF and HTML

After migrating my app from WPF to HTML, CSS, and JavaScript, I noticed a discrepancy in color representation. Despite using the same hex value, the HTML color appears lighter than in WPF. See the comparison image here: https://i.sstatic.net/vB7rR.png In ...

Using Python2.7 and HTMLParser to retrieve a string from HTML

In the process of developing an Alexa skill, I am restricted to using Python (2.7) libraries without access to BeautifulSoup4. Despite this limitation, I aim to extract a specific string ('104 spaces') from a larger HTML page that contains a cons ...

When scrolling, apply a CSS class to a div element once it becomes visible on the

I'm in the process of developing a timeline feature for my website, and I am facing an issue where the addClass function is being applied to all sections, even those that are not currently visible on the screen during scrolling. If you would like to ...

Issues with HTML underline <u></u> not functioning properly in RDLC report

I am facing an issue with my RDLC report where the HTML formatting is not displaying as expected. I have set the markup type to "HTML - Interpret HTML tags as styles" and most of the text displays correctly. However, the "<u> Underline Text </u& ...

Ways to position two buttons in each corner with flexbox in a react js application

I'm fairly new to React JS and have just started building a simple React app. I'm trying to place two buttons on each side using CSS flex, but I'm having trouble achieving it. Can anyone help point out where I might be going wrong? im ...

Safari seems to be disregarding the CSS transform-origin property, and setting `transform-box: fill-box` is not

Currently, I am using transform: scale(0.8, 1) to adjust the width of two-digit numbers on a clock face. However, without implementing transform-origin, the numbers do not stay in their correct positions. The transform-origin that I have set works perfect ...

Guide on relocating the label to the left side of the input fields

Can anyone assist me with aligning labels to the left of input fields in PrimeNG? I am unsure of how to do this. Here is a visual representation of what I am trying to achieve: https://i.sstatic.net/qifpm.png Below is the code that I currently have: Vie ...

How about trying out some dropdowns?

Following the issue I mentioned in my previous question titled Margin-Right on CSS not working, I am currently engaged in a creative endeavor to redesign my school's website homepage. The progress so far has been significant, but I now require some as ...

Tips on how to implement a gravity effect using JavaScript

Google gravity and gravity script are both impressive showcases of gravitational effects. However, there is a lack of access to source code or tutorials, and the original JavaScript files can be quite large. How can I implement a Gravity effect with Drag ...

Is it possible to customize the appearance of password fields with reduced dot size in Safari on iOS 12?

Has anyone experienced the issue of iOS 12 Safari displaying huge dots in the password input field? I'm looking for a way to make them look like they do in Chrome. Is there a known method to customize the style of these inputs using CSS? <input ty ...

Bootstrap-4 grid system is effectively maintaining its responsiveness without collapsing

I'm struggling to display one image per row on smaller screens using the sm breakpoint. This is my first attempt at using bootstrap 4. I followed their grid system guidelines which stated that responsive breaks occur when specifying a size for each c ...

If you utilize a span element with the attribute role="textbox" and contenteditable set to true, the copying and pasting of text within it may not function correctly

Consider the following span: <span class="comment-box2" role="textbox" contentEditable=true></span> Using the following CSS: .comment-box2 { display: block; width: 100%; overflow: hidden; outline: none; ...

What's causing margin-top to be reversed while margin-bottom remains stationary?

I have encountered a specific issue that I can't seem to find a solution for. I am attempting to include the text "2017 Indie" at the bottom of the page. When I apply margin-top to a pixel value, the text remains unchanged and nothing happens. However ...

Version 1 of Vue.js is not compatible with Ajax-loaded HTML files

Currently, I am encountering a minor issue with loading content through ajax requests. I am in the process of developing a web application where everything is located on one page without any reloading. To achieve this, I have split the content into separa ...

Trouble with fetching data using Jquery ajax GET request

I have a list containing the IDs and action URLs of models. @foreach (var dashboard in Model.dashboard) { <li class="drop-text clickMe" data-id="@dashboard.DashID" data-action-url="@Url.Action("GetDashboard", "Dashboard")"& ...

Having trouble accessing the link from the dropdown list in Selenium

While trying to select a link from a dropdown menu, I am facing an issue with Selenium. The element is being located successfully, but it cannot be interacted with and the following exception is thrown: Error in thread "main" org.openqa.selenium.ElementNo ...

What is the best way to pass a form result from a parent component to a child component

In the setup, there is a child component containing a form with various options for selection. The goal is to capture the user's selection and process it within this child component. Despite attempting to utilize an event in the parent component, the ...

Dynamically assigning column values based on object properties

I am currently utilizing the Ionic Framework along with its grid system that is reminiscent of Bootstrap. However, I believe my query leans more towards AngularJS than specific Ionic components. Here is what I have: <ion-col *ngFor="let col of row ...