0

I have the following CSS lines:

.liquid {
display: inline-block;
width: 25px;
height: 25px;
background: #ff8125;
margin-right: 15px; 
}

<h2 class="liquid">Liquid</h2>

It should look like this: https://i.stack.imgur.com/p9Yfr.jpg

But instead looks like this: https://i.stack.imgur.com/K1kCt.jpg

What am i doing wrong here and how to get it exactly like the first pic?

I tried overflow hidden but that only shows Liquid in 25x25 on the block and the rest is not showing.

Any help is much appreciated.

Kind regards,

Majin Buu

MajinB
  • 1

6 Answers6

0

You are styling the font part of the wanted result itself. You should either add an element for the orange square or use a pseudo element. This will get you in the right direction.

.liquid {
  line-height: 1;
}

.liquid:before {
  background: #ff8125;
  content: ''; /* important for pseudo elements */
  display: inline-block;
  height: .9em;
  margin-right: .45em; 
  position: relative;
  top: .1em;
  width: .9em;
}
<h2 class="liquid">Liquid</h2>
Marijan
  • 1,825
  • 1
  • 13
  • 18
  • Thank you very much Mary. Use of a pseudo element is indeed the best practice for this. – MajinB Apr 19 '17 at 13:21
  • Yes, I think so – you’re welcome ;-) Marking this answer as the right one would be appreciated then. – Marijan Apr 19 '17 at 13:24
0

I think you should create another element for the orange square instead of editing the class of the h2 element because the background attribute it will be applied on that element, so I would make something like:

<div class="liquid"></div>
<h2>Liquid</h2>

.liquid {
  float: left;
  width: 25px;
  height: 25px;
  background: #ff8125;
  margin-right: 15px; 
}

To have the square floating to the left of the element.

Manuel
  • 470
  • 4
  • 12
  • Be aware that using empty elements ain’t so good. See here http://stackoverflow.com/questions/14182383/are-empty-divs-bad – Marijan Apr 18 '17 at 09:49
0

Check out CSS position!

.liquid {
display: inline-block;
position: absolute;
width: 25px;
height: 25px;
background: #ff8125;
}

h2 {
  position: relative;
  margin-left: 30px; 
}
<div class="liquid"></div><h2>Liquid</h2>
Felix Haeberle
  • 1,530
  • 12
  • 19
0
Use html like this

<div class="bg_white">
<span class="liquid"> </span><h2>Liquid</h2>
</div>

CSS

.bg_white{background:white; padding:5px; width:auto; float:left;}
.liquid {
    display: inline-block;
    width: 25px;
    height: 25px;
    background: #ff8125;
    margin-right: 15px; 
    float:left;
    font-size:18px;
}
.bg_white h2{float:left; margin:0px;}
chirag solanki
  • 399
  • 2
  • 8
0

Pseudo element is better for this solution:

h2 {
    background: #eee;
    padding: 5px;
    display:inline-block;
}

.liquid::before {
    content:'';
    display: inline-block;
    vertical-align: middle;
    width: 25px;
    height: 25px;
    background: #ff8125;
    margin-right: 15px; 
}
<h2 class="liquid">Liquid</h2>
Germano Plebani
  • 3,461
  • 3
  • 26
  • 38
0

you can use below CSS for this if text is small and always in one line.

.liquid {
    display: inline-block;
    padding-left: 10px;
    border-left: 25px solid #ff8125;
    margin-right: 15px;
    font: 25px/25px Arial;
    font-weight: bold;
}
<h2 class="liquid">Liquid</h2>