Aligning flex items to the right - html

I have a container and inside there are 3 boxes: yellow, green and red.
I gave display:flex to the container and gave justify-content:flex-start for the items to start at beginning.
I wanted to move the red box to the end so I gave margin-right: auto to the yellow box so that the red box could move to end (not sure if this is the exact way to move the red box to end, if not i want help in that too).
So the question is: I want the green box in the vertical middle of the container and I want to move it to the extreme right like the red box (but should be in the middle of container)
How do I do it with flex box?
.container {
height: 500px;
width: 500px;
background-color: royalblue;
display: flex;
flex-direction: row;
justify-content: flex-start;
}
.yellowbox {
color: black;
height: 100px;
width: 100px;
background-color: yellow;
margin-right: auto;
}
.greenbox {
color: black;
height: 100px;
width: 100px;
background-color: green;
align-self: center;
margin-left: auto;
}
.redbox {
color: black;
height: 100px;
width: 100px;
background-color: red;
}
<div class="container">
<div class="yellowbox">
<p>the white text</p>
</div>
<div class="greenbox">
<p>the black text</p>
</div>
<div class="redbox">
<p>the red text</p>
</div>
</div>
THIS IS MY CODEPEN LINK: http://codepen.io/ShamZ/pen/pRLELP

you may increase some of the margins but you must allow the flex children to wrap. and use order to put the green box in last position
.container {
height: 500px;
width: 500px;
background-image:linear-gradient(to left,rgba(0,0,0,0.2) 50%,rgba(0,0,0,0.1) 50%),linear-gradient(to top,rgba(0,0,0,0.2) 50%,rgba(0,0,0,0.1) 50%);
background-color: royalblue;
display: flex;
flex-direction: row;
flex-wrap:wrap;
justify-content:space-between;
}
.yellowbox {
color: black;
height: 100px;
width: 100px;
background-color: yellow;
margin-right: 50%;
}
.greenbox {
order:1;
color: black;
height: 100px;
width: 100px;
background-color: green;
margin: -100px 0 auto auto;
}
.redbox {
margin:0 0 0 auto;
color: black;
height: 100px;
width: 100px;
background-color: red;
}
<div class="container">
<div class="yellowbox">
<p>the white text</p>
</div>
<div class="greenbox">
<p>the black text</p>
</div>
<div class="redbox">
<p>the red text</p>
</div>
</div>
http://codepen.io/gc-nomade/pen/Qdmpbb

Related

How to fix a DIV-Container next right to a centered Container - CSS

I've a simple DIV-Container for the main-content of the webpage. I.E
#main { width: 50%; margin: 0 auto; }
Now I would like to fix another container, right and fixed at the top of the #main-Container. See Screenshot:
You can do something like the following using CSS Flex:
.flex-container {
display: flex;
width: calc(66.66% - 20px);
float: right;
}
.main {
flex: 1;
color: white;
text-align: center;
margin-right: 33.33%;
}
.main:first-child {
width: 50%;
margin: 0 auto;
margin-right: 10px;
}
.red {
background-color: red;
height: 200px;
line-height: 200px;
}
.green {
background-color: green;
height: 100px;
line-height: 100px;
max-width: 15%;
}
<div class="flex-container">
<div class="main red">
Main content
</div>
<div class="main green">
?
</div>
</div>
Add the green div inside the centered div and style it.
<div id="main" style="position:relative;">
<div id="green_div" style="position:absolute; left:100%; margin-left:20px; background:green;">
<div>
</div>

Div container with text on the left side and overflow image on the right

