CSS/HTML background image fixed issue [duplicate] - html

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
CSS
.as {
width: 300px;
height: 300px;
border: 5px solid red;
position: absolute;
left: 25%;
display: flex;
flex-direction: column;
}
.ab {
width: 60px;
height: 60px;
border: 2px solid green;
background-color: green;
}
.cd {
width: 60px;
height: 60px;
border: 2px solid blue;
background-color: blue;
}
HTML
$div class="as"$
$div class="ab"$123$/div$
$div class="cd"$123$/div$
$/div$
When I run it, I have green and blue boxes in a big red box. The green and blue are on the left corner.
since the parent container, which is "as" has display: flex and flex-direction: column
I can't go to the left bottom corner.
Is there a way to go to the left bottom? besides make it position: relative and bottom: %.
Thank you
Hi, I am practicing CSS

Do you need use property the justify-content with value flex-end to the left bottom corner
Look this example bellow
.as {
width: 300px;
height: 300px;
border: 5px solid red;
display: flex;
flex-direction: column;
margin-bottom: 1rem;
}
.bottom-left {
justify-content: flex-end;
}
.bottom-right {
align-items: flex-end;
justify-content: flex-end;
}
.ab {
width: 60px;
height: 60px;
border: 2px solid green;
background-color: green;
}
.cd {
width: 60px;
height: 60px;
border: 2px solid blue;
background-color: blue;
}
<p>Bottom left</p>
<div class="as bottom-left">
<div class="ab">123</div>
<div class="cd">123</div>
</div>
<p>Bottom right</p>
<div class="as bottom-right">
<div class="ab">123</div>
<div class="cd">123</div>
</div>

Related

Container not fitting to screen and flex-direction: row not working properly

