ComputersProgramming

Apply CSS to the center: text and table

The information world in any approximation is represented by elements and systems of elements, words and texts, or in the last formalized instance - texts and tables. You can also consider other already known data structures, as well as images, but to the use of CSS and HTML in text formatting, the other information forms have a different relationship.

The correct structure of the site page involves determining the position of each element. An HTML document is always a sequence of elements that are directly influenced by three basic rules:

  • Absolute positioning;
  • Place in the general flow;
  • Z-index.

Other accommodation options also matter, they require a lot of knowledge to solve the problem using CSS "centered text."

Centered and alignment rules

CSS rules complement the HTML document, but without rules, the latter can handle any kind of alignment. The ubiquitous proliferation of mobile devices and the departure of design in the direction of greater simplicity and "rectangularity" makes the question of centering relevant, while other alignment often loses value.

The main element of the page is a block described by a certain style. The rule of horizontal alignment: text-align can accept, as already happened, only four values (left, center, right, justify). You can perform vertical alignment by combining the values of the height and line-height rules:

{
Height: 88px; // at such a ratio
Line-height: 88px; // css in the center text - vertically
}

You can experiment with other rules. ; In the sources there is a mention of the interpretation of the block by the rules display: table, display: table-cell and vertical-align: middle ; But not always it works.

Align text with tables

The only data structure that was destined to be understood first and live forever is the table. It provides both vertical and horizontal alignment, in addition, the question of how to place the text centered in CSS, when using tables ceases to have a complex value.

The prevailing practice of using CSS rules to achieve a goal in markup does not always satisfy the concept of cascading style sheets. Nothing prevents the developer from centering in any direction by placing content in the central cell of a table of three columns and three rows.

This solution is not standard, but effective. Instead of long and scrupulously solving the problem of cross-browser compatibility of the page or counting the indents on the left, right, etc., you can use the percentage values of the widths of the cells and the heights of the rows in the tables.

Aligning with JavaScript

The question "How to place text in the center in HTML " is solved elementary by algorithm in the browser language and has much more practical and effective execution.

The design of styles files has already been streamed, and almost every self-respecting site management system offers developer stylesets for all occasions. But life always requires specifics: we have to think every time with a clean slate how to use CSS tools to place the text on the center. The option for alignment purposes in a block "glued" to the side of the page, and therefore changing the size, is unlikely to fit a block of a pop-up menu that is built from various elements.

Visible centering is not always just a block in which it is initially known where the left side and how many pixels to the right, where the top is, and not the guarantee that the bottom is the boundary with which vertical alignment is to be performed.

There is a tendency that style should be determined by content. To a greater extent, sites carry information to the visitor, visual and sound accompaniment is just an addition to the usual text. It is the symbols that carry the necessary content (this does not apply to art sites, paintings, music, etc.) - it is a question of the information necessary for the visitor.

Features of text representation and HTML

It turned out that the indivisible element of the content of the page is text, not the word. Semantics is the competence of the developer (the owner of the site). Actually the text can be represented not by one tag, that is, a natural sentence can be not just a sequence of symbols, but a sequence of tags containing symbols. From this it follows that the "self" CSS "centered text" can only be placed in simple cases.

The issuance of the server at the request of the browser is very voluminous and contains a lot of elements that are not always relevant to what will be displayed. When designing a page layout, it is important to correctly place the emphasis in the part of presenting information, in particular, alignment. Something better to do on the server, something on the client, that is, in the browser. Do not always rely on CSS rules, the orientation to the final result is much more practical.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.