How to make two div inside a card display side by side - html

I'm trying to come out with following card for profile details. But the grid system is not giving me the expected result. Any help will be greatly appreciated.
Output:
Expected result:
Code:

Here is how to do it using Grid:
It is very similar the main difference is grid-template-columns, I recommend using Flexbox since this is a 1-dimensional issue which is what Flex was built to solve. Flex and Grid are best used together :D
.grid-container {
display: grid;
width: 300px;
grid-template-columns: 1fr 3fr;
column-gap: 25px;
align-items: center;
border: 1px solid #f4f4f4;
}
.icon img {
height: 100px;
}
.info label {
display: block;
}
<div class="grid-container">
<div class="icon">
<img src="https://www.pngall.com/wp-content/uploads/5/Profile-PNG-Clipart.png" />
</div>
<div class="info">
<label>First Name</label>
<label>Last Name</label>
<label>Position</label>
<label>Identification No.</label>
<label>Email</label>
</div>
</div>

I would recommend using Display Flex in the parent container also setting the labels to block elements so they go on separate lines.
.flex-container {
display: flex;
gap: 30px;
align-items: center;
border: 1px solid #f4f4f4;
width: 300px;
}
.icon img {
height: 100px;
}
.info label {
display: block;
}
<div class="flex-container">
<div class="icon">
<img src="https://www.pngall.com/wp-content/uploads/5/Profile-PNG-Clipart.png" />
</div>
<div class="info">
<label>First Name</label>
<label>Last Name</label>
<label>Position</label>
<label>Identification No.</label>
<label>Email</label>
</div>
</div>

Related

