Unable to center block element horizontally - css

I am unable to center elements that will be a list of blog posts going forward. The Postlist element is loaded into my App.js file. I was able to center the nav bar using the technique mentioned here under the Horizontally section and under the subsection titled Is there more than one block level element? but when I attempt to do the same technique on the list of blog posts, it doesn't work, bear in mind that the list of blog posts will be dynamic in the future.
PostList.css file:
.blog-list-container {
display: block;
text-align: center;
margin: 0 auto;
}
.blog-element {
display: block;
width: 50%;
padding-top: 60px;
padding-bottom: 60px;
padding-right: 30px;
padding-left: 30px;
}
And Postlist.js file:
import React from 'react';
import App from '../../App';
import './PostList.css';
import {
BrowserRouter as Router,
Routes,
Route,
Link,
Outlet,
useParams
} from 'react-router-dom';
const PostList = ( props ) => (
<div className="blog-list-container">
<Router>
<Link to={'/posts/${}'} className="blog-element">element 1</Link>
<Link to={'/posts/${}'} className="blog-element">element 2</Link>
<Link to={'/posts/${}'} className="blog-element">element 3</Link>
</Router>
</div>
);
export default PostList

There is a cleaner, shorthand alternative to some of the suggested:
.blog-element {
display: block;
width: 50%;
padding: 60px 30px;
margin: 0 auto;
}
but I would advise using flexbox though:
While looking at your code and what is mentioned you should consider D.R.Y. when writing components, example:
const PostList = (props) => (
<div className='blog-list-container'>
<Router>
{posts.map((post) => {
return (
<Link key={post.id} to={post.link} className='blog-element'>
{post.name}
</Link>
)
})}
</Router>
</div>
)
export default PostList

It is behaving exactly as you have set the css rules.
.blog-element {
display: block;
width: 50%;
padding-top: 60px;
padding-bottom: 60px;
padding-right: 30px;
padding-left: 30px;
margin: 0 auto; // will center it to your blog list container.
}
Although i would suggest that you set the max-width to the container and remove the width from blog element.

.blog-list-container {
text-align: center;
}
.blog-element {
display: block;
width: 50%;
padding: 60px 30px;
margin: 0 auto; // new line
}
Add this line only in your code
or Code like this
.blog-list-container {
text-align: center;
}
.blog-element {
display: block;
padding: 60px 30px;
}

Related

CSS only works sometimes on dynamic image rendering in React

I am building a recommender system for clothing and for that I am currently implementing a front-end with React. This front end is a chat in which the user can send text messages or upload an image. I dynamically render the messages from the bot (back-end) and the user (front-end). My problem is that the image behaves in an unpredictable way and first gets positioned correctly under the last message but every new messages just "ignores" the image and gets displayed wrong.
This is (part of) the react rendering output:
<div className="body">
{/* Go through the array and display every message either as user or as bot message */}
{messages.map((message, index) => {
if (message[2] === "bot") {
if (message[3] === "image") {
return (
<img
src={message[0]}
key={index}
className="image botImage"
alt="imageDisplayError"
></img>
);
}
return (
<p className="message botMessage" key={index}>
{message[0]}
</p>
);
} else {
if (message[3] === "image") {
return (
<img
src={message[0]}
key={index}
className="image usrImage"
alt="imageDisplayError"
></img>
);
} else {
return (
<p className="message usrMessage" key={index}>
{message[0]}
</p>
);
}
}
})}
</div>
This is the relevant CSS code:
.message {
padding: 10px;
width: fit-content;
border-radius: 10px;
margin-bottom: 15px;
/* Breaks messages into multiple lines */
max-width: 350px;
overflow-wrap: break-word;
word-wrap: break-word;
hyphens: auto;
}
.botMessage {
background-color: #037d7a;
color: white;
}
.usrMessage {
margin-left: auto;
background-color: #07a7a4;
}
.image {
/* padding-top: 15px; */
max-width: 350px;
max-height: 250px;
/* margin-bottom: 15px; */
}
.usrImage {
float: right;
}
This is the output (I have the inspector open so you can see the bounding boxes)
As you can see the image renders correctly under the initial message but every new messages just ignores the bounding box.
I would be grateful for ideas on how to fix this :)
I have already tried different CSS tricks with paddings and margins and so on as well as enclosing every element in a wrapper div so that an image and a text message get handled the same way.
The solution was posted by Nirav.
This CSS seems to do the trick:
.message {
margin: 0;
padding: 10px 18px;
width: fit-content;
border-radius: 5px;
max-width: 350px;
overflow-wrap: break-word;
word-wrap: break-word;
hyphens: auto;
margin-bottom: 5px;
}
.botMessage {
background-color: #037d7a;
color: white;
}
.image {
display: flex;
max-width: 350px;
max-height: 250px;
aspect-ratio: 16/9;
object-fit: cover;
object-position: top;
border-radius: 5px;
margin-bottom: 5px;
}
.usrMessage {
margin-left: auto;
background-color: #07a7a4;
color: white;
}
.usrImage {
margin-left: auto;
}

