How to not display css padding when span element is empty - html

I've been trying to solve the following problem.
If you run this code you will notice blue and red elements.
How can I hide the 'red element' when there is no text to display (span is empty). And I would like to do the same thing with 'blue element' when there is no text inside it shouldn't be visible.
The reason why is displayed is padding, but I would like to have padding because it looks nice.
I am sure you guys are best of the best and find solution.
Regards!
.myClassDer {
font-size: 34px;
color:white;
background: blue;
color: white;
border-radius: 25px;
padding: 7px;
width: auto;
height: auto;
}
.myClassDie {
font-size: 34px;
color:black;
background: red;
color: white;
border-radius: 25px;
padding: 7px;
width: auto;
height: auto;
}
<span class="myClassDer">here</span>
<span class="myClassDie"></span>

If you don't require support for IE8, you can use pseudo-state :empty (here for more examples ) to reset padding for all instances of .myClassDie without content, using the following code.
.myClassDie:empty
{
padding:0;
}
Updating your working example, it becomes:
.myClassDer
{
font-size: 34px;
color:white;
background: blue;
color: white;
border-radius: 25px;
padding: 7px;
width: auto;
height: auto;
}
.myClassDie
{
font-size: 34px;
color:black;
background: red;
color: white;
border-radius: 25px;
padding: 7px;
width: auto;
height: auto;
}
.myClassDie:empty
{
padding:0;
}
<span class="myClassDer">here</span>
<span class="myClassDie"></span>
<span class="myClassDie">ClassDie but with content</span>
In which I inserted two <span class="myClassDie"> to show you the behaviour with and without content.
Due to effective invisibility of "empty" case, if you want a more compact solution, you can collapse the two separate rules into only one, simply setting:
.myClassDie:not(:empty)
{
font-size: 34px;
color:black;
background: red;
color: white;
border-radius: 25px;
padding: 7px;
width: auto;
height: auto;
}
In this case, only if .myClassDie is not empty, you'll apply all properties.
This is equivalent for this specific case, but if you want to see this DIV also if empty, limiting only to reset padding (for example because it has fixed size or borders), you must use first solution, not the more compact one.
Little precisation about :empty pseudo-class
Previous examples run correctly only if empty elements are effectively empty, this means that this code <span class="myClassDie"></span> is correctly targeted, but this one (that contains a whitespace) <span class="myClassDie"> </span> isn't.
In general, this could be an issue because often code is dynamically generated or otherwise contains white spaces due to code indentation.
In the past, Mozilla introduced its proprietary pseudo-class :-moz-only-whitespace, but no other browser currently supports this yet.
W3 also tried to solve this kind of problems, initially with analogue :blank pseudo-class (again with no browser support) in "Selectors Level 3", but this did not have expected success.
So, since beginning of 2018, W3 modified its definition to represent empty user input, rather than empty elements and contemporarily modified :empty definition to consider also white-spaces, but currently this last feature is not implemented too in different browsers.

Empty pseudo class only checks for empty text
.myClassDie:empty{
padding:0;
}
But for whitespaces use blank pseudo class
.myClassDie:blank{
padding:0;
}

There is a css pseudoclass empty which you could use here:
.myClassDie:empty {
display: none;
}
Your updated JSFiddle

You can do the trick with the CSS3 pesudo-class :empty
span:empty{
padding:0;
}
Note: using above selector you will not have to worry about which span
has value and which one has not. it will reset padding for those span
which are blank (empty).

I guess you could use above piece of code to hide the empty span's padding.
span:empty {
padding: 0;
}

you can you :empty also read the below like.
https://developer.mozilla.org/en-US/docs/Web/CSS/pseudo-classes
.myClassDie:empty {padding:0;}

Related

CSS-only way to prevent break after inline block before period character without tag around period

