Styling text within a div similar to links - css

I have the following breadcrumb code.
#breadcrumb{
margin-bottom: 10px;
margin-top: 10px;
display: block;}
#breadcrumb a{background:#FFFFFF;
padding:4px;
margin-right:10px;}
<div id="breadcrumb">
HomeParent CatagoryChild Catagory
</div>
I have styled the 'Home' and 'Parent Catagory' links to have solid colour backgrounds. I would like to style the 'Child Catagory' text with a slightly different colour solid background from the two links. All three elements of the breadcrumb should have gaps between them and should not be touching.
The 'Child Catagory' text is not surrounded by <span> so I am unsure how to achieve this.
If I add styling to the surrounding 'breadcrumb' div, the space between the links is affected.
I need to style just the text in the 'breadcrumb' div, not the div itself and not the links.

This is a terrible hacky approach and should not be used. I strongly recommend wrapping the last bit of the breadcrumb in a span and styling the span.
#breadcrumb {
background:red;
display:inline-block;
}
#breadcrumb a {
background:green;
display:inline-block;
position:relative;
}
#breadcrumb a:after {
content: "";
background: white;
height: 1.5em;
position: absolute;
top: 0;
right: -5px;
width: 5px;
}
<div id="breadcrumb">
Home
Parent Catagory
Child Catagory
</div>
Edit
If you know the width of the last element, you can do the following:
#breadcrumb:after {
background: red;
content: "";
height: 1.5em;
width: 99px;
display: inline-block;
position: absolute;
top: 0;
right: 0;
z-index: -1;
}
#breadcrumb {
overflow: hidden;
display: inline-block;
position: relative;
margin-bottom: 10px;
margin-top: 10px;
}
#breadcrumb a{
background:#ccc;
padding:4px;
margin-right:10px;
}
<div id="breadcrumb">
HomeParent CatagoryChild Catagory
</div>

Option 1 - Edit the HTML
The question is: Can you edit the HTML?
The best thing to do would be to edit the HTML and add a surrounding div to the "Child Category" item, then style it.
<div id="breadcrumb">
Home
Parent Catagory
<div class="child">Child Category</div>
</div>
SCSS
#breadcrumb{
a,
.child{
background: #f5f5f5;
padding: 4px 7px;
margin: 0 5px;
&:first-child {
margin-left: 0;
}
}
.child{
display: inline-block;
background: #222;
color: #fff;
}
}
CSS Output
#breadcrumb a, #breadcrumb .child {
background: #f5f5f5;
padding: 4px 7px;
margin: 0 5px;
}
#breadcrumb a:first-child, #breadcrumb .child:first-child {
margin-left: 0;
}
#breadcrumb .child {
display: inline-block;
background: #222;
color: #fff;
}
Example:
http://codepen.io/anon/pen/vEaPqx
Option 2 -- Javascript workaround
Of course there are other ways using javascript like getting the value from another div and appending it to the breadcrumb div. The issue is that this is could eventually break if the HTML changes. It also depends on the content already on the page.
<div id="existing-child-category-text">Child Category</div>
<div id="breadcrumb">
Home
Parent Catagory
</div>
Javascript:
// Create the child element
var child_cat = document.createElement('div');
// Add a class name to the child element
child_cat.className = 'child';
// Add the text from the existing child category to the child div
child_cat.innerHTML = document.getElementById('existing-child-category-text').innerHTML;
// Add the new div to the breadcrumbs
document.getElementById('breadcrumb').appendChild(child_cat);
Example:
http://codepen.io/anon/pen/vEaPqx
Ultimately I would assume that if you are able to edit javascript then you could also change the HTML which would be a way more robust solution.
Option 3 -- Use Pseudo Elements
Try to avoid this but you could have a similar approach using css pseudo elements. but you would need to set the value via javascript or hardcode the value on css which is not the best solution.

Related

How to add multiple CSS attributes to a tag simultaneously?

