Background image (pattern) after heading using CSS only - html

Is this possible to create following effect using CSS. I only have a H2 element in HTML, and i do not have any control on HTML of the page. I can only change CSS.
I tried it with :before and :after but no success so far.
This will be used in a CMS, so i can not be sure how or where end user will be adding headings.

I think your best shot would be (since you cannot edit any of the HTML and you want a CSS only solution) to play a little with the position and z-index of the container and the h2:after pseudo element:
HTML:
<div id="wrapper">
<h2 class="heading">New Collection </h2>
</div>
CSS:
#wrapper {
width:100%;
height:800px;
background:green;
position:relative;
z-index:-2;
}
.heading {
display:inline;
background:green;
}
.heading:after {
content:'';
display:inline-block;
width:100%;
height:20px;
background:#d3d3d3;
position:absolute;
right:0;
z-index:-1;
}
Check this FIDDLE, I know it's not that fancy, but I think you cannot do more than that using CSS only.
Hope this helps.

The simplest way that I know of is to put a span inside of the H2, but still around the inner text, then apply one style to the span and one style to the H2.
EDIT: You can use a little bit of jQuery to add the span inside your H2 tags.
HTML:
<h2><span>New Collection</span></h2>
CSS:
h2 {
font-family: sans-serif;
font-weight: normal;
font-size: 16px;
background: #eee;
color: #999;
}
h2 span {
padding-right: 10px;
background: #fff;
}
jQuery:
$(document).ready(function() {
$('h2').each(function() {
var title = $(this).html();
$(this).html('<span>' + title + '</span>');
});
});
Here's a Demo

Related

Unwanted margin above div tag when adding a p tag inside

Today when fiddling with some HTML and CSS, i stumbled upon a problem. When I put a paragraph tag inside of a div tag, it adds an ugly unwanted margin to the top of my div.
<div id="header">
<center>
<p>Gavin Sadler.com</p>
</center>
body {
margin:0;
}
#font-face {
font-family: CoolveticaRg;
src: url(Coolvetica_Rg.otf);
}
#header {
width:%100;
height:100px;
background-color:grey;
margin-top:0;
}
#header{
margin-top:0px;
background-color:grey;
}
#header p {
font-family:CoolveticaRg, Arial;
}
I would appreciate the help. Thanks!
You need to set this to you p element like this:
#header p{
font-family:CoolveticaRg, Arial;
margin: 0;
}
and it should fix your problem
You can remove the margin on the paragraph:
#header p {
font-family: CoolveticaRg, Arial;
margin-top: 0;
text-align: center;
}
On a side note, the center tag is deprecated. You should remove it and use CSS to style your elements. I've added the necessary code above to centre your text.

Create contents of div using :before and :after

I have a single div element which I want to insert content into depending on it's class.
I am able to insert 2 elements into this div using the :before and :after attributes in the CSS, but I need to be able to insert at least 5 elements.
Here's what I have for 2:
div {
background: blue;
margin: 0 auto;
width: 50px;
height: 50px;
padding: 0;
display:table-cell;
vertical-align:middle;
text-align: center;
}
div:after {
content:'';
display:block;
border:1px solid #000;
width:40px;
height:40px;
margin:5px auto;
background-color:#DDD;
}
div:before {
content:'';
display:block;
border:1px solid #000;
width:40px;
height:40px;
margin:5px auto;
background-color:#DDD;
}
Is there any way I can create extra :before and :after? Something like div:after:after.
Nope, Sorry you can't
Useful to read : http://css-tricks.com/use-cases-for-multiple-pseudo-elements/
Just add some element inside your element
HTML :
<div>
<div>
</div>
</div>
CSS :
div:after {
content:'1';
}
div > div:after {
content:'2';
}
As Paulie_D comments you can't get :after:after in CSS.
Although you could do it in jQuery by Chaining
$("div").prepend(" *before* ").prepend(" *before before* ").append(" *after* ").append(" *after after* ");
See JSFiddle here
Although it uses jQuery it's a bit more valid a solution, as really you should just use :before and :after CSS selectors for styling.

Is there a way to add padding to text in an element only when text is present?

Using only HTML5 and CSS3 is there a way to add padding or at least what appears to be padding to text in an element only when text is present?
Here's some example code and a demo to work with.
CSS:
.container {
background-color:red;
}
.text {
text-align:center;
font-size:18px;
padding:10px;
}
HTML:
<div class='container'>
<p class='text'>text</p>
</div>
The structure can change as long as when there is no text, container is 0px high.
DEMO
.text {
text-align: center;
font-size: 18px;
padding: 10px;
}
.text:empty {
padding: 0px;
}
Note that this will not work in IE<8. If you need it to, you could either use JavaScript (such as if (text.children.length === 0) text.style.padding = '0px') or a JavaScript fallback library like Selectivizr.
Here is a JSFiddle.
Here is an MDN reference page for the :empty psuedo-class.

