How to select elements with some text after them? - css

I use <div> tags to insert icons in my pages:
<div class="icon warning"></div>There is a warning in the page
The problem is that the icons are too close to the text:
Here is the code for the icon:
div.icon{
display:inline-block;
width:16px;
height:16px;
background-color:transparent;
background-image:url(/images/icons.png);
background-repeat:no-repeat;
vertical-align:text-top;
background-position:0 0;
}
div.icon.warning{
background-position:-48px 0;
cursor:help;
}
I want to place a few pixels distance between the icon and the text only if the icon is being followed by some text. In other words if there is no text after the icon, I don't want that space. In other words for the following code, I want to have 5px distance between div#icon1 and the text "There is a warning in the page" but I don't want any distance between div#icon2 and the elements coming after it:
<li><div id="icon1" class="icon warning"></div>There is a warning in the page</li>
<li><div id="icon2" class="icon warning"></div></li>
Please note that the icons will not always appear within <li> elements so your suggested selectors cannot rely on the context that the icons may appear. The only thing certain about the icons is that if they are followed with some text, there must be some space between them and the text.

You can wrap the text around in a span and apply a padding to it:
<div class="icon warning"></div><span class="warning-text">There is a warning in the page</span>
.warning-text {
padding-left: 5px;
}
Update:
As per the comment below, I decided to change from using span instead of div.
It would be possible to use a div, but with the additional display: inline; CSS attribute.