I'm starting out an application in angular and really struggling getting a basic layout setup; I have experience with angular but actual html/css design is completely new to me
Nothing I've tried seems to allow this container to take up the entirety of the screen. I have tried using multiple different settings on the html and container css classes and nothing will actually fit the container to the screen with width; but the height always seems to fit properly.
Aside from this flex-direction: row does not seem to consistently work. For example, I am trying to get the div "side" and the div "main" inside of the header div to fit next to each-other. Instead of this, those div's act like columns; despite the fact I have nowrap on; I have also tried display: inline-block and that also does not work. I have decreased the width of side and main in hopes that they would then fit next to eachother and that also does not work.
Screenshot:
Full View
html {
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
}
.container {
position: relative;
top: 0;
left: 0;
bottom: 0;
right: 0;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.header {
margin-top: 15px;
border: 1px solid red;
height: 15vh;
flex-direction: row;
flex-wrap: nowrap;
}
.body {
border;
1px solid green;
height: 80vh;
}
.side {
width: 15vw;
border: 1px solid yellow;
}
.main {
width: 50vw;
border: 1px solid blue;
}
<div class="container">
<div class="header">
<div class="side">
<p>HI</p>
<div class="main">
<p>HI2</p>
</div>
</div>
<div class="body">
<p>I am the body</p>
</div>
</div>
</div>
Firstly - there are quite a few typos in your code. You haven't closed the side class or the main class and there is no closing div for the side div.
Secondly - After I'd tidied up your code, I noticed that you were making the .container display: flex; when in fact you needed to make the .header display: flex; as this is the parent of the side and main divs.
This is a great guide for flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
This should work for you:
html {
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
}
.container {
position: relative;
top: 0;
left: 0;
bottom: 0;
right: 0;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.header {
margin-top: 15px;
border: 1px solid red;
height: 15vh;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
}
.side {
width: 15vw;
border: 1px solid yellow;
}
.main {
width: 50vw;
border: 1px solid blue;
}
.body {
border: 1px solid green;
height: 80vh;
}
<div class="container">
<div class="header">
<div class="side">
<p>HI</p>
</div>
<div class="main">
<p>HI2</p>
</div>
</div>
<div class="body">
<p>I am the body</p>
</div>
</div>
main is inside side. if you want them to be beside each other, you will need to arrange them as siblings within the flexbox.
you also forgot to add display: flex on the header css.
try this
html {
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
}
.container {
position: relative;
top: 0;
left: 0;
bottom: 0;
right: 0;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.header {
margin-top: 15px;
border: 1px solid red;
height: 15vh;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.body {
border: 1px solid green;
height: 80vh;
}
.side {
width: 15vw;
border: 1px solid yellow;
}
.main {
width: 50vw;
border: 1px solid blue;
}
<div class="container">
<div class="header">
<div class="side">
<p>HI</p>
</div>
<div class="main">
<p>HI2</p>
</div>
</div>
<div class="body">
<p>I am the body</p>
</div>
</div>

Rectangle with grid

I'm having a bit of difficulty creating a rectangle that looks like this. I'm a novice, any help would be great!
This is what I'm trying to recreate:
I know how to make the rectangle, and I'm assuming you would split the rectangle into two sections, where one would use "table" to create the rows for Name, Diagnosis etc.
#box {
margin-top: 1%;
height: 20px;
width: 562px;
border: 1px solid black;
padding: 100px;
}
.container {
display: table;
width: 100%;
}
.left-half {
position: relative;
left: 0px;
}
.right-half {
position: relative;
right: 0px;
}
Solution
Flex grid <3 they are amazing
I have provided you three examples. Rows, columns and an additional example to show more properties of the flex box.
justify-content and align-items are amazing tools to align things quickly.
Example:
/*ExamplE box*/
.example {
float: left;
width: 200px;
height: 100px;
border: 1px solid black;
display: flex;
flex-direction: row; /*Direction of flex*/
justify-content:center; /*horizontally aligns them to center*/
align-items: center; /*Vertically aligns them to center*/
}
.example__children {
width: 5px;
height: 5px;
margin: 0 5px;
border: 1px solid black;
}
/*Column box*/
.column {
float: left;
width: 200px;
height: 100px;
border: 1px solid black;
display: flex;
flex-direction: column;
}
.column__children {
width: 100%;
height: 25%;
border: 1px solid black;
}
/*Row box*/
.row {
float: left;
width: 200px;
height: 100px;
border: 1px solid black;
display: flex;
flex-direction: row;
}
.row__children {
width: 25%;
height: 100%;
border: 1px solid black;
}
<div class="example">
<div class="example__children"></div>
<div class="example__children"></div>
<div class="example__children"></div>
<div class="example__children"></div>
</div>
<div class="column">
<div class="column__children"></div>
<div class="column__children"></div>
<div class="column__children"></div>
<div class="column__children"></div>
</div>
<div class="row">
<div class="row__children"></div>
<div class="row__children"></div>
<div class="row__children"></div>
<div class="row__children"></div>
</div>

Flexbox: justify-content property in Internet Explorer [duplicate]

I have this simple div with a button inside of it. justify-content: center; works fine using Firefox and Chrome, but does not work on IE 11:
#div {
height: 200px;
width: 50px;
border: 1px solid black;
display: flex;
flex: 0 0 auto;
align-items: center;
justify-content: center;
}
#button {
height: 50px;
width: 200px;
min-width: 200px;
border: 1px solid black;
background-color: red;
}
<div id="div">
<button id="button">HELLO</button>
</div>
My goal is that, when I use transform with rotate(90deg) or rotate(270deg), the button will fit into the div:
#div {
height: 200px;
width: 50px;
border: 1px solid black;
display: flex;
flex: 0 0 auto;
align-items: center;
justify-content: center;
}
#button {
height: 50px;
width: 200px;
min-width: 200px;
border: 1px solid black;
background-color: red;
transform: rotate(90deg);
}
<div id="div">
<button id="button">HELLO</button>
</div>
The height and width of the div and button are always the same, but are customizable.
As much as possible, I prefer not wrapping elements.
IE11 needs the parent to have flex-direction: column.
This example has your button rotated:
#div {
height: 200px;
width: 50px;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column
}
#button {
height: 50px;
width: 200px;
min-width: 200px;
border: 1px solid black;
background-color: red;
transform: rotate(90deg);
}
<div id="div">
<button id="button">HELLO</button>
</div>
In my case I had to make the flex container's height 100%. justify-content worked without a problem after that.
I also had to make the (first level) children's max-width 100% to fix some content overflowing horizontally.

Flexbox row pushing columns down