flexbox isnt displayed properly in react

i am trying to create a flexbox with image in one pill and some text in another pill. here's the code i have written in react->
import NavbarTop from "../navbar/navbar";
import "./style.css";
import React from 'react';
function Home() {
return (
<>
<NavbarTop />
<div id="titleTextDiv" className="d-flex">
<div className="p-2 flex-fill">
<img className="titlePic" src="home/titleLogo.png" />
</div>
<div className="p-2 flex-fill">
<span className="titleText">Dumpster<br></br>Dive</span>
</div>
</div>
</>
);
}
export default Home;
style.css
#titleTextDiv {
margin-top: 30px;
text-align: center;
}
.titlePic {
height: 60px;
width: 80px;
}
.titleText {
text-align: justify;
font-size: 25px;
font-weight: bold;
}
but the flexbox isnt working. the text is displayed under the image for some reason. can someone guide me in right direction?
Do you have display: flex; set anywhere in this component? If not, then the css doesn't know to put it into a flexbox. Add the display: flex; to the #titleTextDiv and they should appear next to each other.
#titleTextDiv {
margin-top: 30px;
text-align: center;
display: flex;
}
.titlePic {
height: 60px;
width: 80px;
}
.titleText {
text-align: justify;
font-size: 25px;
font-weight: bold;
}
Blitz

How to display a fixed navbar in Nextjs?

I have a Nextjs app that displays the same navbar on each page. The navbar has a fixed position. The display is correct on the homepage (written in index.tsx). But when I click on a new page, the new page is hidden behind the navbar!
The issue disappears if I remove the fixed position property. But I can't believe Nextjs doesn't support such a basic task.
The code is very simple:
// _app.tsx
function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<Navbar />
<Component {...pageProps} />
</>
);
}
export default MyApp;
// about.tsx
const About: NextPage = () => {
return (
<section>
<h1>About</h1>
</section>
);
};
export default About
// navbar.tsx
export default function Navbar() {
const router = useRouter();
return (
<nav className={styles.navbar}>
<Link href="/">
<Image
src={icon.src}
className={styles.logo}
alt="logo"
width={70}
height={70}
/>
</Link>
<ul className={styles.list}>
<li
className={
router.route === "/about" ? styles.listItemActive : styles.listItem
}
>
<Link href="/about">About</Link>
</li>
</ul>
</nav>
);
}
//navbar.module.css
.navbar {
background-color: var(--dark);
color: #fff;
height: 80px;
width: 100vw;
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
position: fixed;
z-index: 999;
}
.logo {
cursor: pointer;
}
.list {
list-style: none;
padding: 0;
margin: 0;
display: flex;
align-items: center;
}
.listItem {
cursor: pointer;
}
.listItemActive {
cursor: pointer;
color: var(--red);
}
How to fix this?
Just add a position to your css.
top: 0px;
right: 0px;
left: 0px;
https://developer.mozilla.org/docs/Web/CSS/position
If what you want is to have a sticky navbar, you can do it with pure CSS with position: sticky like this:
header, nav, main {
padding: 1.7rem 1rem;
}
header {
background-color: #d99;
}
nav {
position: sticky;
top: 2rem;
background-color: #9d9;
}
main {
height: 100vh;
background-color: #99d;
}
--
<header>
Header
</header>
<nav>
Navbar
</nav>
<main>
Main
</main>
position: sticky;
top: 0px;
right: 0px;
left: 0px;
this worked for me!

