What is the correct way to do a CSS Wrapper?

后端 未结 9 2148
后悔当初
后悔当初 2020-12-07 12:45

I have heard a lot of my friends talk about using wrappers in CSS to center the \"main\" part of a website.

Is this the best way to accomplish this? What is best pra

9条回答
  •  不知归路
    2020-12-07 13:21

    The best way to do it depends on your specific use-case.

    However, if we speak for the general best practices for implementing a CSS Wrapper, here is my proposal: introduce an additional

    element with the following class:

    /**
     * 1. Center the content. Yes, that's a bit opinionated.
     * 2. Use `max-width` instead `width`
     * 3. Add padding on the sides.
     */
    .wrapper {
        margin-right: auto; /* 1 */
        margin-left:  auto; /* 1 */
    
        max-width: 960px; /* 2 */
    
        padding-right: 10px; /* 3 */
        padding-left:  10px; /* 3 */
    }
    

    ... for those of you, who want to understand why, here are the 4 big reasons I see:

    1. Use max-width instead width

    In the answer currently accepted Aron says width. I disagree and I propose max-width instead.

    Setting the width of a block-level element will prevent it from stretching out to the edges of its container. Therefore, the Wrapper element will take up the specified width. The problem occurs when the browser window is smaller than the width of the element. The browser then adds a horizontal scrollbar to the page.

    Using max-width instead, in this situation, will improve the browser's handling of small windows. This is important when making a site usable on small devices. Here’s a good example showcasing the problem:

    /**
     * The problem with this one occurs
     * when the browser window is smaller than 960px.
     * The browser then adds a horizontal scrollbar to the page.
     */
    .width {
        width: 960px;
        margin-left: auto;
        margin-right: auto;
        border: 3px solid #73AD21;
    }
    
    /**
     * Using max-width instead, in this situation,
     * will improve the browser's handling of small windows.
     * This is important when making a site usable on small devices.
     */
    .max-width {
        max-width: 960px;
        margin-left: auto;
        margin-right: auto;
        border: 3px solid #73AD21;
    }
    
    /**
     * Credits for the tip: W3Schools
     * https://www.w3schools.com/css/css_max-width.asp
     */
    This div element has width: 960px;

    This div element has max-width: 960px;

    So in terms of Responsiveness, is seems like max-width is the better choice!-


    2. Add Padding on the Sides

    I’ve seen a lot of developers still forget one edge case. Let’s say we have a Wrapper with max-width set to 980px. The edge case appears when the user’s device screen width is exactly 980px. The content then will exactly glue to the edges of the screen with not any breathing space left.

    Generally, we’d want to have a bit of padding on the sides. That’s why if I need to implement a Wrapper with a total width of 980px, I’d do it like so:

    .wrapper {
       max-width: 960px; /** 20px smaller, to fit the paddings on the sides */
    
       padding-right: 10px;
       padding-left:  10px;
    
       /** ... omitted for brevity */
    }
    

    Therefore, that’s why adding padding-left and padding-right to your Wrapper might be a good idea, especially on mobile.


    3. Use a
    Instead of a

    By definition, the Wrapper has no semantic meaning. It simply holds all visual elements and content on the page. It’s just a generic container. Therefore, in terms of semantics,

    is the best choice.

    One might wonder if maybe a

    element could fit this purpose. However, here’s what the W3C spec says:

    The element is not a generic container element. When an element is needed only for styling purposes or as a convenience for scripting, authors are encouraged to use the div element instead. A general rule is that the section element is appropriate only if the element's contents would be listed explicitly in the document's outline.

    The

    element carries it’s own semantics. It represents a thematic grouping of content. The theme of each section should be identified, typically by including a heading (h1-h6 element) as a child of the section element.

    Examples of sections would be chapters, the various tabbed pages in a tabbed dialog box, or the numbered sections of a thesis. A Web site's home page could be split into sections for an introduction, news items, and contact information.

    It might not seem very obvious at first sight, but yes! The plain old

    fits best for a Wrapper!


    4. Using the Tag vs. Using an Additional

    Here's a related question. Yes, there are some instances where you could simply use the element as a wrapper. However, I wouldn’t recommend you to do so, simply due to flexibility and resilience to changes.

    Here's an use-case that illustrates a possible issue: Imagine if on a later stage of the project you need to enforce a footer to "stick" to the end of the document (bottom of the viewport when the document is short). Even if you can use the most modern way to do it - with Flexbox, I guess you need an additional Wrapper

    .

    I would conclude it is still best practice to have an additional

    for implementing a CSS Wrapper. This way if spec requirements change later on you don't have to add the Wrapper later and deal with moving the styles around a lot. After all, we're only talking about 1 extra DOM element.

提交回复
热议问题