I am in the middle of assignment. I am trying to make https://joseff-regmi.github.io/Tutangle/ responsive, I am having trouble with the body part
and most of the css3 features (flex..) are not allowed. how can I stack middle box on the top and followed by left and right on mobile version
[![enter image description here][2]][2]
on mobile:
[2]: https://i.stack.imgur.com/M80Co.png
You can use flexbox and its order property :
.parent {
background-color: blue;
height: 400px;
width: 400px;
display: flex;
flex-direction: column;
}
#child1 {
background-color: grey;
height: 100px;
margin: 20px;
order: 2;
}
#child2 {
background-color: green;
height: 100px;
margin: 20px;
order: 1;
}
#child3 {
background-color: aqua;
height: 100px;
margin: 20px;
order: 3;
}
<div class="parent">
<label for="">This is parent</label>
<div class="child" id="child1">This is child1</div>
<div class="child" id="child2">This is child2</div>
<div class="child" id="child3">This is child3</div>
</div>
if you do not want to use flexbox, then this is a way to get your work done
show/hide desktop and mobile div based on needs using media query,
for example: display:block; for class desktop,
and display:none; for class mobile and vice-versa
<div class="parent desktop">
<label for="">This is parent</label>
<div class="child" id="child1">This is child1</div>
<div class="child" id="child2">This is child2</div>
<div class="child" id="child3">This is child3</div>
</div>
<div class="parent mobile">
<label for="">This is parent</label>
<div class="child" id="child2">This is child2</div>
<div class="child" id="child1">This is child1</div>
<div class="child" id="child3">This is child3</div>
</div>
Related
I have this code:
.parent {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
align-items: flex-start;
align-content: flex-start;
width: 200px;
height: 200px;
}
.child,
.child:not(:nth-child(1)) {
width: 100px;
height: 100px;
}
.child:last-child:nth-child(1) {
width: 200px;
height: 200px;
}
.child:nth-child(1),
.child:last-child:nth-child(2) {
width: 100px;
height: 200px;
}
.child:nth-last-child(4) {
width: 100px;
height: 100px;
}
Test with 1 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
</div>
Test with 2 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
<div class="child" style="background-color:blue;">2</div>
</div>
Test with 3 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
<div class="child" style="background-color:blue;">2</div>
<div class="child" style="background-color:green;">3</div>
</div>
Test with 4 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
<div class="child" style="background-color:blue;">2</div>
<div class="child" style="background-color:green;">3</div>
<div class="child" style="background-color:orange;">4</div>
</div>
But I wish the last example had a different order, that is:
1 2
3 4
The important thing is that I cannot add different classes for each of the flex children.
The Twitter gallery behaves similarly, but it's more complicated there (it's flex in flex and probably some JS, although it probably works differently).
I need all the above-mentioned cases to work simultaneously.
You can achieve this by using grid instead of flex. Take a look at this:
.parent {
display: grid;
grid-template-columns: 100px 100px;
grid-template-rows: 100px 100px;
width: 200px;
height: 200px;
}
/* in case there is only one child */
.child:first-child:nth-last-child(1){
grid-column: 1 / span 2;
grid-row: 1 / span 2;
}
/* in case there are 2 children */
.child:first-child:nth-last-child(2),
.child:first-child:nth-last-child(2) ~ .child{
grid-row: 1 / span 2;
}
/* in case there are 3 children, make the first one span 2 rows */
.child:first-child:nth-last-child(3) {
grid-row: 1 / span 2;
}
Test with 1 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
</div>
Test with 2 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
<div class="child" style="background-color:blue;">2</div>
</div>
Test with 3 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
<div class="child" style="background-color:blue;">2</div>
<div class="child" style="background-color:green;">3</div>
</div>
Test with 4 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
<div class="child" style="background-color:blue;">2</div>
<div class="child" style="background-color:green;">3</div>
<div class="child" style="background-color:orange;">4</div>
</div>
In that case, you need to structure your flex grid like this:
.parent {
display: flex;
flex-wrap: wrap;
width: 200px;
height: 200px;
}
.child {
width: 100px;
height: 100px;
}
Test with 4 children
<div class="parent" style="background-color:yellow;">
<div class="child" style="background-color:red;">1</div>
<div class="child" style="background-color:blue;">2</div>
<div class="child" style="background-color:green;">3</div>
<div class="child" style="background-color:orange;">4</div>
</div>
Note that instead of using flex-direction: column we use flex-wrap: wrap to push the elements to the next row.
This question already has answers here:
Maintain the aspect ratio of a div with CSS
(37 answers)
Closed 2 years ago.
I devide the div into two parts, and achieve with Flex Box in each part.
<!--My Trials-->
<body>
<div>
<div class="container1" style="display: flex;">
<div class="item1" style="flex:1;background-color: yellowgreen;">1</div>
<div class="item1" style="flex:1;background-color: lightseagreen;">2</div>
<div class="item1" style="flex:1;background-color: palevioletred">3</div>
</div>
<div class="container2" style="display: flex;">
<div class="item2" style="flex:1;background-color: lightskyblue;">4</div>
<div class="item2" style="flex:2;visibility: hidden;">5</div><!-- hide the 5th div -->
</div>
</div>
</body>
I wonder how to turn each div into a square.
And Is there anyway can achive the layout without the help of the 5th div?
.container {
display: flex;
flex-wrap: wrap;
}
.item1 {
height: 100px;
width: 33%;
background-color: lightblue;
color: black;
}
.item2 {
height: 100px;
width: 33%;
background-color: lawngreen;
color: black;
}
.item3 {
height: 100px;
width: 33%;
background-color: pink;
color: black;
}
.item4 {
height: 100px;
width: 33%;
background-color: orange;
color: black;
}
<body>
<div class="container">
<div class="item1">This is square 1</div>
<div class="item2">This is square 2</div>
<div class="item3">This is square 3</div>
<div class="item4">This is square 4</div>
</div>
</body>
The flex-wrap property allows elements to move to the next row when there is no more space on the current row. Making it completely responsive. And the width property is set to take up 33% of the view port window at all times.
Let me know if that works or if you need help with anything.
I have some structure like this:
.divParent{
height: 100px;
}
.div1 {
height: 40px;
background: blue;
}
.div2{
height: 150px;
background: yellow
}
<div class="divParent">
<div class="div1">Div 1</div>
<div class="div2">Div 2</div>
</div>
<div class="divNew">
<span>Hello</span>
</div>
I want divNew to go to next line and not get overlapped by content of divParent. I tried so many things but nothing is working out.
I know, I can use <br /> tag to do this but I don't want to use that. Is there any other solution.
Use flex
.divParent{
clear:both;
height: 100px;
}
.div1 {
height: 40px;
background: blue;
}
.div2{
height: 150px;
background: yellow
}
.divParent, .divNew {
display: flex;
flex: 1;
flex-direction: column;
}
.divNew {
background:red;
}
<div class="divParent">
<div class="div1">Div 1</div>
<div class="div2">Div 2</div>
</div>
<div class="divNew">
<span>Hello</span>
</div>
You don't need to remove the height, your second div is actually causing an issue. It's bigger than parent. So either increase the parent or decrease div2.
I have a problem with a translated flex item still occupying its original space so that the following flex item does not immediately follow. Please see JSFiddle or snippet for more details. I want the green rectangle to follow immediately after the red so that is will be visible within the border. I can unfortunately not translate the parent, it will have overflow: hidden so that only the child within the border is visible.
Here is a JSFiddle https://jsfiddle.net/3wv9d9dm/
Or a snippet:
.parent {
display: flex;
width: 100px;
margin-left: auto;
margin-right: auto;
border: 1px solid black;
}
.child {
flex: 0 0 100%;
}
<div class="parent">
<div class="child" style="background-color:red;transform:translateX(-100%);"><h1>1</h1></div>
<div class="child" style="background-color:green;"><h1>2</h1></div>
<div class="child" style="background-color:blue;"><h1>3</h1></div>
</div>
From MDN, emphasis mine:
The <transform-function> CSS data type denotes a function used to modify an element's appearance.
Transforming an element only modifies appearance, not position in the document flow. This means that even though the element appears to have moved its position in the DOM it continues to affect sibling/other elements because its physical dimensions remain in place prior to the transformation.
A way to get around this is to animate or modify properties that affect document flow such as margin.
.parent {
display: flex;
width: 100px;
margin-left: auto;
margin-right: auto;
border: 1px solid black;
}
.child {
flex: 0 0 100%;
}
<div class="parent">
<div class="child" style="background-color:red;margin-left:-100%;">
<h1>1</h1>
</div>
<div class="child" style="background-color:green;">
<h1>2</h1>
</div>
<div class="child" style="background-color:blue;">
<h1>3</h1>
</div>
</div>
An alternate way is to transform all elements together. This method is more performant (as it skips the layout and paint steps of the browser rendering pipeline). Visit this article on Rendering Performance for a detailed explanation.
One possible way of doing this:
let children = document.querySelectorAll('.child');
[].forEach.call(children, (child) => {
child.style.transform = 'translate(-100%)';
});
.parent {
display: flex;
width: 100px;
margin-left: auto;
margin-right: auto;
border: 1px solid black;
}
.child {
flex: 0 0 100%;
}
<div class="parent">
<div class="child" style="background-color:red;">
<h1>1</h1>
</div>
<div class="child" style="background-color:green;">
<h1>2</h1>
</div>
<div class="child" style="background-color:blue;">
<h1>3</h1>
</div>
</div>
I have a flexbox layout with two columns.
The left column be fixed, while the right is scrollable.
How can you do that?
Take a look at the following code:
#parent {
display: flex;
}
#left {
flex-grow: 1;
}
#left div {
position: fixed;
background: blue;
top: 0;
left: 0;
height: 100vh;
}
#right {
flex-grow: 5;
background: red;
height: 300vh;
}
<div id="parent">
<div class="child" id ="left">
<div>
ABC
</div>
</div>
<div class="child" id ="right">
DEF
</div>
</div>
Fiddle
If I understand your requirements, you want make the right scroll and the left be fixed. That can be done without the use of fixed position.
I would also personally recommend to not use fixed position, unless it is absolutely necessary, as it can have some unwanted behavior on mobile device, and you kind of loose the benefit non positioned solutions like flexbox offer.
html, body {
margin: 0;
}
#parent {
display: flex;
height: 100vh;
}
#left {
flex-grow: 1;
background: blue;
}
#right {
flex-grow: 5;
background: red;
overflow: auto;
}
#right div {
height: 300vh;
}
<div id="parent">
<div class="child" id ="left">
ABC
</div>
<div class="child" id ="right">
<div>
DEF
</div>
</div>
</div>
here you go
#parent {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
align-items: stretch;
}
#left {
flex-grow: 1;
background: lightgray;
}
#right {
flex-grow: 5;
background: red;
overflow-y: auto;
}
.scroll {
height: 300vw;
}
<div id="parent">
<div class="child" id="left">
<div>
ABC
</div>
</div>
<div class="child" id="right">
<div class="scroll">
DEF
</div>
</div>
</div>
You wrote in the comments:
the width of the fixed div is not the parents width.
When you add position: fixed to an element, you remove it from the normal flow of the document and position it relative to the viewport.
The fixed element has no reason to recognize the parent's width in the HTML structure.
You may be able to achieve what you want by making the entire layout fixed (like in this answer). Otherwise, you'll need a JavaScript alternative.