I have implemented the Angular 2 progress spinner from the below link
https://github.com/angular/material2/tree/master/src/lib/progress-spinner
I would like to have it centered, however, the only way I can seem to get it to work is to remove the
display: block
from the CSS. However, this causes the spinner to appear huge on the page.
Any advice would be great.
just add margin rule:
<md-progress-spinner style="margin:0 auto;"
mode="indeterminate"></md-progress-spinner>
plunker: http://plnkr.co/edit/sEiTZt830ZE7rqjq9YXO?p=preview
UPDATE
Just wanted to share and demonstrate 6 other general centering solutions
FLEX:
.center {
display: flex;
justify-content: center;
align-items: center;
}
/* +++++++ STYLES +++++++ */
.wrapper {
height: calc(100vh - 20px);
background: red;
}
.inner {
background: green;
color: white;
padding: 12px;
}
<div class="wrapper center">
<div class="inner">INNER CONTENT</div>
</div>
GRID:
.center {
display: grid;
place-items: center;
}
/* +++++++ STYLES +++++++ */
.wrapper {
height: calc(100vh - 20px);
background: red;
}
.inner {
background: green;
color: white;
padding: 12px;
}
<div class="wrapper center">
<div class="inner">INNER CONTENT</div>
</div>
LINE HEIGHT + TEXT ALIGN (will not work as desired for multiple lines, use white-space: nowrap; to ensure one line)
.center {
line-height: calc(100vh - 20px);
text-align: center;
}
/* +++++++ STYLES +++++++ */
.wrapper {
background: red;
white-space: nowrap;
}
.inner {
background: green;
color: white;
padding: 12px;
display: inline;
}
<div class="wrapper center">
<div class="inner">INNER CONTENT</div>
</div>
USING ABSOLUTE, TOP, LEFT and TRANSFORM TRANSLATE
.center.wrapper {
position: relative;
}
.center .inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* +++++++ STYLES +++++++ */
.wrapper {
height: calc(100vh - 20px);
background: red;
}
.inner {
background: green;
color: white;
padding: 12px;
}
<div class="wrapper center">
<div class="inner">INNER CONTENT</div>
</div>
USING ABSOLUTE, TOP, LEFT, BOTTOM, RIGHT and MARGIN AUTO (mentioned by György Balássy). Note: inner div width needs to be set.
.center.wrapper {
position: relative;
}
.center .inner {
position: absolute;
inset: 0;
margin: auto;
}
/* +++++++ STYLES +++++++ */
.wrapper {
height: calc(100vh - 20px);
background: red;
}
.inner {
background: green;
color: white;
padding: 12px;
height: max-content;
width: max-content;
}
<div class="wrapper center">
<div class="inner">INNER CONTENT</div>
</div>
Using TABLE
.center {
display: table-cell;
text-align: center;
vertical-align: middle;
}
/* +++++++ STYLES +++++++ */
.wrapper {
height: calc(100vh - 20px);
width: calc(100vw - 20px);;
background: red;
}
.inner {
background: green;
color: white;
padding: 12px;
display: inline-block;
}
<div class="wrapper center">
<div class="inner">INNER CONTENT</div>
</div>
This CodePen helped me to create a page-centered spinner with Material Design in Angular 4: https://codepen.io/MattIn4D/pen/LiKFC
Component.html:
<div class="loading-indicator">
<mat-progress-spinner mode="indeterminate" color="accent"></mat-progress-spinner>
</div>
Component.css:
/* Absolute Center Spinner */
.loading-indicator {
position: fixed;
z-index: 999;
height: 2em;
width: 2em;
overflow: show;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
/* Transparent Overlay */
.loading-indicator:before {
content: '';
display: block;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.3);
}
The first answer doesn't work unless height is set in a parent element.
I fixed it using fxFlex
<div fxLayout="row" fxLayoutAlign="space-around center" style="height:100%">
<mat-spinner diameter="50" strokeWidth="5"></mat-spinner>
</div>
I am using angular 6 with material 2+ and used that CSS code:
.mat-spinner {
position: relative;
margin-left: 50%;
margin-right: 50%;
}
Source: Angular Wiki
For me, this worked the best:
Component:
<div class="center">
<mat-spinner> </mat-spinner>
</div>
Scss:
/** Can be used to center any element */
.center {
left: 50%;
position: absolute;
top: 50%;
transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
}
you can use with grid as well :
.wrapper {
display: grid;
place-content: center;
height: calc(100vh - 20px);
background: red;
}
Related
I'd need to have two side-by-side Boxes, with background that is extended to full page width, but text content should be contained inside main container that is limited a specific size.
I tried the following HTML code:
<div class="wrapper">
<div class="container">
<section class="left">LEFT</section>
<section class="right">RIGHT</section>
</div>
</div>
and here the sCSS:
.wrapper {
background: lightgray;
padding-block: 10px;
overflow: hidden;
}
.container {
border: dotted 1px red;
display: flex;
max-width: 900px;
margin-inline: auto;
section {
padding-block: 100px;
border: solid 1px blue;
flex: 1;
position: relative;
isolation: isolate;
&.left {
background: green;
&:before {
content:"";
display: block;
background: green;
position: absolute;
top: 0;
right: 0;
height: 100%;
width: 500%;
z-index: -1;
}
}
&.right {
background: orange;
&:before {
content:"";
display: block;
background: orange;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 500%;
z-index: -1;
}
}
}
}
Here a working example. Have you any suggestion to have a better solution with almost the same result?
You could add an extra div inside your wrapper and use a linear gradient on it:
.wrapper {
background: lightgray;
padding-block: 10px;
overflow: hidden;
}
.background {
background: linear-gradient(to right, green, green 50%, orange 50%, orange 100%);
}
.container {
border: dotted 1px red;
display: flex;
max-width: 900px;
margin-inline: auto;
}
.column {
width: 50%;
}
<div class="wrapper">
<div class="background">
<div class="container">
<section class="column left">LEFT</section>
<section class="column right">RIGHT</section>
</div>
</div>
</div>
I was building a meme with top and bottom text.
I am in need of rotating an image so I did it with transform: rotate(90deg);, but it's overlapped parent's div like the following example.
h1 {
margin-top: 100px;
}
.parent {
border: 1px solid black;
display: inline-block;
position: relative;
background: #777;
}
.parent .rotate {
transform: rotate(90deg);
width: 100px;
}
.parent h4 {
position: absolute;
left: 0;
right: 0;
text-align: center;
color: white;
z-index: 1;
}
.parent .top {
top: 10px;
}
.parent .bottom {
bottom: 10px;
}
<div class="parent">
<h4 class="top">Top Text</h4>
<img class="rotate" src="http://2.bp.blogspot.com/-7uOyzdXhG2Y/UVpJUbwqGzI/AAAAAAAAAo0/35w5N8tPvHE/s640/iphone-5-hd-wallpapers-hd-41664-tpmw7.jpg" />
<h4 class="bottom">Bottom Text</h4>
</div>
How can I change the style of the parent div to match the position and size of the rotated image?
First, we need to change the height of the div to be same as width.
We can do it by
.parent{
background-color: red;
width: 100%;
padding-top: 100%; /* 1:1 Aspect Ratio */
position: relative; /* If you want text inside of it */
}
Second, we need an additional div inside it that has absolute position with full width and height of it.
We can use flex to center the image inside that absolute div.
Here is a working code.
h1 {
margin-top: 100px;
}
.container {
display: inline-block;
width: 300px;
}
.parent {
border: 1px solid black;
display: inline-block;
position: relative;
background: #777;
width: 100%;
padding-top: 100%;
position: relative;
}
.p-absolute {
bottom: 0;
position: absolute;
top: 0;
left: 0;
right: 0;
margin-top: auto;
margin-bottom: 0;
display: flex;
align-items: center;
justify-content: center;
}
.parent .rotate {
transform: rotate(90deg);
max-width: 100%;
max-height: 100%;
}
.parent h4 {
position: absolute;
left: 0;
right: 0;
text-align: center;
color: white;
z-index: 1;
}
.parent .top {
top: 10px;
}
.parent .bottom {
bottom: 10px;
}
<div class="container">
<div class="parent">
<h4 class="top">Top Text</h4>
<div class="p-absolute">
<img class="rotate" src="http://2.bp.blogspot.com/-7uOyzdXhG2Y/UVpJUbwqGzI/AAAAAAAAAo0/35w5N8tPvHE/s640/iphone-5-hd-wallpapers-hd-41664-tpmw7.jpg" />
</div>
<h4 class="bottom">Bottom Text</h4>
</div>
</div>
In React Native, I have a box with an absolute position, and I'm placing another box after it. The only way that I know how to do this in CSS is by using a pseudo element like in this fiddle and this:
CSS:
#parent {
width: 500px;
height: 500px;
background-color: white;
border: 2px solid blue;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
#parent::before {
content: '';
width: 60%;
height: 60%;
}
#div1 {
background: green;
width: 30%;
height: 30%;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
#div2 {
background: red;
width: 30%;
height: 30%;
}
HTML:
<div id="parent">
<div id="div1"></div>
<div id="div2"></div>
</div>
So how can I do the same thing in React Native? Can I have a pseudo element?
I am trying to create this layout:
LINK
I need to create 3 containers and each container will have an image as a background. Tried to do it with SVG, but it's not an option, because in future images will be changed via CMS, so I need a shape, that images can fill in. Also tried to play with the border, so I can create a shape, but it's also not working the way it looks on the image above. Is there an easier way to achieve this? Let's say using bootstrap classes?
You can do it in two ways 1)using bootstrap classes 2)using #media and for showing proper image according to div size you can use .className{background-size:contain;background-repeat:no-repeat}
You may use flex, transform and pseudo to hold backgrounds:
/* http://codepen.io/gc-nomade/pen/vGvRPZ */
html,
body {
height: 100%;
width:100%;
margin: 0;
overflow: hidden;
}
body > div {
position:relative;
min-height: 100%;
width:100%;
display: flex;
width: 160%;
margin: 0;
margin-left: -30%;
}
div div {
display: flex;
align-items: center;
transform: skew(-30deg);
overflow: hidden;
border-left: solid;
flex: 4;
position: relative;
}
div div h2 {
font-size: 5vw;
color: turquoise;
text-shadow: 0 0 1px black;
position: relative;
text-align: center;
width: 100%;
transform: skew(30deg);
}
div div:nth-child(1) h2 {
padding-left: 50%;
}
div div:nth-child(3) h2 {
padding-right: 50%;
}
div div:before {
transform: skew(30deg);
content: '';
top: 0;
bottom: 0;
right: -50%;
left: -50%;
position: absolute;
background: url(http://hd.wallpaperswide.com/thumbs/grungy_background-t2.jpg ) center tomato;
background-size: 100vw auto;
}
div div:nth-child(2):before {
background: url(http://www.intrawallpaper.com/static/images/desktop-backgrounds-8656-8993-hd-wallpapers_js7gwWA.jpg) center right gray;
background-size: 100vw auto;
}
div div:nth-child(3):before {
background: url(https://wallpaperscraft.com/image/dark_background_colorful_paint_47176_300x188.jpg) center right turquoise;
background-size: 100vw auto;
}
div div:nth-child(2) {
flex: 2.5;
}
<div>
<div>
<h2>title 1</h2>
</div>
<div>
<h2>title 1</h2>
</div>
<div>
<h2>title 1</h2>
</div>
</div>
I would like to achieve something like that: https://jsfiddle.net/svArtist/e1c2tLme, but I need a div instead of the image.
Square != rectangular
Here is the working example: JSFIDDLE
HTML:
<div id='a'>
<div id='b'>
</div>
</div>
CSS:
body {
height: 100%;
margin: 0;
}
#a {
width: 100%;
height: 100%;
}
#b {
position: absolute;
width: 100vmin;
height: 100vmin;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: teal;
color: white;
}
Updated fiddle
You can do it using vmin unit.
.container {
width: 100%;
height: 100%;
padding: 0;
box-sizing: border-box;
background-color: yellow;
text-align: center;
}
.box {
background-color: #AAAAAA;
width: 100vmin;
height: 100vmin;
margin: 0 auto;
}
<div class="container">
<div class="box">
Stuff goes here...
</div>
</div>
MDN