'Put the cross on the top right page instead of the left
how can I put the cross on the top right side of my page? For now it appears on the left side.
.close {
vertical-align: middle;
border: none;
color: inherit;
border-radius: 50%;
background: transparent;
position: relative;
width: 32px;
height: 32px;
opacity: 0.6;
}
.close:focus,
.close:hover {
opacity: 1;
background: rgba(128, 128, 128, 0.5);
}
.close:active {
background: rgba(128, 128, 128, 0.9);
}
.close::before,
.close::after {
content: " ";
position: absolute;
top: 50%;
left: 50%;
height: 20px;
width: 4px;
background-color: currentColor;
}
.close::before {
transform: translate(-50%, -50%) rotate(45deg);
}
.close::after {
transform: translate(-50%, -50%) rotate(-45deg);
}
My snipped code :https://jsfiddle.net/Lcqy3srg/3/
Solution 1:[1]
float
Use float: right on .close
/*Button to quit the form*/
.close {
vertical-align: middle;
border: none;
color: inherit;
border-radius: 50%;
background: transparent;
position: relative;
width: 20px;
height: 20px;
opacity: 0.6;
float: right;
}
.close:focus,
.close:hover {
opacity: 1;
background: rgba(128, 128, 128, 0.5);
}
.close:active {
background: rgba(128, 128, 128, 0.9);
}
/* tines of the X */
.close::before,
.close::after {
content: " ";
position: absolute;
top: 50%;
right: 0;
height: 20px;
width: 3px;
background-color: currentColor;
}
.close::before {
transform: translate(-50%, -50%) rotate(45deg);
}
.close::after {
transform: translate(-50%, -50%) rotate(-45deg);
}
<div>
<button class="close" aria-label="Close"></button>
</div>
position
Using position: absolute and right: 0px will result in the same effect. It however requires more work, and might not work in many cases where you need to use a position other than absolute. (for example in this case, it gives a scroll bar because it was intended to use relative)
/*Button to quit the form*/
.close {
vertical-align: middle;
border: none;
color: inherit;
border-radius: 50%;
background: transparent;
position: absolute; /*Changed to absolute*/
width: 20px;
height: 20px;
opacity: 0.6;
right: 0px;
}
.close:focus,
.close:hover {
opacity: 1;
background: rgba(128, 128, 128, 0.5);
}
.close:active {
background: rgba(128, 128, 128, 0.9);
}
/* tines of the X */
.close::before,
.close::after {
content: " ";
position: absolute;
top: 50%;
right: 0;
height: 20px;
width: 3px;
background-color: currentColor;
}
.close::before {
transform: translate(-50%, -50%) rotate(45deg);
}
.close::after {
transform: translate(-50%, -50%) rotate(-45deg);
}
<div>
<button class="close" aria-label="Close"></button>
</div>
Solution 2:[2]
you simply can add div "content" as parent of div "close"
.content {
position:absolute ;
top:0;
right:0;
}
Code screenshot:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | RBT |