Possible to add image caption with css pseudo content?

I am looking for the easiest, most maintainable way to do this:
These are text slugs that will be appended to certain images throughout the site. They all say this same thing, but the images are varied and come from a CMS.
I know how I would do it with the image set to position relative and a div with "there's a better way" in an absolutely positioned child div.
However, since that requires HTML added to every image that gets this treatment, I was looking for a way to do this with a css class using the :before pseudo element. So far, applying the class to a wrapping link has no effect:
<img src="imagepath" alt="">
.tabw img:before {
content: 'theres a better way';
color: red;
font-size: 18px;
}
Is this sort of thing possible? Having the whole thing in CSS means all I have to do is have the CMS apply the class attribute when needed.
Yeah, ::before and ::after don't work on images. But you can apply them to the wrapper
link:
a{
position: relative;
}
a, a > img{
display:inline-block;
}
a::before{
content: 'theres a better way';
position: absolute;
left: 0;
top: 0;
width: 80%;
height: 20px;
left: 10%;
background: #000;
opacity: 0.4;
color: red;
font-size: 18px;
}
demo
If you want the text added in the HTML, you'll have to put a real element with it in your link (and apply the same rules to it, but without content)
I'll do it like this with jQuery :
Html
<div class="thumb">
<img src="http://www.zupmage.eu/up/NvBtxn7LHl.png" alt="cover"/>
<div class="caption">My caption</div>
</div>
Css
.thumb {
position:relative;
width:230px;
height:230px;
}
.thumb img {
max-width:100%;
}
.thumb .caption {
display:none;
position:absolute;
top:0;
height:20px;
line-height:20px;
width:100%;
text-align:center;
background:rgba(255,255,255,0.5);
}
JQuery
$('.thumb').hover(function() {
$(this).find('.caption').fadeIn();
}, function() {
$(this).find('.caption').hide();
});
See fiddle
NOTE TO ANYONE FINDING THIS THREAD: In Firefox 21 and IE 9/10 this did not work right with oversized images. It forced the images to 100% even if globally set to max-width: 100%
I had to follow the answer I selected above but set the A tag to display:block instead of display:inline-block to fix.

Dash surrounding text

Given the above dynamically generated text (meaning that I can't just use an image), I am trying to recreate the design using just html and css selectors.
I would like to just use a single h4 with the containing text, but am open to other solutions. I would prefer to not use absolute positioning, but again, if that is the only way, then so be it.
I have tried surrounding with span tags, but those are inline elements that don't have an inherent width.
The h4 will be nested within a div, though not always of the same class or id.
Any ideas or resources to get me started?
Check out how they do the text underneath Contact me on this form. Pretty clean and simple. http://www.onextrapixel.com/examples/html5-css3-contact-form/
Here is the actual CSS that does it:
h1 {
font-family: 'Questrial', Verdana, sans-serif;
text-align:center;
font-size:40px;
padding:0;
margin:0 0 20px 0;
position:relative;
color:#8C8C8C;
}
/** have a nice ampersand **/
h1:after {
font-size:25px;
color:#D6CFCB;
content: '&';
text-align:center;
display:block;
width:100%;
font-family: 'Alice', Verdana, serif;
text-shadow: 0px 1px 0px #fff;
}
/** create the gradient bottom **/
h1:before {
position:absolute;
bottom:15px;
content: ' ';
text-align:center;
display:block;
height:2px;
width:100%;
background: linear-gradient(left, rgba(255,255,255,0) 0%,rgba(182,180,180,0.7) 42%,rgba(180,178,178,0) 43%,rgba(168,166,166,0) 50%,rgba(180,178,178,0) 57%,rgba(182,180,180,0.7) 58%,rgba(238,237,237,0.3) 90%,rgba(255,255,255,0) 100%); /* W3C */
}
Sorry for yet another edit, but here is an explanation. h1 { ... } styles the actual text, so "Event Schedule" for you, and h1:before { ... } does the cool line effect. h1:after { ...} does the ampersand, so this is actually where you would put your text i suppose
What you can do is to wrap your text with a span and put it inside a container div.
<div id='container'>
<span id='text'>EVENT SCHEDULE</span>
</div>​
Then give your container border-bottom and less height than your text (half size should give you the effect you are looking for).
#container {border-bottom:solid 1px #333; text-align:center; height:10px;}
#text { background:#fff; padding:0 20px; }​
You can see the live example here: http://jsfiddle.net/vzjxA/13/