If you are willing to restructure your markup a little (without adding any additional size to it):
http://jsfiddle.net/8kcQv/
The key line is:
.icon:empty{ padding-left: 20px; }
This works in IE 9, Chrome, etc. Other browsers will add extra space between empty elements. Here's an alternate version which degrades differently (less space between icon and text) when :empty is not supported:
http://jsfiddle.net/8kcQv/1/
HTML
<div class="icon">This message has text.</div>
<br>
<br>
<div class="icon"></div><div class="icon"></div><div class="icon"></div><div class="icon"></div><div class="icon"></div>
CSS
.icon{
padding: 4px 4px 4px 32px; /* 32px adds extra space to pad against text */
height: 24px;
line-height: 16px;
font-family: sans-serif;
font-size: 16px;
background:url(http://icons.iconarchive.com/icons/pixelmixer/basic/16/warning-icon.png) no-repeat 4px 4px;
display: inline-block;
}
/* empty matches elements with no children (including text nodes) */
.icon:empty{ padding-left: 20px; }
Alternatively, you might be able to do without :empty altogether if you use a style like:
.icon{
padding: 4px 8px 4px 24px;
...
}
This places equal distance on both sides of the icon.

I suggest using addClass jQuery. Add a certain class which will add a space if there is text after it, else don't put any class. You should set the default item with no space at all.

Related

Underline with padding: Non-links

Should the following line of CSS work for adding padding to regular underlines? Not links, just when I underline one or more words for emphasis. Nothing seems to change.
.underline {
padding-bottom: 2px;
border-bottom: black 2px solid;
}
Cheers!
Underlined text doesn't have a class .underline whose settings you could change. It has a setting: text-decoration: underline , which is not the same as border-bottom. Some browsers allow additional parameters for text-decoration to style it to a limited extent, see https://caniuse.com/#search=text-decoration . However, the results really often look different between different browsers...
.a {
text-decoration: underline;
}
.b {
border-bottom: 1px solid black;
padding-bottom: 3px;
}
<p>This is an example for <span class="a">underlined text</span> within a phrase. Usually a <span> element is used to apply "text-decoration: underline". The vertical distance to the baseline of the text depends on the browser and usually can't be changed.</p>
<p>A "border-bottom" is <span class="b">something completely different</span>. Here the distance to the text can be set manually by using a "padding-bottom".</p>

Styling an inline element with linebreak (after) within a content editable element - caret position

I'm styling an inline element within a contenteditable element to visually represent a linebreak.
First text(Linebreak)
[C]Second Line
I want to be able to place the cursor at [C] position which I'm unable to.
I believe there's a reasonable explanation for the current behavior. Anyone care to explain and maybe provide me a different approach?
EDIT: Apparently it works for IE and Firefox but not Chrome.
.lb{
display:inline;
}
.lb:after{
content: "\21B2\A";
white-space: pre;
word-wrap: break-word;
}
.edit-box{
border: 1px solid black;
padding: 10px;
}
<div id="test" contenteditable="true" class="edit-box">
How to style the span element<span class="lb"></span>so it's possible to place the cursor at the beginning of this line, before "S".
</div>
Try the follow css changes in your lb classes with addition of .lb:before:
.lb{
display:inline;
white-space: nowrap;
}
.lb:before{
content:'';
display:block;
}
.lb:after{
content: "\21B2\A";
}
This will achieve what you want. What we are doing is that we are telling the main lb to not wrap the text in and around it. Next we create lb before as block to get it into new line and then a lb after to add the cursor and the rest of the text flows along with it. Hope this helps.
You could wrap each new line in a <p>.
.edit-box {
border: 1px solid black;
padding: 10px;
}
.edit-box p {
margin: 0
}
p:not(:last-child):after {
content: "\21B2";
}
<div id="test" contenteditable="true" class="edit-box">
<p>How to style the span element</p>
<p>so it's possible to place the cursor at the beginning of this line, before "S".</p>
</div>
Or wrap that 2nd line in a span that displays as a block:
.edit-box {
border: 1px solid black;
padding: 10px;
}
.edit-box span {
display: block;
}
<div id="test" contenteditable="true" class="edit-box">
How to style the span element
<span>so it's possible to place the cursor at the beginning of this line, before "S".</span>
</div>
I've come up with a different approach by wrapping the BR element with a span as such:
.edit-box{
border: 1px solid black;
padding: 10px;
}
.icon{
width:15px;
}
.icon::before{
content: "\21B2";
}
<div id="test" contenteditable="true" class="edit-box">
How to style the span element so it's possible to place the cursor<span class="icon"><br></span> at the beginning of this line, before "at".
</div>
Basically, the linebreak sort of receives a visual dimension and keeps its regular behavior.
For the specific contenteditable element, I capture all keypress events for the return key and paste <span class="icon"><br></span>, preventing the default behavior.
It seems to work when you wrap your "second line" in div: https://jsfiddle.net/n944cfgo/2/

HTML5 tag for horizontal line break

I did already find a post about using the <hr> tag to insert a line break, but when I looked up the tag on the w3 website (http://www.w3schools.com/tags/tag_hr.asp) it says that all attributes of the tag are not supported in HTML5. Obviously I want to make my website HTML5 compatible, so what would be the best way to insert a visible horizontal line?
Thanks
You can still use <hr> as a horizontal line, and you probably should. In HTML5 it defines a thematic break in content, without making any promises about how it is displayed. The attributes that aren't supported in the HTML5 spec are all related to the tag's appearance. The appearance should be set in CSS, not in the HTML itself.
So use the <hr> tag without attributes, then style it in CSS to appear the way you want.
Simply use hr tag in HTML file and add below code in CSS file .
hr {
display: block;
position: relative;
padding: 0;
margin: 8px auto;
height: 0;
width: 100%;
max-height: 0;
font-size: 1px;
line-height: 0;
clear: both;
border: none;
border-top: 1px solid #aaaaaa;
border-bottom: 1px solid #ffffff;
}
it works perfectly .
You can make a div that has the same attributes as the <hr> tag. This way it is fully able to be customized. Here is some sample code:
The HTML:
<h3>This is a header.</h3>
<div class="customHr">.</div>
<p>Here is some sample paragraph text.<br>
This demonstrates what could go below a custom hr.</p>
The CSS:
.customHr {
width: 95%
font-size: 1px;
color: rgba(0, 0, 0, 0);
line-height: 1px;
background-color: grey;
margin-top: -6px;
margin-bottom: 10px;
}
To see how the project turns out, here is a JSFiddle for the above code: http://jsfiddle.net/SplashHero/qmccsc06/1/
Instead of using <hr>, you can one of the border of the enclosing block and display it as a horizontal line.
Here is a sample code:
The HTML:
<div class="title_block">
<h3>This is a header.</h3>
</div>
<p>Here is some sample paragraph text.<br>
This demonstrates that a horizontal line goes between the title and the paragraph.</p>
The CSS:
.title_block {
border-bottom: 1px solid #ddd;
padding-bottom: 5px;
margin-bottom: 5px;
}
I am answering this old question just because it still shows up in google queries and I think one optimal answer is missing. Try this code:
use ::before or ::after
See Align <hr> to the left in an HTML5-compliant way

How can I remove letter-spacing for the last letter of an element in CSS?

Here's the image in question of my HTML page.
The text menu is inside a right aligned div, and has 1.2em letter spacing.
Is there a pseudo-selector for this? I would not like to have to resort to relative positioning.
I would love the text menu to end where the block ends.
I've already marked the best answer, but I was asked for the markup regardless by CodeBlock. Here it is.
<div class="sidebar">
<span class="menuheader">MENU</span>
<ul>
<li>Content</li>
<li>Attachments</li>
<li>Sub-pages</li>
<li>New sub-page</li>
</a>
</ul>
</div>
.sidebar {
color: rgb(150,93,101);
display: inline;
line-height: 1.3em;
position: absolute;
top: 138px;
width: 218px;
}
.menuheader {
letter-spacing: 1.1em;
margin: -1.2em;
text-align: right;
}
You can set your element to have a right margin of -1.2em, which would counteract the letter spacing.
e.g.
.menu-header-selector {
display:block;
letter-spacing:1.2em;
margin-right:-1.2em;
text-align:right;
}
To answer your question regarding pseudo-selector, there isn't a per character pseudo-selector as far as I'm aware. (EDIT: Scratch that, there's the :First-Letter selector, which Jonas G. Drange pointed out).
EDIT: You can find a basic sample here: http://jsfiddle.net/teUxQ/
I would call this a browser bug, actually. The spec says it's the spacing between characters, while your browser (and mine) seem to be changing the spacing after characters. You should submit a bug report.
Obviously a very old question, but CSS involved for your specific example worked at that time.
It involves to reset direction to the opposite, give a formating context to your inline element and set a negative text-indent equal to the letter spacing.
Demo below:
.sidebar {
color: rgb(150, 93, 101);
line-height: 1.3em;
width: 218px;
border:solid;
text-align:right;
}
.menuheader {
letter-spacing: 1.1em;
direction:rtl;
display:inline-block;
text-indent:-1.1em;
background:gold
}
<div class="sidebar">
<span class="menuheader">MENU</span>
<ul>
<li>Content</li>
<li>Attachments</li>
<li>Sub-pages</li>
<li>New sub-page</li>
</ul>
</div>
You can add an :after of your element and set a minus margin left equal as the letter-spacing
.menuheader {
letter-spacing: 1.1em;
}
.menuheader:after {
content:" ";
margin-left: -1.1em;
}
Tested on Chrome, Firefox and Edge
You cannot target the last character, only the first (CSS3, :first-letter). You can add a span around the last letter, but that would mean adding meaningless markup which is "worse" than adding positioning to the element.
CSS is perfect for trickery like this :)
No need for changing display to any other kind (<p> paragraph example) or actually doing anything unnecessary with my code. Text-indent set to negative letter-spacing value resolves that problem for me.
text-indent: -2em; works exactly as I want for letter-spacing: 2em; and was the only thing I had to add to my CSS.
You could try adding display: block to the text and then reduce the width by using 100% minus the letter-spacing.
.menuheader {
text-align: right;
display: block;
letter-spacing: 1.1em;
width: calc(100% - 1.1em);
}
I think i have the best answer
You can use ::after and set content as the last word of your word
div{
display:inline-block;}
#demo1{border: 2px blue dashed;
text-align: center;
letter-spacing: 3vw;
}
#demo2{border: 2px blue dashed;
text-align: center;
letter-spacing: 3vw;}
#demo2::after{
content:'g';
letter-spacing:0;}
<div id="demo1">something</div><span> ///here after last letter their is a gap</span></br> </br>
<div id="demo2">somethin</div> <span>///here the gap is removed with the help of ::after sudeo class</span>