How to fit React Modal size children?

I am trying to load a dynamic login page with React Modal. Is there a way to make React Modal resize to the size of its child elements?
//App.js
import "./styles/app.scss";
import Layout from './options/Layout'
import Login from './Components/Login'
import { useState } from "react";
import Modal from 'react-modal'
function App() {
const [OpenModal, setOpenModal] = useState(false)
return (
<div id="app" className="App">
<Layout>
<button onClick = {() => setOpenModal(true)}>open modal</button>
<Modal isOpen = {OpenModal}>
<Login/>
</Modal>
</Layout>
</div>
);
}
export default App;
//login.scss
.login {
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height: 280px;
margin: 0 auto;
background-color: #5c8fc2;
border-radius: 30px;
}
.login_top {
display: flex;
align-items: center;
justify-content: center;
margin-top: -40px;
margin-bottom: 20px;
color: whitesmoke;
}
.login_register_container {
border: none;
outline: none;
border-radius: 50%;
}
button {
margin-right: 12px;
margin-top: 30px;
border: none;
border-radius: 4px;
cursor: pointer;
}
I want to dynamically manage the size according to the child element.
I've tried several methods, but without success. How to fit modal's component to child element???
If you use the developer tools you can see that the content grows because they have applied some styles to the modal.
position: absolute;
inset:40
inset is same as
top: 40px;
left: 40px;
right: 40px;
bottom: 40px;
If you want your content to be exactly the same size as your content, you can remove the right and bottom from the styles.
<ReactModal
isOpen={true}
contentLabel="Minimal Modal Example"
className="Modal"
>
<div>
<div style={{ height: '500px' }}>Some content</div>
</div>
<button onClick={() => {}}>Close Modal</button>
</ReactModal>
CSS:
.Modal {
position: absolute;
top: 40px;
left: 40px;
background-color: papayawhip;
}
You will lose equal spacing on all sides of the modal if you do this though.
Here is an example. https://stackblitz.com/edit/react-vxe3cn

CSS module does not styling some elements when i deploy my app

Im trying to apply css style on specific component in React with css module.
When i run the app localy the styles work correctly, but for some reason when i deploy my app the style does not apply on the outer div, only his childs get the correct style.
why is that?
Her is the code:
// React component
import { ChatContext } from '../../../../contexts/ChatContext';
import classes from './Humburger.module.css';
import React, { useContext } from 'react';
const Humburger = () => {
const { show, setShow } = useContext(ChatContext);
return (
<div className={classes.Humburger} onClick={() => setShow(!show)}>
<span></span>
<span></span>
<span></span>
</div>
);
};
export default Humburger;
// CSS file
/*Here is the style which does not apply when i deploy my app*/
.Humburger {
display: flex;
justify-content: center;
align-items: center;
flex-flow: column;
width: 35px;
height: 100%;
margin-left: 10px;
}
/*==========================================================*/
.Humburger > span {
display: inline-block;
height: 5px;
width: 100%;
border-radius: 5px;
background-color: #fff;
margin-top: 3px;
cursor: pointer;
}
.Humburger > span:first-child {
margin: 0;
}
Have you tried import './Humburger.module.css'; and className='Humburger' ?

Resources