How to centre text as CSS content attribute vertically and horizontally? - css

I would like to center text as CSS3 content attr vertically and horizontally. Centering should have no issues on screen resize.
Please inspect the code at: http://jsfiddle.net/epomschar/3pb3swwe/
<div class="container"></div>
.container {
position: relative;
height: 200px;
width: 200px;
}
.container:after {
position: absolute;
content: 'Center me!';
top: 50%;
right: 0;
bottom: 0;
left: 0;
text-align: center;
transform: translateY(-50%);
}
.container:before {
position: absolute;
content: '';
top: 0;
right: 0;
bottom: 0;
left: 0;
color: white;
background-color: yellow;
border-radius: 50%;
width: 100%;
height: 100%;
}
Solution: http://jsfiddle.net/epomschar/3pb3swwe/7/

Just remove bottom: 0; and you already got the solution.
.container {
position: relative;
height: 200px;
width: 200px;
}
.container:after {
position: absolute;
content: 'Center me!';
top: 50%;
right: 0;
/* bottom: 0; REMOVE THIS LINE */
left: 0;
text-align: center;
transform: translateY(-50%);
}
.container:before {
position: absolute;
content: '';
top: 0;
right: 0;
bottom: 0;
left: 0;
color: white;
background-color: yellow;
border-radius: 50%;
width: 100%;
height: 100%;
}
<div class="container"></div>

Just add a padding to &:after.If you want to change more, you simply add padding-left,padding-right and change the position as you want.This one padding attribute make changes to all over your circle,you can specify changes like what i said you before by using padding-left etc.
.container {
position: relative;
height: 200px;
width: 200px;
&:after {
position: absolute;
content: 'Center me!';
top: 50%;
right: 0;
bottom: 0;
left: 0;
text-align: center;
transform: translateY(-50%);
padding:30px; //MODIFICATION
}
&:before {
position: absolute;
content: '';
top: 0;
right: 0;
bottom: 0;
left: 0;
color: white;
background-color: yellow;
border-radius: 50%;
width: 100%;
height: 100%;
}
}
Else you can add transform: translateY(-30%); and margin:5%; instead of padding:30%;

Do you mean like this? The plus is right in the centre.
.container {
position: relative;
height: 200px;
width: 200px;
}
.container:after {
position: absolute;
content: '+';
color: white;
width: 100%;
font-size: 34px;
top: 50%;
text-align: center;
transform: translateY(-50%);
}
.container:before {
position: absolute;
content: '';
background-color: black;
border-radius: 50%;
width: 100%;
height: 100%;
}
<div class="container"></div>

Related

Border-radius not working during CSS transform

