How to make a fluid layout equally spaced - html

My problem is both a design and code issue, I have a diagonal layout which consists of a paragraph of text paired with an image which is then flipped (image then paragraph) on the next row.
The problem is I don't know the height of the paragraph or the height of the image so how do I make it equally spaced for the end user?
Here is a fiddle that may help explain my question.
My code:
<div class="container">
<div class="one">
<p>
</p>
</div>
<div class="two">
<img src="https://cdn.jamesedition.com/media/W1siZiIsImxpc3RpbmdfaW1hZ2VzLzIwMTcvMDkvMDkvMjEvNTgvNDUvMGJkYTRmYTMtMTY1Yy00YmQzLThhZTEtNTlhNzc2NGYwNDBjL3AxLUoycDhOV0xHdnM4RDNVdUphaW5xRWxMMWxEcnpaQzlyNXg0a2xKREUuanBnIl0sWyJwIiwidGh1bWIiLCIyMDAweCJdLFsicCIsIndhdGVybWFyayJdLFsicCIsImVuY29kZSIsImpwZyIsIi1zdHJpcCAtcXVhbGl0eSA4MCAtaW50ZXJsYWNlIFBsYW5lIl1d/2015-lamborghini-aventador-roadster.jpg?sha=6b14013f088579ce">
</div>
</div>
<div class="container">
<div class="two">
<img src="https://cdn.jamesedition.com/media/W1siZiIsImxpc3RpbmdfaW1hZ2VzLzIwMTcvMDkvMDkvMjEvNTgvNDUvMGJkYTRmYTMtMTY1Yy00YmQzLThhZTEtNTlhNzc2NGYwNDBjL3AxLUoycDhOV0xHdnM4RDNVdUphaW5xRWxMMWxEcnpaQzlyNXg0a2xKREUuanBnIl0sWyJwIiwidGh1bWIiLCIyMDAweCJdLFsicCIsIndhdGVybWFyayJdLFsicCIsImVuY29kZSIsImpwZyIsIi1zdHJpcCAtcXVhbGl0eSA4MCAtaW50ZXJsYWNlIFBsYW5lIl1d/2015-lamborghini-aventador-roadster.jpg?sha=6b14013f088579ce">
</div>
<div class="one">
<p>
</p>
</div>
</div>
<div class="container">
<div class="three">
<p>
</p>
</div>
<div class="two">
<img src="https://cdn.jamesedition.com/media/W1siZiIsImxpc3RpbmdfaW1hZ2VzLzIwMTcvMDkvMDkvMjEvNTgvNDUvMGJkYTRmYTMtMTY1Yy00YmQzLThhZTEtNTlhNzc2NGYwNDBjL3AxLUoycDhOV0xHdnM4RDNVdUphaW5xRWxMMWxEcnpaQzlyNXg0a2xKREUuanBnIl0sWyJwIiwidGh1bWIiLCIyMDAweCJdLFsicCIsIndhdGVybWFyayJdLFsicCIsImVuY29kZSIsImpwZyIsIi1zdHJpcCAtcXVhbGl0eSA4MCAtaW50ZXJsYWNlIFBsYW5lIl1d/2015-lamborghini-aventador-roadster.jpg?sha=6b14013f088579ce">
</div>
</div>
.one{
height:200px;
background:red;
width:50%;
float:left;
}
.two{
float:left;
height:auto;
background:blue;
width:50%;
}
.three{
float:left;
height:400px;
background:blue;
width:50%;
}
.container{
height:auto;
display:flex;
flex-direction:row;
align-items:center;
}
img{
float:left;
width:100%;
}
Class one is the paragraph ( shorter paragraph )
Class .two is the picture
Class .three is a longer paragraph
as you can see row one and two are equally spaced but row three has a larger space which wont look good to the end user.
Any help is appreciated.

