I'm wondering why any image I put in this div container with display flex is automatically stretched out? And if I were to set a width for it, I can't center it with justify-content.
#container {
display: flex;
justify-content: center;
}
#container div {
padding: 25px;
background: blue;
color: white;
width: 500px;
display: flex;
flex-direction: column;
}
#container div h1 {
display: flex;
justify-content: center;
}
#container input,
#container button {
width: 75%;
}
#container img {
display: flex;
justify-content: center;
}
<div id="container">
<div>
<h1>a</h1>
<img src="https://placehold.it/350x150">
<input type="text" name="a">
<input type="text" name="b">
<button>a</button>
</div>
</div>
https://jsfiddle.net/nqt8bw4z/ auto stretch
https://jsfiddle.net/nqt8bw4z/2/ fixed width but doesn't center
An initial setting of a flex container is align-items: stretch. This means that flex items will expand the full length of the container's cross axis. That would be the container's height in flex-direction: row, and width in flex-direction: column.
Since you're working with a column-direction flex container, the image is stretching horizontally by default. You can override this setting with another value. Try align-items: flex-start or center.
#container {
display: flex;
justify-content: center;
}
#container div {
display: flex;
flex-direction: column;
align-items: center; /* NEW */
width: 500px;
padding: 25px;
background: blue;
color: white;
}
#container div h1 {
display: flex;
justify-content: center;
}
#container input,
#container button {
width: 75%;
}
#container img {
display: flex;
justify-content: center;
}
<div id="container">
<div>
<h1>
a
</h1>
<img src="http://placehold.it/350x150" />
<input type="text" name="a" />
<input type="text" name="b" />
<button>
a
</button>
</div>
</div>
In addition to the answer of #Michael_B your code can be simplifed like below as you don't need all these flexbox properties.
#container div {
padding: 25px;
background: blue;
color: white;
width: 500px;
margin:auto;
display: flex;
flex-direction: column;
align-items:center;
}
#container input,
#container button {
width: 75%;
}
<div id="container">
<div>
<h1>a</h1>
<img src="https://placehold.it/350x150">
<input type="text" name="a">
<input type="text" name="b">
<button>a</button>
</div>
</div>
Related
I have this on my codepen in which I was using flex to align items. What I want is to align items even with or without labels.
here is my html code:
<div class="parent-main">
<div class="child1">
<!--<label>checkbox</label> -->
<input type="text"/>
</div>
<div class="child2">
<label>checkbox</label>
<input type="checkbox"/>
</div>
<div class="child3">
<label>checkbox</label>
<input type="radio"/>
</div>
</div>
my css:
.parent-main {
display: flex;
justify-items: center;
border: solid 1px #ccc;
width: 55vh;
height: 25vh;
margin: 5px;
padding: 15px;
gap: 2px;
}
.child1 {
display: flex;
flex-direction: column;
/* position: relative;
top: 2px; */
}
.child2 {
display: flex;
flex-direction: column;
align-items: baseline;
}
.child3 {
display: flex;
flex-direction: column;
align-items: baseline;
}
Here is my codepen link: enter link description here
This is quite tricky on my side but I hope I can have your insights on this, been working this for 2 days now.
As you want to align the input of type "text" with the other div siblings but without it having any label element, we use a line break in order to mimic an empty blank space without any text element in it by using either <br> or
Tip:
Avoid duplication of codes in CSS properties, see the below snippet
.parent-main {
display: flex;
justify-items: center;
border: solid 1px #ccc;
width: 55vh;
height: 25vh;
margin: 5px;
padding: 15px;
gap: 2px;
}
.parent-main>div {
display: flex;
flex-direction: column;
}
.child2,
.child3 {
align-items: baseline;
}
<div class="parent-main">
<div class="child1">
<!--empty space -->
<br>
<!-- can also be used -->
<input type="text" />
</div>
<div class="child2">
<label>checkbox</label>
<input type="checkbox" />
</div>
<div class="child3">
<label>checkbox</label>
<input type="radio" />
</div>
</div>
After googling for hours, I am still stuck at this problem. Since I used flex-direction:column, to align the child divs hence, I used justify-content:center but the child divs look aligned to the left.
<div class=container>
<div class= "wrapper">
<div class = "image-float">
<img class = "profile-picture" src = "{% static 'images/image.jpg'%}">
</div>
<div>
<p>Welcome to Sparison...</p>
</div>
<div class="intro">
<h1>Copy code below and share with friends</h1>
</div>
<div class="url-container input-group">
<input type="text" id="random" class="url input-border form-control" value="">
<div class="input-group-append">
<span class="input-border input-group-append input-group-text">
<i class="far fa-copy url-copy-icon"></i></span>
</div>
</div>
</div>
</div>
The relevant CSS is below:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
font-family: Montserrat;
font-size: 50px;
font-weight: bold;
background-color: var(--very-pale-blue);
width:100%;
height:100vh;
}
.container{
max-width: 100%;
height: 90vh;
margin-top: 0;
}
.wrapper{
display: flex;
flex-wrap: wrap;
max-width: 100%;
height: 90vh;
justify-content: center;
flex-direction: column;
}
.url-container .input-group {
border-radius: 10px;
width: 50%;
}
.input-group {
position: relative;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-ms-flex-align: stretch;
align-items: stretch;
width: 50%;
/* background-color: #1DB954; */
border-radius: 2px;
}
Above is how the page renders. every element under the wrapper div should be in the middle of the page centered vertically.
You can find more information about flex here:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
.wrapper{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
max-width: 100%;
height: 90vh;
}
Link to jsfiddle:
https://jsfiddle.net/y4qra692/
Change the container width
.container {
width: 100%;
}
Then for the wrapper as you have changed the flex-direction to column you now need align-items rather than justify-content as (confusingly) when you change the direction these properties swap around:
.wrapper {
width: 100%;
display: flex;
align-items: center;
}
To make div centered you can add this code to .wrapper
.wrapper{
width: 50%;
margin:auto;
}
To make a div centered:
<section class="middle">
<div class="div">
<p>Hi</p>
</div>
</section>
Style.css
.middle {
height: 100vh; /* or height you want */
width: 100%;
position: relative;
}
.div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
I'd like to achieve the look for my login area similar to this photo.
My current HTML and CSS are as follows:
HTML
<div className="loginArea">
<p>Account Login</p>
<div className="inputBoxes">
<div>
<input type="text" placeholder="Username" />
<input type="password" placeholder="Password" />
</div>
<div>
<p>Sign In</p>
</div>
</div>
</div>
CSS
.loginArea {
margin: auto;
padding-top: 8%;
display: flex;
flex-direction: column;
height: 100%;
max-width: 400px;
justify-content: center;
}
.inputBoxes {
display: flex;
flex-direction: column;
justify-content: center;
border-radius: 15px;
background-color: blue;
}
.inputBoxes div {
display: flex;
justify-content: center;
}
input {
flex-grow: 1;
border: none;
}
My goal is to achieve the styling of this input area with respect to the shape of the inputs/sign in button as what look like one rectangle with rounded corners.
The problems I'm running into are two fold:
I can't seem to restrict the size of the input areas to the parent container. They run outside the parent div.
I can't seem to get the input areas to accept the restricted border-radius of the parent container.
Do I have to style each element individually for the rounded borders and how do I restrict the username and password areas to the width of the parent div?
If there are "better" approaches, I'm open to suggestions.
Since you're already using flexbox, the input element sizes seem to correctly fit inside the confines of the parents without any issue.
With regards to your second issue, adding overflow: hidden to the element .inputBoxes will work.
See proof-of-concept below:
body {
background-color: #ccc;
}
.loginArea {
margin: auto;
padding-top: 8%;
display: flex;
flex-direction: column;
height: 100%;
max-width: 400px;
justify-content: center;
}
.inputBoxes {
display: flex;
flex-direction: column;
justify-content: center;
border-radius: 15px;
background-color: blue;
overflow: hidden;
}
.inputBoxes div {
display: flex;
justify-content: center;
}
input {
flex-grow: 1;
border: none;
height: 35px; /* Just for demo */
}
<div class="loginArea">
<p>Account Login</p>
<div class="inputBoxes">
<div>
<input type="text" placeholder="Username" />
<input type="password" placeholder="Password" />
</div>
<div>
<p>Sign In</p>
</div>
</div>
</div>
Is there a way to prevent stretching on both axis of CSS Flex? In the example below, I want the the option and box layers to adjust to the width and height of the children. Additionally, I want the box to be in the middle of the page.
I am aware of flex-grow and basis, but my understanding is it only manages the space of the main axis.
.container {
background-color: red;
display: flex;
justify-content: center;
}
.box {
display: flex;
flex-basis: 0;
background-color: green;
justify-content: center;
}
.option {
display: flex;
flex-direction: column;
align-items: center;
background-color: yellow;
}
.img-container {
background-color: blue;
}
.img-container img {
max-width: 100%;
}
<div class="container">
<div class="box">
<label class="option">
Option 1
<div class="img-container">
<img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=640:*" />
</div>
</label>
<label class="option">
Option 2
<div class="img-container">
<img src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=640:*" />
</div>
</label>
</div>
</div>
I am using flexbox to center a form vertically and horizontally. Inside this form I'd like to pin a button to the bottom right of the flexbox container. I am not sure how to get the button pinned to the bottom right though.
html,
body {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
form {
border: 1px solid gray;
padding: 1em;
}
.form-button {
margin-top: 1em;
align-self: flex-end;
}
<div class="container">
<form>
<div class="form-input">
<label> Name <input type="text" /></label>
</div>
<div class="form-button">
<button type="submit">Submit</button>
</div>
</form>
</div>
You just need to make the form element a flex container, because flex properties only work between parent and child elements.
In other words, your align-self: flex-end on the .form-button is not working because the parent – form – does not have display: flex or display: inline-flex applied.
Here's a more complete explanation:
Proper use of flex properties when nesting flex containers
html, body {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
form {
border: 1px solid gray;
padding: 1em;
/* NEW */
display: flex;
flex-direction: column;
}
.form-button {
margin-top: 1em;
align-self: flex-end;
}
<div class="container">
<form>
<div class="form-input">
<label> Name <input type="text" /></label>
</div>
<div class="form-button">
<button type="submit">Submit</button>
</div>
</form>
</div>
.form-button {
margin-top: 1em;
align-self: flex-end;
display: flex;
justify-content: flex-end;
}
Just insert float: right;
like this:
.form-button {
float: right;<-----------added
//more code...
}
html,
body {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
form {
border: 1px solid gray;
padding: 1em;
}
.form-button {
margin-top: 1em;
float: right;
}
<div class="container">
<form>
<div class="form-input">
<label> Name <input type="text" /></label>
</div>
<div class="form-button">
<button type="submit">Submit</button>
</div>
</form>
</div>