Unexpected behavior when using padding on the left side of a vertical position

I can't seem to figure out a strange issue. I have a div containing multiple paragraphs, but when I adjust the padding left within the div to align all paragraphs to the left, they not only shift to the left but also move up as if I'm positioning them vertically upwards. After some testing, it appears that increasing the left padding on the parent div causes the paragraphs inside to move further up towards the top. I searched W3C standards for information on padding-left and its relationship with margin-top or bottom, but found no relevant details. Similarly, I scoured StackOverflow for help but came up empty-handed.

While adding more margin-top to the paragraphs within the div might solve the problem by placing them where I want, that's not the solution I'm after. I want to understand why applying padding-left to the parent div not only shifts its paragraph children to the right but also moves them upward as I increase the padding-left value?

UPDATE: Here is the code snippet for better clarity:

Html:

<div id="home-page">
    <div id="intro-page" class="animated FadeInLeft">
        <canvas class="animated fadeInUp" id="canvas"></canvas>

        <p class="animated fadeInUp" id="p1">Lorem ipsum dolor sit amet,  <br> consectetuer adipiscing elit!</p>
        <p class="animated fadeInUp" id="quote">“consectetuer adipiscing elit, <br>no, and WOW! consectetuer adipiscing elit.” <br> consectetuer adipiscing elit</p>
        <hr class="animated fadeInUpBig">
    </div>
</div>

Css: The problematic div is "intro-page". Modify the comment in its css padding-left rule to experience the issue

* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Roboto', sans-serif;
}



body {
background-color: #000;
}

#home-page {
height: 100vh;
width: 100%;

}

#intro-page {
width: calc(100% - 120px);
height: 100vh;
background: url(images/light-bulb.jpeg);
background-size: cover;
display: inline-block;
vertical-align: top;
overflow-y: hidden;
padding-left: 5%;  /* Change this padding to 50%   and see what I 
mean*/
}

#intro-page:before {
content: "Hello!";
color: #ffffff;
position: absolute;
left: 6%;
top: 20%;
font-size: 700%;
animation: upAndDown 6s linear infinite;
font-family: 'PT Serif', serif;
}

@keyframes upAndDown {
0% {
    transform: translateY(-20%);
}

50% {
    transform: translateY(0%);
}


100% {
    transform: translateY(-20%);
}
}

#intro-page p {
color: #bababa;
font-family: 'Roboto Condensed', sans-serif;
}

#intro-page #p1 {
font-size: 240%;
margin-top: 26%;
line-height: 50px;
}

#intro-page #quote {
margin-top: 5%;
font-size: 110%;
font-weight: 300;
letter-spacing: 1px;
}

#intro-page hr {
width: 32%;
margin-top: 2%;
}

Answer №1

According to the margin properties outlined in the W3C Spec:

The percentage is calculated based on the width of the generated box's containing block. This applies to 'margin-top' and 'margin-bottom' as well. If the width of the containing block is impacted by this element, the layout may be undefined in CSS 2.1.

This css section includes:

#intro-page #p1 {
font-size: 240%;
margin-top: 26%;
line-height: 50px;
}

Increasing the padding of #intro-page will decrease the paragraph's width, therefore decreasing the top margin.

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

Utilize the $post parameter when sending data in AngularJS

Trying to send Handsontable table data using $http POST with Angular.js has been a bit of a struggle for me. Here's the code snippet: var $container = $("div#table"); var handsontable = $container.data('handsontable'); $scope.sa ...

turning off row rearrangement feature in jquery datatable

