Setting pseudo-element dimensions in CSS - html

I am currently attempting to set the :before pseudo element width, to match the dimensions of the element itself (in this case, an anchor tag), with the aim of creating an overlay which covers the entire element. To demonstrate, for this example, I will consider Google's web page for a demo:
a.gb_g::before {
background-color: rgba(255, 0, 0, 0.2);
position: absolute;
content: 'hello';
display: block;
position: absolute;
}
<div class="gb_h gb_i">
<a class="gb_g" data-pid="23" href="https://mail.google.com/mail/?tab=wm&ogbl">Gmail</a>
</div>
However, this produces the following result:
I have also attempted to set width: 100%, yet this results in container overflows. Is it possible to set the pseudo element width, to perfectly match that of the element itself (in this case, the 'Gmail'/'Images' link)? The desired result is for the overlay to fully cover each of the anchor tags, without making any CSS changes to the element itself.
Basically, I'm faced with a scenario where I have a fixed web page layout (with existing styling I have no control over), for which I would like to highlight parts of the web page (including some of the links). So, ideally any proposed solutions would cause minimal impact on the existing page layout, which is why I attempted to opt for an entirely pseudo-element based solution.

Does this work for you? Adding position: relative to the element, and width: 100% to the pseudo element solved it for me.
a.gb_g::before {
background-color: rgba(255, 0, 0, 0.2);
position: absolute;
content: 'hello';
display: block;
position: absolute;
width: 100%;
}
a.gb_g{
position: relative;
}
<div class="gb_h gb_i">
<a class="gb_g" data-pid="23" href="https://mail.google.com/mail/?tab=wm&ogbl">Gmail</a>
</div>

Not sure if this is exactly what you are looking for, but you can add "position: relative" for the actual element, then "width: 100%" for ::before. You can use "overflow: hidden" and "white-space: nowrap" to fully contain the pseudo element.
a.gb_g {
position: relative;
}
a.gb_g::before {
background-color: rgba(255, 0, 0, 0.2);
position: absolute;
content: 'overlay text long';
color: white;
display: block;
width: 100%;
overflow: hidden;
white-space: nowrap
}
<div class="gb_h gb_i">
<a class="gb_g" data-pid="23">Original Text</a> Next
</div>

Related

How to prevent css' ::after selector from creating a new column in a table

I have an HTML table and I want to add an "::after" pseudo-element in a row (tr) so I can have a border that always stays on top.
However, adding the "::after" selector creates a new column in the table, even though the position is set to absolute.
css for the tr:
tbody tr {
display: table-row;
position: relative;
}
tbody tr.selected-row::after {
content: '';
width: 100%;
height: 100%;
position: absolute;
left: 0px;
top: 0px;
border-width: 2px;
border-color: rgb(59, 130, 246);
}
[The table rendered][1]
[1]: https://i.stack.imgur.com/3EaSh.png
Edit:
I fixed it using the selector
selected-row > td:first-child::after.
Basically, it makes sure the pseudo-element is in the first cell of the row, so as long as the cell does not have position: relative;, it should work.
Try adding display: block to the ::after psuedo element
Ideally, it should work but it will add some extra space because of content:"".
Check this:
https://codesandbox.io/s/romantic-glade-6s8oi?file=/index.html
using the selector
selected-row > td:first-child::after did the trick.
Basically, it makes sure the pseudo-element is in the first cell of the row, so as long as the cell does not have position: relative;, it should work.

Pseudo element not showing in react

