Responsive HTML (Resize div when window becomes smaller than div) - html

I am curious how this type of responsive layout is achieved (see screenshots).
It looks like the form is in a div that does not start resizing or become responsive until the window has been resized smaller than the div.
I have tried a combination of divs with margins and flex, etc., and have added the <meta name="viewport" content="width=device-width, initial-scale=1.0"> tag to the head as well, but I only manage to make the page responsive to all window resizing.
Any direction here is much appreciated.

There are multiple methods of doing this:
#media queries:
with media queries, you can define style changes for specific breakpoints. In this case it is used to size a column to 100% width instead of 50% at screen at 480px or lower:
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.container > div {
width: calc(50% - 20px);
}
#media only screen
and (max-width: 480px) {
.container > div {
width: 100%;
}
}
input {
display: block;
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div>
<input>
<label>Firstname</label>
</div>
<div>
<input>
<label>Lasttname</label>
</div>
</div>
flex-grow:
This Flexbox approach defines the max-width of a column to 480px. If an element is smaller than 480px it will span the entire given width. flex-wrap: wrap is used to push the 2nd column to the next row. For screens larger than 960 px (2 columns), flex-grow will overwrite the max-width and allow the element to occupy the remaining available space.
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.container > div {
width: 480px;
flex-grow: 1;
}
input {
display: block;
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div>
<input>
<label>Firstname</label>
</div>
<div>
<input>
<label>Lasttname</label>
</div>
</div>
CSS-Grid + auto-fit:
This Grid solution uses minmax and auto-fit to create columns dynamically. It will fit as many columns as possible but ensures that no column is less then 480px (unless the screen is smaller) and resize the columns to occupy remaining space.
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(480px, 1fr));
gap: 20px;
}
input {
display: block;
box-sizing: border-box;
width: 100%;
}
<div class="container">
<div>
<input>
<label>Firstname</label>
</div>
<div>
<input>
<label>Lasttname</label>
</div>
</div>

by set width: 100%; and max-width to any value like max-width: 400px; you can do this
.my-div {
width: 100%;
max-width: 400px;
/* center */
margin-inline: auto;
/* just for demo */
background: #808080;
height: 50em;
}
<div class="my-div"></div>

Related

Flexbox is not fitting on mobile

This is my first week working with flexbox, and I like it a lot until now. I am doing everything I can to get away of floating elements. Therefore my main purpose is not a solution there is floating elements in. I run into 2 problems, that I am not quite sure of the correct saolution.
Problem 1:
When I hit around 1200px I can see the 2 columns is starting moving together. How can that be?
Problem 2:
Why is my columns not fitting on viewport under 768px? I can see on the mobile the 900x200 is going over the edge of the max width on telephone.
Example page of my code here.
.column-layout {
max-width: 1200px;
background-color: #fff;
margin: 40px auto 0 auto;
}
.column-layout-one {
max-width: 1200px;
background-color: #fff;
margin: 40px auto 0 auto;
}
.header-item-one {
order: 1;
}
.header-item-two {
order: 2;
}
#media (min-width: 768px) {
.column-layout-one {
display: flex;
justify-content: space-between;
}
}
<div class="column-layout-one">
<div class="header-item-one">
<img src="http://placehold.it/280x200">
</div>
<div class="header-item-two">
<img src="http://placehold.it/900x200">
</div>
</div>
.column-layout {
max-width: 1200px;
background-color: #fff;
margin: 40px auto 0 auto;
}
img {max-width: 100%;}
.column-layout-one {
max-width: 1200px;
background-color: #fff;
margin: 40px auto 0 auto;
}
.header-item-one {
order: 1;
}
.header-item-two {
order: 2;
}
#media (min-width: 768px) {
.column-layout-one {
display: flex;
justify-content: space-between;
}
}
<div class="column-layout-one">
<div class="header-item-one">
<img src="http://placehold.it/280x200">
</div>
<div class="header-item-two">
<img src="http://placehold.it/900x200">
</div>
</div>
When I hit around 1200px I can see the 2 columns is starting moving together. How can that be?
Your .column-layout-one has max width 1200 set, inner elements has 280+900 = 1180 px, and you have used space-between on its parent. so, when the parent has space more than its defined width, inner items will be seprated by diffrence of 1200-1180 = 20px; as soon as its parent will shrink this space will reduce, because its the space left by these 2 divs,
Space-between = outerWidth - (Total of inner width)
if you want your images to fit in screen on mobile, then provide it
max-width: 100%, if you will not do so, itwill take its orignal width and distort your design.
Set your .header-item-one and -two to flex: 1; to fill up the whole space in your flex box. Then, set max-width values for the expected results.
.header-item-one {
flex: 1;
max-width: 200px;
background-color: red;
height: 300px;
}
.header-item-two {
flex: 1;
max-width: 900px;
background-color: blue;
height: 300px;
}
About the other problem -- the divs go over the viewport boundaries, because you have placeholder images. Images are fixed-width inline block elements, meaning they won't fit the user's viewport, unless you crop them out with overflow-x: hidden; in the parent div or do something like div img { max-width: 100%; }.
Remove the images and try out that css. Fiddle: https://jsfiddle.net/59meh6vs/

