Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS : set the CSS line [duplicate]

Tags:

html

css

.banner-bottom h2 {
  font-size: 2em;
  color: #372568;
  position: relative;
  padding-bottom: 1em;
  text-align: center;
}

.banner-bottom h2:before {
  content: '';
  position: absolute;
  top: 25%;
  left: 25%;
  background: #372568;
  height: 2px;
  width: 8%;
}

.banner-bottom h2:after {
  content: '';
  position: absolute;
  top: 25%;
  right: 25%;
  background: #372568;
  height: 2px;
  width: 8%;
}
<div class="banner-bottom">
  <div class="container">
    <h2>Special Promo</h2>
    <h2>Promo</h2>
    <div>
      <div>

Result :

enter image description here

How to css its line according to the length of writing example as below:

enter image description here

like image 525
illogic Avatar asked Dec 21 '17 06:12

illogic


2 Answers

Try this:

.banner-bottom h2 {
  font-size: 2em;
    color: #372568;
    padding-bottom: 1em;
    text-align: center;
}

.line:before {
  content: '';
    display: inline-block;
    background: #372568;
    height: 2px;
    width: 8%;
    margin: 10px;
}

.line:after {
  content: '';
    display: inline-block;
    background: #372568;
    height: 2px;
    width: 8%;
    margin: 10px;
}
.promo {
  display: inline-block;
}
<div class="banner-bottom">
  <div class="container">
    <h2><span class="line"><span class="promo">Special Promo</span></span></h2>

    <h2><span class="line"><span class="promo">Promo</span></span></h2>
  <div>
<div>
like image 112
VinoCoder Avatar answered Nov 17 '22 19:11

VinoCoder


Setting display property value to inline-block is the good to go with as @Vinothin has answered for you. Here's another trick that you can use.

Using transform property will help you while using absolute position.

.banner-bottom h2 {
  font-size: 2em;
  color: #372568;
  position: relative;
  padding-bottom: 1em;
  text-align: center;
}

.banner-bottom h2:before {
  content: '';
  position: absolute;
  top: 25%;
  transform: translateX(-100%);
  /* no left value is required */
  background: #372568;
  height: 2px;
  width: 8%;
}

.banner-bottom h2:after {
  content: '';
  position: absolute;
  top: 25%;
  /* no right value is required */
  background: #372568;
  height: 2px;
  width: 8%;
}
<div class="banner-bottom">
  <div class="container">
    <h2>Special Promo</h2>
    <h2>Promo</h2>
  </div>
</div>

If you need spacing before and after the word then play with the translate value by increasing or decreasing it.


Using transform property will help you a lot as well in future. Suppose you have a centered list and you need to position the icon left center to the list, then transform trick only the way to go with. I have informed this trick as I have also faced similar problem before.

Hope this helps!

like image 5
Bhojendra Rauniyar Avatar answered Nov 17 '22 20:11

Bhojendra Rauniyar