two divs not align horizontally [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 3 months ago.
I just designed a simple sign-in form to practice CSS and HTML but I can't align 2 divs horizontally to input my name and surname.
Actually, I can't understand why if I apply a width of 50% they are stuck on top of each other and if I apply 49% width they are perfectly horizontally aligned as I want.
MY CSS (child width 50%):
I'm expecting with the child property set to 50% they should take 50% of the parent space.. but actually not, why?
what I'm doing wrong, why have to reduce the width to 49% to align them horizontally
see my image 50% width:
I want them aligned side by side like here:
.title{
padding: 1vh;
text-transform: uppercase;
font-size: 2vh;
}
.wrapper{
margin: 0;
padding: 0;
background-color: yellow;
}
.parent{
background-color: red;
width: 100%;
}
.child{
width: 50%; <--------- HERE THE ISSUE
display: inline-block;
}
<body class="body">
<div class="center">
<h1 class="title">Sign Up New User</h1>
<form class="submit_form">
<div class="wrapper">
<div class="parent">
<div class="child">
<label for="Name">Name:</label><br>
<input type="text" id="Name" name="fname">
</div>
<div class="child">
<label for="Surname">Surname:</label><br>
<input type="text" id="Surname" name="fsurname" >
</div>
</div>
</div>
</form>
</div>
</body>
You can use flex-box to achieve this,
I have added this in parent,
display: flex ;
align-items: center;
justify-content: space-evenly ;
You also have to remove width: 50%; from the .child class and add the following CSS,
display:flex ;
align-items:center;
justify-content:center ;
flex-direction:column;
margin: 4px;
Now you can add further CSS as you want.
.title{
padding: 1vh;
text-transform: uppercase;
font-size: 2vh;
}
.wrapper{
margin: 0;
padding: 0;
background-color: yellow;
}
.parent{
background-color: red;
display:flex ;
align-items:center;
justify-content:space-evenly ;
}
.child{
display:flex ;
align-items:center;
justify-content:center ;
flex-direction:column;
margin:4px ;
}
<body class="body">
<div class="center">
<h1 class="title">Sign Up New User</h1>
<form class="submit_form">
<div class="wrapper">
<div class="parent">
<div class="child">
<label for="Name">Name:</label>
<input type="text" id="Name" name="fname">
</div>
<div class="child">
<label for="Surname">Surname:</label>
<input type="text" id="Surname" name="fsurname" >
</div>
</div>
</div>
</form>
</div>
</body>
If I understand your question correctly this should help. First what you want to do is understand what Flex is in html, this first helped me to understand flex.
So you want to add display: flex; to your container/.parent to tell the script that the container is a flex element. After that you should add text-align: center; to center the text inside the .parent. Finnaly, add justify-content: space-evenly; so this way all of the items inside the .parent are spaced evenly from each other.
Also a lot of the things that you put in .child I removed becasue they where unnecessary to achieve what you are going for. I did add margin: 5px; just to make it look more visually appealing.
Your finished code should look like this
.title{
padding: 1vh;
text-transform: uppercase;
font-size: 2vh;
}
.wrapper{
margin: 0;
padding: 0;
background-color: yellow;
}
.parent{
display: flex;
background-color: red;
width: 100%;
text-align: center;
justify-content: space-evenly;
}
.child{
margin: 5px;
}
<body class="body">
<div class="center">
<h1 class="title">Sign Up New User</h1>
<form class="submit_form">
<div class="wrapper">
<div class="parent">
<div class="child">
<label for="Name">Name:</label><br>
<input type="text" id="Name" name="fname">
</div>
<div class="child">
<label for="Surname">Surname:</label><br>
<input type="text" id="Surname" name="fsurname" >
</div>
</div>
</div>
</form>
</div>
</body>
Hope this will be close to your desired result, I added some CSS to match the posted HTML.
Each input is centered in a box half of the container with flex: 1, so there is no need to set width in the code.
You can add more child field here, and the layout will scale naturally.
Example:
.title {
padding: 1vh;
text-transform: uppercase;
font-size: 2vh;
}
.wrapper {
margin: 0;
padding: 0;
background-color: yellow;
}
.parent {
background-color: red;
flex: 1;
display: flex;
justify-content: center;
}
.child {
color: #fff;
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
}
<div class="center">
<h1 class="title">Sign Up New User</h1>
<form class="submit_form">
<div class="wrapper">
<div class="parent">
<div class="child">
<label for="Name">Name:</label><br />
<input type="text" id="Name" name="fname" />
</div>
<div class="child">
<label for="Surname">Surname:</label><br />
<input type="text" id="Surname" name="fsurname" />
</div>
</div>
</div>
</form>
</div>

How to make div elements appear inline with a flex direction of column?

I am trying to get the text input on the same line as the h1 tag inline then display it as a flex-direction of column. But it only seems to want to set all the inputs in a line and with the h1 on top which is not what I want.
Here is what I mean.
here is the desired output:
.contactuscontent{
border: 1px solid pink;
display: flex;
}
.contactusinput {
display: inline-flex;
flex-direction: column;
border: 1px solid purple;
}
<div class="contactuscontent">
<div class="contactusinput">
<div class="name"><h1>Name</h1> <input type="text"> </div>
<div class="email"><h1>Email</h1> <input type="text"> </div>
<div class="refer"><h1>How did you find us</h1> <input type="text"> </div>
</div>
</div>
You should look into semantics of HTML, <h1> is used for headlines.
If you want to add labels for input fields you should use <label for="...">. You can style the any tag in any way you want so default styling should not be a reason to use a tag at all.
.contactuscontent {
border: 1px solid pink;
display: flex;
justify-content: center;
}
.contactusinput {
width: 400px;
border: 1px solid purple;
}
.contactusinput>div {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
.contactusinput label {
width: 200px;
}
.contactusinput input,
.contactusinput textarea {
width: 200px;
padding: 3px;
}
<div class="contactuscontent">
<div class="contactusinput">
<div class="name"><label for="name">Name</label> <input type="text" id="name" name="name"> </div>
<div class="email"><label for="email">Email</label> <input type="text" id="email"> </div>
<div class="refer"><label for="howtofind">How did you find us</label> <textarea id="howtofind"> </textarea> </div>
</div>
</div>
That's because h1 is a block element, and since it's inside an un-styled div, it will push the input in a new line.
If you make the div that wraps the h1 and the input as flexbox, it will look similar to the image:
.contactusinput div {
display: flex;
}
You don't need flexbox on any of the parents for this to work.
To push inputs in the same line you can add min-width to the h1:
h1 {
min-width: 200px;
}
You will need to apply different styling to smaller screens, likely removing the min-width and showing the h1 in a column instead of row.
Here is a jsFiddle
By the way, heading elements h1-h6 aren't meant for this. You generally want to have only one h1 in the entire site. The better option to use here would be label.
To make the design you want, it is needed to set flexbox on the div which contains input and h1.
So in this case, there will be 3 divs to have the flexbox design and all of them are the direct childs of .contactusinput selector.
So on style, you can set the .contactusinput > div (direct div child of .contactusselector) style to flexbox as follows.
.contactuscontent {
border: 1px solid pink;
display: flex;
}
.contactusinput {
display: inline-flex;
flex-direction: column;
border: 1px solid purple;
}
.contactusinput > div {
display: flex;
justify-content: space-between;
align-items: center;
}
<div class="contactuscontent">
<div class="contactusinput">
<div class="name"><h1>Name</h1> <input type="text"> </div>
<div class="email"><h1>Email</h1> <input type="text"> </div>
<div class="refer"><h1>How did you find us</h1> <input type="text"> </div>
</div>
</div>

How to not wrap an item on a new row? [duplicate]

This question already has answers here:
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Closed 3 years ago.
As the picture describes, I want to wrap items as such.
This is my current HTML and CSS.
<div class="column-container">
<div class="col item">1 <- More text and thus taller than the other ones</div>
<div class="col item">2</div>
<div class="col item">3</div>
<div class="col item">4</div>
<div class="col item">5</div>
</div>
.column-container {
display: flex;
justify-content: center;
flex-flow: row wrap;
height: 100%;
}
.item {
height: fit-content;
min-width: 300px;
max-width: 300px;
margin: 10px;
}
Here's a fiddle as well..
https://jsfiddle.net/3Ly5zh4n/1
Flexbox is probably not the best choice for this since flexbox is used to display content next to each other either vertical or horizontal. I'd suggest using CSS Grid instead. It might be a new area for some, but it's a quite good choice for handling columns in CSS.
The following is an example of how it can be used. The method repeat(auto-fill, ...) fills the whole container with either a full fraction for each element, or the minimum width of 150px, which should be 300px in your case.
.column-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
grid-template-rows: 1fr 1fr;
grid-gap: 10px;
height: 300px;
}
.item {
display: flex;
justify-content: center;
align-items: center;
font-size: 36px;
color: white;
background-color: red;
}
.item--first {
grid-row: 1 / span 2;
}
<div class="column-container">
<div class="item item--first">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
</div>
I'd suggest reading css tricks A Complete Guide to Grid for further information. Hope this helps a bit.
.column-container {
display: flex;
flex-flow: row wrap;
height: 100%;
}
.item {
height: fit-content;
min-width: 150px;
max-width: 150px;
margin: 10px;
border: 1px solid black;
}
<div style="display: flex;justify-content: center">
<div class="column-container">
<div class="col item" style="height: 100px;">1 <- More text and thus taller than the other ones</div>
</div>
<div class="column-container">
<div class="col item">2</div>
<div class="col item">3</div>
<div class="col item">4</div>
<div class="col item">5</div>
</div>
</div>
I think this this will do what you want. Its a simpler approach but it behaves the way you explain in your requested image.
HTML:
<div>
<ul>
<!-- I have set the height of this li to 300px to demo the concept. -->
<li class="col item" style="height: 300px">
1 More text and thus taller than the other ones.
</li>
<li class="col item">2</li>
<li class="col item">3</li>
<li class="col item">4</li>
<li class="col item">5</li>
</ul>
</div>
CSS:
ul {
padding: 0;
}
ul .item {
list-style: none;
float: left;
height: 100px;
width: 300px;
margin: 10px;
border: 1px solid red;
}
This should give you a result of:
Result layout
Hope this helps...

How to make elements stack in mobile view?

I have a small image, a form entry field, and a submit button. They are displaying as I want, side by side horizontally on a site on a normal desktop computer.
When the page is viewed on a small screen, the submit button is dropping down to the line below (good), but the image is staying where its at right next to the text entry field. Ideally, I'd like to force the three elements to stack on top of each other in mobile view. So, the top would be the image (centered), then the text entry field, then the submit button.
I have only a very basic understanding of responsive design. Any tips?
Here is the code that represents my situation:
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
.row {
width: 100%;
}
.flex-item {
padding: 0;
margin: 0;
text-align: center;
}
<div class="flex-container">
<div class="row">
<span class="flex-item"><img src="https://images.unsplash.com/photo-1509718752889-8c69d2c6c11d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=1253b775db4261c2618d0b901b115b38" height="65" width="150"></span>
</div>
<div class="row">
<span class="flex-item">
<form action="find.php" method="post">
<input type="text" name="thingy_id" placeholder="Enter Thingy ID" size="13" maxlength="15">
<input type="submit" value="SUBMIT" class="thingyid" />
</form>
</span>
</div>
</div>
#media (max-width: 1024px) {
.flex-container {
display: block;
}
}
Here is a way of doing this with CSS Grid Layout.
It is pretty heavy on the CSS side, but it allows for incredibly minimal HTML.
You basically just set it up so that it will all display in one column, but if the screen is wide enough, you can make it side by side. You could do it the other way around, setting it up to display side by side, but if the screen is too narrow, you make it one column.
body {
display: grid;
grid-gap: 1em;
}
img {
justify-self: center;
}
form {
display: grid;
grid-template-columns: 1fr auto 1fr;
}
input {
margin: 5px;
}
input[type="text"] {
grid-column: 2;
align-self: center;
}
input[type="submit"] {
grid-column: 2;
align-self: center;
}
#media (min-width: 800px) {
body {
grid-template-columns: 1fr auto auto 1fr;
}
img {
grid-column: 2;
}
form {
grid-column: 3;
grid-template-columns: 1fr auto auto 1fr;
}
input[type="text"] {
grid-column: 2;
}
input[type="submit"] {
grid-column: 3;
}
}
<img src="https://images.unsplash.com/photo-1509718752889-8c69d2c6c11d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=1253b775db4261c2618d0b901b115b38" height="65" width="150" />
<form action="find.php" method="post">
<input type="text" name="thingy_id" placeholder="Enter Thingy ID" size="13" maxlength="15">
<input type="submit" value="SUBMIT" class="thingyid" />
</form>
I just really wanted to do this in Grid. You don't have to do it, but it is a way, and I feel like more people should be aware of it.
You could use the extra Bootstrap class shown below to make an element's column take up a whole row on mobile devices. You could then use CSS to adjust the width and size of the elements.
<div class="flex-container">
<div class="row">
<div class="col-xs-12"> // setting the column to 12 makes the
element take up row entire row pushing the neighbouring
elements to move under it
<span class="flex-item"><img
src="https://images.unsplash.com/photo-1509718752889-
8c69d2c6c11d?ixlib=rb-
0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJ
hcHBfaWQiOjE0NTg5fQ%3D%3D&s=1253b775db4261c2618d0
b901b115b38" height="65" width="150">
</span>
</div>
</div>
<div class="row">
<span class="flex-item">
<form action="find.php" method="post">
<div class="col-xs-12"> // Use here
<input type="text" name="thingy_id" placeh
older="Enter Thingy ID" size="13" maxlength="15">
</div>
<div class="col-xs-12"> // Use here
<input type="submit" value="SUBMIT" class="thingyid" />
</div>
</form>
</span>
</div>
</div>

