Adjust the left and right margins while maintaining the vertical margin spacing

Is there a way to apply horizontal margin while inheriting vertical margin without explicitly setting margin-left and margin-right? I was hoping for something like margin: inherit 20px; to be effective.

Take a look at the example here: https://jsfiddle.net/cm8kwfok/1/

Html:

<div class="outer">
    <div class="inner horizontal-margin-working">
        Working but not ideal
    </div>
    <div class="inner horizontal-margin-notworking">
        Not working as expected
    </div>
</div>

Css: .outer { background: #27ae60; padding: 10px; }

.inner {
    margin: 10px;
}

.horizontal-margin-working {
    margin-left: 50px;
    margin-right: 50px;
}

.horizontal-margin-notworking {
    margin: inherit 50px;
}

Answer №1

Unfortunately, it is not possible to maintain the vertical margins when utilizing the shorthand property. This is due to:

  • In CSS, if multiple selectors are applicable to an element, the values specified in the most recent or more specific selector will take precedence for common properties. In this case, the margin is declared in both the .inner and another class selector, causing the one within .inner to be overwritten.
  • The use of inherit (along with a length value) is not supported for the shorthand margin property, while it is valid for longhand margin-* properties or when used alone in the shorthand. Additionally, since margin: 10px is applied in the .inner selector which pertains to the same element (not the parent), using inherit will not be effective.
  • Although auto is a recognized value for margin, it does not preserve the margin specified in a previous selector. It instructs the User Agent to calculate and assign an appropriate value, potentially setting the margin-top and margin-bottom to
    10px</code based on various factors.</li>
    <li>When providing a length value (in pixels or percentage) for the <code>margin
    property, the User Agent determines how to interpret the value based on the number of values given. For instance, one value sets all sides, two values address vertical and horizontal margins, and so forth.

In essence, overriding the margin-left and margin-right specifically (as seen in .horizontal-margin-working) is the only method to retain the vertical margin.

Answer №2

avoid the margin:inherit 50px;

try this instead

.horizontal-margin-notworking {
    margin-top: inherit;
    margin-bottom: inherit;
    margin-left: 50px;
    margin-right: 50px;

}

Answer №3

Here's how you can use it:

<style>
.outer {
  background: #27ae60;
  padding: 10px;
}

.inner {
  margin: 10px;
}

.horizontal-margin-working {
  margin-left: 50px;
  margin-right: 50px;
}

.horizontal-margin-notworking {

margin-top: inherit;
margin-bottom: inherit;
margin-left: 50px;



}

</style>
<div class="outer">
  <div class="inner horizontal-margin-working">
    This is working but not ideal
  </div>
  <div class="inner horizontal-margin-notworking">
    Unfortunately, this is not working as expected
  </div>
</div>

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

Can a radio button set be designed in two dimensions?

There are three sets of data (dataX, dataY, dataZ) that need to be assigned unique values out of a selection of three options (valueA, valueB, valueC). Each piece of data should be linked to one value and each value should have only one data associated wit ...

Formatting Anchor Tags Inside a Table Data Element

My existing CSS section is as follows: .leftMemberCol { width:125px; vertical-align:top; padding: 13px; border-width:0px; border-collapse:separate; border-spacing: 10px 10px; text-align:left; background-color:#f2f3ea; } I have a td section (a left sideb ...

Tips for refreshing the appearance of a window in angular when it is resized

I have a chat feature integrated into my application. I am looking to dynamically resize an image within the chat window when the width of the window falls below a certain threshold. Is there a method available to modify the CSS style or class based on the ...

Which tag should I use, <b> or <mark>, to emphasize important marketing terms within a paragraph?

Trying to emphasize specific keywords in a paragraph of text can be challenging. I'm currently puzzled about the distinction between the <b> and <mark> tags. These words are highlighted because they play a crucial role in marketing, prompt ...

Guide on incorporating Bootstrap JS into HTML5 reusable web elements

RESOLVED: the solution is in a comment TL;DR: Issues triggering Bootstrap's JS, likely due to incorrect import of JS scripts I've been working on integrating Bootstrap with my custom reusable web components across all pages. Specifically, I&apo ...

Switch out the rowspan attribute in HTML for a CSS alternative

Hello there, I've been experimenting with replacing my table layout with divs and CSS, but I'm struggling to find a way to replicate the behavior of the rowspan attribute. Here is the original code snippet: <table> <tr> <td> ...

Showing hidden errors in specific browsers via JavaScript

I was struggling to make the code work on certain browsers. The code you see in the resource URL below has been a collection of work-around codes to get it functioning, especially for Android browsers and Windows 8. It might be a bit sketchy as a result. ...

The horizontal display of JqueryMobile radiobuttons is not appearing correctly

My aim is to dynamically generate a set of radio buttons using JQueryMobile. Everything seems to be working fine except for the data-type='horizontal' attribute: On looking at this fiddle, you'll notice that the "created-via-js" radio group ...

What method does nosotroshq.com use to achieve the navigation effect when hovering over a menu item?

Seeking advice from skilled individuals familiar with JQUERY: Can anyone explain the technique used by nosotroshq.com in their top navigation bar? Specifically, how do they create the slow animation when hovering over "ABOUT US"? Any insights would be ap ...

What is the solution to making flexbox align-items flex-end work in WebKit/Blink?

I'm attempting to embed one flexbox within another flexbox. The outer box is aligned vertically: +------+ | | +------+ | | | | | | +------+ The top section adjusts to fit the content with flex 0 1 auto, while the bottom half occu ...

Utilizing Python and Selenium for Xpath, extracting text from a span tag within a web table

Struggling with this one, I've carefully gone through all previous posts before reaching out for help. The structure of the HTML web table is provided below. I am specifically interested in extracting the date from the span tag, and here are the vari ...

Determine the width of a div by utilizing SASS and following a series of incremental steps

Let's discuss the current scenario: I have a parent div that consists of multiple child elements which vary in number. (2, 3, 4) The goal is to determine the appropriate width for each step element dynamically: For 2 steps: 50% For 3 steps: 33.3% ...

Creating dynamic color changes with overlapping SVG triangles using CSS

I'm facing a challenging issue: I want to change the color of an SVG line as it intersects with a triangular background created using CSS. Although I've experimented with gradients, they don't produce the desired effect. My goal is for the ...

Generate a structured table display using the JSON information

How can I convert the following JSON data into a tabular view? {"data_report":[{"data":[1,2,0,3],"label":"Test1","backgroundColor":"blue"}, {"data":[3,4,2,5],"label":"test2","backgroundColor":"#a3eaae"}, {"data":[2,3,1,4],"label":" ...

Having trouble with your jQuery image slider?

As a newcomer to jQuery, I have been experimenting with it to gain some experience. I recently tried to integrate a jQuery image slider from slidesjs.com into my local website but unfortunately, it is not functioning as expected. Additionally, I would lik ...

When hovering over A, the DIV element fails to appear

I'm encountering an issue with a specific page on my website () The problem lies with a .dropdown class that is supposed to display a DIV right below the header. In the source code, you can find it underneath <-- START DROPDOWN MENU -->. When I se ...

Enhancing navigation functionality using CSS

I have two separate pages with navigation included in both. This way, it's easier to edit the navigation menu once instead of doing so on each page individually. However, I am now facing uncertainty on how to implement the 'active' class usi ...

Styling the React Material UI DataGrid with the MuiDataGrid-window class using createMuiTheme or makeStyles / styled-components

I've been putting in a lot of effort to customize the css for the .MuiDataGrid-window in MaterialUi's DataGrid. I've been referencing css rules from https://material-ui.com/api/data-grid/ I managed to get it working within createMuiTheme fo ...

Managing touch devices and animations when hovering

Is there a way to remove or prevent all hover effects on a touch device? I've tried various scripts, but none seem to work for me. How can I test it with dev tools? I attempted this script but received an error: Cannot read property 'addEventList ...

What is causing React Js to fail loading css when switching from anchor tag to link tag?

I am learning React and experimenting with creating a basic static website using HTML templates in version ^18.2.0 of React JS. Everything seems to be functioning correctly, however, I have encountered an issue with page refresh. Specifically, when I repla ...