CSS-elrendezés - Vízszintes és függőleges igazítás


Tartalomjegyzék

    Tartalomjegyzék megjelenítése

Center elements
horizontally and vertically


Elemek középre igazítása

Egy blokkelem (például <div>) vízszintes középre állításához használja a margin: auto; parancsot.

Az elem szélességének beállítása megakadályozza, hogy az elem kinyúljon tartályának szélei.

Az elem ekkor a megadott szélességet és a maradék helyet foglalja el egyenlően oszlik el a két margó között:

This div element is centered.

Példa

.center
{
  margin: auto;
   
width: 50%;
   
border: 3px solid green;
  padding: 10px;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  margin: auto;
  width: 60%;
  border: 3px solid #73AD21;
  padding: 10px;
}
</style>
</head>
<body>

<h2>Center Align Elements</h2>
<p>To horizontally center a block element (like div), use margin: auto;</p>

<div class="center">
  <p>Hello World!</p>
</div>

</body>
</html>


Megjegyzés: A középre igazításnak nincs hatása, ha a width tulajdonság nincs beállítva (vagy 100%-ra állítva).


Szöveg középre igazítása

Ha csak egy elemen belül szeretné középre helyezni a szöveget, használja a text-align: center; parancsot.

This text is centered.

Példa

.center {
  text-align: center;
  
border: 3px solid green;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  text-align: center;
  border: 3px solid green;
}
</style>
</head>
<body>

<h2>Center Text</h2>

<div class="center">
  <p>This text is centered.</p>
</div>

</body>
</html>


Tipp: A szöveg igazítására vonatkozó további példákért tekintse meg a CSS-szöveg fejezetet.



Középre helyez egy képet

Egy kép középre állításához állítsa a bal és a jobb margót auto értékre, és alakítsa blokk elemmé:

Paris

Példa

