Unusual padding anomaly seen with inline elements (bottom padding is applied correctly, while top padding is not)

I had a good understanding of how inline elements function. But I came across a helpful explanation here: CSS display: inline vs inline-block.

Regarding inline elements:

  1. respect left & right margins and padding, but not top & bottom

Inline elements only acknowledge right and left padding, disregarding any padding specified for top and bottom. However, my recent tests revealed an unusual behavior. When applying padding to an inline element, it seems to apply it to the left, right, and bottom but not the top.

Can anyone provide an explanation for this particular behavior?

<html>
    <head>
        <style>
            * {
                box-sizing: border-box;
                padding:0;
                margin:0;
            }
            .parent{
                display:inline;
                background-color: red;
                padding: 10rem;
                color:white;
            }
        </style>
    </head>
    <body>
        <div class="parent">Whatever</div>
    </body>
</html>

Answer №1

Utilize the inspect element feature in your browser to observe that there is a padding-top of 10em present, which may not be visible in the snippet provided.

The rationale behind this: Even though there is padding for inline elements, it does not impact the space above and below it - the line (i.e. baseline for the text) remains at the same vertical position with or without the padding. The padding simply generates an overflowing area that only becomes evident if a background color is defined.

Refer to my code snippet where I introduced a wrapper with a 12em padding-top, along with some text before and after the inline div, as well as preceding and following the wrapper div to illustrate the behavior explained.

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

.wrap1 {
  padding-top: 12em;
  background: green;
  /* display: block; is the default here */
}

.parent {
  display: inline;
  background-color: red;
  padding: 10rem;
  color: white;
}

.y {
  background: rgba(255, 255, 0, 0.4);
  border-radius: 50%;
  padding: 0.6em 0.15em 0.6em 0.1em;
}
<body>
  <div>This is in a separate div which preceds the inline div and its wrapper.</div>
  <div class="wrap1">
    this is on the same the line
    <div class="parent">Whatever</div> yes it is
  </div>
  <div>And this is in a separate div following the inline div and its wrapper.</div>
  <p>And here is another line with one (inline) <span class="y">word</span> that has a padding in a way that <em>might</em> be considered useful.</p>
</body>

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 approach to styling a sub-child element within the first child using TailwindCSS?

Currently working with React (Next.js) and TailwindCSS. <ul> <li> <h1 className="bg-red-400">first</h1> </li> <li> <h1 className="bg-green-400">second</h1> </li> &l ...

Having trouble getting the img src to work in Django 2.1 template?

I'm having trouble displaying images in my Django template file. Despite uploading the images to media static files, they do not appear on the template. When I click on the image link in the Django admin page, it shows a "Page not found(404)" error me ...

Arrange the array in chronological order based on the month and year

I'm looking for assistance with sorting an array by month and year to display on a chart in the correct order. Array1: ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'....]; Desired Output: ...

The issue of HTML Service Submit Form not triggering the google.script.run function

Everything was going smoothly with my script until it suddenly stopped functioning properly. A user selects an option from a custom menu, triggering a dialog box (HTML Service form) to collect two parameters as intended. Upon submitting the form, the fol ...

Show a <textarea> for every dynamically generated content

On a dynamically generated page, I have listed questions along with multiple choice options. I need to provide a way for the content management team to add notes for each question if they find any errors. Currently, when clicking on the 'Note' op ...

Urllib2 retrieves HTML source code from various websites

Seeking assistance with retrieving the HTML code of a specific page using urllib2. The code provided is not giving me the exact HTML as displayed in my browser... Here is my current code: import urllib2 request = urllib2.Request('http://fiverr.com/l ...

Modify the paragraph's class upon clicking the radio button

I have been struggling with changing the color of <p> based on the radio button selected using two classes, red and blue. However, for some reason, it's not working as expected. If anyone has any insights or suggestions on how to fix this issue ...

Utilize Jquery for Parsing a Json Document

I am currently facing challenges parsing a JSON file into my HTML page. Below is the structure of my JSON file: { "menu":[ { "id":"contact", "leaf":true, "description":"testing", "link":"", "content":" ...

Running the gulp uncss command with regex to ignore specific elements is not functioning as expected

I have been attempting to integrate uncss into my gulp workflow. In order to exclude certain classes, such as those added through javascript, I am specifying these classes with "ignore" (specifically, I am trying to remove the css from the jquery plugin m ...

Use HTML accept-charset to specify the character encoding for form fields

Curious if a form field could handle characters with accents like Ś or ý and display them as the raw letter (Ś or ý) instead of "random symbols" like percents upon submission. Is this achievable? ...

Do the Push Notification APIs in Chrome and Firefox OS follow the same set of guidelines and standards?

Do Chrome and Firefox OS both use Push Notifications APIs that adhere to the same standard? If not, is either one moving towards standardization? ...

My challenges with optimizing positioning in Media Queries for Hover Buttons

After completing the coding for all Smartphone devices, I moved on to working on Tablets. However, I encountered an issue during this process as I am unsure of how to fix it. Here is a preview of my "Section Skills" layout for mobile: View Section on Mobi ...

What is a method to position an <img> on top of text without obscuring the text or resorting to CSS background or text-indent

To view the related code snippet, click on this link: http://jsfiddle.net/Ws8ux/ Is there a way to position the text under the logo without hiding it through display:none or text-indent? I am aiming to bring the image up while keeping the logo in the back ...

Styling in CSS for aligning a drop-down menu to the right side

I recently incorporated a CSS/JavaScript drop-down menu on my website, which I sourced from the following page: However, I am facing an issue with aligning the far-right drop-down properly. Specifically, I would like the items in the far-right drop-down t ...

JavaScript and CSS animations out of sync in terms of timing

I've been encountering some issues. I have an array containing 5 lines of text that change with a timer, but it seems that the css timer animation might not be synced properly or my @keyframes slidesdown setup could be incorrect. The delay between te ...

jQuery regex failing to display latest changes

I am running into some issues with my custom jQuery snippet that is supposed to dynamically display the results. Each H2 tag contains an image, and I want to ensure the image is positioned correctly next to the word "test." Here is the script in question: ...

The website appears as I envisioned it when using Raw HTML, but when implementing Django, a strange blank space materializes within the for loop

<div id="Project" class="bg-white fnt-white brdr project-div "> <div class="float-left image-for-project-container brdr"> <img src="{% static './Images/manworking.webp' %}" ...

Accessing a variable from an external JavaScript file using jQuery

Can someone help me out with this issue I'm facing? I am having trouble getting a string returned to a variable in my embedded Javascript code. token.js: function token () { return "mysecretstring"; } HTML Code: <!DOCTYPE html> <h ...

Combine the elements within the HEAD tag into a group

I am currently developing a dynamic page system using AJAX. Whenever a link is clicked, an AJAX call is made to retrieve data from the requested page. However, I have encountered an issue where certain data in the page file needs to be placed within the h ...

The printing function for the window system can cause disruptions to the layout of the table

After creating a page with a simple table that can be filled in and printed, I noticed some issues with the table formatting after printing. The intended table design was supposed to look like this: https://i.stack.imgur.com/aAk89.png However, upon print ...