I have a div html tag from a login form, with a div inside that form that shows an error. The error div is at the top of the parent div. When the error is triggered, I want the error div to be inside the form div, without the form going down, but that is not what happens. I've already used position: absolute
in the child div, but it stays in the left corner of the parent div. I've also used z-index
too, so that the child div is over the parent div, but the parent always descends a pixel height the size of the child div. Let me show you the example:
<div id='formulario' style={{height: 400px}}>
{this.props.auth.error && <div id='erro' style={{height:'15px'}}>Nao foi possivel fazer o login!</div>}
<div id='login'>
...
</div>
</div>
When this.props.auth.error
is true, the error appears and the form id size becomes 415px. How do I leave the #formulatrio
div with the size of 400px
and the #login
div in place when the error appears?
Just for you to test I added the background.
<style>
#formulario {
display: grid;
align-content: center;
width: 400px;
height: 400px;
background: grey;
}
#erro {
position: relative;
width: 100%;
height: 15px;
background: yellow;
text-align: center;
}
</style>
<div id='formulario'>
{this.props.auth.error && <div id='erro'>Nao foi possivel fazer o login!</div>}
<div id='login'>
...
</div>
</div>
increase the top CSS property to move the login div down to a certain position, added background for visualization.
class MaintainPosition extends React.Component { render() { return ( <div id="formulario" style={{ height: "400px", backgroundColor: "lightblue" }} > {this.props.auth.error && ( <div id="erro" style={{ height: "15px", color: "red" }}> Nao foi possivel fazer o login! </div> )} <div id="login"> scroll down to toggle error </div> </div> ); } } class App extends React.Component { constructor(props) { super(props); this.state = { error: true }; } render() { return ( <div className="App"> <MaintainPosition auth={{ error: this.state.error }} /> <button onClick={() => this.setState({ error: !this.state.error })}> toggle error </button> </div> ); } } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);
#err { } #formulario { position: relative; } #login { position: absolute; width: 100%; top: 20px; background-color: gray; } #root {text-align: center}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>
Something like this?
Key here is to set the parent to relative
so the child knows what to use a reference.
$('button').on('click', function() { $('#erro').toggle(); });
#formulario { border: 1px solid #EEE; position: relative; } #erro { background-color: rgba(255,0,0,.5); text-align: center; position: absolute; width: 100%; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button>Show/Hide Error</button> <hr> <div id='formulario' style='height: 400px'> <div id='erro' style='height: 15px'>Nao foi possivel fazer o login!</div> <div id='login'> Some<br/> Login<br/> Stuff<br/> </div> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.