I'm trying to prevent period and other punctuation characters after inline blocks (with a specific class) from being wrapped to the next line. I want to avoid wrapping the inline block and subsequent punctuation it in a no-wrap-styled tag, since that requires knowing in advance where all of these situations might come up and I do not know that. I could use JavaScript to inject that structure everywhere it's needed, but I'd like to avoid it if possible, since my intuition is that that could get messy.
It is a requirement to preserve the background and padding, so display: contents will not work:
display: contents makes that the div doesn’t generate any box, so its background, border and padding are not rendered. However the inherited properties like color and font have effect on the child (span element) as expected.
My two-part question is this: can this be done without a tag around the inline-block and punctuation, and, if so, how?
Screenshot (from Firefox 62 on macOS 10.13.6) in case rendering differences mask the problem:
div {
width: 309px;
background: #EEE;
}
span.help-block {
width: 111px;
background: #DDD;
}
span.inline-code {
display: inline-block;
background: #CCC;
color: #333;
padding-left: 4px;
padding-right: 4px;
padding-top: 1px;
margin-top: -1px;
padding-bottom: 1px;
margin-bottom: -1px;
font-family: Courier, Monaco, monospace;
font-size: .9em;
}
<div>
<span class="help-block">To escape commas, put quotes around the value, e.g., <span class="inline-code">a,b,c,"a,b,c"</span>. To escape those quotes, double them, e.g., <span class="inline-code">a,b,"a,""b"",c"</span>.</span>
</div>
https://jsfiddle.net/don01001100/ravjykdb/9/

Why inline-block have different rendering than inline in firefox?

