Site logo

[Resolved] Margin to sides of Header gives unexpected results

Home Forums Support [Resolved] Margin to sides of Header gives unexpected results

Home Forums Support Margin to sides of Header gives unexpected results

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2448529
    Stephen

    I’m using the CSS below to style the header with a margin at the top which works fine.

    However if I add: margin-left: 10px ; or: margin-right: 10px ; then I get an unexpected result where the header is no longer centred and also no longer ‘contained’.
    (ie it goes wider than contained width and floats off to one side.)


    @media
    (min-width: 851px) {
    .site-header {
    margin-top: 20px;
    border: 1px solid #DDDDDD;
    }
    }

    #2448977
    David
    Staff
    Customer Support

    Hi there,

    thats correct, to center an element we use margin-left: auto; and margin-right: auto; so by adding a value you break that.
    Instead try reducing the site header max-width:

    @media (min-width: 851px) {
        .site-header {
            max-width: 1280px;
            margin-top: 20px;
            border: 1px solid #DDDDDD;
        }
    }

    That makes it 20px narrower then the site container ( 1300px ) which will shift it over.

    #2449029
    Stephen

    Hi, super that does explain it.

    I’ve used the CSS below as suggested which does fix the max width problem but the header is not staying in the site container and is off on the left side when over 1300px wide.

    .site-header {
    max-width: 1280px;
    margin: 20px 10px 0 10px;
    border: 1px solid #DDDDDD;
    }

    #2450021
    David
    Staff
    Customer Support

    You need to change:

    margin: 20px 10px 0 10px; to just margin-top: 20px;

    #2450086
    Stephen

    Great thanks, that gives the margins at 1300px +

    So I can use different CSS for different screen sizes ?


    @media
    (min-width: 1300px) {
    .site-header {
    max-width: 1280px;
    margin-top 20px;
    border: 1px solid #DDDDDD;
    }
    }


    @media
    (min-width: 851px) and (max-width:1299px){
    .site-header{
    margin: 20px 10px 0 10px;
    border: 1px solid #DDDDDD;
    }
    }

    #2450156
    David
    Staff
    Customer Support

    Correct 🙂

    #2450436
    Stephen

    great thanks

    #2450493
    David
    Staff
    Customer Support

    You’re welcome

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.