14

たとえば、保留ページ内で使用するために、真のセンターCSS divクロスブラウザを作成するにはどうすればよいですか?

私はこの2007cssトリックを試しました- オブジェクトを正確に中央に配置する方法ですが、コードのJSFiddleからわかるように、100%中央ではありません。

HTML:

<div class="center">
  <p>Text</p>
</div>

CSS:

.center{
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: -50px;
  margin-left: -100px;
  border:5px solid black;
}
4

8 に答える 8

11

この手法では、要素の幅と高さが固定されている必要があります。幅と高さを設定していません。境界線と余白に基づいて、中央に配置するには、幅を190ピクセル、高さを90ピクセルにする必要があります。固定の幅と高さを組み合わせて使用​​すると、テキストと境界線が中央に配置されline-heightます。それを試してみてください。text-align

CSS

.center{
  position: fixed;
  top: 50%;
  left: 50%;
  width: 190px;
  height: 90px;
  line-height: 90px;
  text-align: center;
  margin-top: -50px;
  margin-left: -100px;
  border:5px solid black;
}
于 2013-01-07T03:53:19.983 に答える
10

あなたは純粋なCSSでそれを行うことができます:

html {
  width: 100%;
  height: 100%;
}
body {
  min-width: 100%;
  min-height: 100%;
}
div.centeredBlueBox {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  width: 300px;
  height: 200px;
  background-color: blue;
}

および300pxの派生値(:autoまたは30%)ではなく、具体的な値(例:)を指定することが重要です。widthheight

于 2013-01-20T09:35:41.107 に答える
3
<div style='position:absolute; left:50%; top:50%; margin-left:(-)width_of_your_element/2px; margin-top:(-)height_of_your_element/2px'>

例えば

<!DOCTYPE html>
<html>
<body>
<div style='border:1px solid; width:200px; height:200px; position:absolute; left:50%; top:50%; margin-left:-100px; margin-top:-100px'>hallo</div>
</body>
</html>
于 2013-01-07T03:54:35.303 に答える
2

それが私の解決策です:

<div style="position: absolute; left: 50%; height: 100%;">
    <div style="position: relative; left: -50%; display: table; height: 100%;">
        <div style="display: table-cell; vertical-align: middle;">
            //your content here
        </div>
    </div>
</div>

多くのブラウザで動作します。また、レイアウト後にコンテンツを追加しても問題ありません。

于 2013-07-24T08:15:13.900 に答える
2

これは、IEの垂直方向の配置用に作成した高さセッターを使用した追加の例です。余分なスパンには、vertical-align:middleがあります。

<style type="text/css">
    html, body {
        margin: 0;
        padding: 0;
        overflow:hidden;
        text-align:center;
    }
    html, body{
        height: 100%;
    }
    #loginContainer {
        margin: 0 auto;
        display: table;
        min-width:300px;
        text-align:left;
        height: 85%; /* vertical align not exact in the middle */
    }
    #loginContainer .label{
        width: 25%;
        float:left;
        white-space:nowrap;
        line-height:20px;
    }
    #loginContainer h2{
        border-bottom:2px solid #B4C3E1;
        border-width:0 0 3px;
        padding:2px 4px;
    }
    .mainHeader {
        position:absolute;
        top:0;
        left:0; 
        text-align:center; 
        width:100%; 
        font-size:2em;
        white-space:nowrap;
    }
    .detailsText {
        border-top:1px solid #F60;
        margin-top:5px;
        clear:both;
    }
    /* layout horizontal and vertical */
    .horizontalBox {
        display: table-cell;
        vertical-align: middle; /* not seen by IE6-7 */
        height: 100%;
        white-space: nowrap;
    }
    .verticalBox {
        padding: 55px 0 0 0; /* 55px height of logo */
    }
    .rightText {
        text-align:right;
    }
</style>
<!--[if lte IE 8]>
<style type="text/css">
    #loginContainer {
        width:300px; /* minimum width */
    }
    .horizontalBox {
        text-align: center;
    }
    .verticalBox, .heightSetter {
        display: inline-block;
        vertical-align: middle;
        text-align: left;
        zoom:1;
    }
    .heightSetter {
        height: 100%;
    }
    .verticalBox {
        display: inline;
        height: 0;
    }
    .heightSetter {
        width: 1px;
    }
</style>    
<![endif]-->
<div class="mainHeader">This is the Header content</div>
<div id="loginContainer">
    <div class="horizontalBox">
        <div class="verticalBox">
            <h2>My header of the vertical box</h2>
            <p>My Content</p>
        </div>
        <span class="heightSetter"></span>
    </div>
</div>
于 2013-08-14T20:38:29.740 に答える
2

これを見てください; かなり賢い記事。

于 2013-12-23T23:57:31.287 に答える
2

displayのタイプをに設定DIVtable-cellます。次に、要素vertical-alignと同じように、通常を使用できます。TD

<div style="display: table-cell; vertical-align: middle; height: 200px;">
Centered Text
</div>
于 2014-03-20T00:59:43.837 に答える
1

これは私がしたことです

<html>
<head>
    <title>Page Title</title>

    <style>
       .center { margin:auto; width:500px; background-color:green; }
    </style>
</head>
<body>
    <div class="center">
        <p>Help me please</p>
    </div>
</body>
</html>

お役に立てれば。

于 2013-01-07T03:57:51.897 に答える