Add items one under another when space available Flexbox

I have the following issue, I have forms with inputs inside flex-wrap, but in few forms I have <textarea rows="5"></textarea> and it takes more height than an input field. Is it possible to have the following structure inside flex-wrap:
Have these 2 input fields one below the other, but in their .holder divs?
https://jsfiddle.net/by06rreq/
CSS:
.flex {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.holder {
width: 30%;
}
input, textarea {
width: 100%;
}
.holder.textarea {
width: 65%;
}
HTML:
<div class="flex">
<div class="holder textarea">
<textarea name="test" rows="5"></textarea>
</div>
<div class="holder">
<input type="text">
</div>
<div class="holder">
<input type="text">
</div>
</div>
You can see.. Do you want like this. I have tried with multi-layout css. through column-count
.flex {
column-count: 2;
column-gap: 0;
}
.holder {
width: 50%;
margin-bottom: 15px;
}
input, textarea {
width: 100%;
}
.holder.textarea {
width: 65%;
margin-bottom: 0;
}
<div class="flex">
<div class="holder textarea">
<textarea name="test" rows="5"></textarea>
</div>
<div class="holder">
<input type="text">
</div>
<div class="holder">
<input type="text">
</div>
</div>
I'm not 100% sure on what you're asking for & I can't comment due to lack of reputation so I have no option to make a guess in an answer.
So you'd like a similar layout to this?
-TextArea-
-Input-
-Input-
You could apply display: block and width: 100% to your .holder class, and then adjust the size of your input controls through their relevant css classes.
If I've completely misunderstood what you're trying to achieve, just comment and I'll see if I can help you further :)
EDIT:
After reading your recent comments I can offer another solution. however, it isn't using flex.
.block {
width: 100%:
display: block;
}
.textarea {
display: inline-block;
width: 30%;
}
.textarea textarea {
width: 100%;
}
.holder {
display: inline-block;
vertical-align: top;
}
.holder input {
display: block;
vertical-align: top;
}
<div class="block">
<div class="textarea">
<textarea class="textarea" name="test" rows="5"></textarea>
</div>
<div class="holder">
<input type="text">
<input type="text">
</div>