How to make my picture both vertical and horizontal center? - css

I was using react.js to build a template website.
When I build the first component, I met the problem.
the problem:
the picture is not in the middle of the page(I mean in vertical and in horizontal)
I have already use CSS flex to make it in the middle.
Why my picture's position is not in the middle of the page?
Just put that picture in that circle.
How to do that?
Here is my Center.js:
import React from 'react';
import image from './images/5.png'
import './Center.css';
const Center = () =>{
return(
<div id="center2">
<img id="center" alt="center" src={image}/>
</div>
);
}
export default Center
my app.js:
import React, { Component } from 'react';
import Center from './Components/Center';
class App extends Component {
render() {
return (
<div>
<Center/>
</div>
);
}
}
export default App;
my center.css:
#center2{
text-align:center;
}
#center{
width:300px;
height:300px;
}
my webpage on localhost 3000

If they are static you need to import it:
import image from './images/5.png'
return (
<div id='center'>
<img src={image} />
</div>
)
otherwise you need to move images folder to public directory, and access it by
<div id='center'>
<img src='./images/5.png' />
</div>
And this is the CSS to make that image on center:
#center2 {
display: flex;
justify-content: center;
align-items: center;
}

Related

Unable to alter CSS styles by id

I have a very simple div in my footer:
import React from "react";
import { useHistory } from "react-router-dom";
import style from "./DesktopFooter.module.css";
const DesktopFooter = () => {
const history = useHistory();
return (
<div className={style.container}>
<div className={style.footerNav} id="phoneNumber">
999-999-9999
</div>
<div className={style.footerNav}>
</div>
<div className={style.footerNav}></div>
</div>
);
};
export default DesktopFooter;
In my CSS I want to style both on the class and id:
.footerNav {
width: 50%;
display: flex;
}
#phoneNumber {
font-size: 2rem;
}
However my component is not recognizing the id styling I try to apply.
Could anyone point me in the right direction.
Ya, After you update a question, I found the issue, you are try to load style for id as normal Dom, but its will not work since you are not include css file as is, you are import style file to style param...
so, what you need to do is replace id="PhoneNumber" to this
<div className={styles["footerNav"]} id={styles["phoneNumber"]}>
999-999-9999
</div>
Check the demo url
Full Code:
import React from "react";
import style from "./MyComponent.module.css";
export default function App() {
return (
<div className={style.container}>
<div className={style["footerNav"]} id={style["phoneNumber"]}>
999-999-9999
</div>
<div className={style.footerNav}></div>
<div className={style.footerNav}></div>
</div>
);
}
I would suggest having it as either a class or an id. Classes usually have a higher priority, meaning that the id will be ignored.
This is what it should look like:
.phoneNumber {
font-size: 2rem;
width: 50%;
display: flex;
}
<div class = "phoneNumber">999-999-9999</div>
However, if you would like to get around this, I would use another element within the div, such as:
#myID{
font-size: 2rem;
}
.phoneNumber {
width: 50%;
display: flex;
}
<div class = "phoneNumber">
<p id="myID"> 999-999-999 </p>
</div>
Ok I found a solution, or at least a work around. The problem seems to be that the file was a css module and not just a css file.
I changed the name of the css file from DesktopFooter.module.css to DesktopFooter.css
and I changed my import statement from
import style from "./DesktopFooter.module.css" to import "./DesktopFooter.css

Import .scss file into .js file in Reactenvironment

Global.scss
/src/styles/global/Global.scss
.grid {
display: grid;
}
Meta.js
/src/components/Meta.js
This file contains the import statements for the .scss file
import "../styles/global/Global.scss";
index.js
/src/pages/index.js
This file contains the import statement for the component import Meta from '../components/Meta';
Followed by
function Home() {
return (
<div className="container">
<Meta />
<Header />
<p>Hello world!</p>
<Footer />
</div>
)
}
export default Home;
Footer.js
/src/components/Footer.js
class Footer extends React.Component {
render() {
return (
<footer className="container grid">
<div className="footer-primary-nav">
<p>Primary Nav</p>
</div>
</footer>
);
}
}
export default Footer;
The class .grid isn't being loaded into the Footer.js from the Global.scss file.
On paper, I thought this would be working, but the .grid styling isn't being loaded on the page.Any help would be greatly appreciated!
It turns out the server just needed to be reset / turned off. Looked at yesterday and it was working as expected.