.input-select{
position: relative;
height: 100%;
}
.input-select::before{
content: '';
position: absolute;
display: inline-block;
height: 50px;
width: 50px;
background-color: red;
z-index: 100;
}
<div className="input-group">
<label htmlFor="type">GST Type: </label>
<select name="type" id="type" className="input-select">
<option>Registered</option>
<option>Unregistered</option>
</select>
</div>
I have tried setting body and html height to 100% as well as setting .input-select height to 100%.
I have have tried setting display to block or inline block and setting z-index to a high value.
However so far I have not been able to make it work.
On select, we can't have pseudo selectors. You have to make one wrapper div and then on that div, you can give after, before.
<div class="input-select">
<select>
<option>United Kingdom</option>
<option>Canada</option>
<option>United States</option>
</select>
</div>
.input-select::before{
content: '';
position: absolute;
display: inline-block;
height: 50px;
width: 50px;
background-color: red;
z-index: 100;
}
You can not put a pseudo element in an input element,however you can put a hover pseudo element. Elements starting and closing in a single place like is not a container element.You can use hover here but not ::before and ::after
.input-select{
position: relative;
height: 100%;
}
.input-select:hover{
content: '';
position: absolute;
display: inline-block;
height: 50px;
width: 50px;
background-color: red;
z-index: 100;
}
Pseudo-elements are only allowed to be used on container elements. Elements like inputs, select, images, and any other self-closing element can’t use pseudo-elements because they aren’t “container elements.
The select box can't have a pseudo-selector. So, add one parent div and then use those selectors. Also add top/bottom/left/right positions in CSS.
This might help with troubleshooting-- I was having this problem and found that since I was calling the CSS in a module.css file (which adds extra characters to the class name when the page loads), the ::after pseudo element code wasn't being picked up, even though normal classes and ids were. When I moved just the pseudo element class to a normal / sitewide CSS file, it worked.

Styling quotation marks

