简体   繁体   中英

Centered content and lines on each side filling the remaining space

<div class="text-mid">
  <span>TEXT</span>
</div>

CSS

.text-mid {
  display:block;
  margin: 10px;
  padding; 10px;
}

I want the arrangement as like in image → 在此处输入图片说明

Edit

I would prefer a solution w/o Flexbox

Here is the adjusted CSS, without using flexbox.

 .text-mid { width: 100%; display: block; text-align: center; position: relative; } .middle { padding: 10px; background-color: white; } .start { left: 0; } .finish { right: 0; } .start, .finish { transform: translateY(50%); position: absolute; top: 50%; width: 50%; background-color: black; height: 1px; z-index: -1; } 
 <div class="text-mid"> <span class="start"></span> <span class="middle">TEXT</span> <span class="finish"></span> </div> 

If you don't want to change your markup:

 .text-mid { text-align: center; background-color: white; position: relative; z-index: -2; } .text-mid::before { position: absolute; top: 50%; left: 0; width: 100%; height: 1px; background-color: grey; content: ' '; z-index: -1; } .text-mid span { background-color: white; padding: 0 10px; } 
 <div class="text-mid"> <span>TEXT</span> </div> 

I have added two new DIV 's for left and right lines. I also changed class .text-mid 's display to flex .

 .text-mid { display: flex; } span { display: block; margin: 0 5px; } #left { background-color: #AAAAAA; flex: 1; height: 1px; margin-top: 8px; } #right { background-color: #AAAAAA; flex: 1; height: 1px; margin-top: 8px; } 
 <div class="text-mid"> <div id="left"></div> <span>TEXT AREA</span> <div id="right"></div> </div> 

This is very easily done using the pseudo elements

By using both, this will also be completely transparent towards its background.

The main trick is to set overflow: hidden on the wrapper and a width on each pseudo enough to cover all possible width's

 .text-mid { margin: 10px; padding: 10px; text-align: center; overflow: hidden; } .text-mid span { position: relative; padding: 0 5px; } .text-mid span::before, .text-mid span::after { content: ''; position: absolute; width: 50vw; top: 50%; border-top: 1px solid black; } .text-mid span::after { left: 100%; } .text-mid span::before { right: 100%; } 
 <div class="text-mid"> <span>TEXT</span> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM