How to avoid redundant nested divs using Styled Components - css

I have several components used to build layouts. For example, a Row, a Column, and a Vertical Align component:
export const Row = styled.div`
display: flex;
align-items: flex-start;
flex-wrap: wrap;
width: 100%;
> * {
margin-left: 10px;
&:last-child {
margin-left: 0;
}
}
`
export const Column = styled.div`
flex: 1;
`
export const VerticalAlign = styled.div`
align-items: center;
display: flex;
`
Using them together produces unnecessary nested divs:
<Row>
<Column>
<VerticalAlign>
<Icon />
<input />
</VerticalAlign>
</Column>
<Column>
<VerticalAlign>
// ...
</VerticalAlign>
</Column>
</Row>
If this were just css, I would do this:
<div class="row">
<div class="column vertical-align">
// ...
</div>
</div>
I could extend classes, but that would create an explosion of one-off components for every combination:
const VerticalAlignColumn = styled(Column)`
align-items: center;
display: flex;
`;
const VerticalAlignRow = styled(Row)`
//...
`;
Is there a way to easily combine the styles of multiple components into a single component? Something like <Row+VerticalAlign>...</Row+VerticalAlign>?

I couldn't find a way to do like you want.
And the best approach here is like you said (created extend classes) or work with props, like this:
export const Column = styled.div`
flex: 1;
${({ verticalAlign }) =>
verticalAlign &&
`
align-items: center;
display: flex;
`}
`;
<Column verticalAlign>
<Icon />
<input />
</Column>
<Column>
<Icon />
<input />
</Column>
You can also work with classes, but it has some specificities -> Styling normal React components

Related

Centering and aligning a list of items with image and text in react native

So, I've got the following code:
export const MenuWrapper = styled.ScrollView`
width: 100%;
height: 100%;
padding: 10px;
margin: auto;
`
export const Item = styled(MyButton)`
flex: 1;
flex-direction: row;
background-color: black;
align-items: center;
justify-content: flex-start;
margin: ${remToPx(0.5)}px 0;
width: 80%;
margin: ${remToPx(1)}px auto;
justify-content: center;
`
with MYBUtton just being a pressable and
<View>
<Header title={event.name} noSub />
<MenuWrapper>
<Item>
<ImageWrapper
h_size={1.5}
w_size={1.5}
source={require('../../../assets/icons/home/programacao.png')}
/>
<View>
<MyText color="white">Programação</MyText>
</View>
</Item>
<Item>
<ImageWrapper
h_size={1.5}
w_size={1.5}
source={require('../../../assets/icons/home/copanela.png')}
/>
<MyText color="white">CoPanela</MyText>
</Item>
<Item>
<ImageWrapper
h_size={1.5}
w_size={1.5}
source={require('../../../assets/icons/home/recompensas.png')}
/>
<MyText color="white">Recompensas</MyText>
</Item>
</MenuWrapper>
</View>
that generates the following:
The idea is to have all of them centered, but each image basically on the same column. In normal CSS I'd use a grid and everything would be fine, but here I cant seem to find a way to solve the problem showcased at the different alignment of "Programação" and "Copanela".
I've tried many solutions such as using two other views inside the item with each having 50% of the button and some align-right align-left shenanigans but it didn't work.
How could I accomplish this?
You have to take another view with fixed width inside button

ReactJS and CSS rendering

