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
238 views
in Technique[技术] by (71.8m points)

html - How can i center site header in nav with random width of suff around?

What i want is almost same as this programs header,

Desired program header

The Title is completely centered despite of the random width of the aside stuff. Yeah If course i am not making a software but still i want almost this kind of random stuff around AND Still The TITLE MUST BE NEATLY CENTERED

Please, Don't give me advice like 'Just use grid/flex'
Instead, Tell me how am i gonna use the grid/flex to center it ?? (I am trying and ,,, still Cant solve it.)

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Flexbox won't really work because the title must be centered within the entire header—not in the remaining space between the left and right items. Still, you can use flexbox to pin the left/right items to their respective corners. For the title, use absolute positioning within a relative container (the header).

header {
  display: flex;
  justify-content: space-between;
  position: relative;
  background-color: #333;
  color: #fdfdfd;
  padding: 0.5rem;
}

.title {
  position: absolute;
  left: 50%;
  transform: transalteX(-50%); 
}
<header>
  <div class="left">
    <span>item</span>
    <span>longer item here</span>    
  </div>
  <div class="title">title</div>
  <div class="right">
    <span>item</span>
    <span>item</span>    
  </div>
</header>

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

...