React how to avoid css overide other components? [duplicate]

This question already has answers here:
How to make React CSS import component-scoped?
(6 answers)
Closed 1 year ago.
i am trying to create React aplication which uses AG-Grid table. Previously I customized table and removed borders of the ag grid by overiding css. I used this code to do so.
.ag-root-wrapper {
border: solid 0px !important;
border-color: var(--ag-border-color, white) !important;
}
But i noticed that code which i used only for one component has influenced whole aplication.
I was curious maybe is there way to locally import css only for specific component?
This is the way i was importing css for component
import './Statistics.css'
import { AgGridColumn, AgGridReact } from 'ag-grid-react';
const Statistics = () => {
return (
<div className='content'>
<div className='content-left'>
<div className="ag-theme-alpine">
<AgGridReact
rowData={data}
style={{borderColor: 'red'}}
rowSelection="single"
filter={false}
>
<AgGridColumn
headerName="Date"
field="date"
resizable={true}
filter={false}
width={130}
flex={1}
sort={'asc'}
cellStyle={{fontSize: '1vw'}}
/>
</AgGridReact>
</div>
</div>
</div>
)}
export default Content;
When you use pure css or css preprocessors (Sass, less..), the imported css will always be global to your app. It is really useful to scope every component in a unique classname so you can scope css.
import './Statistics.css'
import { AgGridColumn, AgGridReact } from 'ag-grid-react';
const Statistics = () => {
return (
+ <div className='root-statistics'>
<div className='content'>
<div className='content-left'>
<div className="ag-theme-alpine">
<AgGridReact
rowData={data}
style={{borderColor: 'red'}}
rowSelection="single"
filter={false}
>
<AgGridColumn
headerName="Date"
field="date"
resizable={true}
filter={false}
width={130}
flex={1}
sort={'asc'}
cellStyle={{fontSize: '1vw'}}
/>
</AgGridReact>
</div>
</div>
</div>
+ </div>
)}
export default Content;
.root-statistics .ag-root-wrapper {
border: solid 0px !important;
border-color: var(--ag-border-color, white) !important;
}

How to center an ngx-bootstrap modal

