Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

css - A problem with aligning three elements horizontally, where the middle element grows to take up available space

My goal is to horizontally align three elements, where the middle element is an <hr> that takes up all available space(width: 100%) pushing the outer two elements to the sides.

The result would be like this: AAA-------BBB, where the <hr> element is represented with the hyphens.

What I've tried

I've experimented with display: inline-block but with no luck.

div {
  display: inline-block;
  background: #777;
}

hr {
  width: 100%;
}
<div>
  <h1>AAA</h1>
  <hr />
  <article>
    <p>BBB</p>
  </article>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best way to go about this is to use CSS Flexbox. The guys at CSS Tricks have a really good guide on the technology.

A Complete Guide to Flexbox by Chris Coyier

flex-grow: This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion.

div {
  display: flex;
  background: #777;
}

hr {
  flex-grow: 1;
}
<div>
  <h1>AAA</h1>
  <hr />
  <article>
    <p>BBB</p>
  </article>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...