Trying to make a rounded menu background. But the border-radius is not working while closing
var menuButton = document.querySelector('.btn-menu');
menuButton.addEventListener('click', function(e){
e.preventDefault();
document.body.classList.toggle('menu-open');
});
.btn-menu{
width: 30px;
height: 30px;
border: 2px solid red;
position: relative;
z-index: 5;
float: right;
}
.menu-bg {
position: fixed;
left: 0;
top: 0;
bottom: 0;
width: 100%;
z-index: 40;
pointer-events: none;
z-index: 1;
}
.menu-bg:before {
content: '';
height: 1px;
width: 1px;
background: #000;
position: fixed;
right: 30px;
top: 30px;
transition: all ease .8s;
border-radius: 50%;
transform: scale(1);
overflow:hidden;
}
.menu-open .menu-bg:before {
transform: scale(500);
}
<div class="btn-menu"><span>Menu</span></div>
<div class="menu-bg"></div>
JSFiddle - https://jsfiddle.net/afelixj/ew7b065h/
1px as width/height is not a good idea, I would do it differently and start at scale(0):
var menuButton = document.querySelector('.btn-menu');
menuButton.addEventListener('click', function(e) {
e.preventDefault();
document.body.classList.toggle('menu-open');
});
.btn-menu {
width: 30px;
height: 30px;
border: 2px solid red;
position: relative;
z-index: 5;
float: right;
}
.menu-bg {
position: fixed;
left: 0;
top: 0;
bottom: 0;
width: 100%;
z-index: 40;
pointer-events: none;
z-index: 1;
}
.menu-bg:before {
content: '';
height: 100px;
width: 100px;
background: #000;
position: fixed;
right: -20px;
top: -20px;
transition: all ease .8s;
border-radius: 50%;
transform: scale(0);
overflow: hidden;
}
.menu-open .menu-bg:before {
transform: scale(5);
}
<div class="btn-menu"><span>Menu</span></div>
<div class="menu-bg"></div>
It's a browser bug. Sometimes it works fine and then, if you change window width, it will start messing up (I saw the problem sometimes opening the menu up).
There's a known problem using transform on fixed elements: link You should try to avoid it.
In your case, insteed of transform you could just change your width, height and position to make it work as you may desire.
As an example:
var menuButton = document.querySelector('.btn-menu');
menuButton.addEventListener('click', function(e){
e.preventDefault();
document.body.classList.toggle('menu-open');
});
.btn-menu{
width: 30px;
height: 30px;
border: 2px solid red;
position: relative;
z-index: 5;
float: right;
}
.menu-bg {
position: fixed;
left: 0;
top: 0;
bottom: 0;
width: 100%;
z-index: 40;
pointer-events: none;
z-index: 1;
}
.menu-bg:before {
content: '';
height: 1px;
width: 1px;
background: #000;
position: fixed;
right: 30px;
top: 30px;
transition: all ease .3s;
transform: scale(1);
border-radius: 50%;
}
.menu-open .menu-bg:before {
transition: all ease .6s;
height: 400px;
width: 400px;
right: -90px;
top: -90px;
border-radius: 50%;
}
<div class="btn-menu"><span></span></div>
<div class="menu-bg"></div>

How do I fade out a background image in a gradient so that the elements beneath it are seen?

