P粉2918868422023-08-22 10:44:15
This is a CSS3 way to horizontally align a div within another div.
#container { display: flex; /* 建立flex容器 */ flex-direction: row; /* 默认值;可以省略 */ flex-wrap: nowrap; /* 默认值;可以省略 */ justify-content: space-between; /* 从默认值(flex-start)切换 */ background-color: lightyellow; } #container > div { width: 100px; height: 100px; border: 2px dashed red; }
<div id="container"> <div></div> <div></div> <div></div> </div>
justify-content attribute has five values:
In all cases, the three divs are on the same line. For a description of each value, see: https://stackoverflow.com/a/33856609/3597276
Benefits of flexbox:
Learn more about flexbox:
Browser Support: Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require the vendor prefix . To quickly add a prefix, use Autoprefixer. See this answer for more details.
P粉0290579282023-08-22 00:47:05
Using this CSS, place your div as follows (float first):
<div id="container"> <div id="left"></div> <div id="right"></div> <div id="center"></div> </div>
Note: You can also float the right first, then the left, and then center. It is important that the floating content comes before the "main" center section.
Note: Usually you will want to add this code at the end of #container
: <div style="clear:both;">< /div>
, it will make the height of #container
extend vertically to accommodate the floating content on both sides, instead of just being positioned from the height of #center
, like this This prevents content on both sides from overflowing the bottom.