So I was designing a web app using React and I'm stuck on this issue. Given below is what is being rendered
As I've highlighted using the red rectangle, there is a cut off region in my footer component. I'm not sure what is causing this. The CSS and Component code is as given below
import styled from 'styled-components';
export const Background = styled.section`
background: black`;
export const Column = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
margin-right: 50px;
margin-top: 50px;`;
export const Row = styled.div`
display: flex;
flex-direction: row;
justify-content: center;`;
export const Text = styled.h1`
font-size: 1.5em;
text-align: center;
color: palevioletred;
`;
Footer Component
import React from 'react';
import { Column, Row, Background, Text } from './FooterStyles';
function Footer(props) {
return (
<div>
<Background>
<Row>
<Column>
<Text><h1>Developed By</h1></Text>
<h3>Erwin Smith</h3>
<h3>Peter Jackson</h3>
<h3>Tommy</h3>
</Column>
<Column>
<h1>About US</h1>
</Column>
</Row>
</Background>
</div>
)
}
export default Footer;
A solution to work around this issue is much appreciated.
If you r using bootstrap try creating a custom class with margin-left,right -15px n overflow-x: hidden to body
The defaults properties and values for a body tag are:
body { display : block; margin : 8px; }
so I think you should set that margin to 0.
this is just my guess because you haven't provided so much information and this is all I can say.

How to center an image in ReactJS using css

I am using ReactJS create-react-app to build a website. I'm trying to center an image on my homepage horizontally across the screen using css, but it is remaining aligned left.
I have the .jpg being imported into a .js file with a class declaration and it appears on the page but it doesn't follow the class modifications I am making in my index.css file.
//in Cur.js file
import React from 'react';
import Image from 'react-image-resizer';
import cur from './cur.jpg';
function Cur() {
return (
<div>
<Image
img src={cur} alt="cur" class="center"
height={350}
width={700}
/>
</div>
);
}
export default Cur;
//in index.css file
.center{
text-align: center;
display: block;
justify-content: center;
align-items: center;
margin: auto;
width: 100%;
}
<Image
img src={cur} alt="cur"
height={350}
width={700}
style={{ alignSelf: 'center' }}
/>
You may separate the inline style, or use styled-components instead
Set a class name for the div eg. className="container-div"
And style it in your css style sheet.
.container-div{
display: flex;
height: 100vh;
width: 100vw;
align-items: center;
justify-content: center;
}
The height and width properties will ensure your container covers the whole screen .
try this.. the property is called className not class
import React from 'react';
import Image from 'react-image-resizer';
import from "index.css"
import cur from './cur.jpg';
function Cur() {
return (
<div>
<Image
img src={cur} alt="cur" class="center"
height={350}
width={700}
/>
</div>
);
}
export default Cur;
html:
<div className="image-container">
<img src={logo} alt="logo"/>
</div>
css file:
.image-container {
display: flex;
justify-content: center;
align-items: center;
}
React uses className instead of class.
Change
img src={cur} alt="cur" class="center"
to
img src={cur} alt="cur" className="center"
my problem was missing display: flex in my css styles. adding it to the rest of styles, solved my problem.
In parent div css add:
.parent {
display: flex;
justify-content: center;
}
This will center your image.
import React from 'react';
import Image from 'react-image-resizer';
import cur from './cur.jpg';
function Cur() {
return (
<div>
<Image src={cur} alt="cur" className="center"/>
</div>
);
}
export default Cur;
body,html{
height:100%,
min-height:100%
}
.center{
text-align:center;//for texts
height:100%;
display:flex;
align-items:center;
justify-content:center;
}

CSS class name bracket notation doesn't target inner CSS classes

I import the styles to the component like below. Up: It's a React component with a postcss-loader in Webpack.
import styles from './index.scss';
This is a gist from the component:
<div className={styles['container']}>
<Row type='flex' align='middle' justify='space-between'>
<Col span={24}>
... ...
</Col>
</Row>
</div>
This is the styles:
.container {
margin-left: 24px;
& .ant-row-flex.ant-row-flex-space-between.ant-row-flex-middle {
height: 100%;
display: flex;
& .ant-col-24 {
display: flex;
align-items: center;
}
}
}
Only the .container's margin-left: 24px; gets displayed in the component.
How to get .ant-row-flex.ant-row-flex-space-between.ant-row-flex-middle from Row and .ant-col-24 from Col to display too?

React-Bootstrap centering Tabs

Bootstrap-react's Tab's will align to the left by default.
<Tabs defaultActiveKey={2} id="uncontrolled-tab-example">
<Tab eventKey={1} title="Tab 1">
Tab 1 content
</Tab>
<Tab eventKey={2} title="Tab 2">
Tab 2 content
</Tab>
</Tabs>;
You can pass a pullRight prop to make it align to the right
<Tabs pullRight defaultActiveKey={2} id="uncontrolled-tab-example">
Is there a simple way to make it centered? If not, how would you center the tabs?
On your CSS file, put the following:
ul.nav.nav-tabs {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
padding: 10px;
}
You can just add fill as a prop
<Tabs fill></Tabs>
Perhaps:
.nav-tabs > li {
float: none;
display: inline-block;
zoom: 1;
}
.nav-tabs {
text-align: center;
}
See here: http://jsfiddle.net/j751b1mb/4/
.centered-tabs > nav {
display: flex;
flex-direction: row;
justify-content: center;
}
<div className='centered-tabs'>
<Tabs></Tabs>
</div>
What worked for me was using the grid system in react-bootstrap and adding the class "nav-justified" to the column.
<Container fluid>
<Row className={"justify-content-center"}>
<Col className={"nav-justified"}>
<Tabs defaultActiveKey="profile" id="uncontrolled-tab-example">
<Tab eventKey="user" title="User"></Tab>
<Tab eventKey="aboutMe" title="About Me"></Tab>
<Tab eventKey="posts" title="Posts"></Tab>
</Tabs>
</Col>
</Row>
</Container>;

Resources