WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

CSS Layout - Horizontal Align


In CSS, a few properties can be utilized to adjust components horizontally.


Center Align - Using margin

Setting the width of a square dimension component will keep it from extending out to the edges of its holder. Use margin: auto;, to on a level plane focus a component inside its container.

The component will at that point take up the predefined width, and the rest of the space will be part similarly between the two margins:

Example

.focus {
    edge: auto;
    width: 60%;
    outskirt: 3px strong hotpink;
    cushioning: 10px;
}
Try it yourself »

Tip: Center adjusting has no impact if the width property isn't set (or then again set to 100%).

Tip: For adjusting content, see the CSS Text chapter.


Left and Right Align - Using position

One technique for adjusting components is to utilize position: absolute;:

Example

.right {
    position: absolute;
    right: 0px;
    width: 300px;
    fringe: 3px strong hotpink;
    cushioning: 10px;
}
Try it yourself »

Tip: When adjusting components to position, dependably characterize margin and padding for the <body> component. This is to maintain a strategic distance from visual contrasts in various browsers.

There is additionally an issue with IE8 and prior, when utilizing position. In the event that a compartment component (for our situation <div class="container">) has a predetermined width, also, the !DOCTYPE announcement is missing, IE8 and prior renditions will include a 17px edge the correct side. This is by all accounts space held for a scrollbar. In this way, constantly set the !DOCTYPE statement when utilizing position:

Example

body {
    edge: 0;
    cushioning: 0;
}

.compartment {
    position: relative;
    width: 100%;
}

.right {
    position: absolute;
    right: 0px;
    width: 300px;
    foundation shading: blue;
}
Try it yourself »

Left and Right Align - Using float

Another strategy for adjusting components is to utilize the float property:

Example

.right {
    skim: right;
    width: 300px;
    fringe: 3px strong hotpink;
    cushioning: 10px;
}
Try it yourself »

Tip: When adjusting components to float, dependably characterize margin and padding for the <body> component. This is to stay away from visual contrasts in various browsers.

There is likewise an issue with IE8 and prior, when utilizing float. On the off chance that the !DOCTYPE affirmation is missing, IE8 and prior forms will include a 17px edge the correct side. This is by all accounts space held for a scrollbar. In this way, constantly set the !DOCTYPE presentation when utilizing float:

Example

body {
    edge: 0;
    cushioning: 0;
}

.right {
    glide: right;
    width: 300px;
    foundation shading: blue;
}
Try it yourself »