Achieve horizontal center alignment of elements
P粉268284930
P粉268284930 2023-08-21 10:25:54
0
2
486
<p>How to center one div horizontally inside another div using CSS? </p> <pre class="brush:html;toolbar:false;"><div id="outer"> <div id="inner">Foo foo</div> </div> </pre> <p><br /></p>
P粉268284930
P粉268284930

reply all(2)
P粉002023326

If you don't want to set a fixed width on the inner div, you can try the following:

#outer {
  width: 100%;
  text-align: center;
}

#inner {
  display: inline-block;
}
<div id="outer">  
    <div id="inner">Foo foo</div>
</div>

This will make the inner div an inline element that can be centered via text-align.

P粉617597173

It's very easy to center a div horizontally and vertically using flexbox.

#inner {  
  border: 0.05em solid black;
}

#outer {
  border: 0.05em solid red;
  width:100%;
  display: flex;
  justify-content: center;
}
<div id="outer">
  <div id="inner">Foo foo</div>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!