In the code below, I want to add a line after the div. I have used :after but the line comes before the div. Please help me on this.
div {
background-color: red;
width: 300px;
height: 300px;
}
div:after {
content: "";
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
display: block;
}
<div></div>
You can set this using position
div {
background-color: red;
width: 300px;
height: 300px;
position: relative;
}
div:after {
content: "";
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
<div></div>
The name ::after for the pseudo-element can be a little misleading. It doesn't come after the element the selector is targeting, but inside it, after all the other content.
So your code is equivalent to this:
#div1 {
background-color: red;
width: 300px;
height: 300px;
}
#div2 {
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
display: block;
}
<div id="div1">
<div id="div2"></div>
</div>
(and because your div has no content, it doesn't matter whether you use ::before or ::after).
So you either have to use positioning to move the blue line to be (visually) after the div, or you could apply your ::after to the parent element of the div, which may or may not do what you want:
#inner {
background-color: red;
width: 300px;
height: 300px;
}
#outer:after {
content: "";
background-color: blue;
height: 2px;
width: 120px;
margin: 24px auto 0;
display: block;
}
<div id="outer">
<div id="inner"></div>
</div>
You can set line by using below code
<style type="text/css">
div {
background-color: red;
width: 300px;
height: 300px;
position: relative;
}
div::after {
content: " ";
background-color: blue;
display: block;
position: absolute;
height: 2px;
width: 120px;
left: 100%;
}
</style>
<div></div>
you can make the margin-top property of the after selector equal to your required div height,and add float:left;
div {
background-color: red;
width: 300px;
height: 300px;
float:left;
}
div:after {
content: '';
background-color: blue;
height: 2px;
width: 120px;
margin: 300px auto 0;
display: block;
}
<div></div>
Related
I'm trying to make a JavaScript piano, with the keys being at the bottom of the screen, but setting bottom: 0px; doesn't work
html {
height: 100%;
width: 100%;
position: relative;
}
/*keyboard div*/
#keyboard {
position: relative;
display: table;
width: 1366px;
height: 90px;
bottom: 0px;
}
/*keys, if those are important*/
#wk,
#bk {
display: table-cell;
border-color: #000000;
border-width: 1px;
border-style: solid;
}
#wk {
position: relative;
height: 90px;
width: 1.92%;
background-color: #ffffff;
}
#bk {
z-index: 1;
position: absolute;
height: 52px;
width: 58.05%;
right: -9.04px;
background-color: #000000;
}
<html>
<div id="keyboard">
<div id='wk'>
<div id='bk'></div>
</div>
<div id='wk' class=''>
</div>
</div>
Replace your styles as given below:
#keyboard {
height: 90px;
position: fixed;
bottom: 0;
width: 100%;
background-color:#0000FF;
}
/*keys, if those are important*/
#wk, #bk {
border-color:#000000;
border-width: 1px;
border-style: solid;
}
#wk {
height:90px;
width: 1.92%;
background-color: #ffffff;
}
#bk {
height:52px;
width: 58.05%;
right: -9.04px;
background-color: #000000;
}
Dont set bottom since that will move it from down to up rather set:
top: 50%;
but since your width and height are odd use 41% to place it nicely
also its a nice practice to use em/rem or simply % and not px
I have this HTML:
<div id="container">
<div id="content"></div>
<div id="close-button"></div>
</div>
and this CSS:
#container {
width: 50%;
}
#content {
width: 100%;
height: 50px;
background-color: gray;
}
#close-button {
float: right;
margin-left: 100%;
height: 50px;
width: 50px;
background-color: red;
}
JSFiddle: https://jsfiddle.net/Le53m70b/
How can I make the red box overlaid on top of the gray one, instead of being on a separate line? Note that the size of the container is not fixed, but regardless of its width, I'd like the gray box to cover 100% of it and the red box to be at its very right.
Ah, this finally works: https://jsfiddle.net/Le53m70b/1/
#container {
position: relative;
width: 50%;
}
#content {
width: 100%;
height: 50px;
background-color: gray;
}
#close-button {
position: absolute;
right: 0;
top: 0;
height: 50px;
width: 50px;
background-color: red;
}
You can use z-index property. Which is used to overlay an individual div over another div element.
#container{
width: 200px;
height: 200px;
position: relative;
margin: 20px;
}
#content{
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
opacity: 0.8;
}
#close-button{
z-index: 9;
margin: 20px;
}
Hi I've got follow div:
.box {
width: 100px;
height: 20px;
border: 1px solid black;
}
.box:after {
content: '';
width: 20px;
height: 20px;
background-color: blue;
}
<div class="box"></div>
I would like to add a second div to the .box with pseudo class after. I thought it would work like this, but nothing happens. It should look like this:
How to do this with after?
Thanks.
Try This
.box {
width: 100px;
height: 20px;
border: 1px solid black;
}
.box:after {
content: '';
width: 20px;
height: 20px;
background-color: blue;
display:block;
float:right;
}
<div class="box"></div>
You're code is right, the only thing missing is the property display to that element. Just add a display: block on the :after element. To easily manipulate the pseudo-element, make the main element position: relative, then the :after as position: absolute and place it based on the .box div, something like this :
.box {
width: 100px;
height: 20px;
border: 1px solid black;
position: relative; /* Made it relative */
}
.box:after {
content: '';
position: absolute;
display: block;
width: 20px;
height: 20px;
border: 1px solid blue;
top: -1px; /* To compensate the border on the main element */
background-color: blue;
left: 100%; /* To place it after the main element */
}
If you truly need another div, try adding some javascript, like this
$(document).ready(function() {
$('.box').append('<div class="another-box"></div>');
});
.box {
width: 100px;
height: 20px;
position: relative;
border: 1px solid black;
}
.box:after {
content: '';
width: 20px;
bottom: -1px;
right: -21px;
top: -1px;
position: absolute;
background-color: blue;
}
<div class="box"></div>
On the link there is an example; I have three elements, in this case, the body with the a universe background, the div, with a white background and an img where in the middle there is a hole. I want to see the first UNIVERSE background inside the heart shape and not the second/WHITE.
https://jsfiddle.net/adrianvcch/t053p4hb/
html {
background-color: black;
}
body {
margin: 50px;
background-color: white;
height: 500px;
}
.heart {
position: relative;
width: 500px;
height: 200px;
margin: 0 auto;
overflow: hidden;
}
<div class="heart">
<img src="http://s3.postimg.org/cqraf51bn/heart.png" />
</div>
CSS Masking
Mask # MDN
html {
background-image: url(http://scienceblogs.com/startswithabang/files/2013/02/2xcluster.jpg);
}
body {
margin: 50px;
text-align: center;
}
.heart {
position: relative;
display: inline-block;
height: 200px;
background: white;
-webkit-mask: url(http://s3.postimg.org/cqraf51bn/heart.png);
mask: url(http://s3.postimg.org/cqraf51bn/heart.png);
}
<div class="heart">
<img src="http://s3.postimg.org/cqraf51bn/heart.png" />
</div>
EDIT:
USING AN IMAGE:
Since the real case needs to use an image here's what can be done:
CODE SNIPPET:
body {
margin: 50px;
background-color: white;
height: 500px;
}
html,
.heart {
background-color: black;
}
.heart {
position: relative;
max-height: 200px;
margin: 0 auto;
overflow: hidden;
display: inline-block;
}
<div class="heart">
<img src="http://s3.postimg.org/cqraf51bn/heart.png" />
</div>
SOLUTION:
USING A CSS SHAPE:
Here's something you could try:
Use a heart shape with plain CSS.
Set the same background-color in your html and heart with multiple selectors separated by comma using the same css property.
CODE SNIPPET:
body {
margin: 50px;
background-color: white;
height: 500px;
}
.heart {
display: inline-block;
height: 60px;
width: 60px;
margin: 0 10px;
position: relative;
top: 0;
transform: rotate(-45deg);
}
.heart:before,
.heart:after {
content: "";
border-radius: 50%;
height: 60px;
width: 60px;
position: absolute;
}
.heart:before {
left: 0;
top: -30px;
}
.heart:after {
left: 30px;
top: 0;
}
html,
.heart,
.heart:before,
.heart:after {
background-color: black;
}
.heart-wrapper {
background-color: #c95253;
padding: 105px 80px 35px 80px;
display: inline-block;
}
<div class="heart-wrapper">
<div class="heart"></div>
</div>
Add this changes into your codes, But your image should be transparent to adapt the background color.
html {
background:url(https://source.unsplash.com/category/nature);
}
body {
margin: 50px;
height: 500px;
}
.heart {
position: relative;
width: 200px;
height: 200px;
margin: 0 auto;
overflow: hidden;
}
img{
width:100%;
height:200px;
}
I'm running this JSFIDDLE and I want to set the vertical-align property to bottom for both the float and non-float elements, but it doesn't work :
Here is my html code :
<div class="main">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
CSS :
div {
border: 1px solid;
}
.main {
text-align: center;
}
.main:after {
content: '';
clear: both;
display: block;
}
.main > div {
vertical-align: bottom;
width: 20%;
}
.left {
height: 60px;
float: left;
background-color: red;
}
.right {
height: 40px;
float: right;
background-color: blue;
}
.middle {
height: 20px;
background-color: green;
display: inline-block;
}
Unless you're using with a table cells, vertical-align aligns the element with respect to adjacent elements, in particular text.
Read more about vertical alignment here:
http://phrogz.net/css/vertical-align/index.html
What you can do though is give your wrapper a width, height, and set it it to position: relative
After that you can use absolute positioning for your divs, set them to bottom: 0 they will then stick to the bottom of the wrapper.
Here is new CSS:
div {
border: 1px solid;
}
.main {
position: relative;
width: 100%;
height: 100px;
text-align: center;
}
.main:after {
content: '';
clear: both;
display: block;
}
.main > div {
width: 20%;
}
.left {
height: 60px;
background-color: red;
position: absolute;
bottom: 0;
left: 0;
}
.right {
height: 40px;
background-color: blue;
position: absolute;
bottom: 0;
right: 0;
}
.middle {
height: 20px;
background-color: green;
position: absolute;
transform: translate(-50%, 0%);
left: 50%;
bottom: 0;
}
CODEPEN DEMO