I am currently utilizing the DataTables Row Reordering Add-on (http://jquery-datatables-row-reordering.googlecode.com/svn/trunk/index.html ) and I am seeking a way to deactivate the reordering using JavaScript. I have attempted to use the following code sn ...

Designing Games with Matrix Elements

Greetings to the StackOverflow Community! Currently, I am working on developing a JavaScript version of the well-known game Pentago as a personal side project before resuming my studies in the fall. However, I have hit a roadblock and need some guidance o ...

Synchronization-free API and callback functions

I am in need of utilizing an asynchronous service. My current approach involves sending data to this service using PHP and CURL, as well as receiving data from a URL provided by the service. How can I effectively respond or wait for feedback from this serv ...

What is the quickest method for cycling through the most ancient elements in a collection? (Efficient way to execute a queue)

See edit too I'm currently working in JavaScript, but I believe any readable pseudocode may be able to help answer my question. Describing my issue might be a bit of a challenge, so feel free to ask for clarifications. I'll respond promptly and ...

Adding a slight 1px right margin between an HTML table and its enclosing div in Bootstrap 3

Help! I'm trying to troubleshoot some HTML issues and can't figure out where this pesky 1px gap between my table and the wrapping div is coming from... Just for reference, I am using Bootstrap. I suspect that it could be a glitch in the Bootst ...

Having trouble with the iPhone keypad's 'go' function not working properly when trying to submit using jQuery

My webpage has a form with a submit button that triggers a jquery POST request when clicked, logging the user in. Everything works perfectly when I use the submit button on the page. However, if I press 'Go' on the numeric keypad, it simply refre ...

What is the correct method for successfully incorporating Vue Router through CDN to enable the functionality of history mode?

I insert scripts into an html document in the following way: <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> After adding ...

Dynamic table generation in Next.js based on specific conditions

I have a table of values with varying locations. My goal is to filter the rows in the database based on the location value provided. Here's the code snippet: 'use client'; import { useState, useEffect } from 'react'; import { io } ...

What is the reason for Chrome allowing the preflight OPTIONS request of an authenticated CORS request to function, while Firefox does not?

As I develop a JavaScript client to be integrated into third-party websites, similar to the Facebook Like button, I encounter a challenge. The client needs to access information from an API that requires basic HTTP authentication. Here is how the setup is ...

What could be the reason behind the warning message "MaxListenersExceededWarning" in Node.js?

My Express.js backend server setup is pretty straightforward: const app = require("express")(); const dotenv = require("dotenv"); const bodyParser = require("body-parser"); const cookieParser = require("cookie-parser" ...

Choose a div element based on its title attribute using jQuery

My webpage includes a div that holds multiple other divs without unique IDs assigned to them: <div id="container"> <div title ="jhon" style=" position:absolute; left: 821px; top: 778.44px; width:8px; height:9px; z-index:3;"> </div> < ...

What could be the reason for npm start failing to work following npm init?

Starting a new project using NPM : npm init Installed the socket.io-client package. package.json: { "name": "client", "version": "1.0.0", "description": "", "main": "script.js&qu ...

Transferring information from a Jade file to a Node.js server

I'm currently working on creating a data object within my Jade view page that will be used in my server-side JS. The data object involves dynamic HTML generation that inserts input boxes based on user input. function addDetail() { var det ...

use the defer attribute within the script tag in the hbs document

When coding in JavaScript using Express, I encountered an issue with my index.hbs file, which is similar to an HTML file. I attempted to write the code inside a script tag as follows: <div class="container" style="display: flex; flex-d ...

Working with Ember.js to manage relationships using a select element (dropdown) for create/edit operations

I'm trying to establish a relationship through a dropdown in my Ember application. Here is my Books model: import DS from 'ember-data'; export default DS.Model.extend({ // Relationships author: DS.belongsTo('author'), name ...

Add exclusively fresh entries to the Vue JS table

In my database table, new records are inserted every 10 seconds. Using Vue and Axios, I am displaying the latest 20 records to the user in an HTML table. In the created hook, I have implemented a method that fetches data every 5 seconds. window.setInter ...

Exploring through an array tree structure?

My menu displays the "selected" array as options. When an option is selected, its branches are rendered as new options. To keep track of the traversal, I create an array called select. For example, if someone selects the 3rd option, then the 1st option, a ...

Using JQuery to deactivate a button based on a specific select option

Once a user selects and submits an option from the dropdown list of collections in my form, I aim to disable the button so that they cannot submit that same collection again. Although Ajax is functioning properly and successfully disables the button upon s ...

Apply specific margins to every second element in media queries using the margins that were previously set for every third element

Looking to adjust the margin for every second element instead of every third when using media queries. .title:nth-child(3n+3) {margin-right:0 !important} @media screen and (max-width:1160px) and (min-width:900px){ .title:nth-child(2n+2) {margin-right:0 ! ...