set column to full height of the page not exceeding the page size

I have a navbar on the top and set the html and body to height 100% and i seperated the page into two columns using bootstrap grid and I want to set the height of the two columns that it should not exceed the page so that No scroll bar appears
How to set the column height so that scroll bar do not appears
You can achieve it using height: 100% on the parent and grandparent and so on. You can see a demo on codepen:
https://codepen.io/k-five/pen/gZgwqO
or here:
html,body{
height: 100%;
margin: 0; padding: 0; border: none;
}
body > div.parent {
height: 100%;
}
div.parent > span {
display: inline-block; width: 50%;
}
div.parent > span:first-child {
height: 100%; background-color: #F00;
}
div.parent > span:last-child {
height: 100%; background-color: #FF0;
}
<!DOCTYPE>
<html>
<body>
<div class="parent">
<span>1</span><span>2</span>
</div>
</body>
</html>
And if you want to use bootstrap take look at
https://getbootstrap.com/docs/4.0/utilities/sizing/ which is h-100
Have you tried vh?
.column2{
height: 100vh
}
It stands for viewport height, and will be the exact size of the screen you're viewing this on.
Have you tried using .row-eq-height from Bootstrap? Have a look here.
Be warn though that they're using flex style so check your supported browsers first before using these kind of css.
/*
* Row with equal height columns
* --------------------------------------------------
*/
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
One more thing, for classes like these col-md-6 col-sm-6 col-xs-6 col-lg-6 you don't have to specify all the breakpoints. Just use col-xs-6 and it will be applied to all breakpoints hence "mobile first", to save you some code and filesize. Let me know if that helps. ;)

column flex with height 0 padding trick

