Bootstrap 图像
目标
このチュートリアルでは、画像にスタイルを適用するための3つのBootstrap 3のクラスについて説明します。また、Bootstrap 3がレスポンシブ画像のために提供する内容についても説明します。
一般的なスタイル
Bootstrap 3 が提供する画像スタイルを定義する特別なクラスについて説明する前に、Bootstrap 3 が提供する画像の一般的なスタイルを見ていきます。
img {
border: 0;
}
これは、Bootstrap 3 の CSS で見つかった、画像に対して定義された最初の CSS ルールであり、画像がレンダリングされるときにボーダーを削除するために使用されます。
そして、印刷用のメディアクエリ内で、これらのルールが規定されています。
img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
page-break-inside: avoid;画像内の改ページを避けます。
max-width: 100% !important;画像に対して定義されたスタイルは明白です。ここでは、画像の幅がコンテナを超えていても、コンテナ内に表示されるように制限するために使用されます。これは!importantとともに使用され、このスタイルを壊す他のあらゆるスタイルを上書きします。開発者は、モバイルデバイスでの画像表示をより良くサポートするために、これら2つのルールを特に使用することがあります。
ここに画像に関する別のルールがあります。
img {
vertical-align: middle;
}
このルールにより、画像はdivや他の要素内で垂直方向に中央揃えになります。以下の例のように表示されます。
实例
<html>
<head>
<title>Bootstrap 3 渲染图像实例</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="dist/css/bootstrap.min.css.html" rel="stylesheet" media="screen">
<style>
body {
padding: 50px
}
.mdl {
background-color: silver;
padding: 7px
}
</style>
<!-- HTML5 Shim と Respond.js は、IE8 で HTML5 要素とメディアクエリをサポートします。 -->
<!-- WARNING: Respond。如果通过file://查看页面js将不工作。 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<p class="mdl"><img src="icon-default-screenshot.png.html">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<!-- jQuery (BootstrapのJavaScriptプラグインに必要です--->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- すべてのコンパイル済みプラグイン(下記参照)を含めるか、必要に応じて個別のファイルを含めます -->
<script src="dist/js/bootstrap.min.js.html"></script>
</body>
</html>
ここをクリックして、オンラインでデモを表示
なお、コンテキストに応じて画像を垂直方向に中央揃えする必要がある場合は、追加のスタイルが必要です。
特別なスタイル
Bootstrap 3 は、特定のスタイルで画像を表示するための3つのクラスを提供しています。
img-rounded
このクラスを使用して、角が丸い画像を表示することができます。これを実現するために、Bootstrap はimg-roundedクラスを提供しています。このクラスのスタイルは次のように定義されています。
.img-rounded {
border-radius: 6px;
}
だから、それはborder-radius属性は6ピクセルの値に設定され、関連する画像の角丸を定義します。以下の例では、同じ2つの画像を示しています。最初の画像にはimg-roundedclassがなく、2番目の画像にはclassがあります。img-roundedクラス。なお、2番目の画像は角が丸くなっています。あなたはここをクリックして、オンラインで実例を表示。
实例
<html>
<head>
<title>Bootstrap 3 使用圆角渲染图像实例</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="dist/css/bootstrap.min.css.html" rel="stylesheet" media="screen">
<style>
body {
padding: 50px
}
</style>
<!-- HTML5 Shim と Respond.js は、IE8 で HTML5 要素とメディアクエリをサポートします。 -->
<!-- WARNING: Respond。如果通过file://查看页面js将不工作。 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<img src="rounded-corner-images.png.html" alt="image with rounded corners">
<img src="rounded-corner-images.png.html" alt="image with rounded corners" class="img-rounded">
<!-- jQuery (BootstrapのJavaScriptプラグインに必要です--->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- すべてのコンパイル済みプラグイン(下記参照)を含めるか、必要に応じて個別のファイルを含めます -->
<script src="dist/js/bootstrap.min.js.html"></script>
</body>
</html>
img-thumbnail
これはもう1つの Bootstrap 3 の CSS クラスで、画像にサムネイルを追加します。このクラスのコードは次のように表示されます。
.img-thumbnail {
display: inline-block;
height: auto;
max-width: 100%;
padding: 4px;
line-height: 1.428571429;
background-color: #FFFFFF;
border: 1px solid #DDDDDD;
border-radius: 4px;
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
以下はこのクラスを使用した例です。あなたはここをクリックして、オンラインデモを表示。
实例
<html>
<head>
<title>Bootstrap 3 缩略图</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="dist/css/bootstrap.min.css.html" rel="stylesheet" media="screen">
<style>
body {
padding: 50px
}
</style>
<!-- HTML5 Shim と Respond.js は、IE8 で HTML5 要素とメディアクエリをサポートします。 -->
<!-- WARNING: Respond。如果通过file://查看页面js将不工作。 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<img src="image-with-thumbnail.png.html" alt="image without thumbnail corners">
<img src="image-with-thumbnail.png.html" alt="image with thumbnail corners" class="img-thumbnail">
<!-- jQuery (BootstrapのJavaScriptプラグインに必要です--->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- すべてのコンパイル済みプラグイン(下記参照)を含めるか、必要に応じて個別のファイルを含めます -->
<script src="dist/js/bootstrap.min.js.html"></script>
</body>
</html>
img-circle
border-radius プロパティを使用することで、Bootstrap 3 は円形で表示される画像を作成します。img-circleclass の CSS コードは次のとおりです
.img-circle {
border-radius: 50%;
}
ここをクリックして、オンラインで実例を表示。以下はスクリーンショットとコードです。
实例
<html>
<head>
<title>Bootstrap 3 圆形图像</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="dist/css/bootstrap.min.css.html" rel="stylesheet" media="screen">
<style>
body {
padding: 50px
}
</style>
<!-- HTML5 Shim と Respond.js は、IE8 で HTML5 要素とメディアクエリをサポートします。 -->
<!-- WARNING: Respond。如果通过file://查看页面js将不工作。 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<img src="image-with-circle.png.html" alt="image without circle shape">
<img src="image-with-circle.png.html" alt="image with circle shape" class="img-circle">
<!-- jQuery (BootstrapのJavaScriptプラグインに必要です--->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- すべてのコンパイル済みプラグイン(下記参照)を含めるか、必要に応じて個別のファイルを含めます -->
<script src="dist/js/bootstrap.min.js.html"></script>
</body>
</html>
响应式图像
Bootstrap 3 は、すぐに使用できるレスポンシブ画像を提供していません。。追加する必要がありますimg-responsiveクラスを画像に追加すると、レスポンシブになります。このクラスの CSS コードは次のとおりです。
.img-responsive {
display: block;
height: auto;
max-width: 100%;
}
これは、画像をブロックレベル要素として表示し、高さを画像の高さと同じにし、画像の最大幅を100%にして、画像が配置されているデバイスに応じて表示を制限することを定義しています。
画像をデフォルトでレスポンシブにするには、次の CSS コードを追加できます。img{ }。
その class を使用する例は次のとおりです。〜できますここをクリックして、オンラインでデモを表示。
实例
<html>
<head>
<title>Bootstrap 3 响应式图像</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="dist/css/bootstrap.min.css.html" rel="stylesheet" media="screen">
<style>
body {
padding: 50px
}
img {
margin-bottom:30px
}
</style>
<!-- HTML5 Shim と Respond.js は、IE8 で HTML5 要素とメディアクエリをサポートします。 -->
<!-- WARNING: Respond。如果通过file://查看页面js将不工作。 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<img src="image-with-circle.png.html" alt="without responsive image feature">
<img src="image-with-circle.png.html" alt="with responsive image feature" class="img-responsive">
<!-- jQuery (BootstrapのJavaScriptプラグインに必要です--->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- すべてのコンパイル済みプラグイン(下記参照)を含めるか、必要に応じて個別のファイルを含めます -->
<script src="dist/js/bootstrap.min.js.html"></script>
</body>
</html>
画像にレスポンシブ特性を追加する方法には制限があります。
それぞれ大画面デバイスとモバイルデバイス(画面サイズがより小さく、引き起こす可能性がある性能问题)上で同じ大きさの高解像度画像を読み込むことです。ブラウザは CSS と JS が読み込まれる前に画像を事前読み込みするため、低速なネットワーク接続ではパフォーマンスの問題も発生します。想像してみてください。大きな画像とその中の特定のオブジェクトがあるとき、モバイルデバイスで画像を表示すると、画像は小さいバージョンに縮小され、画像が小さく見えてしまいます。略して艺术指导问题。
開発者はこれらの制限を克服する解決策を考え出しました。私たちは、あるMarc GrabanskiとChristopher Schmitt使用HiSRCの例です。これは、画像の低解像度、中解像度、高解像度のバージョンを自動的に作成するJqueryプラグインで、表示されるバージョンは画像を表示するデバイスの解像度と帯域幅に依存します。
後述のレスポンシブ画像チュートリアルその中で、これらすべての方法について詳しく説明します。
ここをクリックして、オンラインでデモを表示。コードは次のとおりです:
实例
<html>
<head>
<title>Bootstrap 3 でHiSRCを使用したレスポンシブ画像の例</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="dist/css/bootstrap.min.css.html" rel="stylesheet" media="screen">
<style>
body {
padding: 50px
}
img {
margin-bottom:30px
}
</style>
<!-- HTML5 Shim と Respond.js は、IE8 で HTML5 要素とメディアクエリをサポートします。 -->
<!-- WARNING: Respond。如果通过file://查看页面js将不工作。 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<img src="5216852563_eca0af1b0d_m.jpg.html" data-1x="5216852563_eca0af1b0d.jpg" data-2x="5216852563_90c3f9c402_o.jpg" class="hisrc" />
<!-- jQuery (BootstrapのJavaScriptプラグインに必要です--->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- すべてのコンパイル済みプラグイン(下記参照)を含めるか、必要に応じて個別のファイルを含めます -->
<script src="dist/js/bootstrap.min.js.html"></script>
<script>
$(document).ready(function(){
$(".hisrc").hisrc();
});
</script>
<p>Photo courtesy: /http://www.flickr.com/photos/cubagallery/</p>
</body>
</html>