I'm trying to modify my WordPress theme the way I want through the Additional CSS part in Appearance>Customize.
I want all my h1 tags in entry-content class to be like this:
so I used this code:
.entry-content h1 {
background-color: #cfbc00;
display: inline;
background-color:#000000;
}
I want the whole block to be colored #cfbc00 and the background of the text itself to be black. But the browser does not apply these simultaneously to my tag and it applies only one of the attributes. What should I do?
If you don't have access to the HTML code, then here is a CSS workaround:
.entry-content
{
position: relative;
}
.entry-content h1::before
{
content: "";
display: block;
position: absolute;
width: 100%;
background-color: #cfbc00;
height: 40px;
z-index: -1;
}
.entry-content h1 {
display: inline;
background-color: #000000ad; /* Sets transparency according to sample image */
top: 0;
/* Line height to maintain the height of the :before element */
line-height: 40px;
color: #fff;
font-size: 35px;
padding: 5px;
}
<div class="entry-content">
<h1>Test title</h1>
</div>

Preventing hover on pseudo element - but pointer-events: none; doesn't do it

I have a few links on one line next to each other, and I would like to have dividing dashes between them. I chose to make this happen with the use of the ::before pseudo element, and it works nicely.
However, hovering over the dividers also triggers the hover over the element I have the ::before on.
This is a fiddle showing the issue. If you hover over the dashes, the underline appears under the a.
In my search as to how to prevent this from happening, I ran into this stackoverflow question. Together with the documentation on developer.mozilla.org and the caniusethis page on the pointer-events property I was sure this would fix it. But it didn't.
What am I missing here?
You need to make changes in css
.wrap a::before {
content: '----';
padding: 0 15px;
position: absolute;
left: -50px;
pointer-events: none;
}
.wrap a {
text-decoration: none;
position: relative;
margin-left: 50px;
display: inline-block;
}
You will need to use position:absolute for the ---- to make it out of the <a> flow and also position:relative to the parent <a> element.
Stack Snippet
.wrap a {
text-decoration: none;
margin: 0 30px;
position: relative;
}
.wrap p {
margin: 0;
}
.wrap {
font-family: sans-serif;
border: 1px solid green;
padding: 5px;
margin-bottom: 20px;
}
.wrap a:nth-of-type(1) {
margin-left: 50px;
}
.wrap a::before {
content: '----';
position: absolute;
pointer-events: none;
left: -30px;
transform: translateX(-50%);
}
.wrap a:hover {
text-decoration: underline;
}
<div class="wrap">
<p>These links do not have the pointer-events: none; property</p>
<br>
link text one
link text two
link text three
</div>
<div class="wrap">
<p>These do have pointer-events: none; yet their behaviour is the same as with the block above.</p>
<br>
link text one
link text two
link text three
</div>

Make div size as per contents and horizontally center inside it's parent

I have a div message which basically has a text and a link. I want its size to be changing based on the string inside it. Also I want this message div to be centered inside its container.
I have been playing with it for a while without much luck. Here is the link to JSfiddle: http://jsfiddle.net/pDYJ8/
Also I don't know how make that text and link appear one after other ( not on the new line )
Here is the code:
<div class="container">
<div class="message">
<p>do you want to </p>
<a href="somewhere" target="_blank">
buy this product
</a>
</div>
</div>
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
}
.message {
margin-right: auto;
margin-left: auto;
background: #ddd;
width:100px;
}
Tried display inline block to fit its content but then it wouldn't center it inside its parent.
Keeping width 100px for now just to mock my requirements
Just Tweak Some CSS
See the demo fiddle.
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
text-align: center; /*added*/
}
.container .message {
display: inline-block; /*added*/
text-align: left; /*added*/
background: #ddd;
}
.message p { /*added*/
display: inline-block;
}
Explanation
The text-align center causes the now inline-block display of .message to center, which is then reset to have its own text-align back at left (this is not necessary). To get the a on the same line, the p also needs to be some type of inline display, here I chose inline-block as well..
I think you are over complicating things. All you need is a text-align: centeron the container and a display: inline-block on the message:
.container {
background: #666;
font-size: 15px;
color: #e47911;
text-align: center;
}
.container .message {
background: #ddd;
display: inline-block;
}
http://jsfiddle.net/Pevara/pDYJ8/9/
The inline block makes the div act as a word inside text, and the text-align center makes the 'word' align to the center...
Here is a simplified approach to a couple of the answers given. It reduces the amount of HTML and CSS needed.
CSS
.container {
color: #e47911;
text-align: center;
}
.message {
display: inline;
background: #DDDDDD;
}
HTML
<div class="container">
<p class="message">
Do you want to buy this product?
</p>
</div>
I would definitely put your anchor tag, <a> inside the paragraph tag, <p>.
You could even remove display: inline; from .message if you made it a <span> rather than a <p>.
Check this out:
http://jsfiddle.net/pDYJ8/10/
Changes made to above link
.container .message {
margin: 0 auto;
width:auto;
}
span{
background: #ddd;
display:inline;
}
You can simplify it with display: table; and margin: 0 auto;
.container {
display: table;
margin: 0 auto;
background-color: #DDD;
}
<div class="container">
do you want to buy this product
</div>

