Giter Site home page Giter Site logo

centering-in-css-horizontal-and-vertical's Introduction

See the Pen centering-in-css-horizontal-vertical by Trung Vo (@trungk18) on CodePen.

<script async="async" src="https://static.codepen.io/assets/embed/ei.js"></script>

In CSS, several properties can be used to align elements horizontally and vertically. I hope these below tips will help you understand and able to align the element center horizontal and vertical. Before going deeply, you can refer to these below source with good explanation and example.

Center Horizontal

1. Inline element (like text or button..)

To simply center text inside a block element is using: text-align: center

HTML

<div class="border-wrapper horizontal-inline-element-center">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
    <button>Submit</button>
</div>

CSS

.horizontal-inline-element-center {
    text-align: center;
}

2. Block element (div)

To horizontally center a block element (like div), use margin: auto;

HTML

<div class="border-wrapper horizontal-block-element-center">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>

CSS

.horizontal-block-element-center {
    max-width: 40em;
    margin: 0 auto;
}

Center Vertical

3. Vertical Anything

This is a bit more tricky, but with position: absolute and transform: translate(0, -50%), we will be able to align center vertically, even if we don't know its height. Translate negative 50% means move an element from its current position to middle of element's height based on Y-axis

HTML

<div class="wrap-col">
    <div class="text-wrap">
        <h1>
            <a href="#">Pizza</a>
        </h1>
        <p>
            <a href="#">Pizza is a flatbread generally topped with tomato sauce and cheese and baked in an oven</a>
        </p>
    </div>
    <div class="wrap-background backstretch" style="background-image: url(pizza.jpg)"></div>
</div>

CSS

.wrap-col {
    float: left;
    width: 33.333333%;
    position: relative;
}

    .wrap-col::before {
        position: absolute;
        content: '';
        height: 100%;
        width: 100%;
        z-index: 1;
        background: rgba(128, 128, 128, 0.4);
    }

.wrap-background {
    min-height: 250px;
}

.text-wrap {
    position: absolute;
    top: 50%;
    transform: translate(0, -50%);
    width: 100%;
    z-index: 1;
    text-align: center;
}

    .text-wrap a {
        color: #fff;
    }

centering-in-css-horizontal-and-vertical's People

Contributors

trungvose avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.