How to make content appear before and after div - html

I want my content from &:before and &:after appear outside my div.first and before and after.
I thought the best way is using the css above, but it still appears inside my div, am I using the wrong method or is it something else?
Jsfiddle: https://jsfiddle.net/1uuL3sf6/1/
HTML
<div class="first">
</div>
CSS
.first
{
width: 100%;
height: 500px;
background-color:red;
&:before
{
content:"before";
width: 100%;
height: 20px;
}
&:after
{
content:"after";
width: 100%;
height:20px;
}
}

The pseudo elements :before and after do not create content before and after their parent element. They are inserted before and after its actual content.
A pseudo-element does exactly what the word implies. It creates a phoney element and inserts it before or after the content of the element that you’ve targeted.
From "Learning To Use The :before And :after Pseudo-Elements In CSS"
That's why they are always displayed inside the box of their parent.
However, as an example you can use absolute positioning to move the pseudo element out of the box:
&:before {
content:"before";
position: absolute;
top: -20px;
width: 100%;
height: 20px;
}
But you can also float them or display them as block to achieve your desired result.
Demo
Try before buy

.first {
width: 100%;
height: 500px;
background-color:red;
}
.first::before
{
content:"before";
}
.first::after
{
content:"after";
}
<div class="first">
</ br> This is your first div. </ br>
</div>

Here is a solution: https://jsfiddle.net/e8qtoxL9/
.first
{
width: 200px;
height: 200px;
margin: 0 auto;
background-color:lightgreen;
position: relative;
&:before
{
content:"before";
width: 100%;
height: 20px;
display: block;
background: lightblue;
left: -100%;
top: 0;
position: absolute;
}
&:after
{
content:"after";
width: 100%;
height:20px;
display: block;
background: lightblue;
left: 100%;
top: 0;
position: absolute;
}
}

Related

CSS element always on top

Is this possible to have element with class .myelement always on top in my HTML structure?
<div class="zindex1">
<div class="myelement">
want THIS element always be on top
</div>
</div>
<div class="zindex2">
</div>
and with for example this CSS
.zindex1 {
z-index: 1;
background-color: blue;
height: 100px;
position: relative;
}
.zindex2 {
z-index: 2;
background-color: green;
height: 300px;
position: relative;
}
.myelement {
background-color: yellow;
height: 500px;
width: 100px;
position: relative;
}
NOTE: I can't change values of my z-indexes and HTML structure.
Here is full example: https://jsfiddle.net/wLzej01f/
EDIT What if all my classes will have to have position: relative? I forget to mention about it
https://jsfiddle.net/wLzej01f/6/
The z-index CSS property won't apply to static elements:
For a positioned box (that is, one with any position other than
static), the z-index property specifies:
The stack level of the box in the current stacking context.
Whether the box establishes a local stacking context.
More about it here.
So, you need to add:
.myelement {
position: relative;
}
Updated JSFiddle.
Position: relative
.zindex1 {
z-index: 1;
background-color: blue;
height: 100px;
}
.zindex2 {
z-index: 2;
background-color: green;
height: 300px;
}
.myelement {
z-index: 3;
background-color: yellow;
height: 500px;
width: 100px;
position: relative;
}
<div class="zindex1">
<div class="myelement">
want THIS element always be on top
</div>
</div>
<div class="zindex2">
</div>
You forgot to add
position: absolute;
or
position: relative;
as you wish.
Just add position:relative to .myelement:
.myelement {
z-index: 3;
background-color: yellow;
height: 500px;
width: 100px;
position: relative;
}
DEMO
In case someone is trying to keep an element in a fixed position on the rest of the elements or does not know why one element is below another, keep in mind the sticky element.
https://www.w3schools.com/howto/howto_css_sticky_element.asp
.zindex1 {
z-index: 1;
background-color: blue;
height: 100px;
position: relative;
}
.zindex2 {
z-index: 0;
background-color: green;
height: 300px;
position: relative;
}
.myelement {
background-color: yellow;
height: 500px;
width: 100px;
position: absolute;
z-index: 2 !important;
}
**This code works**

Keep img at the bottom of page

I know it's redundant question, but answers which I saw are unbelievable. Multiple lines for such easy task? No way.
I want to keep img at the end of page (not at the end of displayed screen - I have this issue now).
current, wrong code:
#footerimg {
position: absolute;
right: 0px;
bottom: 0px;
z-index:-2;
}
I need a situation, when I will not be able to see the image until I scroll at the bottom of page.
I can't believe that there is no such option in CSS like bottom-page:0px
EDIT:
Meet CSS transform property - apply transform: translateY(100%).
See demo below:
#footerimg {
position: absolute;
right: 0px;
bottom: 0px;
z-index:-2;
transform: translateY(100%);
}
<img id="footerimg" src="http://placehold.it/200x200"/>
EDIT:
Looking at the image added to the question, I think you don't need positioning - just put the img as the last element in the html markup.
A possible solution can be this:
.content {
height: 120vh;
}
section {
text-align: right;
}
img {
vertical-align: top;
}
<section class="content"></section>
<section>
<img id="footerimg" src="http://placehold.it/200x200" />
</section>
You need to define positioned relative block-level element at the end of body. This will create new block formatting context and all inside absolute positioned elements will be placed relatively to it.
Look at snippet example:
body {
width: 100%;
}
.blk1 {
display: block;
width: 100%;
height: 500px;
background: orange;
}
.blk2{
display: block;
width: 100%;
height: 300px;
position: relative;
background: #9c9;
}
img.btm {
position: absolute;
bottom: 0;
right: 0;
opacity: 0;
transition: opacity 1s ease;
}
.blk2:hover .btm {
opacity: 1;
}
<div class="blk1">
</div>
<div class="blk2">
<img src="//placehold.it/100/100" class="btm">
</div>
Here is another solution:
body {
width: 100%;
position: relative;
}
.blk1 {
display: block;
width: 100%;
height: 200vh;
background: orange;
}
body:after {
content: "";
display: block;
position: absolute;
bottom: 0;
right: 0;
width: 100px;
height: 100px;
background-image: url('//placehold.it/100/100');
}
<div class="blk1">
</div>
So you can just add position: relative to body css styles
body {
position: relative;
}
and add body:after:
body:after {
content: "";
display: block;
position: absolute;
bottom: 0;
right: 0;
background-image: url('//placehold.it/100/100');
}
Your question is little bit making confusing me you mentioned in your question like that: "when I will not be able to see the image until I scroll at the bottom of page."
Then I think you do not need any effort you need just place image under footer container, when you will go bottom then you will be found at bottom of the page and this is very traditional way no need any tricky code for that.

