HTML center alignment. div centering and other positioning subtleties html css center alignment

Text alignment determines its appearance and the orientation of the paragraph edges, and can be left, right, centered, or justified. In table. 1 shows the text block alignment options.

Tab. 1. Ways to align text
Left alignment Right alignment Center alignment Justify
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.

The most common option is left-aligned, when the left text is shifted to the edge, while the right remains jagged. Right and center alignment is mostly used in headings and subheadings. It should be borne in mind that when using justification in the text, large intervals may appear between words, which is not very beautiful.

The paragraph tag is usually used to set text alignment.

With the align attribute, which specifies the alignment method. It is also permissible to align a block of text using the tag

with the same align attribute, as shown in Table 1. 2.

Tab. 2. Aligning text with the align parameter
HTML Code Description
Adds new paragraph text, left-aligned by default. Small vertical indents are automatically added before and after the paragraph.

Text

Center alignment.

Text

Left alignment.

Text

Text

Width alignment.
Text Disables automatic line wrapping, even if the text is wider than the browser window.
Text Allows the browser to break the line at the specified location, even if the tag is used .
Text
Center alignment.
Text
Left alignment.
Text
Right alignment.
Text
Width alignment.

Left alignment of elements is set by default, so there is no need to specify it once again. So align="left" can be omitted.

Difference between paragraph (tag

) and tag

in that a vertical indent appears at the beginning and end of the paragraph, which is not the case when using the tag
.

The align attribute is quite versatile and can be applied not only to body text, but also to headings like

. Example 1 shows how to set the alignment in such a case.

Example 1: Text Alignment

Text alignment

How to catch a lion?

enumeration method

We divide the desert into a number of elementary sections, the size of which coincides with the overall dimensions of the lion, but is smaller than the size of the cage. Next, by simple enumeration, we determine the area in which the lion is located, which automatically leads to its capture.

dichotomy method

We divide the desert into two halves. In one part there is a lion, in the other there is none. We take the half in which the lion is located, and divide it in half again. So we repeat until the lion is caught.

The result of the example is shown in Fig. one.

Rice. 1. Align text to the right and left

AT this example the heading is aligned to the center of the browser window, the selected paragraph to the right, and the body text to the left.

When laying out a page, it is often necessary to align to css center-way: for example, center the main unit. There are several options for solving this problem, each of which sooner or later has to be used by any layout designer.

Center text alignment

Often, for decorative purposes, you want to set the text alignment to the center, CSS in this case allows you to reduce the layout time. Previously, this was done using HTML attributes, but now the standard requires text to be aligned using style sheets. Unlike blocks, for which you need to change the margins, in CSS alignment centered text is done with a single line:

  • text-align:center;

This property is inherited and passed from parent to all children. Affects not only the text, but also other elements. To do this, they must be inline (for example, span) or inline-block (any blocks that have the display: block property set). The latter option also allows you to change the width and height of the element, more flexibly adjust the indents.

Often on pages, align is attributed to the tag itself. This immediately makes the code invalid, as the W3C has deprecated the align attribute. Using it on a page is not recommended.

Aligning a block to the center

If you need to center a div, CSS has a pretty handy way: using margins. Indents can be set both for block elements and inline-block elements. The property value must take the values ​​0 (vertical indents) and auto (automatic horizontal indents):

  • margin:0 auto;

Now this option is recognized as absolutely valid. Using margins also allows you to set the alignment of the image to the center: it allows you to solve many problems associated with the positioning of an element on a page.

Align block left or right

Sometimes centering in the CSS way is not required, but you need to put two blocks side by side: one on the left edge, the other on the right. To do this, there is a float property, which can take one of three values: left, right or none. Let's say you have two blocks that need to be placed side by side. Then the code will be like this:

  • .left (float:left;)
  • .right(float:right)

If there is also a third block, which should be located under the first two blocks (for example, a footer), then it needs to set the clear property:

  • .left (float:left;)
  • .right(float:right)
  • footer (clear:both)

The fact is that blocks with classes left and right fall out of the general flow, that is, all other elements ignore the very existence of aligned elements. The clear:both property allows the footer or any other block to see the elements that have fallen out of the flow and disables the wrapping (float) both to the left and to the right. Therefore, in our example, the footer will move down.

Vertical alignment

There are cases when it is not enough to set the center alignment in CSS ways, you also need to change the vertical position of the child block. Any inline or inline-block element can be flush to the top or bottom, be in the middle of a parent element, or be in any position. Most often, a block needs to be centered, which is done using the vertical-align attribute. Let's say there are two blocks, one nested within the other. In this case, the inner block is an inline-block element (display: inline-block). You need to align the child block vertically:

  • top alignment - .child(vertical-align:top);
  • center alignment - .child(vertical-align:middle);
  • bottom alignment - .child(vertical-align:bottom);