Drop the img and use background-image and background-size: cover. With that you can make the image to completely cover the space left.
Stack snippet
.one {
height: 200px;
background: red;
width: 50%;
}
.two {
background: url('https://cdn.jamesedition.com/media/W1siZiIsImxpc3RpbmdfaW1hZ2VzLzIwMTcvMDkvMDkvMjEvNTgvNDUvMGJkYTRmYTMtMTY1Yy00YmQzLThhZTEtNTlhNzc2NGYwNDBjL3AxLUoycDhOV0xHdnM4RDNVdUphaW5xRWxMMWxEcnpaQzlyNXg0a2xKREUuanBnIl0sWyJwIiwidGh1bWIiLCIyMDAweCJdLFsicCIsIndhdGVybWFyayJdLFsicCIsImVuY29kZSIsImpwZyIsIi1zdHJpcCAtcXVhbGl0eSA4MCAtaW50ZXJsYWNlIFBsYW5lIl1d/2015-lamborghini-aventador-roadster.jpg?sha=6b14013f088579ce') center;
background-size: cover;
border: 1ps solid red;
width: 50%;
}
.three {
height: 400px;
background: blue;
width: 50%;
}
.container {
display: flex;
}
<div class="container">
<div class="one">
<p>
</p>
</div>
<div class="two">
</div>
</div>
<div class="container">
<div class="two">
</div>
<div class="one">
<p>
</p>
</div>
</div>
<div class="container">
<div class="three">
<p>
</p>
</div>
<div class="two">
</div>
</div>
Or if the whole picture should be visible, use background-size: contain instead.
Stack snippet
.one {
height: 200px;
background: red;
width: 50%;
}
.two {
background-image: url('https://cdn.jamesedition.com/media/W1siZiIsImxpc3RpbmdfaW1hZ2VzLzIwMTcvMDkvMDkvMjEvNTgvNDUvMGJkYTRmYTMtMTY1Yy00YmQzLThhZTEtNTlhNzc2NGYwNDBjL3AxLUoycDhOV0xHdnM4RDNVdUphaW5xRWxMMWxEcnpaQzlyNXg0a2xKREUuanBnIl0sWyJwIiwidGh1bWIiLCIyMDAweCJdLFsicCIsIndhdGVybWFyayJdLFsicCIsImVuY29kZSIsImpwZyIsIi1zdHJpcCAtcXVhbGl0eSA4MCAtaW50ZXJsYWNlIFBsYW5lIl1d/2015-lamborghini-aventador-roadster.jpg?sha=6b14013f088579ce');
background-position: center center; /* or "left top", and so on */
background-repeat: no-repeat; /* or "repeat-y", and so on */
background-size: contain;
border: 1ps solid red;
width: 50%;
}
.three {
height: 400px;
background: blue;
background-repeat: no-repeat; /* or "repeat-y", and so on */
width: 50%;
}
.three + .two { /* where class .two comes after .three */
background-repeat: repeat-y; /* repeat vertically */
background-position: center top; /* start at "center top" */
}
.container {
display: flex;
}
<div class="container">
<div class="one">
<p>
</p>
</div>
<div class="two">
</div>
</div>
<div class="container">
<div class="two">
</div>
<div class="one">
<p>
</p>
</div>
</div>
<div class="container">
<div class="three">
<p>
</p>
</div>
<div class="two">
</div>
</div>
Updated based on a comment.
If the image shouldn't scale up, but scale down, you either need a script to detect the image size and toggle between background-size: contain (scale down) and background-size: auto (keep org. size), or, as shown below, using the img, combined with transform to center it. The latter won't allow to control repeat though.
Stack snippet
.one {
height: 200px;
background: red;
width: 50%;
}
.two {
position: relative;
width: 50%;
overflow: hidden;
}
.two img {
display: block;
max-width: 100%;
max-height: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.three {
height: 400px;
background: blue;
width: 50%;
}
.container {
display: flex;
}
<div class="container">
<div class="one">
<p>
</p>
</div>
<div class="two">
<img src="http://placehold.it/100" alt="">
</div>
</div>
<div class="container">
<div class="two">
<img src="http://placehold.it/800" alt="">
</div>
<div class="one">
<p>
</p>
</div>
</div>
<div class="container">
<div class="three">
<p>
</p>
</div>
<div class="two">
<img src="http://placehold.it/800" alt="">
</div>
</div>

Related

How to put blocks in line in html

I have the following code, where I wanted to align logo(picture) and one block, which includes Username, his level and progress bar. And used flex for it, but it ate my progress bar, how can I do it so error bar's width would be 100%?
<div class="class" id="user">
<img src="user.svg">
<div>
<h2>username</h2>
<h2> Уровень </h2>
<div id="container">
<div id="progress"> 80% </div>
</div>
</div>
</div>
The CSS of the page:
#user {
display:flex;
}
#container {
background-color: white;
width: 100%;
}
#progress{
background-color: #fd6a72;
width: 100%;
}
give your elements some width.
#user {
display: flex;
align-items: center;
}
#container {
background-color: white;
width: 100%;
}
#progress {
background-color: #fd6a72;
width: calc(100vw - 25px - 8px);
height: 20px;
}
img{
height:25px;}
<div class="class" id="user">
<img src="https://via.placeholder.com/25">
<div>
<h2>username</h2>
<h2> Уровень </h2>
<div id="container">
<div id="progress"> 80% </div>
</div>
</div>
</div>