Let's say I have a div with a background image of a kitten. How can I fade out the the background image in a gradient pattern, without fading out the contents of the div?
I also want to make sure that whatever elements are beneath are seen through the fading out background image, I don't want it to just fade to white.
Example:
Here is some example HTML code, how would I fade out the background image of the element with the class fade-out-background?
<div style="background-image: url('https://i.stack.imgur.com/jypCc.jpg')">
<div class="fade-out-background" style="background-image: url('https://i.stack.imgur.com/BCbFO.jpg')">
This text should not fade out with the background image.
</div>
</div>
Here's what I what the end-result to look like:
Resources: link to kitten image and link to pattern image.
It's possible but it is hacky as hell:
html:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="text/css" href="css/main.css" rel="stylesheet">
</head>
<body><div class="whole-page">
<div class="content-area-bg1"></div>
<div class="content-area-bg2"></div>
<div class="content-area-bg3"></div>
<div class="content-area-bg4"></div>
<div class="content-area-bg5"></div>
<div class="content-area-bg6"></div>
<div class="content-area-bg7"></div>
<div class="content-area-bg8"></div>
<div class="content-area-bg9"></div>
<div class="content-area-bg10"></div>
<div class="content-area"><h1>This text should<br>not fade out with<br>the background<br>image</h1></div>
</div>
css:
:root{
--textcolor-1: #fff;
}
html{
font-size: 62.5%;
box-sizing: border-box;
}
*{
margin: 0;
padding: 0;
}
*, *::before, *::after{
box-sizing: inherit;
}
body {
background-image: url("../kittenbg.jpg");
font-family: 'Open Sans', sans-serif;
font-weight: 400;
line-height: 1.6rem;
font-size: 1.25rem;
color: var(--textcolor-1);
}
.whole-page{
width: 100%;
margin: 0;
padding: 0;
}
.header{
height: 7rem;
text-align: center;
}
h1 {
font-size: 4rem;
line-height: 4.8rem;
}
.content-area-bg1
{
width: 408px;
height: 287px;
display: block;
position: relative;
}
.content-area-bg1::after {
content: "";
background: url("../kitten.jpg");
opacity: 0;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -10;
}
.content-area-bg2
{
width: 408px;
height: 260px;
top: -287;
display: block;
position: relative;
}
.content-area-bg2::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -9;
}
.content-area-bg3
{
width: 408px;
height: 230px;
top: -547;
display: block;
position: relative;
}
.content-area-bg3::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -8;
}
.content-area-bg4
{
width: 408px;
height: 200px;
top: -777;
display: block;
position: relative;
}
.content-area-bg4::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -7;
}
.content-area-bg5
{
width: 408px;
height: 170px;
top: -977;
display: block;
position: relative;
}
.content-area-bg5::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -6;
}
.content-area-bg6
{
width: 408px;
height: 140px;
top: -1147;
display: block;
position: relative;
}
.content-area-bg6::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -5;
}
.content-area-bg7
{
width: 408px;
height: 110px;
top: -1287;
display: block;
position: relative;
}
.content-area-bg7::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -4;
}
.content-area-bg8
{
width: 408px;
height: 80px;
top: -1397;
display: block;
position: relative;
}
.content-area-bg8::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -3;
}
.content-area-bg9
{
width: 408px;
height: 50px;
top: -1477;
display: block;
position: relative;
}
.content-area-bg9::after {
content: "";
background: url("../kitten.jpg");
opacity: .25;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -2;
}
.content-area-bg10
{
width: 408px;
height: 20px;
top: -1527;
display: block;
position: relative;
}
.content-area-bg10::after {
content: "";
background: url("../kitten.jpg");
opacity: 1;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
.content-area
{
width: 408px;
height: 287px;
top: -1547;
display: block;
position: relative;
padding: 1rem;
}

How to Create Circle on corner of line in css

.line-8 {
width: 100px;
height: 1px;
background-color: orange;
position: relative;
left: 240px;
top: 15px;
margin-bottom: 35px;
}
How to make look like this
Try This:
.line-8 {
width: 100px;
height: 1px;
background-color: orange;
position:relative;
left: 240px;
top: 15px;
margin-bottom:35px;
}
.line-8:after {
position: absolute;
content: '';
width: 10px;
height: 10px;
border-radius: 100%;
background-color: orange;
right: 0;
top: 50%;
transform: translateY(-50%);
}
<div class="line-8"></div>

z-index and position relative

I have a modal, that is show/hide using JavaScript.
In the modal an image will be inserted using JavaScript. Also over the image a div element will exist that will simulate cropping (get the coordinates of the image).
I have a problem making the image to stay below the modal-crop.
modal-crop and the image need to be in the center of modal-area.
I can't use grid or flex because I need to support IE9.
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
display: none;
overflow: hidden;
outline: 0;
}
.modal-area {
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
transform: translate(-50%, -50%);
margin: 0 auto;
padding: 30px;
background-color: blueviolet;
border-radius: 4px;
box-shadow: 0 0 50px black;
overflow: hidden;
}
.modal-area img {
margin-left: auto;
margin-right: auto;
}
.modal-crop {
position: relative;
background-color: aliceblue;
left: 0;
right: 0;
top: 0;
margin-left: auto;
margin-right: auto;
width: 200px;
height: 200px;
opacity: 0.2;
z-index: 2;
}
<div class="modal">
<div class="modal-area">
<div class="modal-crop"></div>
#img will be inserted here using Javascript#
</div>
</div>
Your image need to be positionned absolutely like this :
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
overflow: hidden;
outline: 0;
min-height: 300px;
}
.modal-area {
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
transform: translate(-50%, -50%);
margin: 0 auto;
padding: 30px;
background-color: blueviolet;
border-radius: 4px;
box-shadow: 0 0 50px black;
overflow: hidden;
}
.modal-area img {
margin-left: auto;
margin-right: auto;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
.modal-crop {
position: relative;
background-color: aliceblue;
left: 0;
right: 0;
top: 0;
margin-left: auto;
margin-right: auto;
height: 100%;
opacity: 0.2;
z-index: 2;
}
<div class="modal">
<div class="modal-area">
<div class="modal-crop"></div>
<img src="https://lorempixel.com/500/500/">
</div>
</div>
I think I got an understanding of what you want to achieve. And I also know that it can be a pain to overlap elements with absolute positioning in multiple layers (even more so when it comes to different browsers).
I recommend you to use a grid layout, which is quite easy to set up:
HTML
<div class="modal">
<div class="modal-area">
<img src="http://via.placeholder.com/200x200">
<div class="modal-crop"></div>
</div>
</div>
CSS
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
display: block;
overflow: hidden;
outline: 0;
}
.modal-area {
display:grid;
grid-template-rows: auto;
grid-template-columns: auto;
justify-items:center;
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
transform: translate(-50%, -50%);
margin: 0 auto;
padding: 30px;
background-color: blueviolet;
border-radius: 4px;
box-shadow: 0 0 50px black;
overflow: hidden;
}
.modal-area img {
grid-row: 1/span 1;
grid-column: 1/span 1;
z-index:1;
}
.modal-crop {
width: 200px;
height: 200px;
grid-row: 1/span 1;
grid-column: 1/span 1;
background-color: red;
opacity: 0.2;
z-index:2;
}
Be aware though, that in this solution the "modal crop" needs to be set to width and and height of the image. But I used similar solutions in other situations and will not be hard to adjust it so that it works for any image sizes.
Have a fiddle: https://jsfiddle.net/9odLnh7r/
The question is note very clear, but as far as I understand you need to add this css ruleset to your image:
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
/* display: none; */
overflow: hidden;
outline: 0;
}
.modal-area {
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
transform: translate(-50%, -50%);
margin: 0 auto;
padding: 30px;
background-color: blueviolet;
border-radius: 4px;
box-shadow: 0 0 50px black;
overflow: hidden;
}
.modal-area img {
margin-left: auto;
margin-right: auto;
}
.modal-crop {
position: relative;
background-color: aliceblue;
left: 0;
right: 0;
top: 0;
margin-left: auto;
margin-right: auto;
width: 200px;
height: 200px;
opacity: 0.2;
z-index: 2;
}
.centerMe {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="modal">
<div class="modal-area">
<div class="modal-crop"></div>
<img class="centerMe" src="http://www.budgetstockphoto.com/bamers/stock_photo_spectrum.jpg">
</div>
</div>

Given three fixed elements, make them render in a specific order

If I have three divs, each with fixed position. How can I get .inner to appear above the .overlay?
HTML
<div class="container">
<div class="inner">The inner container</div>
</div>
<div class="overlay"></div>
CSS
.container {
position: fixed;
z-index: 1;
background: red;
height: 100px;
width: 100%;
}
.inner {
z-index: 3;
position: fixed;
margin-top: 10px;
width: 100%;
background: yellow;
height: 30px;
}
.overlay {
z-index: 2;
position: fixed;
background: blue;
opacity: 0.5;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
In this JS fiddle, you can see how the "yellow" element renders below the overlay. Is there any change possible while keeping the .container fixed?
http://jsfiddle.net/4ne83oa4/8/
Well, if you must keep the markup as is, you can just play around with some pseudo classes for the .container class.
Markup stays the same, the CSS chages a bit like this: check js fiddle
.container {
position: fixed;
background: red;
height: 100px;
width: 100%;
z-index: 1;
}
.container:after,
.container:before{
content: '';
position: fixed;
}
.container:after{
z-index: -1;
background: red;
height: 100px;
width: 100%;
}
.container:before{
z-index: 1;
background: blue;
opacity: 0.5;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.inner {
position: fixed;
margin-top: 10px;
width: 100%;
background: yellow;
height: 30px;
z-index: 1;
}

Resources