Block-level elements are not affected by text-align or vertical-align.

Possible issues with aligned blocks

Sometimes centering a div in the CSS way can cause little problems. For example, when using float: let's say there are three blocks: .first, .second and .third. The second and third blocks lie in the first. The element with class second is left-aligned, and the last block is right-aligned. After alignment, both fell out of the stream. If the parent element does not have a height set (for example, 30em), then it will no longer stretch to the height of the child blocks. To avoid this error, they use a "spacer" - a special block that sees .second and .third. CSS code:

  • .second(float:left)
  • .third(float:right)
  • .clearfix(height:0; clear: both;)

The :after pseudo-class is often used, which also allows you to put blocks back in place by creating a pseudo spacer (in the example, the div with the container class lies inside .first and contains .left and .right):

  • .left(float:left)
  • .right(float:right)
  • .container:after(content:""; display:table; clear:both;)

The above options are the most common, although there are several variations. You can always find the easiest and most convenient way to create a pseudo spacer through experimentation.

Another problem that layout designers often face is the alignment of inline-block elements. A space is automatically added after each of them. Helps to deal with it margin property A that is given a negative offset. There are other ways that are used much less often: for example, zeroing In this case, font-size:0 is written in the properties of the parent element. If there is text inside the blocks, then the required font size is already returned in the properties of the inline-block elements. For example, font-size:1em. The method is not always convenient, so the option with external indents is much more often used.

Aligning blocks allows you to create beautiful and functional pages: this is the layout of the general layout, the location of goods in online stores, and photos on a business card site.

Aligning elements horizontally and vertically can be done in various ways. The choice of method depends on the type of element (block or inline), on the type of its positioning, size, etc.

1. Horizontal alignment to the center of the block / page

1.1. If the block has a width:

div ( width: 300px; margin: 0 auto; /*center the element horizontally within the parent block*/ )

If you want to align an inline element this way, you need to set it to display: block;

1.2. If a block is nested within another block and has no/width set for it:

.wrapper(text-align:center;)

1.3. If the block has a width and needs to be fixed in the center of the parent block:

.wrapper (position: relative; /*set the parent box to relative position so we can absolutely position the box inside it later*/) .box ( width: 400px; position: absolute; left: 50%; margin-left: -200px; / *shift the block to the left by a distance equal to half its width*/ )

1.4. If no width is set for blocks, you can center using the parent wrapper block:

.wrapper (text-align: center; /*center the content of the block*/) indent between blocks*/ )

2. Vertical alignment

2.1. If the text takes up one line, for example, for buttons and menu items:

.button ( height: 50px; line-height: 50px; )

2.2. To align a block vertically inside the parent block:

.wrapper (position: relative;) .box ( height: 100px; position: absolute; top: 50%; margin: -50px 0 0 0; )

2.3. Vertical alignment by table type:

.wrapper ( display: table; width: 100%; ) .box ( display: table-cell; height: 100px; text-align: center; vertical-align: middle; )

2.4. If the box has a width and height set and needs to be centered on the parent box:

.wrapper ( position: relative; ) .box ( height: 100px; width: 100px; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; overflow: auto; /*to content did not spread */ )

2.5. Absolute positioning to the center of the page/block using CSS3 transform:

if the element has dimensions

div ( width: 300px; /*set the block width*/ height:100px; /*set the block height*/ transform: translate(-50%, -50%); position: absolute; top: 50%; left: 50% ; )

if the element has no dimensions and is not empty

Some text here

h1 ( margin: 0; transform: translate(-50%, -50%); position: absolute; top: 50%; left: 50%; )

2.5. Absolute block positioning

in the center of the page

div ( width: 500px; height: 100px; /* if the height is not explicitly set, it will be 100% */ position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; )

in the center of the block

.wrapper ( position: absolute; ) .box ( width: 100px; height: 100px; /* if the height is not explicitly set, it will be 100% */ position: absolute; top: 0; bottom: 0; left: 0 ; right: 0; margin: auto; )

If you cut any site created on html based, then you will see a certain layered structure. And their appearance it will be similar to a layer cake. If you think so, then most likely you have not eaten for a long time. So satisfy your hunger first, and then we'll show you how to center the div layer on your site:

Benefits of layout using a tag

There are two main types of site structure building:

  • tabular;
  • Block.