CSS: margin/padding on IE cause floats to be placed incorrectly

I'm having trouble getting IE7 to float my elements correctly (FF and Chrome work as expected). I'd like to have the "Delete" buttons (here a span with class "sbutton") to be floated all the way to the right. On IE the sbutton no the first line floats all the way to right as expected, but the subsequent lines float just to the left of the sbutton above them:
Something some other text ----------------------------------[Delete]
Something Else some other text ---------------------[Delete]--------
This is related to paddings and margins on the sbutton:
.sbutton {
background-color: #2E3239; color: white; border: 1px solid gray;
font-family: "Trebuchet MS"; font-size: 90%; padding: 1px 3px; margin: 2px;
-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;
}
If I remove the padding and margin they all float as expected.
Here's the HMTL:
<li>
<span class="left"><b>Something</b> (#1102) some other text</span>
<span class="right">Delete</span>
</li>
<li>
<span class="left"><b>Something Else</b> (#1103) some other text</span>
<span class="right">Delete</span>
</li>
I get similar behavior on FF and Chrome if I remove the outer span (the one with class "right").
Why is this happening, and what's the right way to do this?
You need to clear your floats. Here's one way of doing it:
.clear {
clear:both;
font-size:0;
}
<li>
<span class="left"><b>Something</b> (#1102) some other text</span>
<span class="right">Delete</span>
<span class='clear'></span>
</li>
#parand - it's not necessary because IE has a different box model than FF or Webkit (which is the engine that powers chrome & safari, among others). By forcing this clear, it's a workaround from the differences.

Resources