Using the last line of an inline element as the width for :after pseudo element

I'm facing an issue that I'm struggling to overcome.
This is best demonstrated with an example I guess:
As a fiddle: http://jsfiddle.net/wchv2hmn/3/
In the browser:
div {
background-color: blue;
text-align: center;
}
span {
position: relative;
background-color: green;
}
span:after {
content: '';
display: block;
position: absolute;
left: 0;
right: 0;
height: 10px;
width: 100%;
background-color: red;
}
<div>
<span>
htejuteujetjtjehrehreheherhrehrehre sghosgjosjoskoskfosjofshohofshofusofhrehrhrehehhrehrherheheuorfos
</span>
</div>
I need the :after pseudo element to take on the width of the last line of text within the <span>, not the first.
Adding inline-block to the span, results in the text just being displayed as a block level element, as seen here in chrome and Firefox 39:
div {
background-color: blue;
text-align: center;
}
span {
position: relative;
background-color: green;
display:inline-block;
}
span:after {
content: '';
display: block;
position: absolute;
left: 0;
right: 0;
height: 10px;
width: 100%;
background-color: red;
}
<div>
<span>
htejuteujetjthehtehtehethetje sghosgjosjoskoskfosjofshohofshofusethehetthehehterfos
</span>
</div>
It's as if the <span>s min-width is set to the length the <span> will occupy when all the text fits on one line. So when the window shrinks, and the text splits to occupy two or more lines, the width can't shrink any smaller than it's assumed min-width...
Does anyone have any ideas? Preferably without having to alter the DOM, although it can be done if absolutely necessary.
You need to set the span as block element.
Html
<div>
<span>
htejuteujetjtje sghosgjosjoskoskfosjofshohofshofusofuorfos
</span>
</div>
CSS
div {
background-color: blue;
}
span {
position: relative;
background-color: green;
}
span{display:block;}
span:after {
content: '';
display: block;
height: 10px;
width: 100%;
background-color: red;
position: absolute;
}
Add display:inline-block to span style
span {
position: relative;
background-color: green;
display:inline-block;
}
Result:

CSS responsive slanted edge

I'm creating a basic webpage, but for the footer there is going to be a slanted edge that will run at the bottom of the page. Where I am having issues as you are unable to add 100% on a border, as i am using bootstrap, so the page must be responsive. Is there a way to achieve this affect whilst being responsive.
div {
width:200px;
height:80px;
background: red;
top:150px;left:100px;
position: relative;
}
div:before {
content: '';
position: absolute;
top: 40px; right: 0;
border-right: 200px solid white;
border-top: 40px solid red;
width: 20;
}
http://jsfiddle.net/2bZAW/3675/
This should work for you. Again, I've used a pseudo element in order to alter the color, but the general consensus is the same. Both the linked question and this use overflow:hidden on the parent and hence won't scroll. As for rotating, since I've used a pseudo element, this should not be an issue:
.wrap {
height: 500px;
width: 100%;
display: inline-block;
position: relative;
overflow: hidden;
z-index: 8;
}
.wrap:after {
content: "";
position: absolute;
height: 130%;
width: 100%;
transform: skewY(-4deg);
background: tomato;
top: -50%;
z-index: -2;
left: 0;
}
.lower {
position: absolute;
bottom: 15%;
right: 0;
}
<div class="wrap">
Hello, World!
<div class="lower">Wanted text here?</div>
</div>

Before and After Pseudo Elements

JS Fiddle
I have a div:
<div id="strip"></div>
With the CSS:
width: 100%;
height: 130px;
background: grey;
I want a pseudo element after it and to be at the base of the strip.
#strip:after {
content: "";
display: block;
width: 100%;
height: 10px;
background: blue;
}
I also have a pseudo element before the strip.
#strip:before {
content: "";
display: block;
width: 100%;
height: 10px;
background: yellow;
}
The problem is, the after pseudo element does not sit at the bottom of the strip div. Where am I going wrong. Please note I have simplified the question, I know there are alternative ways to get strips of color at the top and bottom of a div.
The CSS specification says :
The :before and :after pseudo-elements interact with other boxes as if they were real elements inserted just inside their associated element.
In order to solve your issue, you can use the solution suggested by Nico O. The after pseudo-element is absolute positionned at the bottom of the main element.
#strip{
width: 100%;
height: 130px;
background: grey;
position: relative;
padding-bottom: 10px;
}
#strip:after {
content: "";
display: block;
width: 100%;
height: 10px;
background: blue;
position: absolute;
bottom:0;
}
#strip:before {
content: "";
display: block;
width: 100%;
height: 10px;
background: yellow;
}