Is there a way to adjust the margin/padding percentage based on height rather than width by default in CSS?

When we define margin-top:10%;/*(for example)*/, the margin-top is typically 10% of the page's width. However, I am looking to have it be 10% of the page's height instead.

Answer №1

The margin percentage will always be based on the width of the containing element, as that is how it is defined and functions. For top and bottom margins, the only option for flexibility is using the auto value which allows the browser to calculate the appropriate margin automatically. It is not possible to have the percentage apply to the height of the element. Similarly, the padding-top declaration follows the same principle when dealing with percentages.

To achieve a specific margin percentage based on height, you may need to introduce an additional container above the element with the desired height percentage specified. The effectiveness of this approach may vary

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

Using CSS to style an alternate list elements to float

I'm working on a webpage that displays messages from a database using ajax. Currently, the output is just stacked downwards. I'm hoping to style the list elements to alternate from left to right, similar to an iOS Message chat. Does anyone have ...

Unexpected changes are observed in the size of DIV elements when adjusting the SVG viewBox

My aim is to make the <svg> element have the same dimensions for the viewBox as the <div> it is inside. This means that the viewBox values need to match the dimensions of the containing <div>. Here is an example: <div style="width ...

`Troubleshooting website design`

Hello, I am relatively new to the world of design and would like to expand my knowledge in this area. Currently, I am facing an issue with my website where the content is being hidden underneath the footer as it grows. Can anyone provide guidance on which ...

Creating a gradient border that rotates 360 degrees on mouse hover

.brand-img::after { content: ''; position: relative; background-image: url('https://i.sstatic.net/Y2vyB.png'); background-repeat: no-repeat; float: left; margin-left: 15px; transition: all 1.8s ease; width: 135px; height: 135px ...

Focus on a specific div element while bypassing its parent

Can I directly target the element with .div-3 class, even if its parent does not have an id or class? <div class="wrapper"> <div> <div> <div class="div-3"> </div> </div> </div> </div> I am aware ...

Is it possible to automatically scroll the contents inside a div tag?

I have a large amount of data within a div. To manage this, I've utilized the overflow property to hide the excess content. Now, my goal is to automatically scroll the scrollbar to the left in order to display the 4th, 5th, and 6th elements of the fir ...

What is the best way to display multiple divs in a single location?

I am facing an issue with displaying different text on mouseover for three buttons. I successfully implemented it for one button, but when I added the other two, it stopped working. Here is the code I used for the first button: Using jQuery- document.get ...

Creating a line with CSS is a straightforward process that involves using properties

I am trying to achieve a yellow line effect similar to the image shown using CSS. I have managed to create line holes so far, but I'm struggling with creating vertical straight lines. Here is an example of my current code: https://i.sstatic.net/MqRUE ...

Utilizing the '::marker' pseudo-element for generating Markdown-inspired headers

This code snippet is functioning correctly, however, I have a suggestion to make it more appropriate. Instead of using '::before', why not try using '::marker'? I attempted this adjustment but encountered an issue. Can anyone explain wh ...

Displaying random divs and subsequently animating them downwards using JavaScript

I am in the process of creating a random appearing div that then falls down. Here is the code I have so far: $(document).ready(function(){ $('#test').animate({top: 80 + '%'},900); }); <div id="test" style="background:#98bf21;heigh ...

Is there a way to retrieve a CSS file using AJAX from a separate origin?

Whenever I am trying to load a CSS file via AJAX from my dreamhost account, I encounter the error message that says No 'Access-Control-Allow-Origin' header is present on the requested resource. After searching for solutions online, I stumbled upo ...

How can I enhance the appearance of my custom field using CSS in Advanced Custom Fields?

Utilizing the Wordpress Advanced custom field plugin, I have successfully added a custom field to my site. Within this custom field, I have inserted images and now I am looking to center them on my page using CSS styles such as margin: 0 auto. If you&apo ...

Anti-aliasing with @font-face in Internet Explorer versions 7 through 9

Having some trouble getting @font-face to display correctly in IE, and I haven't found a solution yet. I've looked into this resource: but unfortunately it didn't work on my Windows 7 IE. Does anyone have a better suggestion? ...

Looking to maintain the black font color of the menu when hovering over the submenu

When using the superfish menu, I encountered an issue where the parent menu color changes to white when hovering over the submenu. I would like to keep the parent menu font color black instead of changing it to white. Is there a way to accomplish this? Any ...

React Bootstrap ToggleButton firing function multiple times unnecessarily

I've encountered an issue with my react-bootstrap ToggleButtons. Whenever I click on them, the handlePlatformChange() function is triggered twice - first with the correct id and then immediately after with null. I tried including e.preventDefault() in ...

Aligning images in a mobile-friendly layout

I'm currently designing a website using Bootstrap and FontAwesome, which involves clickable images and font-awesome elements overlapping. I have devised the following code: <!doctype html> <html lang="en"> <head> < ...

What is the CSS method for styling a color label on an active input box?

I am trying to achieve a specific effect where the label changes color when the input box is in focus. However, my current CSS code does not seem to be working as expected. Can someone please explain why and provide a solution to fix this issue? .test-l ...

Personalized designing of each letter

Is there a way to style numbers without using something like <span></span>? I'm looking for a clean way to do this for each sign. Attached is an example showing a "flip clock" effect that I would like to achieve. I have come across plugi ...

Using a Bootstrap Glyphicon on an ASP.NET Button

Is there a way to utilize glyphicons as icons for ASP Buttons using C# instead of HTML? Normally, you would apply the proper glyphicon CSS to an <i> element and then nest it under the desired button. But I want to accomplish this programmatically. ...

adjust image positioning without compromising text layout

Trying to move a clickable image to the right of text, but encountering issues: Initially used position: absolute;, which resulted in overlapping when resizing page: https://i.sstatic.net/fkdUb.png Switched position to relative, now text is not moving as ...