I have run into and issue when styling quotes. So what I'm trying to do is pull the quotation marks down a bit relative to the text so that it lines up well. I played around with relative and absolute positioning but could not figure it out. This program will become a random quote generator and the position of the end quote has to be such that it lines up the same way relative to the text if it there is a quote that takes up several lines.
body {
background-color: rgb(44, 62, 80);
}
.quoteMachine {
margin: 100px auto 0 auto;
padding: 40px 60px;
max-width: 600px;
min-height: 225px;
border-radius: 5px;
background-color: white;
}
.theQuote {
text-align: center;
font-size: 30px;
color: rgb(44, 62, 80);
}
.quotetationMarks {
font-size: 60px;
font-weight: 600;
}
.quoteAuthor {
text-align: right;
font-size: 20px;
color: rgb(44, 62, 80);
}
.twitterButton {}
<div class="quoteMachine">
<div class="theQuote">
<blockquote><span class="quotetationMarks">“</span > They call me Mister Tiibs <span class="quotetationMarks">”<span></blockquote>
</div>
<div class="quoteAuthor">
- hello
</div>
<button class="twitterButton"></button>
<button class="newQuoteButton"></button>
</div>
Since the spans are inline elements, you could add vertical-align: middle; to .quotetationMarks and that would move them down toward the middle of the rest of the string.
Alternatively, you could add position: relative; top: 10px; if you need more precise control.
Maybe adding vertical-align: sub; to .quotetationMarks is what you are looking for?
You can also use fontawesome, that's always a good option. -> http://fontawesome.io/icon/quote-right/
Edit: While vertical-align: middle; is a very valid and elegant approach, sometimes you've got a very specific position in mind for the quotation marks. If you need to match a mockup to pixel perfection, this approach grants you the flexibility.
You might get some mileage out of using pseudo-elements to render the quotes, and relative/absolute positioning to get them "just so".
This is especially important to help position them across line breaks. (I've edited my example to force a line break, in order to illustrate the robustness of this approach.)
From MDN:
Just like pseudo-classes, pseudo-elements are added to selectors but instead of describing a special state, they allow you to style certain parts of a document. For example, the ::first-line pseudo-element targets only the first line of an element specified by the selector.
And specifically for the ::before pseudo element:
::before creates a pseudo-element that is the first child of the element matched. It is often used to add cosmetic content to an element by using the content property. This element is inline by default.
These quotes you're styling are cosmetic content, so I think that this is a great use-case for the ::before pseudo element.
I've forked your codepen here: http://codepen.io/cam5/pen/kkxpbX, but here are the relevant parts
<!-- quote HTML -->
<blockquote>
<span class="quotationMark quotationMark--left"></span >
They call me…<br /> Mister Tiibs
<span class="quotationMark quotationMark--right"></span >
</blockquote>
and the CSS:
/* quote css */
.quotationMark {
position: relative;
}
.quotationMark--left::before,
.quotationMark--right::before {
font-size: 60px;
font-weight: 600;
position: absolute;
top: -15px;
}
.quotationMark--left::before {
content:"\201C";
left: -45px;
}
.quotationMark--right::before {
content:"\201D";
right: -45px;
}
This CSS Tricks resource is great when you're trying to locate the ISO for putting a certain glyph into a CSS content rule: https://css-tricks.com/snippets/html/glyphs/
Setting the parent element, the .quotationMark to display: relative; will mean that the top, right, left values passed to the children (the pseudo-elements) of it bearing the position: absolute; property are calculated relative to their parent.

CSS - Create a custom dotted line out divs

I want to create a line with circles. Can this be done with background-repeat? Or do I need to set a picture as background? The circles should have a 5px radius.
p:after {
content: '';
background: 'rounded div of size 10x10px' repeat-x
width: 50%;
}
This is the only solution without using background-image or border-image encoded in base64 or using external files.
https://jsfiddle.net/3r6xsr0m/
html:
<div class="line"></div>
css:
.line:before {
content: "..................................................................................................";
display: block;
font-size: 60px;
font-family: Georgia;
color: #aaa;
max-width: 100%;
overflow: hidden;
}
Dots may differ depending of browser font rendering algorithm.
You'll have to create a 10px x 10px image of the dot and then use your method of repeating the background using either pseudo or just a new element. I'd go with a new div element if you can to prevent any issues across browsers like IE8. You'll also have to give your element a width if you go pseudo.

Double borders in CSS

I'm creating PHP, Javascript based photo-gallery from scratch
The problem is, I want to make difference between simple picture and photo-album.
So simple picture borders look like that
Is that possible to create facebook like photo-album borders (double borders, which creates multiple images effect) via css or CSS3?
P.S Don't know if it will be possible with old css standarts. I mean, CSS3 probably can do it but it will not be backward compatible. In other hand, currently my php side generates 100x100 px thumbs. I need something that will not be ruined if I will increase size of thumbs.
Thx in advance
Use a pseudo element like :before or :after, for example:
Turns out, most browsers don't like :before on images because it's not a text-containing element. You could still do this if you did it on an alternative element, like a div, and set the div's background to the original image. Or, you could try:
http://jsbin.com/otivaj/edit#html,live
Is this what you're looking for?
jsfiddle
HTML:
<div class="facebook-album"></div>
CSS:
.facebook-album, .facebook-album:before
{
background: red;
width: 100px;
height: 100px;
margin: 20px;
border: 3px solid #FFF;
box-shadow: 0 0 0 1px #999;
position: relative;
}
.facebook-album:before
{
margin: 0;
content: "";
position: absolute;
top: -7px;
left: -7px;
background: white;
z-index: -1;
}
You could just look at Facebook's source to figure it out. This will also work:
http://jsfiddle.net/g9A6a/
Yep, you can definitely do this with CSS. It looks like all your images are the same size, too, which will make this very straightforward. Simply place your <img> inside a containing element with position: relative; and an offset. Both the container and image should have a border, with padding and offsets you so desire. Set the width and height of the containing element based off the child image's dimensions.
Here is a
DEMO on jsfiddle
I'm not sure you can achieve that effect with simply CSS2. If adding more markup is an option, I would do something like this:
<ul>
<li><img></li>
</ul>
li {
position: relative;
border: 1px solid gray;
}
img {
padding: 6px;
border: 1px solid gray;
position:absolute;
top:6px;
left: 6px;
background-color:white;
}