Css Text In Div Vertikal Zentrieren
Centering elements vertically with CSS ofttimes gives trouble. However, in that location are several means of vertical centering, and each is easy to use.
The vertical-marshal property is used to vertically centre inline elements.
The values of the vertical-align property align the element relative to its parent chemical element:
- Line-relative values vertically align an chemical element relative to the entire line.
- Values for tabular array cells are relative to the table-meridian-algorithm, which commonly refers to the height of the row.
Example of vertically adjustment a text:
<!DOCTYPE html> <html> <head> <title>Title of the certificate</title> <style> div { brandish: table-prison cell; width: 250px; height: 200px; padding: 10px; border: 3px dashed #1c87c9; vertical-align: middle; } </style> </head> <torso> <div>Vertically aligned text</div> </body> </html>
Outcome
With Flexbox, information technology is possible to align elements vertically (or horizontally) with the marshal-items, align-self, and justify-content properties.
To learn how to create flexible layouts optimized for multiple devices, read our Flexbox Guide.
Case of vertically aligning a text with Flexbox:
<!DOCTYPE html> <html> <head> <title>Title of the document</championship> <mode> department { display: flex; width: l%; height: 200px; margin: auto; border-radius: 10px; border: 3px dashed #1c87c9; align-items:heart; justify-content:center; } </way> </caput> <body> <section> <p> I'k centered with Flexbox!</p> </section> </trunk> </html>
With the display holding, we're going to prepare the elements to "tabular array" and "table cell". Nosotros center the content with the vertical-align holding.
Instance of vertically aligning a text with the CSS display property:
<!DOCTYPE html> <html> <caput> <title>Title of the document</title> <manner> #parent { display: tabular array; width: 100%; height: 200px; border: 3px dashed #1c87c9; text-align: middle; } #kid { display: tabular array-cell; vertical-align: center; } </mode> </head> <torso> <div id="parent"> <div id="child">I am vertically centered</div> </div> </trunk> </html>
Add the line-acme property to the element containing a text larger than its font size. By default, equal spaces will exist added above and below the text, and you'll get a vertically centered text.
Example of vertically adjustment a text with the CSS line-height holding:
<!DOCTYPE html> <html> <caput> <championship>Title of the certificate</title> <fashion> p { tiptop: 90px; line-top: 90px; text-align: center; border: 3px dashed #1c87c9; } </mode> </head> <body> <p>I am vertically centered</p> </body> </html>
The following example works for a text with unmarried and multiple lines. All the same, it requires a fixed height container.
Case of vertically aligning a text by using the CSS line-height property with a stock-still meridian container:
<!DOCTYPE html> <html> <head> <championship>Title of the document</title> <style> div { display: inline-block; width: 100%; top: 200px; vertical-marshal: middle; line-height: 200px; text-align: center; border: 3px dashed #1c87c9; } </way> </head> <trunk> <div>I am vertically centered</div> </torso> </html>
When vertically aligning a text with the padding holding, nosotros must set the peak and bottom padding of the parent element to be equal.
Example of vertically aligning a text with the CSS padding property:
<!DOCTYPE html> <html> <head> <title>Title of the certificate</title> <style> .eye { padding: ten% 0; border: 3px dashed #1c87c9; } </way> </caput> <torso> <div class="center"> <p>I am vertically centered.</p> </div> </torso> </html>
When setting the padding, use % to aid them grow dynamically. This method requires some calculations to understand what values are needed on the superlative and bottom, so as they tin abound dynamically. If you gear up the height to "relative", the adding will non be needed.
Nosotros can vertically align a text with the CSS position and margin properties used with block-level elements. Do non forget to set the acme of the element that you want to center.
- Set the position to "relative" for the "parent" class, and "absolute" for the "child_1" and "child_2" classes.
- Prepare both the top and left properties to 50% to center the left corner of the child <div>.
- Ready the width and height of the child <div> elements in a way and so it will be moved to upward and left.
- Give a negative margin, which halves the acme and width.
Instance of vertically adjustment a text with the CSS position and margin properties:
<!DOCTYPE html> <html> <head> <championship>Title of the document</title> <style> .parent { position: relative; width: 100%; height: 220px; background: #1faadb; color: #fff; } .child_1, .child_2 { position: accented; top: l%; left: 50%; width: 110px; height: 70px; background: #8ebf42; text-align: heart; } .child_1 { margin: -35px 0 0 -55px; } </mode> </head> <body> <div class="parent"> <div course="child_1">Vertically Centered Text</div> </div> <br/> <div course="parent"> <div class="child_2">Non Centered Text</div> </div> </body> </html>
If the content exceeds the container, it will visually disappear.
By setting absolute positioning and stretching, we instruct the browser to automatically fix the margins of the kid element and then as they become equal.
- Set the position for the parent to "relative", and for the child, set it to "accented".
- Gear up the top, lesser, left, and right properties for the child.
- Set the margin to "machine" to make all margins equal and make the child <div> to be centered vertically as well every bit horizontally.
Example of vertically aligning a text with the CSS position property:
<!DOCTYPE html> <html> <caput> <title>Title of the document</title> <style> .parent { position: relative; text-align: center; meridian: 300px; background-color: lightblue; } .child { position: absolute; top: 0; bottom: 0; left: 0; right: 0; width: 50%; meridian: 10%; margin: auto; font-size: 20px; line-elevation: 28px; padding: 10px; background-colour: pinkish; } </style> </head> <body> <div class="parent"> <div form="child">Example</div> </div> </body> </html>
When we accept position: absolute, peak: 50%, left: fifty%, the calculations are fabricated starting from the upper left corner. To position the text in the middle, we must "move" information technology -fifty% left and 50% up past setting transform: translate (-50%;-l%).
Example of vertically aligning a text with the CSS transform property:
<!DOCTYPE html> <html> <head> <title>Title of the document</championship> <style> .parent { position: relative; width: 100%; elevation: 220px; background: #1faadb; colour: #fff; } .child_1, .child_2 { position: absolute; peak: 50%; left: 50%; width: 90px; peak: 90px; padding: 5px; groundwork: #8ebf42; text-align: centre; } .child_1 { transform: translate(-50%, -50%); } </style> </caput> <body> <div grade="parent"> <div class="child_1">Vertically Centered Text</div> </div> <br/> <div class="parent"> <div class="child_2">Not Centered Text</div> </div> </body> </html>
Using the floater div method requires to have an empty <div>, which is floated.
- Set the bladder property to "left".
- Set the heigth to 50%.
- Clear the child <div> by using the articulate belongings.
- Set a negative margin-bottom on the floater <div>.
Remember to have an empty <div> and set the acme of the child element.
Example of vertically adjustment a text with the CSS float property:
<!DOCTYPE html> <html> <caput> <style> #parent { height: 300px; border: 1px solid #1c87c9; } #floater { float: left; width: 100%; height: 50%; margin-bottom: -50px; } #child { clear: both; height: 100px; background: #8ebf42; } </manner> </head> <body> <h1>Example of vertically aligned text with CSS float property:</h1> <div id="parent"> <div id="floater"> </div> <div id="kid"> </div> </div> </body> </html>
Css Text In Div Vertikal Zentrieren,
Source: https://www.w3docs.com/snippets/css/how-to-vertically-center-text-with-css.html
Posted by: brownrive1996.blogspot.com
0 Response to "Css Text In Div Vertikal Zentrieren"
Post a Comment