I am attempting to adjust the width of the react-datepicker input box and surprisingly have found little information out there and am unable to effect its width. I would just like to make the input width 100% of its containing div.
Expected behavior
The width should expand to the width of its parent container.
My react-datepicker
<div className="myContainer">
<DatePicker
className="myDatePicker"
fixedHeight
readOnly={true}
isClearable={false}
placeholderText="End date"
selected={this.props.defaultValue}
onChange={(date) => this.props.handleDateChange(date)}
/>
</div>
Expected behavior
The following should result in the myDatePicker being the same width as the parent myContainer, but the width remains unchanged.
.myContainer {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
flex: 1 1 0px;
}
.myDatePicker {
width: 100%; // note: if I change this value to a fixed value (such as 500px) then the width is effected
}
Closest Alternative attempt
The closest attempt was this, but it effects the width of the popup as well, causing it to stretch beyond the length of the entire screen, so this does not work as a solution either:
.myContainer div {
width: 100%;
}
Actual behavior
The date picker remains the same length, unless a specific px value is set for the width.
Does anyone understand how to set the input width to 100% for react-datepicker?
EDIT
I believe the reason it does not work like a typical input field is because react-datepicker is an input that is embedded deeper inside other divs that have their own styling (or lackthereof)
EDIT #2: Here is a codepen showing the issue - https://codepen.io/anon/pen/bjxENG
I had the same issue and solved it thanks to Rbar's answer.
Wrap your DatePicker component with a custom container. Then assign the width to that container and its children like below:
import "./customDatePickerWidth.css";
<div className="customDatePickerWidth">
<DatePicker dateFormat="dd/MM/yyyy" />
</div>
Inside the customDatePickerWidth.css:
.customDatePickerWidth,
.customDatePickerWidth > div.react-datepicker-wrapper,
.customDatePickerWidth > div > div.react-datepicker__input-container
.customDatePickerWidth > div > div.react-datepicker__input-container input {
width: 100%;
}
I also stumbled into this issue as well. To solve this, there is a property that you can add directly to the DatePicker component which is the "wrapperClassName" and this property will allow us to directly apply classes to the "react-datepicker-wrapper".
Example:
<DatePicker wrapperClassName="datepicker" />
and then
.datepicker {
width: 100%;
}
or if you are using tailwindcss, just directly apply
<DatePicker wrapperClassName="w-full" />
Note: This will only make the wrapper to be full width, therefore you will also need to apply "width: 100%" on the datepicker component if you want the input field to occupy full width as well.
Simple solution: Add this to your css file.
.react-datepicker__input-container {
width: inherit;
}
.react-datepicker-wrapper {
width: 100%;
}
This is my html
<div class="container">
<div>
<div class="react-datepicker-wrapper">
<div class="react-datepicker__input-container">
<input class="form-control" type="text" value="">
</div>
</div>
</div>
</div>
I have used a wrapper div around the react date picker and used a specific css selector to make the wrapper divs react date picker applies around the input and the div I added to be width 100%
this is the css selector applied to a div which wraps the above snippet
.container > div,
.container > div > div.react-datepicker-wrapper,
.container > div > div > div.react-datepicker__input-container {
width: 100%;
}
The result is the input box width stretches to 100% but the pop-out date picker box stays the same width
//html file
<div className="customDatePickerWidth">
<DatePicker
placeholderText="Select Schedule Date"
/>
</div>
//css file
.customDatePickerWidth,
.customDatePickerWidth > div.react-datepicker-wrapper,
.customDatePickerWidth > div > div.react-datepicker__input-container
.customDatePickerWidth > div > div.react-datepicker__input-container input {
width: 100%;
height: 100%;
}
.react-datepicker__input-container {
width: inherit;
height: inherit;
}
.react-datepicker__input-container input {
width: inherit;
height: 100%;
}
.react-datepicker-wrapper {
width: 100%;
}
Gives the child (unnammed input) 100% while also give it's parent (__input-container) a block display.
.react-datepicker__input-container input {
display: block;
width: 100% !important;
}
just use:
<DatePicker containerStyle={{width: '100%'}}/>
To prevent the flex items from shrinking, set the flex shrink factor to 0:
.myContainer .myItem {
flex-shrink: 0;
}
.myContainer {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
flex: 1 1 0px;
}
.myDatePicker {
width: 100%;
}
<div class="myContainer">
<input class="myDatePicker">
</div>
The above snippet just works so there MUST be other code interfering with css or scaffolding.
Using styled-components
import styled from 'styled-components';
const DatePickerStyled = styled.div`
/* STYLE FOR WITH */
.react-datepicker-wrapper {
width: 100%;
}
`;
encloses the component
<DatePickerStyled>
<DatePicker dateFormat="dd/MM/yyyy" />
</DatePickerStyled>
I just passed a className to the component:
<DatePicker className='date-input-field' />
Here's how I set the width:
.date-input-field {
max-width: 7rem;
}
Another option:
Remove all styling there is by default.
Introduce the width of 100%
My React datepicker:
<div className="col-3">
<DatePicker
selected={st}
onChange={(date: Date) => checkAndChangeStartTime(date)}
showTimeSelect
showTimeSelectOnly
timeIntervals={5}
timeCaption="Time"
dateFormat="HH:mm"
timeFormat="HH:mm"
/>
</div>
The CSS to make this option work:
.react-datepicker-wrapper>.react-datepicker__input-container input {
all: unset !important;
width: 100% !important;
}
This forces the div to be 100% width, but requires you to start styling the element the beginning.
Related
I'm currently writing a page using Angular and have created a dialog window for users to copy columns from an Excel document. This window consists of two large textareas lined up as columns next to each other, which I achieved using css' column-count attribute.
The issue is that when the first textarea is focused, there is a glow around it. The bottom of the glow shows up in the next column above the second textarea. Is there something I can do to fix this?
I don't want to remove the glow because it helps the user know they're focusing on that input. Worst case scenario I'll just keep it as is.
Here's a picture of what it looks like to have the first text area focused.
copy-paste-dialog.component.html
<h1 mat-dialog-title>Copy/Paste Parts From Excel</h1>
<div id="dialogInput" mat-dialog-content>
<div>
<h4>Part Numbers</h4>
<textarea rows="20" class="form-control" placeholder="Part Numbers" [(ngModel)]="result.supplierPNs" ></textarea>
</div>
<div>
<h4>Descriptions</h4>
<textarea rows="20" class="form-control" placeholder="Descriptions" [(ngModel)]="result.descriptions" ></textarea>
</div>
</div>
<div mat-dialog-actions class="dialogButtons" >
<button mat-raised-button color="primary" [mat-dialog-close]="result">Submit</button>
<button mat-raised-button color="warn" (click)="cancel()">Cancel</button>
</div>
copy-paste-dialog.component.css
.dialogButtons {
margin: 10px 0;
}
#dialogInput {
column-count: 2;
}
textarea {
resize: none;
}
if it doesn't matter for you, you can just use flexbox:
.dialogButtons {
margin: 10px 0;
}
#dialogInput {
display: flex;
width: 100%;
margin: 0 -10px;
}
textarea {
resize: none;
}
#dialogInput div {
flex: 1;
margin: 0 10px;
}
display: flex works likes a row, it puts the divs next to each other.
flex: 1 means it will take the remaining space, so by giving both the divs within #dialoginput, it will take the even amount of space, which in this case is 50%.
At last, I added some margin.
Flex-box instead of column-count: 2 will solve this issue
CSS Change
.dialogButtons {
margin: 10px 0;
}
#dialogInput {
display: flex;
}
#dialogInput textarea {
margin-right: 1em;
}
textarea {
resize: none;
}
I am using the Material-UI library for React.
I am trying to make a simple form that looks like:
However, I can't figure out how to align the button with the TextField.
I tried changing the margin-top but it increases the margin of the entire wrapper.
Below is the code sandbox for it.
Any tips on getting this fixed?
https://codesandbox.io/embed/material-demo-y5eg7?fontsize=14&hidenavigation=1&theme=dark
In your styles.css file you can add {position: relative; top: 10px} to the small-button className then adjust the top value until you are happy with the position alternatively you might be able to wrap the row in a div and use {display:flex; flex-direction:row; justify-content:center;} to align them all.
Change the below styles in styles.css
.horizontal-form { /* This is newly added style */
display: flex;
align-items: center;
}
.input-text-wrapper {
/* margin-bottom: 1.2em; */ /* comment these styles */
}
.input-text-wrapper-small {
width: 33%;
/* margin-bottom: 1.2em; */
display: inline-block;
}
.small-button {
width: 10%;
/* display: inline-flex;
align-items: center; */
}
jsx
Remove the small-button div from inside the input-text-wrapper div and Then Enclose the input-text-wrapper div and small-button div inside a newly created horizontal-form div
...
</Typography>
<div className="horizontal-form">
<div className="input-text-wrapper">
<div className="input-text-wrapper-small">
...
</div>
<div className="input-text-wrapper-small">
...
</div>
<div className="input-text-wrapper-small">
...
</div>
</div>
<div className="small-button">
<Button variant="contained" color="primary">
Add
</Button>
</div>
</div>
</CardContent>
....
Just replace this line
<div className="input-text-wrapper">
with
<div className="input-text-wrapper" style={{display:"flex", alignItems:"center"}} >
AND remove margin-bottom from your style.css
.input-text-wrapper-small {
width: 30%;
/*margin-bottom: 1.2em;*/
display: inline-block;
}
I have a mostly responsive Wordpress website with some images aligned either left or right. I want to remove the float property of these aligned images when they occupy a percentage of the screen (about %50) as it's causing issues with how the text is displayed (one word next to an image, then followed by the rest of the paragraph).
When I remove the float property I get exactly the behaviour I want from the website, but I don't know how to set it so it only triggers under these conditions.
Below is the CSS for the affected images.
img {
display: inline-block;
max-width: 100%;
}
.align-right {
float: right;
}
You should use javascript to calculate the width of window and compare to width of image.
function myFunction() {
var img = document.getElementById("Image");
var div = document.getElementById("myDiv");
if (img.offsetWidth > window.innerWidth / 2) {
div.style.cssFloat = "none";
}
else {
div.style.cssFloat = "right";
}
}
img {
display: inline-block;
max-width: 100%;
width: 400px;
background-color: lightgrey;
height: 200px;
}
<body onresize="myFunction()" onload="myFunction()">
<div>
<img id="Image" src="" alt="Image" />
<div id="myDiv" style="background-color: lightgrey;">
Hello This is Div.
</div>
</div>
</body>
Show the result in full page for your better results.
I am trying to use Flexbox in my React app to create a simple two column webpage that occupies the full width and height.
I can get this to work with HTML and CSS on their own but not within a React app.
So far I have:
:root {
overflow-x: hidden;
height: 100%;
}
body {
min-height: 100%
}
.flexbox {
height: 100%;
display: flex;
}
.left {
flex: 0 0 200px;
height: 100%
}
.right {
flex: 1
}
and:
<div class="flexbox">
<div class="left">
Left
</div>
<div class="right">
Right
</div>
</div>
I realise that I need to account for the additional <div id="root"></div> tag in my index.html so I have also added the following to my css:
#root {
height: 100%;
}
And my render function:
render() {
return (
<div className="flexbox">
<div className="left">Left</div>
<div className="right">Right</div>
</div>
)
}
but this doesn't work. The columns exist but are not full height. Why not?
If you use create-react-app, it adds an element with class=App and an element with id=root to the DOM. They should also get height: 100%
html, body, #root, .App {
height: 100%;
}
Use viewport height units in css:
#root {
min-height: 100vh;
}
You forgot that <html> is also a tag. Moreover, it is parent to all parents! That's why you should give it height of 100% as well.
html, body {
height: 100%;
}
The body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well.
However, the content of the body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.
html {
height: 100%;
}
body {
height: 100%;
}
To the parent div of the page, add the following css.
position: fixed;
overflow: auto;
height: 100vh;
Try This:
[data-reactroot]
{height: 100% !important; }
Alternatively, if anything suggested above doesn't seems to be working for some reason, try using normalize.css package. It wipes all browser provided CSS and creates a fresh window for the app.
"normalize.css": "^8.0.1" //package.json (yarn add or npm install)
import "normalize.css/normalize.css"; //in app.js
Then in css file define the body height and width as 100vh & 100vw respectively.
docs: https://necolas.github.io/normalize.css/
There are a lot of answers here but none of them is working. I have tested all of them.
The best way to achieve minimum height in react js is to use the main tag inside your app and then add the minimum height for that tag.
Your App.js code should look like this.
<React.Fragment>
<Header />
<main>
<Container>
</Container>
</main>
<Footer />
</React.Fragment>
Note
You can simply remove React.Fragment if you want to use empty fragments.
then your code should look like this.
<>
<Header />
<main>
//Write your code here
</main>
<Footer />
</>
Now add CSS to your CSS file.
main {
min-height: 80vh;
}
Important
Few things you have to keep in mind.
Your header and footer or component call should be outside of the main tag. Like the code, I have shared above. I have called the header and footer outside the tag.
If you want to use routing then simply add Router above the tag.
Your code with routing and components call should look like this.
<React.Fragment>
<Router>
<Header />
<main>
<Container>
<Route path='/' component={HomeScreen} exact />
<Route path='/products' component={ProductScreen} />
<Route path='/cart' component={CartScreen} />
<Route path='/login' component={LoginScreen} />
</Container>
</main>
<Footer />
</Router>
</React.Fragment>
I have a responsive layout. One block has a form input and button. How can I make the elements have a combined width of 100%?
Im using Twitter Bootstrap 3 but I cant see any classes they provide for this.
Ive tried using display table on the the container and display table-cell on the the children but it doenst work, im assuming text input doenst render the styles in the same way a div would.
I could use absolute positioning but then the CSS would break if the button's text was lengthened. So I would rather stay clear of this method.
I dont want to set a fixed % width eg 80% for the input and 20% for the button. I want the button to take up the space it requires, and for the input to take whatever is left.
http://codepen.io/anon/pen/jEPoRG
<div class="form-group">
<input type="text" placeholder="Search">
<button type="submit" class="btn btn-default">Submit</button>
</div>
.form-group {
background: grey;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
width: 30%;
}
If you put a div around the search bar, then you can use display: table/table-cell on .form-submit and its children. I assumed that .search_bar_div's width would have been auto, but that didn't quite stretch all the way. But then I tried 100% and this seems to be working as you want.
I tested Mozilla and Chrome only.
<style type="text/css">
.form-group {
background: grey;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
width: 30%;
display: table;
}
.search_bar_div {
width: 100%;
display: table-cell;
}
.form-group .search_bar_div #search_bar {
width: 100%;
}
.form-group .btn {
display: table-cell;
}
</style>
<div class="form-group">
<div class="search_bar_div">
<input id="search_bar" type="text" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</div>