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="/puzzles/">パズル</a> </li><li> <a href="/us-gk/">アメリカGk</a> </li><li> <a href="/linux-unix-tools/">Linux Unix ツール</a> </li><li> <a href="/sql-pl-sql/">Sql-Pl/Sql</a> </li><li> <a href="/other/">他の</a> </li><li> <a href="/converter-tools/">コンバータツール</a> </li><li> <a href="/web-technologies/">ウェブ技術</a> </li><li> <a href="/chemistry-class-10-cat/">化学-10級</a> </li><li> <a href="/java-advanced/">Java 上級</a> </li><li> <a href="/kafka-tutorial/">カフカのチュートリアル</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">隣接行列とは何ですか?</h2>
                        <div class="content">
                            <div class="item">
                                 <blockquote class="quote">                                  
                                    <span> <i class="fa fa-quote-left"></i> ブログとの隣接行列とは何ですか、quora とは何ですか、yandex とは何ですか、連絡先ページ、duckduckgo 検索エンジン、検索エンジン ジャーナル、facebook、google chrome、firefox など。</span>
                                </blockquote>                
                                                                                             
                            </div><!--//item-->
                            
                            <p> <a class="more-link" href="/what-is-an-adjacency-matrix"> <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="/python-numpy-program/">Python Numpy プログラム</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/java-methods">Javaメソッド</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/jake-paul-net-worth-2024-salary">ジェイク・ポール純資産2024年(給与、推薦、住宅)</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/how-install-google-chrome-kali-linux">Kali Linux に Google Chrome をインストールする方法</a> </strong> </span>
                                    
                                </li><!--//item--><li class="item">
                                    <span class="title"> <strong> <a href="/cpp-constructor/">Cpp-コンストラクター</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="/architecture-linux">Linuxアーキテクチャ</a>
</li><li><a href="/loops-java">ループタイプのJava</a>
</li><li><a href="/smtp">SMTPインターネットプロトコル</a>
</li><li><a href="/mysql-show-users-list-all-users">mysqlユーザーリスト</a>
</li><li><a href="/ddl-commands-sql">SQL DDLコマンド</a>
</li><li><a href="/what-is-25-100">25/100</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="//ko.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>
	<script>
!function(){"use strict";let t=document.createElement("button");t.id="toTopBtn",t.innerHTML="↑";let e=`
        #toTopBtn {
            position: fixed;
            bottom: 25px;
            right: 25px;
            z-index: 9999;
            opacity: 0;
            visibility: hidden;
            background-color: #213141;
            color: white;
            border: none;
            border-radius: 8px;
            width: 50px;
            height: 50px;
            font-size: 24px;
            cursor: pointer;
            transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
        }
        #toTopBtn:hover {
            background-color: #555;
        }
    `,i=document.createElement("style");i.type="text/css",i.innerText=e,document.head.appendChild(i),document.body.appendChild(t),window.addEventListener("scroll",()=>{let e=window.scrollY||document.documentElement.scrollTop;e>300?(t.style.opacity="1",t.style.visibility="visible"):(t.style.opacity="0",t.style.visibility="hidden")}),t.addEventListener("click",()=>{window.scrollTo({top:0,behavior:"smooth"})})}();
</script>
</body>
</html>