CSS-Layout – Inline-Block


Inhaltsverzeichnis

    Inhaltsverzeichnis anzeigen


Die Anzeige: Inline-Block-Wert

Im Vergleich zu display: inline, dem Major Der Unterschied besteht darin, dass display: inline-block dies zulässt um eine Breite und Höhe für das Element festzulegen.

Auch mit Anzeige: inline-block, die oberen und unteren Ränder/Abstände werden berücksichtigt, aber mit display: inline sind sie es nicht.

Im Vergleich zu display: block, dem Major Der Unterschied besteht darin, dass display: inline-block dies tut Fügen Sie nach dem Element keinen Zeilenumbruch ein, damit das Element neben einem anderen stehen kann Elemente.

Das folgende Beispiel zeigt das unterschiedliche Verhalten von display: inline, display: inline-block und display: block:

Beispiel

 span.a {
  display: inline; /* the default for span */
  
  width: 100px;
  height: 100px;
  
  padding: 5px;
  border: 1px solid blue; 
  background-color: yellow; 
}
span.b {
    display: 
  inline-block;
  width: 100px;
  height: 
  100px;
  padding: 5px;
  border: 1px 
  solid blue; 
  background-color: yellow; 
}

  span.c {
  display: block;
  width: 
  100px;
  height: 100px;
  padding: 5px;
  
  border: 1px solid blue; 
  background-color: yellow; 
}

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<head>
<style>
span.a {
  display: inline; /* the default for span */
  width: 100px;
  height: 100px;
  padding: 5px;
  border: 1px solid blue;  
  background-color: yellow; 
}

span.b {
  display: inline-block;
  width: 100px;
  height: 100px;
  padding: 5px;
  border: 1px solid blue;    
  background-color: yellow; 
}

span.c {
  display: block;
  width: 100px;
  height: 100px;
  padding: 5px;
  border: 1px solid blue;    
  background-color: yellow; 
}
</style>
</head>
<body>

<h1>The display Property</h1>

<h2>display: inline</h2>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consequat scelerisque elit sit amet consequat. Aliquam erat volutpat. <span class="a">Aliquam</span> <span class="a">venenatis</span> gravida nisl sit amet facilisis. Nullam cursus fermentum velit sed laoreet. </div>

<h2>display: inline-block</h2>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consequat scelerisque elit sit amet consequat. Aliquam erat volutpat. <span class="b">Aliquam</span> <span class="b">venenatis</span> gravida nisl sit amet facilisis. Nullam cursus fermentum velit sed laoreet. </div>

<h2>display: block</h2>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consequat scelerisque elit sit amet consequat. Aliquam erat volutpat. <span class="c">Aliquam</span> <span class="c">venenatis</span> gravida nisl sit amet facilisis. Nullam cursus fermentum velit sed laoreet. </div>

</body>
</html>



Verwenden von Inline-Block zum Erstellen von Navigationslinks

Eine häufige Verwendung für display: inline-block besteht darin, Listenelemente horizontal statt vertikal anzuzeigen. Die folgende Beispiel erstellt horizontale Navigationslinks:

Beispiel

 .nav {
  background-color: yellow; 
  list-style-type: none;
  text-align: 
  center; 
  padding: 0;
  margin: 0;
}
.nav li {
  display: inline-block;
    font-size: 20px;
  padding: 
  20px;
}

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<head>
<style>
.nav {
  background-color: yellow; 
  list-style-type: none;
  text-align: center;
  margin: 0;
  padding: 0;
}

.nav li {
  display: inline-block;
  font-size: 20px;
  padding: 20px;
}
</style>
</head>
<body>

<h1>Horizontal Navigation Links</h1>
<p>By default, list items are displayed vertically. In this example we use display: inline-block to display them horizontally (side by side).</p>
<p>Note: If you resize the browser window, the links will automatically break when it becomes too crowded.</p>

<ul class="nav">
  <li><a href="#home">Home</a></li>
  <li><a href="#about">About Us</a></li>
  <li><a href="#clients">Our Clients</a></li>  
  <li><a href="#contact">Contact Us</a></li>
</ul>

</body>
</html>