I want to recreate the following structure:
With black is div container and inside the container on the left there will be text and on the right i need an image bigger than the container.
I tried to do this by grids but things got funky real quick.
As it seems to be important that the containing div maintains the dimensions (as shown by its border), this snippet adds in the actual image as a background on a pseudo element that is absolutely positioned.
That way the protruding bit of image does not alter the container div dimensions.
Here's a simple snippet using a grid to position the left and right sides. Of course you will want to alter proportions to suit your particular case, add styling to the leftside and so on:
.container {
display: grid;
grid-template-columns: 3fr 2fr;
width: 50vw;
height: auto;
margin-top: 10vh;
border: solid 2px black;
}
.leftside {
padding: 1vw;
}
.rightside {
position: relative;
width: 100%;
height: 100%;
}
.rightside::before {
content: '';
background-color: pink;
background-image: url(https://picsum.photos/id/1015/500/200);
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
width: 50%;
height: 140%;
bottom: 0;
left: 25%;
position: absolute;
}
<div class="container">
<div class="leftside">
<h2>Heading</h2>
<div>text1</div>
<div>text2</div>
</div>
<div class="rightside"></div>
</div>
go with the flexbox.
.main-container{
display:flex;
display: flex;
justify-content: space-evenly;
border:1px solid black;
margin:30px;
height:300px;
padding:10px;
}
.image{
width:50vw;
position:relative;
}
img{
width:100%;
height:150%;
width: 100%;
height: 150%;
top: -50%;
position: absolute;
}
.text{
display:flex;
align-items:center;
}
<div class="main-container">
<div class="text">
<p>Somthing Somthing</p>
</div>
<div class="image">
<img src="https://loremflickr.com/640/360" />
</div>
</div>
Here you go:
.background {
padding: 25px;
display: flex;
border: 1px solid black;
height: 150px;
position: relative;
margin-top: 50px;
}
.text {
border: 1px solid green;
width: 50%;
padding: 10px;
}
.img {
text-align: center;
width: 50%;
display: flex;
justify-content: center;
}
.img>div {
border: 1px solid blue;
width: fit-content;
padding: 10px;
height: 200px;
position: absolute;
bottom: 25px;
}
<div class="background">
<div class="text">
<p>
text1
</p>
<p>
text2
</p>
<button>
Click me
</button>
</div>
<div class="img">
<div>
me img
</div>
</div>
</div>
Hope this helps

Centre content to parent with box to the left

I would like to have a layout as follows:
Whereby I have a parent container, and centred inside of that is a breadcrumb. However, I also would like a logo inside of the container which floats to the left of the breadcrumb, but respects the boundaries of the breadcrumb.
I have been playing around with flexbox and can only get it to work with absolutely positioning the logo, which means the breadcrumb does not respect the boundaries of the logo.
I have put together a JSFiddle playground here: https://jsfiddle.net/joyqwpc1/25/.
The difficult thing is, the logo can be a variable width, so setting a margin is not viable for this.
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
justify-content: center;
height: 50px;
padding: 0 50px;
}
#logo {
height: 50px;
width: 50px;
background-color: blue;
position: absolute;
left: 0;
}
#breadcrumb {
width: 200px;
height: 20px;
background-color: green;
}
<div id="container">
<div id="logo"></div>
<div id="breadcrumb"></div>
</div>
I've created 2 separate containers for the logo and breadcrumbs and set them a width. Then, I aligned elements inside these containers.
https://jsfiddle.net/dmitriifrlv/vbhxrj1u/39/
<div id="container">
<div class="logoContainer">
<div id="logo">
</div>
</div>
<div class="breadcrumbContainer">
<div id="breadcrumb">
</div>
</div>
</div>
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
justify-content: flex-start;
height: 50px;
}
.logoContainer{
width: 10%;
height: 100%;
background: yellow;
}
#logo {
height: 50px;
width: 100%;
background-color: blue;
}
.breadcrumbContainer{
width:90%;
display: flex;
justify-content: center;
}
#breadcrumb {
width: 200px;
max-width: calc(100% - 2rem);
height: 20px;
background-color: green;
}
For clean solution a little bit of JavaScript is needed:
Make sure to click "Run with JS" button: https://jsbin.com/ziziqidole/edit?html,output
<html>
<head>
<script>
function handleResize() {
var crumb = document.getElementById("breadcrumb");
var logoWidth = document.getElementById("logo").offsetWidth;
var calcWidth = (window.innerWidth - crumb.offsetWidth) / 2 - logoWidth;
if (calcWidth < 10) {
calcWidth = 10;
}
crumb.style.marginLeft = calcWidth;
}
</script>
<style media="all">
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
height: 50px;
padding-right: 50px;
}
#logo {
height: 50px;
width: 150px;
background-color: blue;
flex-shrink: 0;
}
#breadcrumb {
width: 200px;
height: 20px;
background-color: green;
}
#center {
width: 200px;
height: 20px;
margin: 0 auto;
background-color: khaki;
text-align: center;
}
</style></head
>
<body onresize="handleResize()" onload="handleResize()">
<div id="container">
<div id="logo"></div>
<div id="breadcrumb"></div>
</div>
<div id="center">Page Center</div>
</body>
</html>
Solution without JavaScript. But some hardcoding needed e.g. logo width and crumb width.
https://jsbin.com/juxijowova/edit?html,output
<html>
<head>
<style media="all">
body {
margin: 0;
}
#container {
background: red;
display: flex;
align-items: center;
height: 50px;
padding-right: 50px;
}
#logo {
height: 50px;
width: 150px;
background-color: blue;
flex-shrink: 0;
}
#breadcrumb {
width: 200px;
height: 20px;
background-color: green;
position: absolute;
left: max(260px, 50%); /* logo width + breadcrumb width/2 + margin*/
transform: translate(-50%, 0);
/*margin: 0 auto;
margin-left: 10px;/*use this if want to center on remaining area instead of screen center*/
}
#center {
width: 200px;
height: 20px;
margin: 0 auto;
background-color: khaki;
text-align: center;
}
</style></head
>
<body>
<div id="container">
<div id="logo"></div>
<div id="breadcrumb"></div>
</div>
<div id="center">Page Center</div>
</body>
</html>
This is usually how I lay something like this out: 3 containers, the side 2 will flex to fill space equally because they have the same exact basis (auto basis would break this because the left "Logo" content would be included in the basis for the left container). The middle is sized to the content and stays centered unless it becomes too wide and will start to take up space on the right and become uncentered.
.f-row {
display: flex;
}
.left-box {
flex: 1 1 0.0000001px;
padding: 1em;
border: 1px solid blue;
}
.middle-box {
padding: 1em;
border: 1px solid red;
justify-self: center
}
.right-box {
padding: 1em;
border: 1px solid green;
flex: 1 0 0.0000001px;
}
<div class="f-row">
<div class="left-box">Logo</div>
<div class="middle-box">Breadcrumb</div>
<div class="right-box"></div>
</div>
<br><br>
<div class="f-row">
<div class="left-box">Logo</div>
<div class="middle-box">Breadcrumb > Longer > Space</div>
<div class="right-box"></div>
</div>
<br><br>
<div class="f-row">
<div class="left-box">Logo</div>
<div class="middle-box">Breadcrumb > Longer > Space > Too Much Now It's Taking Up Space From the Right, Uncentered Now</div>
<div class="right-box"></div>
</div>

