Flex left element position to the top - css

How can I place the blue box next to red box at the top. The yellow box should be below the blue box.
I cannot change the HTML structure. And also I cannot use margin-top for the blue box because the height of the boxes will change dynamically.
Please help me! Thanks!
Here is my code:
.wrapper {
width: 80%;
margin: 0 auto;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
.item {
float: left;
padding: 0 8px;
box-sizing: border-box;
}
.item1 {
width: 60%;
height: 400px;
background-color: red;
}
.item2 {
width: 60%;
height: 100px;
background-color: green;
}
.item3 {
width: 30%;
height: 200px;
background-color: blue;
}
.item4 {
width: 30%;
height: 500px;
background-color: yellow;
}
<div class="wrapper">
<div class="item item1"></div>
<div class="item item2"></div>
<div class="item item3"></div>
<div class="item item4"></div>
</div>

there is a typo error in your CSS file.
in your second item selector, you wrote itme2, and here is the problem.
❌.itme2 {...}
✅.item2 {...}
the logic is fine! is just a typo
.wrapper {
width: 80%;
margin: 0 auto;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
.item {
float: left;
padding: 0 8px;
box-sizing: border-box;
}
.item1 {
width: 60%;
height: 400px;
background-color: red;
}
.item2 {
width: 60%;
height: 100px;
background-color: green;
}
.item3 {
width: 30%;
height: 200px;
background-color: blue;
}
.item4 {
width: 30%;
height: 500px;
background-color: yellow;
}
<div class="wrapper">
<div class="item item1"></div>
<div class="item itme2"></div>
<div class="item item3"></div>
<div class="item item4"></div>
</div>

Related

CSS problem with divs and inline components

Imagine a code like this:
.div {
width: 100%;
height: 100px;
background-color: green;
}
.div1 {
width: 100px;
height: 100px;
background-color: red;
}
.div2 {
width: 100px;
height: 100px;
background-color: blue;
}
.main {
background-color: yellow;
}
<div class="main">
<div>
<div class="div"></div>
<div class="div1"></div>
</div>
<div class="div2"></div>
</div>
It will render something like this:
I want that the blue div comes up and stay on the right of the red div. Imagine that I can´t change the divs from where they are, so I need to do it in css. How can I do it?
Without changing the markup, if you set float: left to the red <div> then you could put the blue <div> to its right side
.div {
width: 100%;
height: 100px;
background-color: green;
}
.div1 {
width: 100px;
height: 100px;
background-color: red;
float: left;
}
.div2 {
width: 100px;
height: 100px;
background-color: blue;
display: inline-block;
vertical-align: top;
}
.main {
background-color: yellow;
}
<div class="main">
<div>
<div class="div"></div>
<div class="div1"></div>
</div>
<div class="div2"></div>
</div>
The previous solution which uses float on the red div works well, but here is another possible solution:
Apply position: relative; to the blue div (to be able to move it in relation to its default position) and add top: -100px; left: 100px; to move it up next to the red div:
.div {
width: 100%;
height: 100px;
background-color: green;
}
.div1 {
width: 100px;
height: 100px;
background-color: red;
}
.div2 {
width: 100px;
height: 100px;
background-color: blue;
position: relative;
top: -100px;
left: 100px;
}
.main {
background-color: yellow;
}
<div class="main">
<div>
<div class="div"></div>
<div class="div1"></div>
</div>
<div class="div2"></div>
</div>
This can also be done with the grid CSS. Here I used a named template box and then in the "chatty verbose" CSS I put the positional related for each "block". I added classes to the CSS just for clarity but you could update to your classes.
I added some color and things just for clarity and visual references but kept the "position relate" in separate CSS chunks.
.main {
font-size: 2rem;
display: grid;
grid-template: "box";
background-color: yellow;
}
.main *,
.main::before {
grid-area: box;
}
.green-block {
place-self: start;
}
.red-block {
width: 50%;
place-self: end start;
}
.blue-block {
width: 50%;
place-self: end end;
}
.green-block {
height: 3rem;
background-color: green;
}
.red-block {
height: 3rem;
background-color: red;
}
.blue-block {
background-color: blue;
}
.blue-block,
.green-block,
.red-block {
/* color for clarity and just to super center the text in the blocks */
display: grid;
color: cyan;
font-family: sans-serif;
text-transform: uppercase;
place-items: center;
}
<div class="main">
<div>
<div class="div green-block">green</div>
<div class="div1 red-block">red</div>
</div>
<div class="div2 blue-block">blue</div>
</div>

Keep div scrollable with visible shadow

This is the situation:
body {
margin: 0;
border: 0;
padding: 0;
}
.one {
margin: 0;
border: 0;
padding: 20px;
width: 200px;
height: 200px;
background-color: red;
}
.two {
margin: 0;
border: 0;
padding: 0;
width: 200px;
height: 200px;
background-color: green;
overflow-y: auto;
}
.three {
margin: 0;
border: 0;
padding: 0;
background-color: blue;
width: 200px;
height: 100px;
box-shadow: 0 0 10px 10px black;
}
<div class="one">
<div class="two">
<div class="three"></div>
<div class="three"></div>
<div class="three"></div>
</div>
</div>
As you can see the property overflow-y: auto; cuts off the shadow of the elements with class three.
What I would like to have is something like this, but with containing block scrollable:
Is there a different way other then shrinking the three elements?
why not an inset shadow
body {
margin: 0;
border: 0;
padding: 0;
}
.one {
padding: 20px;
height: 200px;
width:200px;
background-color: red;
}
.two {
height: 200px;
background-color: green;
overflow-y: auto;
}
.three {
background-color: blue;
height: 100px;
box-shadow: 0 0 10px 10px inset black;
}
<div class="one">
<div class="two">
<div class="three">
</div>
<div class="three">
</div>
<div class="three">
</div>
</div>
</div>

Border radius not dependent on height and width

I need a border-radius rule that will make that element will be rounded the same not matter what is the size(height) of the element.
Here is my example:
<div class="container">
<div class="element first"></div>
<div class="element sec"></div>
<div class="element third"></div>
</div>
$blue: #0084ff;
.container {
display: flex;
flex-flow: column;
.element {
background-color: $blue;
border-radius: 100px;
width: 500px;
margin-bottom: 1px;
&.first{
height: 50px;
}
&.sec{
height: 150px;
}
&.third{
height: 250px;
}
}
}
https://jsfiddle.net/ballkar/75tda12q/29/
Here is effect that I need to achive:
you have to apply he border-radius: 25px; instead of border-radius: 100px;.
for more understanding look the following snippet:
.container {
display: flex;
flex-flow: column;
}
.element {
background-color: #0084ff;
width: 500px;
margin-bottom: 1px;
border-radius: 25px;
}
.element.first {
height: 50px;
}
.element.sec {
height: 150px;
}
.element.third {
height: 250px;
}
<div class="container">
<div class="element first"></div>
<div class="element sec"></div>
<div class="element third"></div>
</div>
Following is the sass code for you:
$blue: #0084ff;
$blue-darker: darken($blue, 5);
.container {
display: flex;
flex-flow: column;
.element {
background-color: $blue;
border-radius: 25px;
width: 500px;
margin-bottom: 1px;
&.first{
height: 50px;
}
&.sec{
height: 150px;
}
&.third{
height: 250px;
}
}
}

CSS - inline divs size

I have 4 divs.
1 - Divs '.left' and '.right' should be 150px min. - > OK
2 - Divs '.middle1' and '.middle2' should get the remaining with
divided by them. -> NOT OK
Why do I have a blank space between the '.middle2' and '.right'?
.container {
width: 100%;
display: table;
}
.left {
float: left;
display: table-cell;
padding: 0;
background-color: red;
height: 30px;
width: 15%;
min-width:150px;
}
.middle1 {
float: left;
display: table-cell;
padding: 0;
background-color: blue;
height: 30px;
width: 35%;
}
.middle2 {
float: left;
display: table-cell;
padding: 0;
background-color: grey;
height: 30px;
width: 35%;
}
.right {
display: table-cell;
background-color: green;
height: 30px;
width: 15%;
min-width:150px;
}
input {
margin:5px;
width:100px;
background: #FFF;
}
.list{
margin: 0 2px;
color: #FFF;
}
<div class="container">
<div class="left">
<input type="text">
<span class="list">list</span>
</div>
<div class="middle1"></div>
<div class="middle2"></div>
<div class="right"></div>
</div>
Not sure how do you want to display. Instead of playing around with floats, you can use display:flex to achieve the same.
Check the below snippet
.container {
display:flex;
}
.left {
padding: 0;
background-color: red;
height: 30px;
width: 15%;
min-width:150px;
}
.middle1 {
padding: 0;
background-color: blue;
height: 30px;
width: 35%;
}
.middle2 {
padding: 0;
background-color: grey;
height: 30px;
width: 35%;
}
.right {
background-color: green;
height: 30px;
width: 15%;
min-width:150px;
}
input {
margin:5px;
width:100px;
background: #FFF;
}
.list{
margin: 0 2px;
color: #FFF;
}
<div class="container">
<div class="left">
<input type="text">
<span class="list">list</span>
</div>
<div class="middle1">m1</div>
<div class="middle2">m2</div>
<div class="right">right</div>
</div>
Hi this is not the best way to do this.
You should make a div with three sections of it and divide the middle one into two sections.
So basically the structure you need is
Div > 3 columns and then the middle column has two columns conatined.
However, here is a quick fix for your code :
- Apply float:right to .middle2
- Fix the width percetage for .middle1 and .middle2
.container {
width: 100%;
display: table;
}
.left {
float: left;
display: table-cell;
padding: 0;
background-color: red;
height: 30px;
width: 15%;
min-width:150px;
}
.middle1 {
float: left;
display: table-cell;
padding: 0;
background-color: blue;
height: 30px;
width: 31%;
}
.middle2 {
float: right;
display: table-cell;
padding: 0;
background-color: grey;
height: 30px;
width: 31%;
}
.right {
display: table-cell;
background-color: green;
height: 30px;
width: 15%;
min-width:150px;
}
input {
margin:5px;
width:100px;
background: #FFF;
}
.list{
margin: 0 2px;
color: #FFF;
}

How to make contents fit to divs using css

How to make the inside divs fit to the contents in the below html
I tried with display:inline-block but it moves the 2nd div to the bottom.
<div class="ms-table">
<div class="tableCol-75">
</div>
<div class="tableCol-25">
</div>
</div>
There you go:
.ms-table {
width: 80%;
}
.tableCol-70 {
float: left;
width: 70%;
border: 1px solid black;
margin-right: 10px;
}
.tableCol-25 {
float: left;
width: 25%;
border: 1px solid blue;
}
<div class="ms-table">
<div class="tableCol-70">
My name is abc and I live in ams.
</div>
<div class="tableCol-25">
I love junk food even though it is unhealthy
</div>
</div>
use display: table
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.ms-table{
display: table;
width: 100%;
height: 100px;
}
.table-cell{
display: table-cell;
vertical-align: top;
padding: 15px;
}
.tableCol-75{
width: 75%;
background: #ccc;
}
.tableCol-25{
width: 25%;
background: #000;
color: #fff;
}
<div class="ms-table">
<div class="table-cell tableCol-75">75%</div>
<div class="table-cell tableCol-25">25%</div>
</div>
use display: inline-block;
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.ms-table{
width: 100%;
min-height: 100px;
}
.table-cell{
display: inline-block;
vertical-align: top;
padding: 15px;
}
.tableCol-75{
width: 75%;
background: #ccc;
}
.tableCol-25{
width: 25%;
background: #000;
color: #fff;
}
<div class="ms-table">
<div class="table-cell tableCol-75">75%</div><!--
--><div class="table-cell tableCol-25">25%</div>
</div>

Resources