I have a headline and I want to insert a block with a certain background before and after the headline with the CSS pseudo elements :before and :after, and I want all them to float left so they're in one line.
The problem is that I can't figure out how to select the actual content of an element. Let's see this example realized with DIVs:
HTML:
<div class="line"></div>
<h1>Text here</h1>
<div class="line"></div>
CSS:
.line {
display: block;
width: 150px;
height: 20px;
float: left;
background-color: grey;
}
h1 {
display: block;
padding: 0 10px 0 10px;
width: 100px;
height: 20px;
font-size: 20px;
float: left;
margin: 0;
text-align: center;
}
So this will work. However, is there a way to realize the same with the :before and :after elements for h1? Can I somehow select the "Text here" and apply the :after class to it?
Thanks in advance!
EDIT:
Let me explain this in a different way. See this link: [link]http://css-tricks.com/examples/hrs/
I want to achieve the same as the last example, only instead of inserting the text via content: "$"; in the CSS, I want that dollar-sign to be inserted as a <h1>-element - ergo the "hr" to wrap around the h1.
If I'm understanding you correctly, you want something like this:
h1, h1:before, h1:after {
display:inline-block;
float:left;
}
h1 {
height: 16px;
width: auto;
position:relative;
padding:0 20px;
}
h1:before {
content: " ";
background:url(...);
height: 16px;
width: 16px;
position:absolute;
left:0;
}
h1:after {
content: " ";
background:url(...);
height: 16px;
width: 16px;
position:absolute;
right:0;
}
Related
I am trying to add a pseudo before and after vertical line to a textfield for styling purposes. These elements need to be flush to the text -20px left and -20px right.
This works fine when the text is on one line as an inline-block, but as soon as the text spans multiple lines the width expands to that of the parent and the pseudo elements are no longer just 20px from the text.
Is there a way in which I can accomplish this using CSS?
.container {
width: 500px;
background-color: red;
text-align: center;
margin-left: 40px;
}
h2 {
position: relative;
display: inline-block;
margin: 0;
padding: 0;
background-color: blue;
color: white;
}
span {
position: relative;
background-color: green;
}
h2::before,
h2::after {
content: '';
position: absolute;
top: 0;
width: 4px;
height: 20px;
background: black;
}
h2::before {
left: -20px;
}
h2::after {
right: -20px;
}
<!-- Single line example works as the black bars are 20px away from the start/end of text-->
<div class="container">
<h2><span>This is a title</span></h2>
</div>
<br> <br>
<!-- double line doesn't work because the h2 is now the full width of the container -->
<div class="container">
<h2><span>This is loooonnggggggggggggggggggggggeeeeerrr</span></h2>
</div>
Edit: Here is a working version using tables, but if anyone has a better solution I'd love to hear it: https://codepen.io/anon/pen/MqveLQ
So from what i can see is the issue here is where you are applying the borders with before and after. You need to alter where you apply your borders. Remove them from the h2, and add in a new html element that wraps the h2 and apply there.
eg:
<div class="container">
<div class="headerwrap">
<h2><span>This is loooonnggggggggggggggggggggggeeeeerrr</span></h2>
</div>
</div>
<div class="container">
<div class="headerwrap">
<h2><span>This is a title</span></h2>
</div>
</div>
CSS
.headerwrap::before,
.headerwrap::after {
content: '';
position: absolute;
top: 0px;
bottom:0;
margin:auto;
width: 4px;
height: 20px;
background: black;
}
.headerwrap::before {
left: 10px;
}
.headerwrap::after {
right: 10px;
}
Here is a working example: https://codepen.io/FEARtheMoose/pen/VGbJjO?editors=1100#0
Edit: altered example after comments - https://codepen.io/FEARtheMoose/pen/VGbJjO
I have moved your code to this fiddle: https://jsfiddle.net/n2Lr6xy5/13/ and removed position: absolute along with stripping out some of the other styles as they seemed unnecessary and I think I have created what you're after.
Here is the updated CSS:
.container {
width: 500px;
background-color: red;
text-align: center;
margin-left: 40px;
}
h2{
display: inline-block;
}
h2:after,
h2:before {
content: "";
width: 4px;
height: 20px;
background: #000000;
display: inline-block;
margin: 0 10px;
}
I'm trying to have a background image to the right of a div, which isn't covering the whole div.
Right now it's like this (div1 is background-color):
<div id="div1">
<div id="image"></div>
Text
</div>
CSS:
.div1 {
background: #324458;
color: #FFF;
font-size: 0.9em;
font-weight: bold;
padding: 10px;
width: 100%;
position: relative;
border-radius:4px;
height:40px;
clear:both;
overflow: hidden;
}
.image {
background: url("url here");
background-position: center center;
background-size: cover;
opacity: 0.3;
height: 39px;
margin: -10px;
width: 300px;
position:absolute;
right: 10px;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
z-index: 0;
}
But is it possible to have the image shown in it without having it as a div inside div1? Like using :after, :before or something else? I only want the div image to show to the right of div1 and be X width.
For an background image to show on pseudo-elements like ::after and ::before you should include content: ''; on them.
I've fixed (you were trying to target ids with class selectors) and added the mentioned background image on on this fiddle. But it goes like this:
.div1 {
background: #324458;
color: #FFF;
font-size: 0.9em;
font-weight: bold;
padding: 10px;
width: 100%;
position: relative;
border-radius: 4px;
height: 40px;
clear: both;
overflow: hidden;
}
.div1::after {
content: '';
background: url("https://unsplash.it/200/300");
background-position: center center;
background-size: cover;
opacity: 0.3;
height: 39px;
margin: -10px;
width: 300px;
position: absolute;
right: 10px;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
z-index: 0;
}
<div class="div1">
Text
</div>
There are several ways to place an image to the right of a div. You should consider displaying the image with an image tag as follows:
Also, in your html you define ids, then in css you need to use # isntead of .. Check Difference between id and class in CSS and when to use it
A way to do this:
HTML:
<div id="div1">content</div>
<img id="image" src="url"/>
CSS:
#div1 {
display:inline-block;
float:left;
}
#img {
float:left;
}
By default, div containers stretch their width all the way to match 100% the width of their parent container. Setting 'display:inline-block' will make it wrap their content and allow stacking different containers (including images) to the sides.
This is a test of :before and :after, with which you can place text or an image before and after each HTML element.
p.test:before {
padding-right: 5px;
content: url(/pix/logo_ppk.gif);
}
p.test:after {
font-style: italic;
content: " and some text after.";
}
so I want to make a HR and a H2 element display on the same line, so that it looks like this:
http://gyazo.com/7943d8d8b6d23ebffc80a60c0acc872f
But it looks like this:
http://gyazo.com/30f80e33d627e7bbedf56565afbd5509
And here is the code:
hr {
display:inline-block;
width:60%;
float:right;
margin-right:20px;
height:5px;
background-color:#FFFFFF;
}
<h2 class="minecrafter" style="padding-left:10px;padding-top:10px;letter-spacing:3px;">Miners Union</h2> <hr>
Now I know I can easily fix this by having the HR tag infront of the H2 tag, but I know that this is bad practice and I don't think it will work in xHTML.
Thank you for reading and any replies :)
Edited to correct typo as pointed out
You could use :after element like this: fiddle
h2:after {
content: '';
display: inline-block;
height: 3px;
background: red;
width: 60%;
float: right;
margin-top: 15px;
}
You can accomplish this by changing your css to:
hr, .minecrafter {
display: inline-block;
}
hr {
width: 60%;
}
Hope this helps.
I would do something like this:
<h2 class="minecrafter"><span>Miners Union</span><hr /></h2>
body {
background: #444;
color: #fff;
}
h2 {
padding-left: 10px;
padding-top: 10px;
letter-spacing: 3px;
overflow: hidden;
}
h2 span {
float: left;
margin-right: 20px;
}
h2 hr {
border: 0;
background: #fff;
height: 4px;
}
A :after approach would be a "cleaner" way to do it, however.
https://jsfiddle.net/0sh4a1wL/2/
EDIT: figured it out - see the answer below for solution.
Background: I have two elements, an h1 and a span. I'm trying to place the h1 "Title" element on the left of the container element and the span "category" element on the right of the container, which I've done with float: left and :right.
Problem: I'd like a dotted line to appear between the h1 and span elements. If possible, I'd like to use a pseudo-element for this since it's purely aesthetic, so I'm trying to get the h1:after pseduo-element to fill the remaining width of the container element between the h1 and span.
I'm trying to keep my HTML as close to the following as possible:
<header>
<h1>Title</h1>
<span class="category">Category</span>
</header>
My CSS so far - the :after pseudo-element is currently positioned beneath the h1 element, as opposed to between the h1 and span:
header {
display: block;
background: cyan;
width: 80%;
margin: 0 auto;
}
h1 {
display: block;
float: left;
}
h1:after {
display: block;
float: left;
width: 100%;
border-bottom: 1px dotted black;
content: " ";
}
span {
display: block;
float: right;
background: green;
}
use this code:
jsFiddle
HTML
<div class="container">
<span>title</span>
<span class="category">category</span>
</div>
CSS
div{
width:100%;
position:relative;
height:50px;
border:1px solid;
}
span{
line-height:50px;
background:#fff;
padding:0 10px;
float:left;
position:relative;
z-index:1;
}
span.category{
float:right;
}
.container:after{
content:"";
position:absolute;
width:100%;
height:0;
border-bottom:1px dotted #4c5660;
top:50%;
left:0;
}
OK after more research, I discovered that what I'm looking for is called a dot leader. While the W3C Working Draft has a section on them here, they don't seem to be well-implemented yet. I found another approach on SO here. Using that answer, I revised my code as follows:
HTML:
<header>
<h1>Title</h1>
<span>Category</span>
</header>
CSS:
header {
overflow: hidden;
}
h1 {
float: left;
padding: 0 .4em 0 0;
margin: 0;
}
span {
float: right;
padding: 0 0 0 .4em;
margin: 0;
}
/* Dot Leader */
header:after {
content: "";
display: block;
overflow: hidden;
height: 1em;
border-bottom: 1px dotted;
}
Here's a JSFiddle with the result: http://jsfiddle.net/dx48R/
How can I style list items like this?
So some kind of colored square before the text. I was looking into this but couldn't find it :
http://www.w3schools.com/cssref/pr_list-style-type.asp
They don't have to be list items but it seemed to me convinient because it is some kind of list which I would like to style and display in the same line not in different lines like in image above.
I've tried this :
<ul class="ordering">
<li><div></div><span>One</span></li>
<li><div></div><span>Two</span></li>
<li><div></div><span>Three</span></li>
</ul>
and with this css :
.ordering {
list-style:none;
min-width:250px;
}
.ordering div{
width: 12px; height: 12px; float: left;
}
.ordering span{
display: block;
float: left;
font-size: 11px;
line-height: 11px;
padding-left: 5px;
}
The problem I encounter is that I can't seperate them (they're glued together), and the other one when I have many items which don't fit in the size of ordering class they break line but the square remains in the previous line so square + text isn't being treated as one part.
You have tried in the right way. Only additional thing is you can minimize the html structure and use nth-child() property
HTML
<ul class="ordering">
<li><span></span>One</li>
<li><span></span>Two</li>
<li><span></span>Three</li>
</ul>
CSS
ul.ordering { list-style:none; min-width:250px; }
ul.ordering li{
display:block;
clear:left;
vertical-align:middle;
font-size: 20px;
line-height: 20px;
padding-left: 5px; }
ul.ordering li span{ width: 12px; height: 12px; background:red; display:inline-block; margin-right:6px }
ul.ordering li:nth-child(2) span{ background:green !important; }
ul.ordering li:nth-child(3) span{ background:blue !important; }
LIVE DEMO
I don't think you need to float anything:
.ordering div {
width: 12px; height: 12px; display: inline-block;
}
.ordering span{
display: inline-block;
font-size: 11px;
line-height: 11px;
margin-left: 5px;
}
(If you want it this to work in IE 7 and earlier, you'll need to change the <div> to a <span>, or some other inline element) e.g.:
.ordering span.colouredSquare {
width: 12px; height: 12px; display: inline-block;
}
The problem I encounter is that I can't seperate them (they're glued together), and the other one when I have many items which don't fit in the size of ordering class they break line but the square remains in the previous line so square + text isn't being treated as one part
All you need to do is give your li elements a display property of display: inline-block. That should fix your spacing issue and make the square & text move as a "block" i.e.
.ordering li
{
display: inline-block;
}
Alternatively, you can clean up your markup a little by the power of pseudo elements.
HTML (note: only one child element to the list item)
<ul>
<li><p>Bender</p></li>
<li><p>Fry</p></li>
<li><p>Leela</p></li>
</ul>
CSS
ul { margin: 10px; }
li { position: relative; }
p:before {
background: #bb3333;
content: ".";
display: inline-block;
height: 10px;
margin: 0 5px 0 0;
text-indent: -10000px;
width: 10px;
}
li:nth-child(2) p:before { background: #33bb33; }
li:nth-child(3) p:before { background: #3333bb; }
See my fiddle: http://jsfiddle.net/uNR2M/2/
Instead of nth-child I think I would apply classes instead, as it is slightly better for performance.
This should do the trick:
<li>
<div></div><span>One</span>
<div class="clearfix"></div>
</li>
You should add some clearfix before the closing li.
.clearfix {
clear: both;
}
I wouldn't use the native list bullet points as these are not stylable.
You can use nested divs and spans but IMO it's cleaner to style :before pseudo-element, eg.:
Demo
html:
<ul>
<li>item</li>
<li class="red">item</li>
<li class="blue">item</li>
</ul>
css:
li {
position: relative;
list-style: none;
display: inline-block;
margin-left: 30px;
}
li:before {
content: ' ';
width: 10px;
height: 10px;
position: absolute;
left: -20px;
top: 5px;
background: green;
}
.red:before { background: red; }
.blue:before { background: blue; }