How to make sticky right side, until seeing all

How can I make sticky sidebar, but start sticky after the whole right sidebar has been seen while scrolling. Because at the moment only when you go all the way down you can see the right side all parts
.wrapper {
display: flex;
gap: 24px;
position: relative;
}
.left {
width: 70%;
}
.right {
width: 30%;
}
.sticky {
position: sticky;
top: 10px;
}
.box {
border: 1px solid;
height: 300px;
}
.box1 {
height: 400px;
background: green;
}
.box2 {
height: 200px;
background: pink;
}
<div class="wrapper">
<div class="left">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
<div class="right">
<div class="sticky">
<div class="box1">
box 1
</div>
<div class="box2">
box 2
</div>
</div>
</div>
</div>
So I need both block1 and block2 when you start scrolling to be seen first, and after that to stay sticky. Can someone help please

Content Div overlaps Menu Div - HTML & CSS

I have content navigation div overlapping menu navigation div. Please let me know what am i missing here. Please find fiddle link below:
https://jsfiddle.net/y4c2xs5j/1/
HTML:
<div class="top-nav">
<div class="menu-nav">
<div class="row">
<div class="col-md-12">
<span>Test</span>
</div>
</div>
</div>
<div class="content-nav">
<div class="row">
<div class="col-md-12">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div>
<p>
Card content
</p>
</div>
</div>
<div class="col-md-4">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: red;
height: 100vh;
}
.top-nav {
width: 100vw;
}
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
}
As per my understanding, you want to cover only 60px width with menu-nav, and rest want to cover with content-nav, According to below code:
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
}
If I am getting correct then you just neeed to add one more property with content-nav, overflow:hidden;
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
overflow:hidden;
}
By adding overflow hidden, you will get complete width rest 60px with content-nav, That is issue cause by float:left, when we are use float property, then the issue is generated, for the same we have to use overflow:hidden
Try this code. Is this what you needed ?
<div class="top-nav">
<div class="menu-nav">
<div class="row">
<div class="col-md-12">
<span>Test</span>
</div>
</div>
</div>
<div class="content-nav">
<div class="row">
<div class="col-md-12">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div>
<p>
Card content
</p>
</div>
</div>
<div class="col-md-4">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: red;
height: 100vh;
}
.top-nav {
width: 100vw;
display: flex;
flex-direction: column;
}
.menu-nav {
width: 100vw;
background: green;
height: 20vh;
float: left;
}
.content-nav {
width: calc(100vw - 100px);
background: yellow;
height: 100vh;
}
You just need to add one property in ".content-nav" and also add clearifx class in the parent of both tag (.menu-nav, .content-nav)
<div class="top-nav clearfix">
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
float: left;
}
Whenever you use rows and columns, please check if you have at least one container that contains them. The gap you see on the right is caused by the negative margins from the rows.
The easy fix is to have .container-fluid on or inside menu and content nav.
On menu and content nav
<div class="top-nav">
<div class="menu-nav container-fluid">
...
</div>
<div class="content-nav container-fluid">
...
</div>
</div>
demo: https://jsfiddle.net/davidliang2008/x9d3bvLp/8/
Inside menu and content nav
<div class="top-nav">
<div class="menu-nav">
<div class="container-fluid">
...
</div>
</div>
<div class="content-nav">
<div class="container-fluid">
...
</div>
</div>
</div>
demo: https://jsfiddle.net/davidliang2008/x9d3bvLp/7/
You don't need to calculate the width for content-nav as fluid container will set its width to 100%:
.content-nav {
/*width: calc(100vw - 60px);*/
background: yellow;
height: 100vh;
}