Friends,
My question is why Firefox adds additional pixel (padding) below the box if I use display: inline-block?
Let's see what we have here: http://jsfiddle.net/xbU5s/9/
HTML - Two perfectly same elements.
<div class="wrap">
<section class="ib">Hello world</section>
<section class="il">Hello world</section>
</div>
CSS - Everything is the same, but our first section is inline-block and second one is inline.
.wrap { font-size: 0; }
.ib { display: inline-block; }
.il { display: inline; margin-left: 10px; }
section {
background: #000; border-radius: 3px; color: #fff; font-size: 11px; font-family: Sans-serif;
padding: 3px 5px;
}
And here's our 1px padding:
display: inline-block; vs display: inline;
Is is just rendering glitch (cause it's only happens in firefox) or I'm misinformed about inline-block's behavior?
Perhaps the answer is already explained here in old post
I will like to clear the difference..
If the element is with style display:inline the style restricts the object in line-height.
But, when block comes with inline the behavior of the same changes.
It is inline but with block it will expand to the possible height or width available.
For a change. select the text in both the box, you will see the second box is selecting out of the box. that is overflow of line-height which is restricted by inline but with inline-block it will grow with overflow caused by padding + line-height
I think this will clear most of the doubts, please refer the old post for more details.

Add background to first letter of a row of text. How?

Please look at the attached image, below:
This, I made up easily in Photoshop and is for the corporate identity on papers and such. However: I now need to create that for an email signature. Though.. I don't have a clue how to achieve the effect of having a square/rectangular background to the - well let's say - first letter of the sentence.
Since It should not cut off the text to the next row, I can't use a <p> tag.
I hope someone could help me! However, it's for an E-mail signature and all CSS must be inline. edit: And besides that: You can't use DIV's either.. Thank you very much!
You can use :first-letter
div:first-letter {
padding: 0 3px;
background: #f00;
}
Demo
Or a better one
div:first-letter {
padding: 2px 5px;
background: #174D95;
font-family: Arial;
color: #fff;
font-weight: bold;
margin-right: 2px;
}
Note: You can replace div with a p element too, but :first-letter will not work on inline elements.
Demo 2 (Using p tag)
As you wanted to do this with a span tag, you need to define it as inline-block; to make the :first-letter work.
Doing this with a span tag - Demo
span:first-letter {
padding: 2px 5px;
background: #174D95;
font-family: Arial;
color: #fff;
font-weight: bold;
margin-right: 2px;
}
span {
display:block
}

Centering text vertically in button

It should be simple to center text in a button. Unfortunately, across different browsers and platforms, I get different results.
I've tried for hours to fix it, but nothing works everywhere.
Chrome, mac OS X:
(source: d.pr)
Chrome, Windows 8
(source: d.pr)
IE 10, Windows 8
(source: d.pr)
So, yeah. The big block doesn't appear in IE if I set a defined height, but I don't get why it breaks down in the first place.
Here's the code:
.btn-call-to-action {
background: #8e8287;
margin-bottom: 15px;
color: #f5f3e2;
padding: 3px 18px 3px 10px;
margin-top: 6px;
display: inline-block;
position: relative;
border-bottom: none;
border-radius: 2px;
white-space: nowrap;
.btn-call-to-action a:after {
content: url('../img/general-white-arrow.svg?1369574895');
position: absolute;
width: 35px;
right: 15px;
top: 0px; }
and the HTML (pretty simple) :
Want more ?
and the site: http://aurelieremia.be/tfa/
// edit: I think I get it. Still not centered in windows but by resetting the line height, the button looks a bit more normal. IE problem resolved, I'll try using a background-image instead (thanks Ana)
I'm not sure if this will help but cross browser centering in css is a big pain so I use Twitter Bootstrap and overwrite some of the classes.
If this sounds like something you'd consider you can check out the solution here
Leave :after in static .
vertical-align to middle or explicite value (depends of where really stand arrow in svg/img).
white-space:nowrap to parent box to secure, but not necessary:
http://codepen.io/gcyrillus/pen/vzrGj
How about something like this:
HTML:
<a href="about.html">
<div class="btn-call-to-action">
<span>Want more? <img src="http://bkids.sisuweb.co/wp-content/uploads/2013/04/postArrowR.png" />
</span>
</div>
</a>
CSS:
.btn-call-to-action{
width:160px;
height:80px;
background: #8e8287;
padding: 3px 18px 3px 10px;
margin:8px;
color: #f5f3e2;
border-radius: 2px;
display:table;
text-align:center;
}
.btn-call-to-action span{
display:table-cell;
vertical-align:middle;
}
Fiddle here: http://jsfiddle.net/MQHVE/3/
The important part here is to have the wrapper (the a tag) display:table and the content (span) display:table-cell. Then you can apply vertical-align:middle to the span.

Stop box from breaking to next line due to extra margin?

Not sure if this is even possible, but I think it's worth asking.
What I am trying to do is have boxes created via my PHP script with ease, where I don't have to check which column, row, etc. each box (.boxes) belongs to. I just want to be able to do a foreach loop of the boxes, and echo them.
Here's what I am talking about: http://jsfiddle.net/LUFFP/4/
In that example, you can see that I am trying to get the wrapper (#main) to ignore the margin-right, and have two boxes per row, with no extra space on the right, without having to assign different classes to every other DIV.
Any help is appreciated.
You can use nth-of-type(2n) to target every other div. Then set the right margin to zero.
http://jsfiddle.net/LUFFP/5/
.boxes_3:nth-of-type(2n) { margin-right: 0px; }
Depending on your browser requirements, there's a CSS3 selector to do that:
http://jsfiddle.net/ttpK6/1/
Compatability list here: http://kimblim.dk/css-tests/selectors/
#main_2
{
background: #FF0000;
width: 404px;
border: 1px solid #000;
overflow: hidden;
}
.boxes_2
{
background: #F3F3F3;
width: 195px;
display: inline-block;
margin-right: 10px;
}
Notice that I changed the width and set overflow to hidden. No special CSS3 requirements.
try it
#main_2 {background: #FF0000; width: 411px; border: 1px solid #000;}
.boxes_2 {background: #F3F3F3; width: 205px; float:left;}