How do you easily horizontally center a <div> using CSS?

I'm trying to horizontally center a <div> block element on a page and have it set to a minimum width. What is the simplest way to do this? I want the <div> element to be inline with rest of my page. I'll try to draw an example:

page text page text page text page text
page text page text page text page text
               -------
               | div |
               -------
page text page text page text page text
page text page text page text page text

非固定宽度 div的情况下(即,您不知道div将占用多少空间)。

#wrapper {
  background-color: green; /* for visualization purposes */
  text-align: center;
}
#yourdiv {
  background-color: red; /* for visualization purposes */
  display: inline-block;
}
<div id="wrapper">    
    <div id="yourdiv">Your text</div>
</div>

在大多数浏览器中,这将起作用:

div.centre {
  width: 200px;
  display: block;
  background-color: #eee;
  margin-left: auto;
  margin-right: auto;
}
<div class="centre">Some Text</div>

margin: 0 auto;

正如ck所说,所有浏览器都不支持min-width

链接地址: http://www.djcxy.com/p/29990.html

上一篇: 串联之间的空间

下一篇: 你如何轻松地水平居中使用CSS的<div>?