positioning eight divs onto the page

So I have this html and this css. I want the .big elements be displayed in the corners of my .About
This is how it looks like so far using this code
The output of the code
I would like the first .big .box from the .left-box .container to be displayed on the top of .left-box .container and the first .big .box from the .right-box .container to be displayed on the top of .right-box .container. Rest of the div's should stay the same as they are.
.box {
width: 100%;
height: 300px;
position: relative;
border: 1px solid black;
}
.container {
display: inline-block;
width: 33%;
}
.big {
height: 400px;
}
.arrow {
border: 1px solid green;
position: absolute;
width: 20px;
height: 20px;
}
img {
position: absolute;
width: 100%;
height: 300px;
}
.top {
top:0
}
.middle {
top:50%;
margin-top:-10px;
}
.bottom {
bottom:0
}
.left {
left:0;
}
.center {
left:50%;
margin-left:-10px;
}
.right {
right:0;
}
<div class="About">
<div class="left-box container">
<div class="big box">
<div class="arrow bottom right"></div>
</div>
<div class="big box">
<div class="arrow top right"></div>
</div>
</div>
<div class="middle-box container">
<div class="box">
<div class="arrow bottom center"></div>
</div>
<div class="box">
<img src="images/marks.jpg" alt="marks" />
</div>
<div class="box">
<div class="arrow top center"></div>
</div>
</div>
<div class="right-box container">
<div class="big box">
<div class="arrow bottom left"></div>
</div>
<div class="big box">
<div class="arrow top left"></div>
</div>
</div>
</div>

2 column layout issue - stacking and floating

Probably a fairly basic solution to this, but I can't seem to figure it out... have set up a jsfiddle to demonstrate:
http://jsfiddle.net/AxKq8/1/
HTML
<div class="wrapper">
<div id="box-1" class="box">
</div>
<div id="box-2" class="box">
</div>
<div id="box-3" class="box">
</div>
</div>
CSS
.wrapper{
width: 100%;
}
.box {
width: 50%;
}
#box-1 {
height: 200px;
background-color: blue;
}
#box-2 {
height: 100px;
background-color: red;
}
#box-3 {
height: 300px;
float:right;
background-color: green;
position: relative;
top:0px;
right:0px;
}
I have 3 divs. What I'd like to do is have the top of the green div align with the top of the blue div.
As you can see I tried floating the first two divs left, and the third div right. That didn't work, so tried a relative positioning. Also tried using clear aswell, but it's eluding me!
Any suggestions on how to make this work?
Thanks!
Jon
Positioned the third div absolute with top:0
#box-3 {
height: 300px;
float:right;
background-color: green;
position: absolute;
top:0px;
right:0px;
}
Working CODE:JSFIDDLE
You can put the blue and red box in a container, and then a green box in another container. Float the two containers rather than the boxes.
http://jsfiddle.net/AxKq8/9/
HTML
<div class="wrapper">
<div class="container">
<div id="box-1" class="box">
</div>
<div id="box-2" class="box">
</div>
</div>
<div class="container">
<div id="box-3" class="box">
</div>
</div>
</div>
CSS
.wrapper{
width: 100%;
}
.container {
float: left;
width: 50%
}
#box-1 {
height: 200px;
background-color: blue;
}
#box-2 {
height: 100px;
background-color: red;
}
#box-3 {
height: 300px;
background-color: green;
}
Give this a try: JSFiddle
HTML:
<div class="wrapper">
<div class="box-group box">
<div id="box-1" class="box2"></div>
<div id="box-2" class="box2"></div>
</div>
<div class="box-group box">
<div id="box-3" class="box2"></div>
</div>
</div>
CSS:
.wrapper{ width: 100%; }
.box { width: 50%; }
.box2 { width: 100%; }
.box-group { float: left; }
#box-1 { height: 200px; background-color: blue; }
#box-2 { height: 100px; background-color: red; }
#box-3 { height: 300px; background-color: green; }
I created columns with the .box-group class, I grouped the first two items into the first column div so the stacking and floating will appear properly.