Tabular layout has been dominant since the dawn of the Internet. Its advantages include the accuracy of the given positioning. But, nevertheless, it has obvious shortcomings. The main ones are the volume of the code and low speed downloads.

When using tabular layout, the web page will not be displayed until it is fully loaded. Whereas when using div blocks, the elements are rendered immediately.

In addition to high loading speed, block construction of the site allows you to reduce the volume several times html code. Including through the use of CSS classes.

However, tabular layout should be used to structure the display of data on the page. A classic example of its use is the display of tables.

Block building based on tags

also called layered, and the blocks themselves are layers. This is because when using certain values properties, they can be placed one on top of the other like layers in Photoshop.

Positioning aids

In block layout, layer positioning is best done using cascading style sheets. The main CSS property responsible for the position

, is a float.
Property syntax:
float: left | right | none | inherit,
Where:

  • left - aligns the element to the left edge of the screen. The rest of the elements wrap around to the right;
  • right - alignment on the right, wrapping the rest of the elements - on the left;
  • none - wrapping is not allowed;
  • inherit - inherit the value of the parent element.

Consider a lightweight example of positioning divs using this property:

Left block

Now let's try to use the same property to position the third div in the center of the page. But unfortunately float doesn't have a center value. And when a new block is given an alignment value to the right or left, it shifts in the specified direction. Therefore, it remains only to set all three blocks to float: left :

But this is not the best option. When the window is reduced, all layers line up in one row vertically, and when the size is increased, they stick to the left edge of the window. So we need a better way to center divs.

Centering Layers

In the following example, we will use a container layer to place the rest of the elements on. This solves the problem of shifting blocks relative to each other when the window is resized. Centering the container in the middle is done by setting the margin properties zero value margins from the top and auto on the sides (margin: 0 auto ):

Left block

central block

This same example shows how you can center a div horizontally. And if you slightly edit the above code, then you can achieve vertical alignment of the blocks. To do this, you just need to change the length of the container layer (reduce it). That is, after editing its css, the class should look like this:

After the change, all blocks will line up strictly in a row in the middle. And their position will not change at any size of the browser window. This is what a div centered vertically looks like:

In the following example, we used a number of new css properties to center the layers inside the container:

A brief description of the css properties and their values ​​that we used in this example to center a div inside a div :

  • display: inline-block - lines up a block element into a line and wraps it with another element;
  • vertical-align: middle - aligns the element in the middle relative to the parent;
  • margin-left - sets the margin to the left.

How to make a link from a layer

As strange as it sounds, it is possible. Sometimes a div block as a link may be needed when laying out different types of menus. Consider a practical example of the implementation of a link layer:

Link to our site

In this example, using the line display: block , we set the link to the value of a block element. And to make the entire height of the div become a link, set height : 100%.

Hiding and showing block elements

Block elements provide more options for expanding the functionality of the interface than the outdated tabular layout. It often happens that the design of the site is unique and recognizable. But you have to pay for such an exclusive lack of free space.

Especially it concerns home page, the cost of advertising on which is the highest. Therefore, there is a problem where to "shove" another advertising banner. And here you can't get away with aligning the div to the center of the page!

A more rational solution is to make some block hidden. Here is a simple example of such an implementation:

This is the magic button. Clicking on it will hide or show the hidden block.

In this example, the position of the div blocks does not change in any way. Here we use the simplest JavaScript function that changes the value css properties display after pressing the button ( onclick event).

It should be easy for the visitor to find necessary information On the page. For this, as well as for expressing some expression, various HTML tags. This article will discuss the nuances of working with tables, in particular, their alignment.

Basic subtleties

First of all, it should be noted that this graphical form of data presentation allows you to structure information, which greatly facilitates its assimilation. Almost any content can be placed inside the table cells: from text to video. It is important to consider not only the size, but also its location.

How to make the table itself centered

Most often, you need to arrange the table in the center of the page, although initially it is pressed to the left side of the page. In order to align it to the center, you need to set its margin property to auto.

...

This causes the table padding to be calculated automatically. After that, the table will be centered on the page.

Center alignment in cells

Just as often, you need to align data to the center of a cell. In this case, there are three ways: horizontal, vertical and absolute. From their name it is clear on which axis the centering will occur. In any case, the tag is used , which is responsible for a specific cell in the row. Next, you need to assign its valign (vertical) and / or align (horizontal) attributes to "center", depending on your task:

Text in the center of the cell

If you want to make this formatting standard for the entire site or page (so as not to rewrite every table), then you should use CSS styles. To do this, add following code inside the tag :

Using this method, you can also set the alignment for a specific cell, as well as for a column, row, or the entire table as a whole. As you can see, any of the methods is very simple.

Internet