Modify just one of the padding axis while keeping the other constant by utilizing a media query

My div currently has the following padding set:

padding: 14px 150px;

I want to change the left/right padding without affecting the top/bottom padding using a media query.

@media screen and (max-width: 700px){
     #paddingDiv{
         padding: same as before (t/b)  change (l/r)
     }
}

The solution I came up with is to adjust it like this:

@media screen and (max-width: 700px){
     #paddingDiv{
         padding-left: 14px;
         padding-right: 14px;
     }
}

Assuming the default properties for the div are as follows:

#paddingDiv{
  height: 200px;
  width: 400px;
  padding: 14px 150px;
  background-color: red;
}

Example.

Note: I don't want to alter the top/bottom padding. For instance, this approach is not what I'm looking for:

@media screen and (max-width: 700px){
     #paddingDiv{
         padding: 14px 14px;
     }
}

Note 2: My example showcases matching top/bottom and left/right padding values for clarity, but in reality, they could be different. To reiterate: I only want to modify the left/right padding while maintaining the top/bottom padding.

Is there a way to achieve this elegantly without duplicating the value for top/bottom padding?

Thank you!

Answer №1

After reviewing both the CSS Working Group specification and the documentation on MDN, it appears that there is no concise way to specify only horizontal padding in CSS.

MDN explains that the padding property serves as a shortcut to setting padding for all four sides individually (padding-top, padding-right, padding-bottom, padding-left).

The shorthand combinations available for the padding property include:

/* Apply to all four sides */
padding: 1em;

/* vertical | horizontal */
padding: 5% 10%;

/* top | horizontal | bottom */
padding: 1em 2em 2em; 

/* top | right | bottom | left */
padding: 2px 1em 0 1em;

For more information, please refer to:

MDN's explanation of padding.

The current draft from CSSWG.

W3 Box model padding properties.

Answer №2

The CSS specification does not mention a specific way to achieve this, but the suggested approach is to follow the implementation demonstrated in the second example.

@media (max-width: 1000px){
     padding-left: 14px;
     padding-right: 14px;
}

Answer №3

Below is a simple explanation of padding using shorthand notation. It specifies no padding on the top and bottom, while setting 14 pixels of padding on the right and left sides.

@media (max-width: 1000px){
     padding:0 14px;
}

Answer №4

If your goal is to adjust the padding in your design, there are a few options you can consider:

padding: 14px 150px;

The first value controls the top and bottom padding, while the second value determines the left and right padding. If you want all sides to have the same padding, you can simplify it with:

padding: 14px;

Alternatively, if you wish to remove the left and right padding entirely, you can do so by setting it to:

padding: 14px 0;

Keep in mind that when adjusting padding in a media query, you will need to redefine it accordingly. It's best practice to specify the new padding in a single line to optimize your code, based on the specific selectors you are targeting.

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

Issues with the local or browser display of @font-face

I'm really struggling to get my @font-face to display correctly, whether I view it locally or in the browser preview. This is the CSS code I am using: @font-face { font-family: chopin-script.regular; src: local('chopin-script.regular'), ...

How can you switch alignment from left to right when the current alignment settings are not functioning as expected in HTML?

I need the buttons +, count, and - to be right-aligned on my page. The numbers on the right are taking up the same amount of space, while the names on the left vary in length. I want the buttons to always remain in the same position, regardless of the name ...

The tooltip feature for icon buttons within Material UI list items is not functioning properly as anticipated

Just starting out with Material UI and React, I've encountered a strange UI problem that I can't quite figure out. Hopefully someone here can help me identify what I did wrong. My Approach: I have a List in my code where each list item has butto ...

Apply a border to the div that has been selected

I have a tool for storing information and I am using *ngFor to display each instance in a line. Is there a way to add a border when clicking on a line? The border should only appear on the clicked line, disappearing from the previous one if another line i ...

SVG: spin the entire text

I need assistance rotating a list of words at an angle, specifically tilting only the characters: Here is my code: <svg width="2000" height="130"> <g *ngFor="let fruit of fruits"> <g> <text [attr.x]="fruit.x" ...

