CSS: Responsive way to center a fluid div (without px width) while limiting the maximum width?

前端 未结 6 909
Happy的楠姐
Happy的楠姐 2020-12-04 12:54

I\'ve seen a million questions about how to center a block element and there seem to be a couple popular ways to do it, but they all rely on fixed pixels widths. Then either

6条回答
  •  甜味超标
    2020-12-04 13:26

    Something like this could be it?

    HTML

     
    SOMETHING

    CSS

    body{
    
        text-align: center;
    }
    
    .random{
    
        width: 60%;
        margin: auto;
        background-color: yellow;
        display:block;
    
    }
    

    DEMO: http://jsfiddle.net/t5Pp2/2/

    Edit: adding display:block doesn't ruin the thing, so...

    You can also set the margin to: margin: 0 auto 0 auto; just to be sure it centers only this way not from the top too.

提交回复
热议问题