This question already has answers here:
Prevent flex items from rendering side to side
(3 answers)
Closed 5 years ago.
I'm trying to place some <h1></h1> text, and an email form from Angular Material, into the center of a <div></div> section that has a colored background. The items are coming out stacked on top of each other, as if they were layers. The email form needs to be underneath the <h1></h1> tags. The only way I could get this to align properly was with position:flex, which I suspect is the underlying cause.
Here's my html and css:
<div class="top-section">
<h1 class="top-h1">
mytitle
</h1>
<md-input-container class="email-form" color="accent">
<input mdInput placeholder="Email us" value="Your email address">
</md-input-container>
</div>
.top-section {
height: 350px;
background-color: #04041b;
align-items: center;
display: flex;
justify-content: center;
}
.top-h1 {
color: #E8E8E8;
font-size: 60px;
position: absolute;
}
.email-form {
color: white;
font-size: 30px;
}
Any thoughts?
You're using position: absolute on the h1 which removes it from the flow of the page, and positions it relative to it's closest positioned parent. So other elements won't flow around it. Remove that. Then your items will display side-by-side since the default flex-direction for a flex parent is row. To display the items vertically, use flex-direction: column and the items will stack on top of one another in a column instead of side-by-side in a row.
.top-section {
height: 350px;
background-color: #04041b;
align-items: center;
display: flex;
justify-content: center;
flex-direction: column;
}
.top-h1 {
color: #E8E8E8;
font-size: 60px;
}
.email-form {
color: white;
font-size: 30px;
}
<div class="top-section">
<h1 class="top-h1">
mytitle
</h1>
<md-input-container class="email-form" color="accent">
<input mdInput placeholder="Email us" value="Your email address">
</md-input-container>
</div>
I would imagine this is due to the fact that you have .top-h1 with the position set to absolute.
Create something like the following and it should sort out your issue:
<div class="container">
<h1>Example</h1>
<div class="form">
<!-- ignore this div, this is an example of where your form will be. -->
</div>
</div>
.container {
height: 350px;
background-color: #E0E0E0;
width: 100%;
margin: auto;
text-align: center;
}
.form {
width: 100%;
margin: auto;
}
This should do what you want. If I have misunderstood the question please reply and I can adjust my response but this is what I get from you wanting the elements not to stack and for the form to be located under the h1 but remain centred.
For future reference, if you ever need to align a div, give it a width and then use margin: auto;
Best of luck.
Related
I am trying to place 4 divs over a background image that would keep relative position and size as the browser is resized.
Here is the desired layout:
I have a big background (pink) that is placed with:
#screenFiller {
width: 100vw;
height: 100vh;
background-size: contain;
background-image: url("myimg.png");
background-repeat: no-repeat;
}
There are 4 main divs (red boxes). The two tops ones (side by side) contain a button each (blue boxes) with text (white squiggly lines) above the buttons but still in the red. I am positioning the divs next to each other using:
.flex {
display: flex;
justify-content: Left;
flex: none;
}
The main problem I am having is with the two top divs in that one is not keeping its height (it shrinks to content). I do understand that I will need to handle reducing the text size using a media query or something.
The stripped-down HTML looks like:
<div class="flex ">
<div class="boxme">
<div>
<p class="boldtext">Blha blah balh</p>
<div style="margin:10%;">
<button type="button" class="bigbut gborder5" onclick="window.location.href=''">Syart New</button>
</div>
</div>
</div>
<div class="boxme marl100">
<div>
<p class="boldtext">blah</p>
<div>
<button type="button" class="bigbut gborder5" onclick="window.location.href=''">Start New</button>
</div>
</div>
</div>
</div>
With boxme being:
.boxme {
background-color: white;
width: 25%;
height: 10%;
text-align: center;
}
Finally, all four divs are wrapped in a div with the following css
.relpos {
position: relative;
top: 36%;
left: 4%;
width: 85%
}
Please feel free to take me on an alternate path.
Bootstrap is available if that helps but currently, everything is just HTML and CSS.
Thank you in advanced for any consideration.
Normally, you never want to have fixed width, height, margin or paddings.
For your question, your flex values should be to put your content in the bottom left corner:
.container {
display: flex;
align-items: flex-end;
flex-direction: row; // default
}
Here is the full example that can be run as code snippet:
body {
height: 100vh;
}
body {
background-color: #fcf;
}
.container {
display: flex;
align-items: flex-end;
gap: 20px;
height: 100%;
padding: 20px;
}
.container>div {
background-color: #faa;
padding: 10px;
}
<div class="container">
<div>
<p>Text</p>
<button>Cick</button>
</div>
<div>
<p>Text</p>
<button>Cick</button>
</div>
</div>
I would strongly advise you to learn the fundamentals of html and css from other resources.
This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 1 year ago.
I know this question came up many times but none of the suggested solutions is working for me so I opened up a code sandbox and made an example.
What I want
I want to place an item at the bottom of a div that already has some children. In my code sandbox the desired div to place at the bottom has a className of note__actions.
What I tried
Setting the container to position: relative and the item to position: absolute
Using flexbox and setting the container to display: flex and the item to align-self: flex-end
Multiple other things like vertical-align, and making empty items to fill up the space in-between
Div setup
<div className="flexBoxContainer" />
<div className="item">
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
<div className="bottom__item">Actions>/div> // Only this div should go to the bottom of the "item" div
</div>
</div>
Sandbox
https://codesandbox.io/s/infallible-sound-wf166?file=/src/App.js
Give the container display: flex; justify-content: space-between; - that will pull the content from top to bottom. If you want only the last item on the bottom and the rest on the top, wrap the rest with a div, like
<div className="flexBoxContainer" />
<div className="item">
<div>
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
</div>
<div className="bottom__item">Actions>/div>
</div>
</div>
You are already using flexbox which is great. Now, use margin-top:auto on note__actions to push the div to the bottom.
.note__actions {
margin-top: auto;
}
You could add a flex: 1; to the flexible content, for instance
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
height: 400px;
width: 200px;
padding: 24px;
display: flex;
flex-direction: column;
}
.note-content {
flex: 1;
}
.note-action {
align-self: flex-end;
}
<div class="note">
<div>Peter Holbert - 09. Jul 21 23:28</div>
<div>Note Title</div>
<div class="note-content">Aldus PageMaker including versions of Lorem Ipsum.</div>
<div class="note-action">Action 1</div>
</div>
You can use a grid for that.
Change this in your Style on codesandbox.io:
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
width: 200px;
padding: 24px;
display: grid;
/*flex-direction: column;*/
grid-template-rows: 50px 80px 1fr 80px; /* height: 100%; */
/* align-items: flex-end; */
/* position: relative; */
}
.note__actions_bot {
background-color: red;
height: 100px;
display: grid;
justify-content: center;
align-items: center;
}
I recommend this awesome Video to get a nice overview and some hint's how to do, and when to use which one of flex, grid and other solutions:
https://www.youtube.com/watch?v=qm0IfG1GyZU&feature=youtu.be
Add margin-top : auto to the note__actions element
This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 2 years ago.
So, I am new at coding. I was trying to make a very basic static webpage of a calculator using html and css and js.
This is the html
#input {
margin: 0 auto;
}
#num {
border-radius: 25px;
background: #73AD21;
display: inline-block;
padding: 15px;
width: 200px;
height: 100px;
}
<body>
<div id="input">
<div id="num">
<label for="num1">Enter the first number</label>
<input type="number" name="num1" id="num1">
</div>
<div id="num">
<label for="num2">Enter the second number</label>
<input type="number" name="num2" id="num1">
</div>
</div>
<div id="op">
<div id="opadd"><input type="submit" name="add" class="add" value="Add" onclick="add()"></div>
<div id="opsbtrct"><input type="submit" name="subtract" class="sbtrct" value="Subtract" onclick="sbtrct()"></div>
<div id="opmult"><input type="submit" name="multiply" class="mult" value="Multiply" onclick="mult()"></div>
<div id="opdvde"><input type="submit" name="divide" class="add" value="Divide" onclick="dvde()"></div>
</div>
</body>
I want that the #num be centered horizontally.
I tried using
margin: auto;
and
margin: 0 auto;
but nothing works. Please help.
I've been at it for hours.
Here is the complicated way of doing this.
You should create a container div, so you can center the object in.
#container {
display: flex; /* establish flex container */
flex-direction: row; /* default value; can be omitted */
flex-wrap: nowrap; /* default value; can be omitted */
justify-content: space-between; /* switched from default (flex-start, see below) */
background-color: lightyellow;
}
#container > div {
width: 100px;
height: 100px;
border: 2px dashed red;
}
<div id="container">
<div></div>
<div></div>
<div></div>
</div>
when you add margin: 0 auto be sure the html tag not inline or inline-block it should be 'block' css with specific width.
#num {
border-radius: 25px;
background: #73AD21;
display: block;
padding: 15px;
width: 200px;
height: 100px;
margin: 1em auto;
}
label{
white-space: nowrap;
}
just replace this code hope your problem will fix.
add white-space:nowrap for the label to use one line text. #num should be 'block' css with specific width like 200px and display block. thanks
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
The header is aligning to the center, and the checkbox-div to the next line, what css should i use to align the checbox-div to the right end on the same line as the header!
Here is the sample code!
<div class="header-wrapper">
<h2 class="header">Header</h2>
<div class="checkbox-div">
<input type="checkbox" class="checkbox" value="Some Value" id="checkbox">
<label for="sub-folder-checkbox">Some Name</label>
</div>
</div>
Currently the css am using is,
.header {
text-align: center;
}
Thanks in advance!
Here is a flexbox Example using an empty div as a "spacer" Element. I left comments in the code that explain what the code below them does. I added colors to some elements so you can see what happens to them.
.header {
text-align: center;
}
.header-wrapper {
display: flex;
/*We want 1 row and we dont want items to wrap into other rows*/
flex-flow: row nowrap;
width: 100%;
background-color: green;
/*Positions elements to the start, end and whatever is between while keeping some space between them */
justify-content: space-between;
/*You can add this if you also want to horizontally align items*/
align-items: center;
}
/*gives all divs (the spacer and the checbox-div) inside of the header-wrapper the same size and leaves the rest of space for the header, with this the header is centered and looks better*/
.header-wrapper div {
width: 33%;
}
.checkbox-div {
background-color: red;
}
<div class="header-wrapper">
<!--Add an empty container to fill in the place on the left-->
<div class="empty-div"></div>
<h2 class="header">Header</h2>
<div class="checkbox-div">
<input type="checkbox" class="checkbox" value="Some Value" id="checkbox">
<label for="sub-folder-checkbox">Some Name</label>
</div>
</div>
Here is a second snippet with a different solution, code is commented for explanation again.
.header-wrapper {
/*make the container a flex item and make it relative*/
display: flex;
position: relative;
width: 100%;
background-color: green;
/*Center the header*/
justify-content: center;
/*if horizontal centering is required add this*/
align-items: center;
}
.checkbox-div {
/*give the div an absolute position inside the parent container all the way on the right*/
position: absolute;
right: 0;
background-color: red;
}
<div class="header-wrapper">
<h2 class="header">Header</h2>
<div class="checkbox-div">
<input type="checkbox" class="checkbox" value="Some Value" id="checkbox">
<label for="sub-folder-checkbox">Some Name</label>
</div>
</div>
I recommend using CSS grid (Basic Concepts of grid layout (on MDN)).
We make the wrapper a grid with three columns. The first column is ignored, the header goes into the second one and the checkbox div in the last one.
Then we align (vertical) and justify (horizontal) the grid items (i.e. the header and the div).
Note that I added borders to help see the boxes.
Also note that in your example code, the id of the checkbox doesn't match the for attribute on the label.
Here's the code:
.header-wrapper {
display: grid;
/* Creates three equally sized columns. */
grid-template-columns: 1fr 1fr 1fr;
align-items: center;
/* Centering is done with this.
* Also centers the div. */
justify-items: center;
}
.header {
grid-column: 2;
border: 1px blue solid;
}
.checkbox-div {
grid-column: 3;
border: 1px red solid;
/* If you don't want to center the checkbox div: */
justify-self: end;
}
<div class="header-wrapper">
<h2 class="header">Header</h2>
<div class="checkbox-div">
<input type="checkbox" class="checkbox" value="Some Value" id="sub-folder-checkbox" />
<label for="sub-folder-checkbox">Some Name</label>
</div>
</div>
I need to align 3 items inside Div.
Every item must be placed vertically in the center.
First item 2% margin-left.
Second item in the center and must be a textbox.
Third item 2% margin-right.
Item First and Third must fit the div(square).
I used alignment but it didn't work. Here there is a graphic example:
HTML
<div id="container">
<div class="box" id="left"></div>
<input type="text" name="search" placeholder="SEARCH HERE" id="searchbox">
<div class="box" id="right"></div>
</div><!-- end #container-->
CSS
#container {
display: flex;
justify-content: space-between;
align-items: center;
width: 95%;
height: 90px;
background-color: orangered;
}
.box {
height: 75px;
width: 75px;
background-color: lightgreen;
}
#left {
margin-left: 2%;
}
#right {
margin-right: 2%;
}
input {
width: 250px;
padding: 18px;
font-size: 2em;
}
DEMO: http://jsfiddle.net/xptx9wxn/2/
For more information about CSS Flexbox visit:
A Complete Guide to Flexbox
Using CSS flexible boxes - Web developer guide | MDN