img
{
  display: block;
    margin-left: auto;
  margin-right: auto;
  width: 40%;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
img {
  display: block;
  margin-left: auto;
  margin-right: auto;
}
</style>
</head>
<body>

<h2>Center an Image</h2>
<p>To center an image, set left and right margin to auto, and make it into a block element.</p>

<img src="paris.jpg" alt="Paris" style="width:40%">

</body>
</html>



Balra és jobbra igazítás - A pozíció használata

Az elemek igazításának egyik módja a position: abszolút; használata:

In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.

Példa

.right
{
  position: absolute;
   
right: 0px;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.right {
  position: absolute;
  right: 0px;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}
</style>
</head>
<body>

<h2>Right align with the position property</h2>

<p>An example of how to right align elements with the position property:</p>

<div class="right">
  <p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.</p>
</div>

</body>
</html>


Megjegyzés: Az abszolút pozicionált elemek kikerülnek a normál áramlásból, és átfedhetik az elemeket.


Balra és jobbra igazítás - úszó segítségével

Egy másik módszer az elemek igazítására a float tulajdonság használata:

Példa

.right
{
  float: right;
   
width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.right {
  float: right;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}
</style>
</head>
<body>

<h2>Right align with the float property</h2>

<p>An example of how to right align elements with the float property:</p>

<div class="right">
  <p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.</p>
</div>

</body>
</html>



A clearfix Hack

Megjegyzés: Ha egy elem magasabb, mint az azt tartalmazó elem, és lebegve van, akkor túlcsordul a tartályán kívül. A "clearfix hack" segítségével javíthatja ezt (lásd az alábbi példát).

Clearfix nélkül

Clearfix-szel

Ezután a javítandó elemhez hozzáadhatjuk a clearfix hacket ez a probléma:

Példa

 .clearfix::after {
  content: "";
  clear: both;
  
  display: table;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
div {
  border: 3px solid #4CAF50;
  padding: 5px;
}

.img1 {
  float: right;
}

.img2 {
  float: right;
}

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}
</style>
</head>
<body>

<h2>Without Clearfix</h2>

<p>This image is floated to the right. It is also taller than the element containing it, so it overflows outside of its container:</p>

<div>
  <img class="img1" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet...
</div>

<h2 style="clear:right">With New Modern Clearfix</h2>
<p>Add the clearfix hack to the containing element, to fix this problem:</p>

<div class="clearfix">
  <img class="img2" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet...
</div>

</body>
</html>



Függőlegesen középre - párnázással

A CSS-ben sokféleképpen lehet egy elemet függőlegesen középre igazítani. Egy egyszerű megoldás a felső és alsó kitöltés használata:

I am vertically centered.

Példa

.center {
  padding: 70px 0;
  border: 3px solid 
green;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  padding: 70px 0;
  border: 3px solid green;
}
</style>
</head>
<body>

<h2>Center vertically with padding</h2>

<p>In this example, we use the padding property to center the div element vertically:</p>

<div class="center">
  <p>I am vertically centered.</p>
</div>

</body>
</html>


Függőleges és vízszintes középre állításához használja a padding és a text-align: center használatát:

I am vertically and horizontally centered.

Példa

.center {
  padding: 70px 0;
  border: 3px solid 
green;
  text-align: center;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  padding: 70px 0;
  border: 3px solid green;
  text-align: center;
}
</style>
</head>
<body>

<h2>Center with padding and text-align</h2>

<p>In this example, we use padding and text-align to center the div element both vertically and horizontally:</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>



Függőleges középre - Vonalmagasság használatával

Egy másik trükk az, hogy a line-height tulajdonságot egyenlő értékkel használjuk a height tulajdonsághoz:

I am vertically and horizontally centered.

Példa

.center {
  line-height: 200px;
  height: 200px;
  border: 3px solid green;
  text-align: center;
}
/* If the text has multiple lines, add the 
following: */
.center p {
  line-height: 1.5;
  display: inline-block;
  vertical-align: middle;
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  line-height: 200px;
  height: 200px;
  border: 3px solid green;
  text-align: center;
}

.center p {
  line-height: 1.5;
  display: inline-block;
  vertical-align: middle;
}
</style>
</head>
<body>

<h2>Center with line-height</h2>

<p>In this example, we use the line-height property with a value that is equal to the height property to center the div element:</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>



Függőleges középre - Pozíció és átalakítás használata

Ha kitöltés és vonalmagasság nem opciók, egy másik megoldás a pozicionálás és a transform tulajdonság használata:

I am vertically and horizontally centered.

Példa

.center { 
  height: 200px;
  position: relative;
  border: 3px solid green; 
}

.center p {
  margin: 0;
  
position: absolute;
  top: 50%;
  
left: 50%;
  transform: translate(-50%, -50%);
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.center { 
  height: 200px;
  position: relative;
  border: 3px solid green; 
}

.center p {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
</style>
</head>
<body>

<h2>Center with position and transform</h2>

<p>In this example, we use positioning and the transform property to vertically and horizontally center the div element:</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>


Tipp: A transzformációs tulajdonságról többet tudhat meg a 2D-s átalakításainkból Fejezet.


Függőleges középre - Flexbox használatával

Használhatja a flexboxot a dolgok központosítására is. Csak vegye figyelembe, hogy a flexbox nem támogatott az IE10-ben és a korábbi verziókban:

I am vertically and horizontally centered.

Példa

 .center {
  display: flex;
  justify-content: center;
  
  align-items: center;
  height: 200px;
  border: 3px solid 
  green; 
}

Próbálja ki Ön is →

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  border: 3px solid green; 
}
</style>
</head>
<body>

<h2>Flexbox Centering</h2>

<p>A container with both the justify-content and the align-items properties set to <em>center</em> will align the item(s) in the center (in both axis).</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>


Tipp: A Flexboxról a CSS Flexbox fejezetünkben tudhat meg többet.