How to make an element width: 100% minus padding ? Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will see different examples to make an element's width: of 100% minus padding. We have a few methods to make an element width: 100% minus padding which are described in detail below. Table of ContentUsing 'calc()' and 'box-sizing'Using Flexbox Container MethodApproach 1: Using 'calc()' and 'box-sizing'Here, we are using CSS properties like 'calc()' and 'box-sizing' for creating responsive and adjusting proper element width '100% minus padding'. These properties also assure consistent and visually appealing layouts across various devices and screen sizes.Example: In this example, we are using the 'calc()' and 'box-sizing' to make an element width: 100% minus padding. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styles.css"> <title>Element Width Examples</title> </head> <body> <div class="container"> <h1 class="geeks-title"> GeeksforGeeks </h1> <div class="example"> <h2>Without 100% Minus Padding</h2> <p> This input element has its width set to 100%, including padding. </p> <div class="input-container"> <label for="input-with-padding"> Input without 100% - padding: </label> <input type="text" id="input-with-padding" class="input-with-padding" placeholder="Enter text"> </div> </div> <div class="example"> <h2>With 100% Minus Padding</h2> <p> This input element uses <code>width: 100% - padding</code> to maintain full width after padding. </p> <div class="input-container"> <label for="input-minus-padding"> Input with 100% - padding: </label> <input type="text" id="input-minus-padding" class="input-minus-padding" placeholder="Enter text"> </div> </div> </div> </body> </html> CSS /* style.css */ body { font-family: Arial, sans-serif; background-color: #f0f0f0; margin: 0; padding: 0; } .container { max-width: 800px; margin: 0 auto; padding: 20px; } .geeks-title { color: green; text-align: center; } .example { margin: 20px 0; padding: 20px; background-color: #fff; border: 1px solid #ccc; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .input-container { margin-top: 15px; } .input-with-padding, .input-minus-padding { padding: 10px; border: 2px solid #ccc; width: 100%; transition: border-color 0.3s; } .input-minus-padding { width: calc(100% - 20px); /* Subtracting 20px from the width to account for padding */ box-sizing: border-box; /* Including padding and border in the calculated width */ border-color: dodgerblue; background-color: #f5faff; } .input-with-padding:focus, .input-minus-padding:focus { border-color: dodgerblue; } Output:Approach 2: Using Flexbox Container MethodHere, we are achieving the "width: 100% - padding" for an element by involving the flex container ('display: flex') with the centered content alignment of ('justify-content: center'). This assures that the element's width is the full width of its parent while taking the specified padding. Example: In this example, we are using Flexbox Container Method to make an element width: 100% minus padding. HTML <!--Example 2--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styles.css"> <title>Element Width Examples</title> </head> <body> <div class="container"> <h1 class="geeks-title"> GeeksforGeeks </h1> <div class="example"> <h2>Without 100% Minus Padding</h2> <p> This <code><div></code> element has its width set to 100%, including padding. </p> <div class="box-with-padding"> Content without 100% minus padding </div> </div> <div class="example"> <h2>With 100% Minus Padding</h2> <p> This <code><div></code> element uses a flex container to achieve <code>width: 100% - padding</code>. </p> <div class="box-minus-padding-container"> <div class="box-minus-padding"> Content with padding </div> </div> </div> </div> </body> </html> CSS /* style.css*/ body { font-family: Arial, sans-serif; background-color: #f0f0f0; margin: 0; padding: 0; } .container { max-width: 800px; margin: 0 auto; padding: 20px; } .geeks-title { color: green; text-align: center; } .example { margin: 20px 0; padding: 20px; background-color: #fff; border: 1px solid #ccc; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .box-with-padding, .box-minus-padding { padding: 10px; margin-bottom: 10px; color: white; text-align: center; } .box-with-padding { background-color: #007bff; /* Blue color for the box with padding */ } .box-minus-padding-container { width: 100%; padding: 10px; /* Padding for the container */ box-sizing: border-box; background-color: #ffffff; /* Light blue background color */ } .box-minus-padding { background-color: #dc3545; /* Red color for the box with minus padding */ } .box-minus-padding-container { display: flex; justify-content: center; } Output: Comment More infoAdvertise with us Next Article CSS Introduction G gauravggeeksforgeeks Follow Improve Article Tags : Web Technologies CSS CSS-Questions Similar Reads CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or 7 min read CSS Introduction CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable.It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the 4 min read CSS Syntax CSS is written as a rule set, which consists of a selector and a declaration block. The basic syntax of CSS is as follows:The selector is a targeted HTML element or elements to which we have to apply styling.The Declaration Block or " { } " is a block in which we write our CSS.HTML<html> <h 2 min read CSS Selectors CSS Selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.Basic CSS Selectors: These are used to target elements by tag, .class, or # ID for fundamental styling needs.Combinato 7 min read CSS Comments CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works. 2 min read CSS Colors CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more.You can try different formats of colors here- #content-iframe 5 min read CSS Borders Borders in CSS are used to create a visible outline around an element. They can be customized in terms ofWidth: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ he 5 min read CSS Margins CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability.Syntax:body { margin: value;}HTML<html> <head> 4 min read CSS Height and Width Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto.Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centim 4 min read CSS Outline CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element.Syntaxselector{ outline: outline-width outline-type outline-color 4 min read Like