I have 2 divs that are placed on top of each other. For purposes of alignment, I am using display: flex and flex: column on the div containing these two divs. However, the first div uses the "height 0 padding" trick for videos. The problem I'm having, is that when using flex: column and change the width of the screen, the height doesn't change (and I want the height to change so that it matches the ratio for the video). What ends up happening is that the div stays the same, and the video shrinks within it and it looks ugly because there is extra background.
Plunker: https://plnkr.co/edit/TaeF5f8VufJWPU3GRZPr?p=preview
(in short, I want it such that when I change the width of the browser, the red div's height gets smaller)
CSS
/* Styles go here */
body {
height: 100%;
}
.container {
height: 80vh;
display: flex;
flex-flow: column;
}
.video {
flex: none;
height: 0;
padding-bottom: 30%;
background-color: red;
width: 80%;
}
.next-content {
flex: 1 0 auto;
width: 80%;
background-color: blue;
}
HTML:
<body>
<h1>Hello Plunker!</h1>
<div class="container">
<div class="video"></div>
<div class="next-content"></div>
</div>

Flexbox width of header aligned with outer part of content

I'm using flexbox to align my content blocks in the middle of the page, now i'm willing to align the header with the outer content blocks
In the image you can see what needs to happen
This is my current result:
And this is how it need's to look like
So when scaling the browser the content is going to the first line if there is space, the header needs to grow with this at that moment.
Here is a codepen with the flexbox in it
.flex {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
http://codepen.io/Dirkandries/pen/XmpGzw
Is this possible without using media queries and keeping the content boxes the same size?
*The padding and margin can be removed
Challenge #1
In looking at your code, you have a 10px margin applied to each box:
section {
background: red;
width: 300px;
height: 200px;
margin: 10px;
}
So one problem you will encounter in trying to align the header edges with the box edges is that there's an additional 10px of transparent space beyond the border of each box. But you're asking for the header to align with the border of the box. So we can either remove the margin from each box, or adjust the width of the header. I've gone with the latter in my solution below.
Challenge #2
You've specified a fixed width for each box (300px). This makes it difficult to match the header width with the row of boxes. What happens when the screen is 750px or 1150px wide? The boxes don't stretch to fill the width, a gap is created as a result, and the box row doesn't align with the header.
The box row width is 960px but the header width is 1150px.
One possible solution (or step in the right direction)
Align the flexbox in a column direction to vertically stack the header and the boxes (which are wrapped in a new container)
Use a nested flexbox to align the boxes in a row
Use calc for width values
Use media queries for screen adjustments
HTML
<article class="flex">
<header>
Header needs to be alignd with the container outer part
</header>
<div id="nested-inner-container"><!-- new container for nested flexbox -->
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</div>
</article>
CSS
body { margin: 0; }
.flex {
display: flex;
flex-direction: column; /* main groups (header and div) in column direction */
align-items: center;
}
header {
height: 50px;
width: calc(100% - 20px); /* width accounts for left and right box margins */
background-color: blue;
}
#nested-inner-container {
display: flex; /* box group (flex item) becomes flex container, as well */
justify-content: center;
flex-wrap: wrap;
width: 100%; /* width equal to header width */
}
section {
flex: 1 1 calc(25% - 80px); /* flex basis equals four boxes per row less margins */
height: 200px;
margin: 10px;
background: red;
}
#media screen and (max-width: 1500px) {
section { flex-basis: calc(33.33% - 60px); } /* three boxes per row less margins */
}
#media screen and (max-width: 1250px) {
section { flex-basis: calc(50% - 40px); } /* two boxes per row less margins */
}
#media screen and (max-width: 1000px) {
section { flex-basis: calc(100% - 20px); } /* one box per row less margins */
}
DEMO: http://codepen.io/anon/pen/wKgVYb
NOTE: This answer may or may not be exactly what you're looking for. The question didn't address all the details (like "can the margins be adjusted?", "are the box widths adjustable?", "are media queries not an option or just something you're hoping to avoid?"). So my goal in this answer was to offer up some concepts that hopefully get you where you want to go. AT A MINIMUM, the header and the boxes align in all screen sizes :-)
As far as I know, there is no solution to your question using only CSS and the flex box model.
If what you want is just to align the background of the header, you can fake it.
You need another flex container, that will give you a background with the same rules as the real one. (And that will be used only for this... not really semantic)
.flex{
display: flex;
flex-wrap: wrap;
justify-content: center;
position: relative;
}
header{
width: 100%;
background: lightblue;
height: 50px;
margin: 0px 170px;
text-align: center;
}
section{
background: red;
width: 300px;
height: 200px;
margin: 10px;
}
.headerbkg {
position: absolute;
left: 0px;
right: 0px;
top: 0px;
height: 50px;
display: flex;
flex-wrap: wrap;
justify-content: center;
overflow: hidden;
z-index: -1;
}
.headerbkg div {
background: lightblue;
width: 300px;
height: 50px;
margin: 0px 10px;
}
<article class="flex">
<header>
Header needs to be alignd with the container outer part
</header>
<div class="headerbkg">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</article>
The real header is given a margin that will keep it always narrower that the background, so that won't spoli the effect.
To make this less evident, I have given it a centered text
The following are my adressment of the question.
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<meta name = "viewport"
content = "width = device-width, initial-scale = 1.0"
>
<link href = "CSS/Example.css"
rel = "stylesheet"
type = "text/css"
>
</head>
<body>
<header>
Header needs to be alignd with the container outer part
</header>
<div class="flex">
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</div>
</body>
</html>
CSS
html,
*
{
border : 0;
box-sizing : border-box;
margin : 0;
padding : 0;
}
.flex
{
display : flex;
flex-wrap : wrap;
justify-content : center;
}
header
{
background : blue;
display : block;
height : 50px;
margin : auto;
width : 300px;
}
#media only screen and (min-width : 620px)
{
body header
{
width : 620px;
}
}
#media only screen and (min-width : 940px)
{
body header
{
width : 940px;
}
}
#media only screen and (min-width : 1260px)
{
body header
{
width : 1260px;
}
}
#media only screen and (min-width : 1580px)
{
body header
{
width : 1580px;
}
}
#media only screen and (min-width : 1900px)
{
body header
{
width : 1900px;
}
}
section
{
background : red;
display : block;
width : 300px;
height : 200px;
margin : 10px;
}
Firstly, in the CSS file there is the segment...
*
{
border : 0;
box-sizing : border-box;
margin : 0;
padding : 0;
}
By setting border, margin and padding to 0 for all elements you can eliminate any default settings for those properties that some or all browsers might display for an element. By adding box-sizing : border-box; any border or padding settings you make will be contained within the width of an element rather than without, making laying out a page much simpler.
I tend to include this snippet as a precaution.
As for setting the width of your header, it will need to be set to a fixed width that will need to be changed whenever the concentration of section's (themselves of a fixed width) to a line changes, which will occur whenever the viewport reaches certain widths.
The width of the header, and the width of the viewport at which it is changed, will equal the total width of the sections in one full line plus the total width of the margins between them, nnamely 300px, 620px, 940px, 1260px, 1580px, and 1900px. I have used media queries to perform the changeover at those points.
Note : Because of specificity issues caused when seeking to override a property using media queries I have placed body in front of the definition of header in each of the media queries.
Since the width of header isn't flexible and flex is mainly there to distribute the section's, I have placed header outside of flex.
If you have any questions or comments about my answer, then please feel free to reply.
It is much easier then it looks. Here is the CodePen: http://codepen.io/mikestratton/pen/PPmpKQ
Add the following two classes to your CSS:
.sleft {
background: red;
width: 50%;
height: 200px;
margin: 0;
float: left;
}
.sright{
background: red;
width: 50%;
height: 200px;
margin: 0;
float: left;
}
Then change your HTML to:
<article class="flex">
<header>
Header needs to be alignd with the container outer part
</header>
<section class="sleft">content</section>
<section class="sright">content</section>
<section class="sleft">content</section>
<section class="sright">content</section>
<section class="sleft">content</section>
<section class="sright">content</section>
</article>
Its Not the same as you want but i tried to do some little hacks & tweeks
http://codepen.io/anon/pen/GpEdKq
Check is this ok or you can use grids instead and it will be responsive too, Sorry if it doesn't help you Thanks
Html Code:
<article class="flex">
<div class="wrapper">
<header>
Header needs to be alignd with the container outer part
</header>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
<section>content</section>
</div>
</article>
css code:
body{
margin: 0;
}
.wrapper{width:80%; margin-left:10%;margin-right:15%;}
.flex{
display: flex;
flex-wrap: wrap;
justify-content: center;
}
header{
width: 100%;
background: blue;
height: 50px;
}
section{
background: red;
width: 300px;
height: 200px;
margin: 10px;
float:left;
}

