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

When using Ruby and Rack on Heroku to serve a static site, the CSS styling may not be

Trying to deploy a static site consisting of HTML, CSS, font, and image files with Ruby Rack has been a bit challenging. While the HTML displays perfectly in Chrome when running it locally, the CSS assets do not seem to be loading or being applied to the H ...

Toggle visibility of div content when hovering over a link by leveraging the data attribute, with the div initially visible

I have a collection of links: <p><a href="#" class="floorplan initial" data-id="king"><strong>King</strong></a><br> 4 Bedrooms x 2.5 Bathrooms</p> <p><a href="#" class="floorplan" data-id="wood">< ...

Ensure that a div remains active even after it has been selected through AJAX using jQuery

I am currently utilizing ajax to display information from a database. The application I am working on is a chat app, where clicking on a specific conversation will append the data to a view. The structure of my conversation div is as follows: <div clas ...

Building a Div Tag Layout with CSS for Full Width and Left Position of 300px

Experiencing some trouble with styling a div element. My goal is to have the div start at left:300px and stretch across the entire width of the browser window. However, when I apply width:100%, the div extends beyond the boundaries of the browser screen. ...

Tips for utilizing both the Element Selector and Class Selector in Jquery

I am working with a simple table that has TDs assigned either the 'PLUS' or 'MINUS' class. My goal is to use jQuery to implement functionality for collapsing all or expanding all. When the Expand All button is clicked, I need to chang ...

Removing accordion borders in Bootstrap and AngularJS can be achieved by customizing

Can anyone help me figure out how to hide the border in each accordion group that contains a panel and inner elements? I've tried using classes like .accordion and .inner but nothing seems to work. Any advice on where to find the specific class or ID ...

Delicate seams break up the different sections of the webpage

I'm facing an issue with my website where thin lines are appearing between each section when previewed in Safari and Chrome, but not in Firefox (as shown in the images). I've checked the CSS code for each section in Dreamweaver, where the lines d ...

Tips for expanding an md-nav-item in Angular Material

Need help on stretching the md-nav-item element illustration 1 ------------------------------------------------ | ITEM1 | ITEM 2 | ------------------------------------------------ illustration 2 ------------------------------------------------ | ...

python conducting automation tests with Selenium WebDriver on a mouse

Hello, I'm having trouble with opening the Mouser website and using the search bar to send some data. Below is a sample of the code I've been working on, but I can't seem to find the correct CSS selector. Any help would be greatly appreciate ...

Avoid having Masonry load all divs simultaneously

I have experience using jQuery Masonry on Tumblr, but now I want to incorporate it into my portfolio website for displaying my photography. However, I'm struggling to find a solution that allows sets of images to load in as the user scrolls to the bot ...

Struggling with aligning images in the center while ensuring they remain responsive on various devices

My website has an image with dimensions of 955x955, which is a square size. I want this image to be responsive on all devices, but the section it's in is not squared. This causes the image to look distorted when viewed on smaller screens. The focus of ...

Remain concealed once the lights dim

Looking for a way to fade out a logo in a preloader div after a call, but having issues with it becoming visible again once fully faded out. Ideally, I would like to keep it faded out or set it to display none using only CSS, although I can use jQuery if n ...

The clip-path in Div: Css is malfunctioning and not displaying correctly

I am trying to create a chat bubble using CSS with rounded corners and a bottom-right arrow. However, I am having trouble getting the arrow to display correctly. https://i.stack.imgur.com/gVXOV.png The CSS styling for the chat bubble is as follows: ...

Embracing Elegance with jQuery

Is there a way to customize the appearance of my list (<ul>) in the following code snippet? $.widget( "custom.catcomplete", $.ui.autocomplete, { _renderMenu: function( ul, items ) { var self = this, currentCategory = ""; ...

What could be causing my sticky positioning to not function properly when I scroll outside of the designated

My website is organized into sections, with each section corresponding to a hyperlink in the navigation menu. I've applied the CSS property position: sticky to my navbar, which works as expected within the section bounds. However, I want my red navbar ...

Updating the CSS link href in ASP.NET using code behind

I'm struggling with changing the CSS href in the code-behind of my .ASPX page. I've tried various methods but haven't found a solution that works as intended. HTML Markup: <link id="linkCSS" runat="server" href='/css/1.css' re ...

Discover captivating visuals that effortlessly occupy the entire breadth, while gracefully encompassing a mere quarter of the total vertical space on the

It's become quite challenging for me to locate images that can occupy the entire width of a page while maintaining just 25% of the page height. Whenever I make adjustments to the image's CSS, it ends up appearing distorted because its width is di ...

JavaScript hovering drop-down feature

Hi there, I'm just starting out with javascript and could use some help with a simple script. I have a shopping cart drop down that currently activates when clicked. However, I want it to fade in when hovered over instead. I've tried using .hove ...

Tips for achieving a static background image while allowing scrolling on a webpage in asp.net

I am struggling to implement this on my website. The background image needs to be fixed while the content of the webpage should scroll in asp.net <head runat="server"> <title></title> <style type="text/css"> body { backgr ...

Circular picture contained within a Bootstrap column on an irregularly-shaped image file

I am looking to create a circular effect for an image that is originally rectangular, resembling a typical profile picture. I have come across several sources on how to do this. The challenge arises in trying to achieve this effect within a bootstrap colu ...