I've been using flex layouts for a while now.
mainly the holy grail type layout and it works well.
Today decided to try create the same thing to help me learn more about flexbox.
but using columns on the right side for sidebar area, and rows as the logo/heading and page div's.
I've put this in a codepen so you can see the markup code yourself.
my issue is the logo/heading and main page when set to row pushes the columns downwards.
<div class="wrapper">
<div class="nav">logo content and site banner goes here
<div class="spacer">
</div>
<div class="content">main page content goes here</div>
</div>
<div class="one">page index goes here</div>
<div class="two">twitter logo goes here</div>
<div class="three">chatbox goes here</div>
</div>
div {
box-sizing: border-box;
}
.wrapper {
border: 2px solid blue;
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: flex-end;
position: relative;
}
.nav {
flex-direction: row;
border: 2px solid green;
width: 100%;
height: 100px;
align-self: flex-start;
padding-left: 8px;
margin-bottom: 4px;
}
.spacer {
height: 140px;
width: 100%;
}
.content {
flex-direction: row;
border: 2px solid green;
height: 40px;
padding-left: 8px;
align-self: flex-start;
}
.one {
width: 200px;
height: 200px;
border: 2px solid red;
flex-grow: 0;
padding: 8px;
}
.two {
width: 200px;
height: 200px;
border: 2px solid red;
flex-grow: 0;
padding: 8px;
}
.three {
width: 200px;
height: 200px;
border: 2px solid red;
flex-grow: 0;
padding: 8px;
}
https://codepen.io/anon/pen/GmZWvp
even if setting the width to something smaller before it even reaches the columns
the block pushes the columns, down, how could i do this where it doesn't effect the column on the right?
I know i could position: absolute the main page div, or create a nested div in logo/heading
and give it height of say 120px; to get it to go below the logo heading.
But there must be a way of putting a row next to a column without it pushing the column down.
since the div/row for the logo heading/main page is a block element.
If you know how to do this please let me know what to change, where and why etc.
Again this isn't for production, it's just to see if I can solve this quirky puzzle.
First, even if you set display: flex on the wrapper, you can't set flex-direction: row (or column) on its children to change their direction. The flex-direction property should be set on the flex container, in this case the wrapper. If you want to change direction on flex items belonging to one flex container, you need to wrap them and make that nested wrapper be both a flex item and a flex container, here done with the side wrapper in my first sample
If you rearrange your markup a little, you can get this
div {
box-sizing: border-box;
}
.wrapper {
border: 2px solid blue;
display: flex;
}
.nav {
border: 2px solid green;
width: 100%;
height: 100px;
padding-left: 8px;
margin-bottom: 4px;
}
.content {
flex: 1;
border: 2px solid green;
height: 40px;
padding-left: 8px;
}
.side {
width: 200px;
}
.one {
height: 200px;
border: 2px solid red;
padding: 8px;
}
.two {
height: 200px;
border: 2px solid red;
padding: 8px;
}
.three {
height: 200px;
border: 2px solid red;
padding: 8px;
}
<div class="nav">logo and site banner goes here</div>
<div class="wrapper">
<div class="content">main page content goes here</div>
<div class="side">
<div class="one">page index goes here</div>
<div class="two">twitter logo goes here</div>
<div class="three">chatbox goes here</div>
</div>
</div>
If you can't, or don't want, to change markup, then you need to combine flexbox with absolute positioning.
div {
box-sizing: border-box;
}
.wrapper {
position: relative;
display: flex;
flex-direction: column;
}
.nav {
border: 2px solid green;
width: 100%;
height: 100px;
padding-left: 8px;
margin-bottom: 4px;
}
.content {
width: calc(100% - 200px);
border: 2px solid green;
height: 40px;
padding-left: 8px;
}
.one, .two, .three {
position: absolute;
right: 0;
width: 200px;
height: 200px;
border: 2px solid red;
padding: 8px;
}
.one {
top: 104px;
}
.two {
top: 304px;
}
.three {
top: 504px;
}
<div class="wrapper">
<div class="nav">logo and site banner goes here</div>
<div class="content">main page content goes here</div>
<div class="one">page index goes here</div>
<div class="two">twitter logo goes here</div>
<div class="three">chatbox goes here</div>
</div>

How can I align 2 buttons on the middle of the left and right sides of a div

I have a frame containing a picture, I would like to add 2 buttons on the left and right side of it so users can click on those to view different pictures. I can handle JavaScript but I can't figure out a way to align these 2 buttons in proper positions. Thank you.
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft {
float: left;
background: red;
}
.btnRight {
float: right;
background: blue;
}
<div class="frame">
<div class="content"></div>
</div>
<div class="btn btnLeft"></div>
<div class="btn btnRight"></div>
You were on the right track but needed justify-content: space-between; not justify-content: center; and you needed to put .btnLeft and .btnRight inside .frame.
Here's what different values of justify-content do:
Image from CSS-Tricks
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: space-between; /* not center */
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft{
background: red;
}
.btnRight {
background: blue;
}
<div class="frame">
<div class="btn btnLeft"></div>
<div class="content"></div>
<div class="btn btnRight"></div>
</div>
You may want to put the buttons inside the frame so you can reference the left and right positions. Then make those buttons position:absolute then set left and right position for each buttons
Code:
.frame {
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.btn{
position: absolute;
top: 50%;
transform:translateY(-50%);
}
.btnLeft{
left: 0;
}
.btnRight{
right: 0;
}
Updated fiddle:
https://jsfiddle.net/y0ty7t80/3/
First you could set the position of the frame to relative so it gets set as a root for following positionings. You could then set the positions of the two buttons both to "absolute" and put them inside of your frame so they get taken out of the text flow. By setting both to a left/right property of 0 and a top property of 50% they get placed exactly in the middle of the frame. Heres an example of what i mean:
.frame {
position:relative;
background: #e0e0e0;
width: 300px;
height: 350px;
border: 1px solid #b9b9b9;
display: flex;
justify-content: center;
align-items: center;
}
.content {
background: #FFF;
width: 200px;
height: 200px;
}
.btn {
width: 15px;
height: 20px;
}
.btnLeft{
position:absolute;
top:50%;
left:0;
background: red;
}
.btnRight {
position:absolute;
top:50%;
right:0;
background: blue;
}
<div class="frame">
<div class="btn btnLeft"></div>
<div class="btn btnRight"></div>
<div class="content"></div>
</div>