What is the best way to incorporate padding that scales with the browser's width while centering content using `display: table;`?

^As mentioned in the title^. I am looking to adjust padding based on the width of a browser. My attempt so far has been:

html, body{
  height: 100%;
}

body{
  display: table;
  margin: 0 auto;
}

#center{
  display: table-cell;
  vertical-align: middle;
}

.box{
  padding: 25%;
  background: #000;
  color: white;
}

h1{
  margin: 0;
  padding: 0;
}
<div id="center">
  <div class="box">
    <h1>Hello world</h1>
  </div>
</div>

However, this method did not produce the desired outcome. Are there any alternative approaches that can achieve this? My goal is to centrally align the div element using display: table;, without resorting to flexbox or position: absolute. Thank you.

Answer №1

Utilize the vw unit, which represents a percentage of the viewport width.

html, body{
  height: 100%;
}

body{
  display: table;
  margin: 0 auto;
}

#center{
  display: table-cell;
  vertical-align: middle;
}

.box{
  padding: 5vw;
  background: #000;
  color: white;
}

h1{
  margin: 0;
  padding: 0;
}
<div id="center">
  <div class="box">
    <h1>Hello world</h1>
  </div>
</div>

Visit this fiddle link to adjust the viewable area and observe the changes in padding size.

Answer №2

To achieve vertical centering, it is recommended to utilize flex-box.

html, body{
  height: 100%;
}

body{
  display: flex;
  flex-direction: column;
  justify-content: center
}

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

Exploring the Intersection of Windows 8 Store Applications and jQuery: Leveraging MSApp.execUnsafeLocalFunction

Developing a Windows 8 JavaScript Store App (using Cordova) has led to some complications when using jQuery. It seems that in order to utilize certain functions, I have had to modify the jQuery library by adding: MSApp.execUnsafeLocalFunction While this ...

Tips for accessing jQuery UI tab elements and adjusting visibility for specific panels

How can I retrieve the panel numbers of jQuery UI tabs and adjust their visibility using CSS? I am looking to identify the panel numbers of each tab and control the visibility of certain tabs. <div id="tabs"> <ul> <li><a href="#"> ...

Using the viewport meta tag in HTML and CSS within Laravel's Blade template while also

Greetings everyone! I have a question regarding Blade Laravel: Can we add a condition to the meta name="viewport" content="width=device-width, initial-scale=0.4"? I want the scale to start at 0.4 for smartphones and at 0.6 for tablets. ...

When the children within a CSS container are floated, set the height of the container to

I've established a simple layout <div class="container"> <div class="sidebar"> </div> <div class="content"> </div> <div style="clear:both;"></div> </div> where .sidebar and .content ...

Rotate CSS elements dynamically using jQuery

I'm trying to figure out how to change the rotation value of an image element in my code. Currently, the rotation is set to 315 degrees, but I want to change it to 0 when a click event occurs. Can anyone help me with this? ...

Apply CSS styles conditionally to an Angular component

Depending on the variable value, I want to change the style of the p-autocomplete component. A toggle input determines whether the variable is true or false. <div class="switch-inner"> <p [ngClass]="{'businessG': !toggle }" clas ...

Is it possible to rotate a group within an SVG without altering the orientation of the gradient fill it contains?

In my search on SO, I came across a lot of information about rotating gradients. However, my issue is the opposite. I have an icon created from multiple paths that I want to fill with a vertical gradient. I have successfully done this and it works well. ...

Using Jquery to swap out div elements and reinitialize code after selecting a menu <a href>link<</a>

I have a jQuery script that swaps out a div when a href="#1" is clicked. The same link, a href="#1", is then replaced by a href="#2" and vice versa. Here is the jQuery code: $('.child2, a[href="#1"]').hide() $('#replacepagelinks a').c ...

Choose from a variety of video play options

Being new to javascript, I've successfully combined two functions that control video playback, but they seem to be conflicting with each other. The first function is designed to offer custom pause and play controls for the video // VIDEO CONTROLS STA ...

Angucomplete Alternative solves the challenge of accessing remote URLs

I have been using the Angucomplete Alt directive for creating an autocomplete feature. It has been working well so far, but now I want to customize a specific request to be sent to my server. <div angucomplete-alt id="input-name" ...

With FlexLayout, the content compresses rather than shrinking and taking up the entire width

When utilizing Angular and the flex Layout to develop a dashboard, I am encountering an issue where the items do not shrink as expected when opening the navigation. This results in the last item being pushed to the next line. Below is the HTML for the pare ...

Creating duplicates of elements and generating unique IDs dynamically

I'm in the process of cloning some form elements and I need to generate dynamic IDs for them so that I can access their content later on. However, I'm not well-versed in Jquery/Javascript and could use some guidance. Here's a snippet of my ...

When hovering, the CSS border-bottom should extend

I am looking to create an animated border effect. When hovering over the link, I want the border-bottom to extend seamlessly from left to right. After much searching, I am still unsure what this effect is called. Below is the code I have: .a{ width ...

To change the font color to red when clicked, I must create a button using HTML, CSS, and Javascript

Currently, I am utilizing CodePen to assess my skills in creating a website. Specifically, I am focusing on the HTML component. My goal is to change the font color to blue for the phrase "Today is a beautiful sunny day!" Here is the snippet of code that I ...

The issue arises when attempting to render an SVG with JavaScript embedded inside using the img, object, or

Issue with the title ... please follow these steps: (view codes below) Create an svg + open it separately (name it keysaway.svg) Create html + open it individually When you observe, the svg displays a simple up and down animation but fails to work when l ...

Firefox experiencing trouble handling flexbox overflow

Having some trouble with a test project that involves using flexbox. The task at hand is to create a dashboard with multiple lists of cards arranged side-by-side with infinite overflow. I was able to achieve this layout, however, the issue arises when eac ...

How to resolve the issue of a sticky header not functioning properly with a resizable column in Primeng

Currently, I am facing an issue while trying to incorporate both column resize functionality and sticky header in my table. Interestingly, the sticky header feature works perfectly fine when used alone without the column resize. However, when I attempt to ...

Can an <option> element in WebKit be customized by adding an icon to it?

I have integrated a WebView into one of my software applications, and within the HTML page it is rendering, there is a <select> tag. I am interested in enhancing the <option> elements within this select tag by adding icons to them: (The shadow ...

I'm having trouble modifying the border color of a TableCell component in material-ui

I am struggling to change the border color of a table cell. Despite numerous attempts, I have been unsuccessful in my efforts. Can someone please assist me with this? const customTheme = createTheme({ overrides: { MuiTableCell: { root: { ...

Validation script needed for data list selection

<form action="order.php" method="post" name="myForm" id="dropdown" onsubmit="return(validate());"> <input list="From" name="From" autocomplete="off" type="text" placeholder="Starting Point"> <datalist id="From"> <option ...