This question already has answers here:
Create leading dots in CSS
(17 answers)
Closed 9 years ago.
I'm interested in reproducing a table which uses dots to pad cell contents. Note that this is not text-overflow with an ellipses because the dots are not truncating content, they are just filling things up. And I understand :before with the "content" property is restricted to fixed content rather than a dynamic number of repeating characters, so I don't think that can be made to work.
Here's some HTML to produce effectively what the padding looks like:
<table>
<tr><td>ID</td><td>Col1</td><td>Col2</td></tr>
<tr><td>1...</td><td>cats............</td><td>rain</td></tr>
<tr><td>2...</td><td>dogs...........</td><td>snow</td></tr>
<tr><td>15..</td><td>elephants...</td><td>snow</td></tr>
</table>
How might I do this padding using CSS without needing to utilize "." everywhere?
You can use ::before or ::after pseudo-elements to display generated content with CSS.
In this case, I would change the elements in the top row to be table headers (<th>) and apply the rule to all tds that aren't the :last-child of their row:
td:not(:last-child):after {
content: '........................................................................................';
}
Here's a demo.
EDIT The above just stretched the table to fit however many characters were in the generated content, which (obviously) wasn't the desired effect. Using exactly the same HTML, you can still get this to work by setting overflow: hidden on the <td>s and position: absolute on the ::after elements:
td {
overflow: hidden;
}
td:not(:last-child):after {
content: '........................................................................................';
position: absolute;
}
Revised demo.
However, this still won't work in Firefox because it (alone) respects the fact that the overflow property shouldn't work on non-block elements. So if you want to target Firefox (and who doesn't?) you'll have to insert <div>s inside each cell, and apply the overflow: hidden and the ::after element rules to them.
None of the above will create infinite generated content though: you'll just need to use a very long string of dots in your CSS. For infinite dots you'd have to use a background-image as suggested in the response to the duplicate question.
Related
This question already has answers here:
Select all block level elements with css
(4 answers)
Closed 2 years ago.
By default, most browsers will add a top and bottom margin of 1em to many block elements, like ol, ul, blockquote, h1, h2, etc.
Is there any short and clever selector that can change theses vertical margins (let say to 1.45em) all at once, without having to list every element in the CSS file?
Note: This question have been marked as a [duplicate.] But I am not trying to select all block elements by their properties (which I know is imposible in CSS), nor do I want to select all block elements by listing them one by one. I am concern in creating an equal vertical (top and bottom) margin in all elements that by default create a new line, by using the simplest selector posible.
The best way I have found so far is to use the Universal Selector:
:root {
--custom-vertical-margin: 1.5rem;
}
* {
margin-top: var(--custom-vertical-margin);
margin-bottom: var(--custom-vertical-margin);
}
Even though the Universal Selector will also target inline elements, these are not and will not be affected by vertical margins.
This accomplishes what the question is about: to set equal vertical margins on any block element at once, without having to list every element [with display:block property] in the CSS file, nor by trying to target elements by their properties (which is imposible in CSS).
This question already has answers here:
Why do the :before and :after pseudo-elements require a 'content' property?
(5 answers)
Closed 3 years ago.
I see patterns like this from time to time:
::before {
content: '';
}
::after {
content: '';
}
li::first-child {
content: '';
}
I don't understand what it really does. I understand that the 'content' property replaces an element, but what generally would we replace? Is there a "problem" that exists that we use this as a solution?
To be clear, I understand what psuedo-selectors are. My question is specifically about why we use content: ''. If ::before is creating content BEFORE an element is painted to the DOM, then why are we creating an empty string of content? What value does that add?
As far as I understand nature of your question there's 2 main things we have to point out:
how to think about ::before and ::after pseudo-elements
These pseudo-elements are not getting put before or after element they refer to. Nor in time manner (so it's not getting rendered before or later) nor in spacial manner (not in-front/behind or before/after). ::before and ::after pseudo-elements are children of element they refer to. ::before is a first child, inserted before all other, and ::after is the last child, inserted after all other children. It's a very important concept about pseudo-elements we need to remember.
what is content?
I'll lean on W3C documentation with that.
User Agents are expected to support this property on all media, including non-visual ones.
The content property dictates what is rendered inside an element or pseudo-element.
For elements, it has only one purpose: specifying that the element renders as normal, or replacing the element with an image (and possibly some associated "alt text").
For pseudo-elements and margin boxes, it is more powerful. It controls whether the element renders at all, can replace the element with an image, or replace it with arbitrary inline content (text and images).
Your way of thinking, that it's because of some problem we had to solve, may be close to the real answer. That's probably connected to what user agents/browsers needed to render any element and that's why it's obligatory to add content: '' to pseudo-elements to render it at all. With other elements with no visible content like <div></div> it's done automatically so we could use it to our own purpose, even with no visible content but with styles applied to it.
These are called pseudo-elements , you would usually use them when creating some content: in front of or after the element. How it works is they create a tag in your html structure that you can then style in your style-sheet with ::before and ::after selectors. Also content property doesn't replace anything it's a value that your pseudo-element holds. You can read more about them here: https://css-tricks.com/almanac/selectors/a/after-and-before/
This is in addition to my recent question:
Is it possible to use pseudo-elements to make containing elements wrap around an absolutely-positioned element (like a clearfix)?
JSFiddle: http://jsfiddle.net/derekmx271/T7A7M/
I'm trying to use pseudo-elements to "clearfix" absolutely positioned images. I have gotten as far as getting the same image to display behind the slides, but cannot seem to apply widths to the inserted image. Am I crazy, or can you NOT apply widths to pseudo elements whose content is content: url(img/image.jpg)? I tried different variations of display:block, etc. to no avail...
#slider ul:after {
content: url(http://www.cs7tutorials.com/img/slide1.jpg);
display: block;
position:relative;
width:70px;
}
I need to set the width of the pseudo-element image to 100% and the max-width to 800px, so that it has the same dimensions as my slides.
You're not crazy: it is indeed not possible to change the dimensions of an image that is inserted using content, whether it's inserted with url(), image(), image-set(), element(), or a CSS gradient. The image is always rendered as is. This is known as replaced content, or a replaced element (except we're not talking about elements here).
However, since replaced elements can be resized using width and height as described in section 10 of the CSS2.1 spec, this raises the question of why the properties don't seem to apply here. The answer to this, it would seem, is that the properties do apply, but to the pseudo-element box instead — you can see this by giving your pseudo-element a background color. Rather than replacing the pseudo-element box itself, the image is rendered as a child of the pseudo-element box, and therefore cannot be styled at all (as it would require another pseudo-element which doesn't exist).
And that lends itself to another question: why doesn't it replace the pseudo-element box altogether? Unfortunately, CSS2.1 does not specify this behavior at all, so the implementation that was agreed on is to render the content as a child of the pseudo-element box instead:
CSS2.1 doesn't really clearly define the processing model of 'content' on ::before and ::after, but the informative examples in CSS 2.1, the fact that 'content' specifies a list of things, and the desire for consistency has led to UA behavior being the following: the 'content' property specifies a list of things that become children of the ::before or ::after box.
-Boris
Hopefully this will be further addressed in CSS Generated Content level 3, on which rewriting work has just begun.
In the meantime, if you want to be able to resize the :after pseudo-element and the image that's generated, you will need to apply it as a background image instead and — assuming browser support isn't an issue — use background-size along with width and height to scale it (based on the understanding that those properties apply to the pseudo-element box instead).
You can set either width or height, but not both, by using display: flex and setting flex-direction to the opposite axis of the dimension you wish to set.
For example:
.setWidth::before {
content: url("myImg.png")
display: flex;
flex-direction: column;
width: 200px;
}
.setHeight::before {
content: url("myImg.png")
display: flex;
flex-direction: row;
height: 200px;
}
Demonstration here.
Have a look at the demo using background-image: http://jsfiddle.net/T7A7M/12/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove the space between inline-block elements?
If I have several child elements with display:inline-block, I can't have whitespace between the elements because that messes up the total width. I either have to put no whitespace in the source, or "cheat" by putting an empty <?php [whitespace] ?> between the elements, or use JavaScript to remove the empty text nodes.
Is there any way to make the whitespace not be rendered in CSS?
You can set the elements as block-level elements by using display: block; or float: left;. If you must use inline-block, then you'll have to adjust your HTML either by removing the spaces in the HTML itself or stripping it out with Javascript.
As #jValdron pointed out, setting font size to 0 for parent elements, and then re-setting the font size on the elements which need it, also works. There are potential issues with this, however (for instance, what if you have text in the parent element which isn't already wrapped in another element?). Regardless, the font-size solution does in fact work and it's one I've used myself, before.
CSS Tricks has a good article on dealing with the inline-block whitespace issue:
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
Apparently, setting the font-size to 0 on a parent element, and then restoring it on the elements themselves, should fix the problem.
Solution from here: How to remove the space between inline-block elements?
we are trying to limit the length of an html element using the width and overflow css rules. as an example:
div.hidetext { width: 100px; overflow: hidden; }
what we would like to do is add three dots '...' at the end of the string IF the text has been hidden. If the text is short enough to do then the '...' should not show up. my first thoughts is to use the :after pseudo-class but not sure how to make that conditional based on whether the text was hidden or not
i'm pretty sure this can be done with javascript/jquery by comparing the width of the text with the width of the html element containing it but i would prefer a css solution.
any thoughts on how this could be achieved?
text-overflow: ellipsis in browsers supporting that part of the draft specification.