logo

CSSのdivブロック内のテキストを(水平方向と垂直方向)中央揃えにするにはどうすればよいですか?

div ブロック内のテキストを水平方向と垂直方向の両方で中央揃えにする 視覚的に魅力的なレイアウトを作成するために重要です。フレックスボックス、グリッド、CSS 変換などのさまざまな方法は、明確な利点と欠点を備えたソリューションを提供します。この記事では、div ブロック内でテキストを中央揃えにするための一般的なアプローチを検討します。

Java replaceall文字列

目次

フレックスボックスの使用:

  • 親コンテナを次のように設定します。 ディスプレイ: フレックス; これにより、 フレックスボックス そして親コンテナをフレックスコンテナに変えます。
  • 使用 コンテンツの位置揃え: 中央 子要素を親コンテナ内で水平方向の中央に配置します。
  • 使用 項目の配置: 中央 子要素を親コンテナ内で垂直方向の中央に配置します。

例: この例では、 の flexbox プロパティを使用して div 内のテキストを中央揃えにする方法を示します。 CSS



HTML
   div ブロック タイトル内のテキストを水平方向と垂直方向に中央揃えにします><style>body { テキスト整列: 中央;  .container { 高さ: 300px;  幅: 645ピクセル;  ディスプレイ: フレックス;  コンテンツの位置揃え: 中央;  整列項目: 中央;  境界線: 2 ピクセルの黒一色。  } h1 { 色: 緑;  スタイル>頭> <body> <div> <h1>GeekforGeeksh1> div> body> html>>></pre> </code> <p dir='ltr'>  <b>  <strong>出力:</strong>  </b>  </p>  <img src='//techcodeview.com/img/css-questions/74/how-center-text-horizontally.webp' alt=""><h2 id='using-grid'><span>グリッドの使用</span></h2><ul><li value='1'> <span>CSSグリッド</span> <span>は、複雑で柔軟なグリッドベースのレイアウトを作成できる、もう 1 つの人気のあるレイアウト ツールです。</span></li><li value='2'><span>親コンテナを次のように設定します。</span>   <b>  <strong>表示: グリッド</strong>  </b>   <span>。これにより、CSS グリッドの使用が有効になり、親コンテナがグリッド コンテナに変わります。</span></li><li value='3'><span>使用</span>  <b>  <strong> </strong>  </b>    <b>  <strong>アイテムを配置: 中央</strong>  </b>   <span>プロパティを使用して、子要素をグリッド セル内で水平方向と垂直方向の両方の中央に配置します。このプロパティは両方の短縮形です。</span>  <b>  <strong>項目の位置揃え</strong>  </b>  <span>そして</span>  <b>  <strong>アイテムの整列</strong>  </b>  <span>。</span></li></ul><p dir='ltr'>  <b>  <strong>例:</strong>  </b>  <span>この例では、CSS の Grid プロパティを使用して div 内のテキストを中央揃えにする方法を示します。</span></p>HTML<code class='hljs'> <pre class='hljs'> <html lang='en'> <head> <title>div ブロック タイトル内のテキストを水平方向と垂直方向に中央揃えにします><style>.container { 高さ: 300px;  幅: 645ピクセル;  表示: グリッド;  アイテムを配置: 中央;  境界線: 2 ピクセルの黒一色。  } h1 { 色: 緑;  スタイル>頭> <body> <div> <h1>GeekforGeeksh1> div> body> html>>></pre> </code> <p dir='ltr'>  <b>  <strong>出力:</strong>  </b>  </p>  <img src='//techcodeview.com/img/css-questions/74/how-center-text-horizontally.webp' alt=""><h2 id='using-text-align'>  <b>  <strong>テキスト整列の使用:</strong>  </b>  </h2><ul><li value='1'><span>の</span> <span>テキスト整列</span> <span>プロパティは、div ブロック内でテキストを水平方向に中央揃えにするシンプルで簡単な方法です。</span></li><li value='2'><span>親コンテナを次のように設定します。</span>  <b>  <strong>テキスト配置: 中央。</strong>  </b>  <span>これにより、子要素が親コンテナ内で水平方向の中央に配置されます。</span></li><li value='3'><span>使用</span>  <b>  <strong>行の高さ:</strong>  </b>  <span>子要素を親コンテナ内で垂直方向の中央に配置します。の値は親コンテナの高さと同じである必要があります。</span></li></ul><p dir='ltr'>  <b>  <strong>例:</strong>  </b>  <span>この例では、CSS のテキスト配置を使用して div 内のテキストを中央揃えにする方法を示します。</span></p>HTML<code class='hljs'> <pre class='hljs'> <html lang='en'> <head> <title>div ブロック タイトル内のテキストを水平方向と垂直方向に中央揃えにします><style>.container { 高さ: 300px;  幅: 645ピクセル;  テキスト整列: 中央;  行の高さ: 400px;  境界線: 2 ピクセルの黒一色。  } h1 { 色: 緑;  スタイル>頭> <body> <div> <h1>GeekforGeeksh1> div> body> html>>></pre> </code> <p dir='ltr'>  <b>  <strong>出力:</strong>  </b>  </p>  <img src='//techcodeview.com/img/css-questions/74/how-center-text-horizontally.webp' alt=""><h2 id='using-table-cell'><span>表のセルの使用</span></h2><ul><li value='1'><span>親コンテナを次のように設定します。</span>  <b>  <strong>ディスプレイ: テーブル。</strong>  </b>  <span>これはテーブルの動作をエミュレートします。</span></li><li value='2'><span>子要素を次のように設定します</span>  <b>  <strong>表示: テーブルセル</strong>  </b>  <span>。これは表のセルの動作をエミュレートします。</span></li><li value='3'><span>使用</span>  <b>  <strong>垂直配置: 中央</strong>  </b>  <span>子要素を親コンテナ内で垂直方向の中央に配置します。</span></li><li value='4'><span>使用</span>  <b>  <strong>テキスト配置: 中央</strong>  </b>  <span>子要素を親コンテナ内で水平方向の中央に配置します。</span></li></ul><p dir='ltr'>  <b>  <strong>例:</strong>  </b>  <span>この例では、CSS のテキスト テーブル セルを使用して、div 内のテキストを中央揃えにする方法を示します。</span></p><blockquote style="text-transform: capitalize;font-size: 20px; padding: 10px;"><a href="/sql-case">SQLの大文字と小文字は何ですか</a>
</blockquote>HTML<code class='hljs'> <pre class='hljs'> <html lang='en'> <head> <title>div ブロック タイトル内のテキストを水平方向と垂直方向に中央揃えにします><style>.container { 高さ: 300px;  幅: 645ピクセル;  表示: テーブルセル;  テキスト整列: 中央;  垂直配置: 中央;  境界線: 2 ピクセルの黒一色。  } h1 { 色: 緑;  スタイル>頭> <body> <div> <h1>GeekforGeeksh1> div> body> html>>></pre> </code> <p dir='ltr'>  <b>  <strong>出力:</strong>  </b>  </p>  <img src='//techcodeview.com/img/css-questions/74/how-center-text-horizontally.webp' alt=""><h2 id='using-transform-property'><span>Transform プロパティの使用:</span></h2><ul><li value='1'><span>親コンテナを次のように設定します。</span>  <b>  <strong>位置: 相対的。</strong>  </b>  <span>これにより、子要素に絶対配置を使用できるようになります。</span></li><li value='2'><span>子要素を次のように設定します</span>  <b>  <strong>位置: 絶対。</strong>  </b>  <span>これにより、子要素に絶対配置を使用できるようになります。</span></li><li value='3'><span>子要素の設定</span>  <b>  <strong>上</strong>  </b>  <span>そして</span>  <b>  <strong>左</strong>  </b>  <span>プロパティを</span>  <b>  <strong>50%</strong>  </b>  <span>。これにより、子要素が親コンテナの中央に配置されます。</span></li><li value='4'><span>使用</span>  <b>  <strong>変換: 変換(-50%、-50%)</strong>  </b>  <span>子要素を水平方向と垂直方向の両方で中央揃えにします。</span></li></ul><p dir='ltr'>  <b>  <strong>例:</strong>  </b>  <span>この例では、CSS の transform プロパティを使用して div 内のテキストを中央揃えにする方法を示します。</span></p>HTML<code class='hljs'> <pre class='hljs'> <html lang='en'> <head> <title>div ブロック タイトル内のテキストを水平方向と垂直方向に中央揃えにします><style>.container { 高さ: 300px;  幅: 645ピクセル;  位置: 相対的;  境界線: 2 ピクセルの黒一色。  } h1 { 位置: 絶対;  トップ: 50%;  色: 緑;  左: 50%。  変換: 変換(-50%, -50%);  スタイル>頭> <body> <div> <h1>GeekforGeeksh1> div> body> html>>></pre> </code> <p dir='ltr'>  <b>  <strong>出力:</strong>  </b>  </p>  <img src='//techcodeview.com/img/css-questions/74/how-center-text-horizontally.webp' alt=""><p dir='ltr'><span>要約すると、選択するアプローチは、特定の使用例、他の要素との互換性、および望ましいデザインの美学によって異なります。これらの方法を使用すると、さまざまなレイアウトやデザインでテキスト ブロックを中央揃えにすることができます。</span></p>  <br>  <br></article><div class="rekl_placeholder"><script type="text/javascript">atOptions = {'key' : 'f2b09f3e7178b263531e10998e9a32fa','format' : 'iframe','height' : 250,'width' : 300,'params' : {}};</script><script type="text/javascript" src="//spiritscaution.com/f2b09f3e7178b263531e10998e9a32fa/invoke.js"></script></div> </article>
                         
                        </div><!--//content-->
                    </div><!--//section-inner-->                 
                </section><!--//section-->
    
            </div><!--//primary-->
            <div class="secondary col-md-4 col-sm-12 col-xs-12">
                  <aside class="info aside section">
                    <div class="section-inner">
                        <h2 class="">カテゴリ</h2>
                        <div class="content">
                            <ul class="list-unstyled">
                                <li> <a href="/python-functions/">Python 関数</a> </li><li> <a href="/array-range-queries/">配列範囲クエリ</a> </li><li> <a href="/interview-questions/">面接の質問</a> </li><li> <a href="/agile-tutorial/">アジャイルチュートリアル</a> </li><li> <a href="/roman-numerals/">ローマ数字</a> </li><li> <a href="/computer-network/">コンピュータネットワーク</a> </li><li> <a href="/culture-gk/">カルチャー合同会社</a> </li><li> <a href="/minecraft-blogs/">マインクラフトのブログ</a> </li><li> <a href="/vba-tutorial/">Vba チュートリアル</a> </li><li> <a href="/intellij-idea-tutorial/">Intellij Idea チュートリアル</a> </li>
                                
                            </ul>
                        </div><!--//content-->  
                    </div><!--//section-inner-->                 
                </aside><!--//aside-->
                
                 <aside class="testimonials aside section">
                	 <div class="section-inner">
                        <div class="content">
                            <div class="item">
  								<div class="rekl_placeholder" id="sidebar_placeholder_1"> <script type="text/javascript">atOptions = {'key' : 'f53f1abf3c3a788f849c12a956865db1','format' : 'iframe','height' : 300,'width' : 160,'params' : {}};</script><script type="text/javascript" src="//spiritscaution.com/f53f1abf3c3a788f849c12a956865db1/invoke.js"></script> <br></div>                                                   
                            </div>
                        </div>
                    </div>
                   <div class="section-inner">
                        <h2 class="heading">ベスト米国史リージェントレビューガイド 2020</h2>
                        <div class="content">
                            <div class="item">
                                 <blockquote class="quote">                                  
                                    <span> <i class="fa fa-quote-left"></i> 米国史リージェントの勉強をしていますか?専門家によるレビュー ガイドを活用して、優れた成果を上げるための最高のヒントと旅を入手してください。</span>
                                </blockquote>                
                                                                                             
                            </div><!--//item-->
                            
                            <p> <a class="more-link" href="/best-us-history-regents-review-guide-2020-131126"> <i class="fa fa-external-link"></i> 続きを読みます</a> </p> 
                            
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
                
               
                            
                 <aside class="languages aside section">
                    <div class="section-inner">
                        <h2 class="heading">興味深い記事</h2>
                        <div class="content">
                            <ul class="list-unstyled">
                                <li class="item">
                                    <span class="title"> <strong> <a href="/cancer-moon-sign-personality-131310">蟹座の月星座:性格と人間関係</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/c-algorithm/">C++ アルゴリズム</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/magnitude-physics">物理学における大きさ</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/java-array-programs/">Java 配列プログラム</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/what-is-insert-key-laptop">ラップトップの Insert キーとは何ですか?</a> </strong> </span>
                                    
                                </li><!--//item-->
                               
                            </ul>
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
                
              
                 <aside class="list music aside section">
                    <div class="section-inner">
                        <h2 class="heading">人気の投稿</h2>
                        <div class="content">
                            <ul class="list"> <li><a href="/java-scanner">Javaスキャナ</a>
</li><li><a href="/run-command-linux">Linuxのコマンド実行</a>
</li><li><a href="/natasha-dalal">ナターシャ・ダラル</a>
</li><li><a href="/javascript-global-variable">グローバル変数js</a>
</li><li><a href="/an-array-strings-c">C言語の文字列配列</a>
</li> 
                                
                                
                            </ul>
                        </div><!--//content-->
                    </div><!--//section-inner-->
                </aside><!--//section-->
              
            </div><!--//secondary-->    
        </div><!--//row-->
    </div><!--//masonry-->
    
	    <footer class="footer">
        <div class="container text-center">
                <span>
Copyright ©2025 全著作権所有 |  <a href="//lv.techcodeview.com/">techcodeview.com</a> | <a href="/disclaimer" rel="nofollow noopener noreferrer" target="_blank">免責事項</a>  |  <a href="/about-us" rel="nofollow noopener noreferrer" target="_blank">私たちについて</a>  |  <a href="/privacy-policy" rel="nofollow noopener noreferrer" target="_blank">プライバシーポリシー</a>  </span>
        </div>
    </footer>
 
         
    <script type="text/javascript" src="https://techcodeview.com/template/assets/plugins/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://techcodeview.com/template/assets/plugins/bootstrap/js/bootstrap.min.js"></script>    
    

    <script type="text/javascript" src="https://techcodeview.com/template/assets/js/main.js"></script>     
	
	<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
	
</body>
</html>