Having trouble grasping the concept of padding in CSS when using percentage values

Is it silly of me to ask this question? I haven't found a satisfactory answer anywhere, so I'll ask anyway.

I'm working on creating a responsive webpage where I need to set the padding of a div named content that contains another div called text. This content div is inside a container element with predefined height, width, and position: relative properties. The issue arises when I set padding: 45% 45%; - it works fine for width but overflows the parent container in terms of height.

.container
{
box-sizing: border-box;
  position: relative;
  border: 1px;
height: 100px;
width: 600px;
  margin:0 auto;
}

.content{
    background-color: skyblue;
    padding: 43% 43%;

}
#textlogo {
font-size: 4em;
}
<div class="container">
  <div class="content">
  <div id="textlogo">Text</div> 
</div> </div>

Answer №1

It appears that the issue lies within the box-sizing property,

To resolve this, try adjusting the box-sizing: padding-box, which allows the browser to calculate the total width of the element including the padding, eliminating any overlap. Additionally, your current padding may be set too high, contributing to the problem.

Keep in mind that the overflow is likely due to the large size specified in font-size: 4em. This oversized font will cause the content div to expand in order to accommodate it, particularly the #textlogo content.

Answer №2

It is advised to limit the use of the padding property in CSS, dating all the way back to Internet Explorer 4 where the margin property functioned correctly.

Instead of applying padding to the parent element, consider using margin on the child element. Using padding on the parent will automatically affect all child elements. To apply margin to all child elements selectively, utilize the #parent > div selector and then individually adjust secondary margins for specific elements.

Using padding is recommended if there are no child elements present or if you encounter rendering bugs in engines or standards (e.g., when implied "logic" is employed instead of direct logic).

While the box-sizing property is widely supported now, taking shortcuts like this may hinder a true grasp of CSS fundamentals as originally intended.

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 could be the reason for my user input not being captured and saved as variable data on the HTML webpage?

Here is the code I am working with: var g = document.getElementById("al").value; function start() { document.getElementById("test2").innerHTML = typeof(g) + parseFloat(g); } <p id="test2">Output will be displayed here:</p> <form> ...

Is there a way to efficiently update the CSS class attribute for all list items using just pure JavaScript?

I am looking to use pure Javascript, not jQuery, to access all elements within a <ul> list and remove the active class from every item except for the one selected in my menu. Shown below is the list: <ul id='flash-menu'> <li id=" ...

Retrieve the checkbox value and assign it to an object in the $scope of an Angular

As a newcomer to angularjs, I've been struggling with this code snippet for the past two days. My goal is to retrieve values from selected checkboxes within my angular js controller. I have experimented with both ng-true-value and ng-false-value, bu ...

Full width display without any scrollbars

I need some help with adjusting the width of a section on my webpage to fit the browser size. Currently, I am using width: 100%, but this is causing scrollbars to appear. Unfortunately, I can't use overflow-x: hidden; as it will hide some of the conte ...

Alpha 4.0.3 Bootstrap Carousel with Shadow

Hello there, I've been tinkering with the bootstrap website and I'm looking to add an inset shadow effect to the carousel. Here's a snippet of the HTML: <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"& ...

Creating multiple div columns dynamically with jQuery

Check out the code snippet below: <div id="container"> <div class="b">test1</div> <div class="b">test2</div> <div class="b">test3</div> <div class="b">test4</div> <div class="b"& ...

What is the best way to create a dynamic textbox or label within a specific div class?

I need assistance with my dynamic asp.net page where I am generating labels and text boxes in a form. Is there a way to ensure that these elements are generated within a specific div class? Below are the relevant code snippets: for (int i = 0; i < cou ...

Different width can be set for the Google Maps template specifically for mobile mode by following these steps

Is there a way to adjust the width of a Google Maps template specifically for mobile mode? While it looks perfect on desktop, the map overflows the screen size on mobile. See my code snippet below: @section ('map') <div id="map"></d ...

Include a scroll bar for menu items that contain submenus, while ensuring that the submenu items remain visible

Looking for a way to add a scrollbar to the courses menu list due to its excessive length (refer to the image below). The fixed top navbar prevents scrolling, making it necessary to implement a scrollbar within the menu itself. After applying the followin ...

Unable to generate dynamic HTML through AJAX request

I made an Ajax API call and received the response in JSON format. I need to create dynamic HTML to display each value in HTML elements. I tried getting the response from the API but couldn't generate the HTML. Can someone assist me with this? Also, I ...

What is the best way to assign a percentage width based on a variable in jQuery?

Is there a way to assign a dynamic width value to an element? Here is the code I am using: $(".menu_list_item").css({ width: width + "%" }); Unfortunately, this doesn't appear to be functioning correctly. If anyo ...

What are the steps to make a unique and eye-catching text underline animation?

I'm encountering an issue while attempting to create a hover animation for an <a> tag with an underline effect, similar to the example showcased on this particular webpage. Even though the tutorial is readily available, my implementation seems t ...

Turn off browser feature that automatically adds overflow:hidden on touch screen devices

Encountering a problem with browsers (potentially Chrome) adding CSS rules on touch-enabled devices. In a website developed for a client (specifically for Chrome), the scroll bars disappear on elements when accessed from their ultrabooks. The client still ...

Show the appropriate tooltip message based on the circle that I hover over

I have a collection of 5 clickable circles, each with a unique ID and its own tooltip text that is sourced from an xml Data Interface. jQuery(function ($) { $.ajax({ type: "GET", url: 'steel_agg_bof_flash_en.xml', ...

A miniature triangle-shaped bubble crafted with 1px of CSS3 styling

Currently, I am experimenting with CSS3 and creating bubble shapes. However, I have encountered an issue. I am trying to create a triangle with a 1px border, but the result is a thicker triangle than desired. You can view my code on this fiddle : FIDDLE ...

The Guide to Utilizing Autonomous Models in Directive

Hey there! I have a question regarding a radio button directive. In this directive, I pass a model for the collection that will be set to the selected radio button. var app = ChartsModules.getInstance().getModule("amkaiCharts.directives"); ...

Interactive image sliders in a Netflix-inspired style with live previews on hover, fully compatible with Bootstrap framework

I'm looking for suggestions on Twitter Bootstrap compatible jquery plugins that can create a Netflix-style continuously scrolling image carousel with mouse-over functionality. I've tried the carousel included in the Bootstrap JS library, but it r ...

What could be the reason that the results of my quick sort function are not appearing on the screen

I'm having trouble getting any output from this code. Can someone help me figure out what's wrong? function Ascending() { var array = new Array(); array[0]=parseInt(document.getElementById("1").value); array[1]=parseInt(document.getElementById(" ...

Guide on creating HTML content within a ToolTip function for a table row

I'm working on implementing a tooltip feature that will appear when hovering over a row with extensive HTML content. This feature is intended to be used alongside Foundation framework. However, I am encountering issues getting the tooltip to work prop ...

What are some effective CSS styles that can be used to illustrate the concept of "loading . . ."?

I've been leveraging the power of blockUI successfully, but now I'm faced with the challenge of dynamically loading a table row. My research indicates that blockUI may not be compatible with HTML table rows. My idea is to use: jquery.AddClass(" ...