How to position a Navbar on top of a grid background structure

I am currently working on developing a responsive website, and I have encountered a challenge that I need help with. I am looking to create a header using a grid system, where I have divided the header into two parts: col-md-4 : For an Image col-md-7 : ...

`Unable to activate Bootstrap dropdown feature`

Having an issue with the dropdown menu on my Bootstrap navbar - when I click it, nothing happens. I've tried various solutions found online, such as moving the <script type="text/javascript" src="js/jquery-3.3.1.min"></script> file to the ...

Difficulty displaying SVG in Opera browser when using the Raphael JS library

Currently, I am experimenting with Raphael JS to create visually appealing SVG graphics on a webpage. You can view my progress at . Most browsers display the graphics properly except for Opera 11. In Opera, the graphics fail to render when clicking on any ...

It appears that the query parameters are impacting the speed at which the page loads

I am currently developing a project on this platform. It is using c9.io, an innovative browser-based collaborative programming IDE. The index.php file includes the following script: <?php $path = ltrim($_SERVER['REQUEST_URI'], '/&ap ...

Overflowing content in a table within a div element

Issue: I am encountering a problem with resizing a table inside a div that contains input elements. When I adjust the browser window size or change to a lower resolution, the div element resizes and scales down, causing a horizontal scroll bar to appear. H ...

Can you explain the distinctions among <Div>, <StyledDiv>, and <Box sx={...}> within the MUI framework?

When exploring the MUI documentation, I came across a table of benchmark cases that can be found here. However, the differences between the various cases are not clear to me. Can someone please explain these variances with real examples for the following: ...

Image remains fluid within a static div without resizing

Any assistance would be greatly appreciated. I am currently facing an issue with a fixed div that is floating at the bottom of the screen, serving as ad space for the mobile version of a website. The problem arises when attempting to resize the browser win ...

Is there a way I can ensure that two elements have identical heights?

Is there a way to ensure that two different elements in HTML are always the same height using CSS? I attempted to achieve this by setting each element to 50vh in the CSS, but it didn't work. codepen: https://codepen.io/ichfickedeinemutterdudummerwich ...

Mastering Tooltip Placement Using CSS or JavaScript

I have been working on creating a CSS-only tooltip for a web application and so far I have managed to create some useful tooltips with different classes: tooltip-up tooltip-down tooltip-left tooltip-right The distinguishing factors between them are t ...

Dynamically hiding elements within tabs using jQuery

Previously, I created a functionality for handling a single set of tabs. However, as the application has expanded, this functionality is no longer sufficient to accommodate multiple sets of tabs. The initial function looked like this: var iconTabs = func ...

The content momentarily flashes on the page during loading even though it is not visible, and unfortunately, the ng-cloak directive does not seem to function properly in Firefox

<div ng-show="IsExists" ng-cloak> <span>The value is present</span> </div> After that, I included the following lines in my app.css file However, the initial flickering of the ng-show block persists [ng\:cloak], [ng-cloak], ...

Unable to access account: HTML Javascript login issue

Problem with Login Code As a newcomer to HTML, CSS, and almost unknown in Javascript, I sought help from others to create a login code. The code was working fine earlier, but now it's not functioning as expected. Despite checking everything, I can&ap ...

Excess space appearing to the right of the text box in Internet Explorer 9 standards mode and Google Chrome

Looking for help on how to eliminate the space between the right side of an input textbox and a red border? Check out these examples: IE: http://jsfiddle.net/fQHGA/ <div style="float:left;border:1px solid red"> <label style="float:left">a ...

What's the best way to show floating point numbers in a concise format while also maintaining the ability to perform calculations within this Vue app?

I'm currently developing a compact calculator application using Vue 3 and implementing some custom CSS. For the most part, everything seems to be functioning correctly, except for when the results are long numbers that extend beyond the display limit ...

CSS style takes precedence over inline JavaScript transitions occurring from a negative position

I am puzzled by a JavaScript fiddle I created which contains two small boxes inside a larger box. Upon clicking either of the buttons labeled "Run B" or "Run C", the corresponding box undergoes a CSS transition that is controlled by JavaScript. Below is t ...