How to make a div occupy 100% within another div in Vue

I want to make two div inside other div. But the second(green) div is passing the size of the main(black). I tried to set the height to 100%, but something happens that is going beyond the size of the main box, does anyone have any solutions?
.block {
width: 300px;
height: 200px;
background: black;
}
.box1 {
width: 200px;
height: 50px;
background: red;
vertical-align: top;
margin: auto;
}
.box2 {
width: 200px;
height: 100%;
background: green;
margin: auto
}
<div class="block">
<div class="box1">
</div>
<div class="box2">
</div>
</div>
If you set child's height to 100% then the height of the parent will be inherited. If you are looking for an option where the 2nd box (green) fill the remaining space leftover by 1st box(red)
.block {
width: 300px;
height: 200px;
background: black;
display: flex;
flex-direction: column;
align-items: center;
}
.box1 {
width: 200px;
height: 50px;
background: red;
vertical-align: top;
}
.box2 {
flex: 1;
width: 200px;
background: green;
}
<div class="block">
<div class="box1">
</div>
<div class="box2">
</div>
</div>
I am using Flex and there is no need to use overflow: hidden
You should add the overflow: hidden; to the main black box, just like the below snippet. This will make the overflow clipped.
.block {
width: 300px;
height: 200px;
background: black;
overflow: hidden;
}
.box1 {
width: 200px;
height: 50px;
background: red;
margin: auto;
}
.box2 {
width: 200px;
height: 100%;
background: green;
margin: auto;
}
<div class="block">
<div class="box1">
</div>
<div class="box2">
</div>
</div>
But if you don't want to get rid of the remaining piece of the second box, you can do it with flexbox also. This will not trim the green box but instead, it will resize it to make sure the green box will remain in the parent black box.
.block {
width: 300px;
height: 200px;
background: black;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.box1 {
width: 200px;
height: 50px;
background: red;
}
.box2 {
width: 200px;
height: 100%;
background: green;
}
<div class="block">
<div class="box1">
</div>
<div class="box2">
</div>
</div>
NOTE: In the flexbox version, you also won't need to use margin: auto; in the child boxes, because in the flexbox column direction align-items: center; will take care of child positions with the available attributes it gave to us.

How to place circle and text in one line?

I have one stripe with white half-circle and two-lined text which is supposed to be next to circle and inside of the stripe. But it goes beyond the borders. If I apply span to these divs, nothing changes. How can I solve this?
JSFiddle
HTML
<div class="summarize">
<div class="top-button">
<span class="half-circle"></span>
<div>First line</div>
<div>Second line</div>
</div>
</div>
CSS
.summarize {
background-color: #B7B7B7;
}
.top-button {
height: 10vh;
}
.half-circle {
background: white;
height: 100%;
width: 20%;
border-radius: 00px 50px 50px 0px;
display: flex;
align-items: center;
}
Just give float: left; to .half-circle
.half-circle {
background: white;
height: 100%;
width: 20%;
border-radius: 00px 50px 50px 0px;
display: flex;
align-items: center;
float: left;
}
Working Fiddle
While the answer to add float: left; certainly works, here is an option that uses flexbox.
The reason I prefer flexbox is because it can be easier aligned correctly.
.summarize {
background-color: #B7B7B7;
}
.top-button {
height: 50px;
display: flex;
flex-direction: row;
align-items: center;
}
.half-circle {
background: #F0F0F0;
height: 100%;
width: 20%;
border-radius: 00px 50px 50px 0px;
display: block;
}
.text {
flex: 1;
padding-left: 15px;
}
.text span {
display: block;
}
<div class="summarize">
<div class="top-button">
<div class="half-circle"></div>
<div class="text">
<span>line 1</span>
<span>line 2</span>
</div>
</div>
</div>
One way to achieve what you want using a lot less markup would be to use an absolutely positioned pseudo-element, like so:
div{
background-color:#b7b7b7;
height:10vh;
padding-left:calc(20% + 10px);
/* Left padding should be at least the same width as the pseudo-element - I added 10px to create some space between it and the content */
position:relative;
}
div::before{
background:#fff;
border-radius:0 50px 50px 0;
bottom:0;
content:"";
left:0;
position:absolute;
top:0;
width:20%;
}
/** Add the following if you want to center the children vertically in the parent **/
div{
align-content:center;
display:flex;
flex-wrap:wrap;
}
p{
flex:0 0 100%;
}
/** Presentational stuff for this Snippet **/
*{box-sizing:border-box;font-family:sans-serif;margin:0;padding:0;}
div{border:1px solid #999;margin:10px;}
<div>
<p>First line</p>
<p>Second line</p>
</div>
you did not use flex properly nor in the right place.
If you make your circle and a container holding the text sibblings, it will easily work:
.summarize {
background-color: #B7B7B7;
}
.top-button {
height:3em;/* vh might not be the best idea ... */
display: flex;
align-items: center;
}
.half-circle {
background: white;
height: 100%;
width: 20%;
border-radius: 00px 50px 50px 0px;
}
p {
margin: 0;
}
<div class="summarize">
<div class="top-button">
<span class="half-circle"></span>
<div>
<p>
First line</p>
<p>
Second line</p>
</div>
</div>
</div>
https://jsfiddle.net/Loj4Lgkq/4/
increase container height ?
.summarize {
background-color: #B7B7B7;
}
.top-button {
height:10em;/* vh might not be the best idea ... */
display: flex;
align-items: center;
}
.half-circle {
background: white;
height: 100%;
width: 20%;
border-radius: 00px 50px 50px 0px;
}
p {
margin: 0;
}
<div class="summarize">
<div class="top-button">
<span class="half-circle"></span>
<div>
<p>
First line</p>
<p>
Second line</p>
</div>
</div>
</div>
That will work:
.summarize {
background-color: #B7B7B7;
}
.top-button {
height: 10vh;
}
.half-circle {
background: white;
height: 100%;
width: 20%;
border-radius: 00px 50px 50px 0px;
display: inline-block;
align-items: center;
}
<div class="summarize">
<div class="top-button">
<span class="half-circle"></span>
<span>First line</span>
<span>Second line</span>
</div>
</div>