Container height no longer than screen-header

I asked a question today about good and bad practises in CSS/HTML/jQuery and when it is appropriate to use jQuery to set container dimensions. I got some good answers
So, understanding that jQuery is not the best option, I decided to ask maybe some of you can give some input about this "problem"
So, I have a page put together with php. I have one header for all of my pages and content is being changed with php (I am saying this only to let you guys know that wrapping header and div in one container is not an option):
include ("header.php");
include ("$lang/$section.php");
include ("footer.php");
I have a header with fixed hight (100px + 100px margin-bottom) and after that I have a div which on screens smaller than 768px(height) I want to be no longer than the remaining space. If the screen is larger, I want my div to be
max-height: 420px;
with
padding: 100px 0;
Inside of this div I have 3 floated columns. I need them to fill the space in the parent div.
What I would usually do is- use jQuery and calculate screen height and subtract header height and all the margins and paddings. But as I've learned today, that is not a good practise.
So, to wrap it up: I NEED THE DIV TO FILL THE SPACE BETWEEN HEADER AND BOTTOM OF THE SCREEN FOR VIEWPORT HEIGHT SMALLER THAN 768px. MAX-HEIGHT FOR THIS DIV IS 420px. With jQuery it is super easy but I can't figure out the clean css way.
Maybe some of you have an idea?
Here is my fiddle, so you guys don't have to type out all of the code.
Thank you in advance!
You can use calc() and vh (viewport height).
calc() browser support: http://caniuse.com/#search=calc
vh browser support: http://caniuse.com/#search=vh
So we use calc(100vh - 200px) being 100vh the height of the viewport and 200px the height of the header.
Also, we add a media query so that when the screen is bigger than 768px height we limit the height to 420px.
Try this:
header { height: 100px; background: #ccc; margin-bottom: 100px; box-sizing: border-box; }
section { width: 100%; height: calc(100vh - 200px); padding: 50px 0; background: yellow; box-sizing: border-box; }
.col1, .col2, .col3 { float: left; width: 33%; }
.colPadding { padding: 25px; background: blue; }
.cb { width: 100%; height: 1px; clear: both; }
body {
margin: 0;
}
#media screen and (min-height: 768px) {
section {
max-height: 420px;
}
}
<header>
This is my header with 100px bottom margin
</header>
<section>
<div class="col1">
<div class="colPadding">
section with padding: 50px 0; and max-height: 420px;
</div>
</div>
<div class="col2">
<div class="colPadding">
Column 2
</div>
</div>
<div class="col3">
<div class="colPadding">
Column 3
</div>
</div>
<div class="cb"></div>
</section>
Gave it a shot with CSS3 flex-box model and screen media queries. Here is my fiddle.
I used 300px instead of 764px for the fiddle. (you can change it if you want, I just used 300px so that it's easier to test)
Applied CSS
* { box-sizing: border-box; } /* force sizing based on border */
body {
display: flex; /* flex for body since wrapping header and section is not allowed */
flex-flow: column wrap;
}
header {
height: 100px;
background: #ccc;
margin-bottom: 100px;
flex: 0 0 auto; /* make header size fixed */
}
section {
width: 100%;
max-height: 420px;
padding: 50px 0;
background: yellow;
/* to occupy remaining space */
flex: 1 1 auto;
/* for columns inside to occupy full width */
display: flex;
flex-flow: row wrap;
/* for immediate children to stretch to max height possible */
align-items: stretch;
}
.col1, .col2, .col3 {
float: left;
/* to occupy remaining width */
flex: 1 0 auto;
}
.colPadding {
padding: 25px;
background: blue;
}
.cb {
width: 100%;
height: 1px;
clear: both;
}
/* Custom CSS */
/* style to apply when the screen is less than or equal to 300px (you can change this to 768px) */
#media screen and ( max-height: 300px ){
body {
height: 100vh; /* for body to have a size of the full screen */
}
header {
margin: 0px; /* remove margin bottom */
}
section {
padding: 0px; /* remove margin bottom and top/bottom padding */
margin: 0px;
}
}
More on CSS3 flex-box here.