Best Website-BuildersBest Website-Builders
    What's Hot

    Oscars winners so far at the 95th Academy Awards

    March 13, 2023

    Texas Judge Wanted to Delay Publicizing Abortion Pill Hearing: Report

    March 13, 2023

    Newspaper headlines: Lineker ‘to return’ and Silicon Valley Bank collapse

    March 13, 2023
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    Best Website-BuildersBest Website-Builders
    • Home
    • CSS

      Almost Bare Bone WebR Starter App

      March 12, 2023

      Best AI Tools for Web Designers (2023)

      March 12, 2023

      PSPad 5.0.7.770 | Neowin

      March 11, 2023

      Battle of Memphis

      March 11, 2023

      How to create a recipe book using HTML, CSS and JavaScript

      March 11, 2023
    • Joomla

      Pros, Cons, & Pricing Compared

      March 11, 2023

      Give your website a place to call home for a lifetime of web hosting for just $100

      March 11, 2023

      Give your website a place to call home for a lifetime of web hosting for just $100

      March 11, 2023

      12 Best Free Web Hosting Sites to Choose From

      March 10, 2023

      cPanel vs SPanel: Which is the Better Web Hosting Control Panel?

      March 10, 2023
    • PHP

      Lawsuit says teacher pushed student for not saying pledge of allegiance

      March 12, 2023

      Paul Flores sentenced to 25 years for murder of Christine Smart

      March 12, 2023

      Most Effective Skin Serum, According to Reviewers and Dermatologists

      March 12, 2023

      Man sues ex-wife’s friend for helping ex-wife get abortion

      March 11, 2023

      Perfect indoor and outdoor slippers to wear around the house or on errands

      March 11, 2023
    • UX

      New York City worker saw company hiring for her job but paid $90,000 more

      March 11, 2023

      Best March Madness Apps of 2023: NCAA, ESPN, CBS

      March 11, 2023

      Best March Madness Apps of 2023: NCAA, ESPN, CBS

      March 11, 2023

      I found my job listing with a much higher salary, so I reapplied

      March 10, 2023

      I found my job listing with a much higher salary, so I reapplied

      March 10, 2023
    • Web Builders
      1. Web Design
      2. View All

      What Comes First in Website Development — Design or Copy?

      February 2, 2023

      Modern Campus Honors Best Higher Education Websites of 2022

      February 2, 2023

      Premier SEO Consultant in Las Vegas, Nevada with Unparalleled Customer Service

      February 2, 2023

      Can Religious Freedom Be Saved? This group is racing the clock to teach America’s first freedom

      February 2, 2023

      How i Create New Google Account

      February 7, 2023

      CWT powers tools for meeting and event planners

      January 31, 2023

      Best Website Builder – Website Builders

      January 24, 2023

      Is There A Market For Rap-Themed Slot Games? – Rap Review

      January 19, 2023
    • WordPress

      A big Samsung Galaxy S23 camera update is rumored to be in the works

      March 12, 2023

      Not impressed with the Oculus Quest 2? Here’s how the VR headset of the future beats it.

      March 12, 2023

      Sleep Week 2023 – 7 days tips for better sleep

      March 12, 2023

      These mobile games are just trying to steal your crypto assets, warns FBI

      March 12, 2023

      Latest Google Pixel 7a leak reveals mid-range photos and specs

      March 12, 2023
    • Realtoz
      • Our Other Sites
    • More News
    Best Website-BuildersBest Website-Builders
    Home » How to Center a Div Using CSS Grid — SitePoint
    CSS

    How to Center a Div Using CSS Grid — SitePoint

    websitebuildersnowBy websitebuildersnowSeptember 29, 2022No Comments4 Mins Read
    Facebook Twitter LinkedIn Telegram Pinterest Tumblr Reddit WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    In this article, we’ll look at four ways to center a div horizontally and vertically using CSS grid. Of course, these centering techniques can be used for any kind of element. We’ve seen before how to center elements horizontally and vertically using flexbox, and how to position them using transforms.

    set up

    First, let’s create a container with a simple box element inside that we’ll use to demonstrate these centering methods. Here is the HTML:

    <article>
      <div></div>
    </article>
    

    And this is the first CSS.

    article {
      width: 100%;
      min-height: 100vh;
      background: black;
      display: grid;
    }
    
    div {
      width: 200px;
      background: yellow;
      height: 100px;
    }
    

    The yellow square at the top left of the black container is the starting position

    In all examples, display: grid property. This will <article> Use an element as a grid container and generate a block-level grid for that container. (Here’s a demo template on CodePen if you want to try it out.)

    Now let’s look at different ways to center a div.

    1. Center a div using CSS Grid and place-self

    My favorite way to center an element in a grid is to use place-self property. (Learn more about.)

    Centering a div is as simple as:

    article {
      display: grid;
    }
    
    div {
      place-self: center;
    }
    

    look at the pen
    Centering with Grid and place-self with SitePoint (@SitePoint)
    with a code pen.

    of place-self The properties are align-self (Vertical) and justify-self (horizontal) property (useful for centering along one axis). You can try them out in this CodePen demo.

    use place-self It’s so simple that it’s an out-of-the-box solution. But this is not the only way to center elements using Grid. So let’s look at some other methods.

    Advantages to use place-self This means it can also be used to center other elements within the same container. (Add more div elements to the CodePen demo and see what happens.)

    2. Center the div using CSS Grid, justify-content, align-items

    Now let’s see what’s involved in using the Grid. justify-content and align-items Center the div.

    of justify-content The property is used to horizontally align the items in the container when the items do not use all the available space.There are many ways to configure justify-content property, but here we just set center.

    like justify-content properties, align-items The property is used to align the content inside the container, but align the content vertically instead of horizontally.

    Return to your test HTML and add the following code to the parent container.

    article {
      display: grid;
      justify-content: center;
      align-items: center;
    }
    

    look at the pen
    Centering with grid, align-self and justify-self with SitePoint (@SitePoint)
    with a code pen.

    The obvious advantage of this method is that it requires less code as the centering is handled by the container. However, targeting a div through a container is also disadvantageous in some ways, as other elements within the container are also affected.

    3. Center Divs with CSS Grid and Auto Margins

    As usual, target the parent container display: gridI also use divs to assign auto margins. margin: autoThis will cause the browser to automatically calculate the available space around the child div, split it vertically and horizontally, and center the div.

    article {
      display: grid;
    }
    
    div {
      margin: auto;
    }
    

    look at the pen
    CSS Grid with SitePoint (@SitePoint), centering elements using justify-content and align-items
    with a code pen.

    (As an aside, there are lots of other cool things you can do with CSS margins.)

    4. Center Divs with CSS Grid and place-items

    of place-items Properties are used to align items vertically and horizontally within the grid. It can be used to center a div by targeting the container like this:

    article {
      display: grid;
      place-items: center;
    }
    

    look at the pen
    Center a Div using CSS Grid and Auto Margins with SitePoint (@SitePoint)
    with a code pen.

    like place-self property, place-items In this case, it’s a shorthand for two properties. justify-items (horizontal) and align-items (vertical). You can try them out in this CodePen demo.

    in contrast to place-self, place-items is applied to the container, making it slightly less flexible.

    Conclusion

    Each of these methods allows you to center a div horizontally and vertically within its container.As I said, my preference is place-self This is primarily because it applies to elements that are centered rather than containers.it is the same margin: auto Method.Of course, if you just want to center the elements in one direction, you can also use either align-self again justify-self.

    In the demo example I used an empty div, but of course you can add content to the div and centering will still work. Also, these centering techniques work on elements other than divs.

    References:



    Source link

    Share this:

    • Tweet
    • Email
    • Pocket
    • Mastodon
    • WhatsApp
    • Telegram
    • Share on Tumblr
    • Print
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleOakland Web Designers Nonprofit Gives Up-and-coming Creators a Jump Start
    Next Article WordPress Gutenberg 14.2 offers a better user experience
    websitebuildersnow
    • Website

    Related Posts

    Almost Bare Bone WebR Starter App

    March 12, 2023

    Best AI Tools for Web Designers (2023)

    March 12, 2023

    PSPad 5.0.7.770 | Neowin

    March 11, 2023
    Add A Comment

    Leave a Reply Cancel reply

    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    Advertisement
    Demo

    This website provides information about CSS and other things. Keep Supporting Us With the Latest News and we Will Provide the Best Of Our To Makes You Updated All Around The World News. Keep Sporting US.

    Facebook Twitter Instagram Pinterest YouTube
    Top Insights

    Oscars winners so far at the 95th Academy Awards

    March 13, 2023

    Texas Judge Wanted to Delay Publicizing Abortion Pill Hearing: Report

    March 13, 2023

    Newspaper headlines: Lineker ‘to return’ and Silicon Valley Bank collapse

    March 13, 2023
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2023 bestwebsite-builders. Designed by bestwebsite-builders.
    • Home
    • About us
    • Contact us
    • DMCA
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.