One item not aligning with CSS positioning (but all others are)

All my html elements are being positioned where I want them, except one, and I can't see why it should be the exception. The css snipped to exclude non relevant parts is:
body {
position:relative;
}
ul {
position:absolute;
list-style:none;
margin:0px;
padding:0px;
}
li: {
position: relative;
top: 90px;
display: block;
height: 80px;
}
#track_title {
position:absolute;
top: 1px;
left: 80px;
font-size: 15px;
font-weight: bold;
}
<ul>
<li>
<img src="image.png">
<h2 id = "track_title">Title</h2>
<h3 id = "artist_name">Name</h3>
However as you can see from the screenshot the Title is appearing more than 1px from the top of its parent li. What am I doing incorrectly?
There are default margins and padding for h2 & h3 element. You should set it with your fixed values.

Two pages and one CSS, different behavior with a hover popup

For the same markup, on two different pages, there's a discrepancy where a hidden div will popup and show itself based on the position of the mouse cursor. Basically, div.blurb will popup way off in the corner on page 1, whereas on page 2 it shows up near the cursor where it's supposed to. Page 2 is missing bolded elements below due to the different layout it has.
Markup hierarchy (bold indicate those that are present on page 1 but not page 2):
html
body
div#cn-body-inner-2col
div#cn-cols
div#cn-centre-col
div#cn-center-col-inner
table.plainTable
tbody
tr
td
div#contact_sheet
div.box
a
img
My markup:
<div id="contact_sheet">
<div class="box">
<img />
<div class="blurb">
</div>
</div>
<div class="box">
<img />
<div class="blurb">
</div>
</div>
...
</div>
CSS for markup:
#contact_sheet{
margin-top: 50px;
}
#contact_sheet .box {
width: 150px;
margin: 5px;
padding: 5px;
float: left;
border: 1px solid #887767;
text-align: center;
background-color: #fff;
}
#contact_sheet .box a img{
height: 100px;
}
#contact_sheet .box .blurb {
position:absolute;
display:none;
z-index:9999;
background-color: #fff;
color:#000;
border: 1px solid black;
width: 300px;
text-align: left;
}
jQuery:
$(document).ready(function(){
$("#contact_sheet div.box").bind("mousemove", function(event) {
$(this).find("div.blurb").css({
top: (event.pageY) + "px",
left: (event.pageX - 150) + "px"
}).show();
}).bind("mouseout", function() {
$("div.blurb").hide();
});
});
Is the following CSS (included within the layout) the problem to my popup hovering further away on page 1 than it should? I used Chrome to go through the hierarchy and grabbed all the styles related to positionning from the bolded items in the markup hierarchy mentionned above.
#cn-body-inner-2col #cn-centre-col {
margin-right: -100%;
}
#cn-centre-col, #cn-centre-col-gap {
float: left;
}
#cn-centre-col {
width: 100%;
}
#cn-centre-col, #cn-head, #cn-foot, #cn-left-col, #cn-right-col {
position: relative;
}
Bad:
http://i.imgur.com/kVbh4.png
Good:
http://i.imgur.com/rJi7n.png
What I've tried:
Adding position: static to my contact_sheet ID - didn't work
I think your problem is the
float:left;
And the
position:relative;
in your #cn-centre-col ID
It make a huge difference for positioning a popup inside it

Resources