Trying to center this ngx-boostrap modal using CSS like this but it's not working:
.modal.fade.in{
display: flex;
justify-content: center;
align-items: center;
}
But in the dev tool, I'm able to add CSS like this:
.modal.dialog{
top: 50%
}
So at least it is centered vertically, but this is in the dev tool, and there is no .modal.dialogclass in the html template
Is there a way to center properly the ngx-bootstrap modal ?
I want to create a generic modal component to use it anywhere, by providing an input message and adding a yes/no dialog and output the user choice (using EventEmitter)
I've found an example in the following Plunker, but not able to reproduce it in a separate custom component.
The plunker example comes from this website: https://github.com/valor-software/ngx-bootstrap/issues/2334
Update:
After #Wira Xie answer, when I use the Static modal and this CSS:
.modal-sm
{
top: 50%;
left: 50%;
width:30em;
height:18em;
background-color: rgba(0,0,0,0.5);
}
The modal shows centered, but only the Esc key can hide it, so when I click outside the modal, it's still visible.
Why not to use bootstrap modal-dialog-centered class:
this.modalRef2 = this.modalService.show(ModalContentComponent,
{
class: 'modal-dialog-centered', initialState
}
);
in the .ts file you have a code like this (to open modal popup)...
private showModal(template: TemplateRef<any>): BsModalRef {
return this.modalService.show(
template,
{ class: 'modal-lg modal-dialog-centered',
ignoreBackdropClick: true,
keyboard: false
});
}
You can see that I've added modal-dialog-centered to the class. after doing this you can also modify the modal-dialog-centered class in your css.
Try adding this attribute to your CSS: vertical-align: middle to your .modal.dialog class
Plunker for modal
.modal.fade {
display: flex !important;
justify-content: center;
align-items: center;
}
.modal-dialog {
vertical-align:middle;
height:18em;
background-color: rgba(0,0,0,0.5);
}
You need to use the bootstrap class.
Add .modal-dialog-centered to .modal-dialog to vertically center the modal.
import { Component, OnInit, TemplateRef } from '#angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
#Component({
...
})
export class ModalComponent {
modalRef: BsModalRef;
// Here we add another class to our (.modal.dialog)
// and we need to pass this config when open our modal
config = {
class: 'modal-dialog-centered'
}
constructor(private modalService: BsModalService) { }
openModal(template: TemplateRef<any>) {
// pass the config as second param
this.modalRef = this.modalService.show(template, this.config);
}
}
You have to pass the modal-dialog-centered bootstrap class for the BsModalService.show() function like this:
const initialState = {
organization: organization,
};
this.modalRef = this.modalService.show(AdminOrganizationCreateComponent, {
class: 'modal-dialog-centered', initialState
});
Note: if you need to pass initial data you can pass it using the initalState object.
A bit late, but for reference.
The reason the styling of the dialog in the component is not working, is because component styling is isolated and restricted to elements within the component. The dialog created through the bsmodalservice is outside of this component. (directly under <body>).
So, while your styling is encapsulated in the component and some random identifier, the dialog itself is not. The final css (like mycomponent[_ngcontent_bla_323] .modal) does not apply to the div.modal that is added by the service.
Possible solutions are:
Move your css for this dialog to some global (s)css file instead of the (s)css for the component
Instead of using bsModalService with a template, use the bsmodal directive with a div within your component. That way the component local (s)css will apply.
I have added mat-step inside the modal and because of that it didn't align centered for modal-dialog-centered bootstrap class ether. But modal-lg class worked for me. Sample code is very similar to the accepted answer. Only change the bootstrap class which is passed to the modal.
this.modalRef = this.modalService.show(AddDevelopersGroupComponent,{
class: 'modal-lg',
initialState,
});
This is a snippet from my personal project using ngx-bootstrap too.
.modal-sm
{
top: 50%;
left: 50%;
width:30em;
height:18em;
margin-top: -9em;
margin-left: -15em;
background-color: #001b00;
/*position:fixed;*/
}
<!--typecript files-->
<script>
//here is the typesciprt file
export class AppComponent
{
//for default ngx-bootstrap modal
public modalRef: BsModalRef;
constructor(private modalService: BsModalService) {}
public openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template);
}
}
</script>
<!--end of ts file-->
<!--Modal-->
<div class="container">
<div bsModal #smModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left">Small modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="smModal.hide()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
</div>
</div>
</div>
<!--enf of modal-->
According to the documentation you can center the modal vertically via setting centered property to true (as it is false by default)
const modalRef = this.modalService.open(ConfirmationDialogComponent, {
size: dialogSize,
centered: true
});

Any way to anchor a footer section in React?

I created a wrapper for my app in React. I am trying to get the footer to stay at the absolute bottom of the page but when the children render, the footer stays at the original bottom of the page and does not resize with the page. Code below. (imagine a scrollable page with a footer in the middle now after child renders).
import React, { Component } from 'react';
import Header from "./Header";
import Footer from "./Footer";
export default class Wrapper extends Component {
render() {
return (
<div className="app-wrapper">
<Header/>
<div className="content">
{this.props.children}
</div>
<div>
<Footer/>
</div>
</div>
);
}
}
I'm not that good at CSS. Any ideas as to what I could try? I have already tried googling around for solutions to no avail.I have tried the basic solutions such as position:relative; left:0; bottom:0; right:0; position: fixed, position: absolute etc.
You should give position: relative|fixed (depending on your needs) to the parent container.
For exemple :
<div style={{ position: 'fixed', bottom: '0' }}>
<Footer />
</div>
HTML dock to bottom
sorry for the confusion guys. I had the style redefined in my css file so my styles were being overridden. :(

Resources