Skip to main content
Inspiring
September 20, 2018
Question

automate margin on one side to be removed when at mobile breakpoint

  • September 20, 2018
  • 2 replies
  • 1376 views

Hi,

What is the best way to approach this without having to re-write media query styles for mobile breakpoints?

Example: img floating left within a div with text to the right of it. The text wraps to create a paragraph wherever it hits the img. I had to add margin-right to the image so the text & image had a decent gap between.

However, once this gets to mobile, that excess margin-right will no longer be needed as the single col will display this image and all the text collapsed underneath it. The image will need to be 100% centered at that point.

Is there a better approach for this to better automate details like this?

Thank you.

    This topic has been closed for replies.

    2 replies

    BenPleysier
    Community Expert
    Community Expert
    September 20, 2018

    Co-incidentally, I have just finished watching Building a Page Layout with Bootstrap 4 - YouTube which more or less touches on the subject.

    All of the instruction videos making life a lot easier for users of Dreamweaver can be found at DMXzone - YouTube

    Warning: these are commercial extensions for Dreamweaver.

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    r_tistAuthor
    Inspiring
    September 20, 2018

    Another example. Similar thing. Button is set to 50% of its container for desktop. Once it collapses to mobile, it needs to be 100% and centered in the single col. - Am I expected to write media queries for all such details? Again, is there something I am missing that requires less effort?

    Legend
    September 20, 2018

    r_tist  wrote

    Am I expected to write media queries for all such details?

    Yes. There's is no other way if you want either to get rid of a css attribute or add one.........short of not doing it at all, which a lot of developers seem to do if you look at their mobile layout efforts. It can be tedious work if you are not organised or familiar with how to do it but its quite quick once you get the feel for it.

    One tip is to keep all your styling together in your css stylesheet. It makes for easy management. If you keep the css styling altogether and in order you have a good snapshot of what your container/element is doing at various breakpoints. If you splash them about all over the place it will make life more dificult......good managemnet is key when it comes to web-development otherwise you will find yourself with your nose in browser developer tools all day rather than doing any developing. I rarely need the browser development tools as Im organised and write my own code.

    /* Desktop */

    .wrapper {

    width: 75%;

    font-size: 20px;

    background-color: red;

    }

    /* Tablet */

    @media screen and (max-width:768px) {

    wrapper {

    width: 90%;

    }

    }

    /* Smartphone */

    @media screen and (max-width:480px) {

    wrapper {

    width: 100%;

    }

    }

    Nancy OShea
    Community Expert
    Community Expert
    September 20, 2018

    At the risk of getting flamed for herecy,  I'll just say that Bootstrap 4x has conveniently added spacing utilities to handle these situations.  You can selectively add margins or padding classes to elements based on viewport size (xs, sm, md, lg, xl).   No extra CSS media queries required. 

    https://getbootstrap.com/docs/4.1/utilities/spacing/

    Nancy O'Shea— Product User & Community Expert