HTML/CSS - Mobile friendly and resizing easily - html

My classmates and I are trying to figure how to make this code be completely mobile friendly. We tried using #media screen but it did not work. We want to make this happen with HTML and CSS.
HTML
<!--List Content Start-->
<div class="listcontent">
<div class="listnumber">1</div>
<div class="listtitle">This div tag emphasizes the title.</div>
<div class="listpic"></div>
</div>
<br><br>
<!-- List Content End-->
<div class="listcontent">
<div class="listnumber">2</div>
<div class="listtitle">This div tag emphasizes the title.</div>
<div class="listpic"></div>
</div>
</div>
CSS
.listcontainer {
width: 100%;
height: 100%;
}
.listcontent {
width: 500px;
height: 400px;
margin: auto;
background-color: #F5EFEF;
padding:5px;
}
.listnumber {
width: 50px;
height: 50px;
float: left;
background-color: #B33638;
padding: 5px;
color: white;
font-size: 45px;
text-align: center;
}
.listtitle {
width: 425px;
height: 50px;
padding: 5px;
float: right;
background-color: #fff;
}
.listpic {
width: 100%;
height: 335px;
margin-top: 65px;
}
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

Here is the fix I created for you in terms of your coding snippet: JSFiddle
.listcontainer {
width: 100%;
height: 100%;
}
.listcontent:first-child {
margin-bottom: 40px;
}
.listcontent {
max-width: 500px;
min-width: 320px;
width: 100%;
height: 400px;
margin: auto;
background-color: #F5EFEF;
padding: 0;
}
.titlewrapper {
width: 100%;
}
.listnumber, .listtitle {
display: inline-block;
}
.listnumber {
width: 50px;
height: 50px;
background-color: #B33638;
padding: 5px;
color: white;
font-size: 45px;
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
float: left;
}
.listtitle {
height: 50px;
line-height: 50px;
padding: 5px;
background-color: #fff;
white-space: nowrap;
width: calc(100% - 70px);
max-width: 100%;
}
.listpic {
width: 100%;
height: 335px;
margin-top: 65px;
}
<div>
<!--List Content Start-->
<div class="listcontent">
<div class="titlewrapper">
<div class="listnumber">1</div>
<div class="listtitle">
This div tag emphasizes the title.
</div>
</div>
<div style="clear:both;" />
<div class="listpic"></div>
</div>
<!-- List Content End-->
<div class="listcontent">
<div class="titlewrapper">
<div class="listnumber">2</div>
<div class="listtitle">
This div tag emphasizes the title.
</div>
</div>
<div style="clear:both;" />
<div class="listpic"></div>
</div>
</div>
Ok so let's dive in, what are the reason for all these CSS and HTML changes?
To make something mobile responsive you need to consider the behavior it needs to have. When it comes to element widths, a general rule of thumb is the following.
CSS code example:
.some-wrapper-element {
width: 100%;
min-width: 320px;
max-width: 100%;
}
This makes a wrapping element, such as your .listcontent to become responsive with and without media queries being used. Note how I applied this throughout the CSS to give elements which needed to resize as the page resized, a dynamic width.
Your HTML layout needed a little more thought behind it. You are trying to horizontally align two elements and make them responsive. I will admit this is not a straight forward and easy to implement solution, but there are standard things to look at:
A wrapping element to ensure horizontal alignment occurs.
A CSS rule to keep the elements in line, such as display: inline-block or float: left, or a combination... the implementation depends on what works for you.
The elements to be horizontally aligned and made responsive, need to fit next to each other. This is important and it is the reason for all the added CSS code. See a very good reference here: How to place two divs side by side where one sized to fit and other takes up remaining space?
Media queries..., my rule of thumb is: does x element need to change responsively in a way which cannot be done with CSS styling first? Such as hiding/showing an image on certain screen widths, then your answer is yes please. Otherwise think of our layout first, how to make it responsive first and last how to use media queries for the things you cannot make responsive.
The <div style="clear:both;" /> code that was put there. That exists only to help separate your title section from your image section. It is another layout sugar I put there for you, because it will help keep things in place and separate content that does not need to be mixed. Awesome right!
line-height: 55px; This is simple: if you have text inside a small element (like the one you have) and you want it to look well, center it using line-height that is equal to the element's height. I did this just because I thought it looks nice, but change it if you think it is unnecessary.
Anyways, I hope this helps let me know if you have any questions.

The listcontainer should have the fixed width, while the listcontent fill them by 100%. All you have to do then is just fill the media querys like this:
#media only screen
and (min-device-width : 320px)
and (max-device-width : 1024px) {
.listcontainer {
width: 100%;
height: 100%;
}
}
This way the site will have a fixed width for desktop usage, once the browser is too small to display the entire page (in this case 1024px but that depends on the page - in your example probably 500px) it will go to 100% dynamically, which is the most common approach. I can't tell you all of the media querys, since it depends on the developer to decide what the bevahiour should look like.
If you want to have a really mobile friendly site I recommend you using a framework like bootstrap - it does most of the job for you and you'll learn exactly how media querys are working and how you are supposed to use them properly.

Related

How to properly align 2 text boxes side by side with css flexbox and cap the width for desktop view only?

I have 2 text boxes in a css flexbox. Left side is a tagline and right side is a brief summary. I am trying to have them display on a webpage side by side in the center of the page for desktops only (i.e. min width 1024px) but let these boxes stack up below that breakpoint (i.e. on tablets, phones with widths below 1024px). The challenge here is that both of these text boxes combined cannot exceed 50% of the width of the page (horizontally). Right now, when expanding screen size, the text stretches to the ends of the screen. I tried many different fixes and nothing I tried worked properly!
My HTML Code:
<div class="info-block">
<div class="info-item">
<div class="info-col">
<h4>Modern Approach, Innovative Solutions, Accessible Support</h4>
</div>
<div class="info-col">
<div class="info-text">We approach problems with holistic and practical solutions, each and every time.</div>
</div>
</div>
</div>
My CSS code:
.info-item {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
justify-content: space-between;
margin: 0;
}
.info-item:last-child {margin: 0;}
.info-col {
width: 46.6%;
font-size: 24px;
}
.info-col:first-child {
text-align: right;
}
.info-text {
margin: 0 0 20px;
font-size: 24px;
line-height: 115%;
font-weight: 200;
}
#media screen and (min-width: 1900px) {
.info-text {
margin-right: 200px;
max-width: 420px;
margin-top: 5px;
}
.info-col {
max-width: 420px;
}
Any help or suggestions would be greatly, greatly appreciated!
Easy. Add this to your CSS:
#media screen and (max-width: 1024px) { /* If screen size is maximum 1024px */
.info-item {
flex-direction: column; /* Set the flex direction to */
}
.info-col {
width: 100%; /* Setting width to 100% */
}
}
To keep the items centered, you need to set the width to 100% while you stack the columns on smaller screens.

HTML/CSS Fit Div to Contents unless screen is small

How do I setup HTML/CSS to have my DIV follow the screen size for width, but stop expanding once it fits the contents (it should scroll left/right when the div cannot fully contain the contents).
Pseudo-Code:
HTML:
<div class="image-container">
<img width="1000">
</div>
CSS:
.image-container {
/* ??? */
display: inline-block; /* ??? */
overflow: auto;
}
EDIT: Per Evadore's answer, I was able to come up with the following CSS.
.image-container {
display: inline-block;
overflow: auto;
}
/* optimize these px dimensions, 900 worked for my application */
#media (max-width: 900px) {
.image-container {
max-width: 710px;
}
}
/* redundant, I plan to tweak this range later */
#media (min-width: 901px) and (max-width: 1575px) {
.image-container {
max-width: 710px;
}
}
#media (min-width: 1576px) {
.image-container {
max-width: 1385px;
}
}
The following reference also helped: w3schools
Use CSS Media queries to setup for various screen sizes.
view source code of this page to see how media queries were used.
for this set the parent div width to fit-content and max-width to 100%. now the parent div will remain between the width of the content and the with of the screen if the screen size is not enough. And lastly for scrolling inside the parent div on the small screen devices put overflow:scroll.
Here is the Codepen demo
.parent {
background-color: green;
width: fit-content;
max-width: 100%;
overflow: scroll;
}
.child {
padding: 30px;
width: 700px;
background-color: red;
color: #fff;
}
<div class="parent">
<div class="child">
test string
</div>
</div>
ps: I've added bg colors just for reference purposes, to show whether the parent component is expanding or not.

Media Queries not working properly

I've been trying to write some simple media queries, but I was stuck right after I started. It seems like media queries only work on text and not on divs and images.
This is my css code along with the html.
#media (max-width: 720px) {
.logo {
margin-top: 30px;
margin-bottom: 20px;
width: 100%;
}
<!-- only this piece of query works --> .text {
border-bottom: 2px solid red;
}
.gif {
clear: right;
}
}
body {
background-image: url('website/resources/images/body.png')
}
.logo_container {
width: 700px;
height: auto;
margin-top: 60px;
margin-bottom: 40px;
}
#logo {
width: 100%;
height: auto;
}
.text {
font-size: 30px;
margin-bottom: 60px;
}
.gif {
float: right;
}
<center>
<div class="logo_container">
<img id="logo" src="logo.png"></img>
</div>
<div class="text">some text ...</div>
<div class="gif">
<img src="under_construction.gif"></img>
</div>
</center>
Acording to this code image should strech to 100% of the window width right after window size comes under 720px and gif which float to the left of the text should clear its float and go under the image. But nothing happens, except text gets a red border.
I've tried some different formats of media queries, #media () {}, #media screen () {}, #media only screen () {}, #media screen and () {}, #media only screen and () {} but none of these seem to work for images and divs.
Here is my whole code:
http://pastebin.com/0bvUrZnU
OK so your media queries are not great.
Firstly lets change media to : #media handheld,screen and (max-width: 720px)
This will allow your query to be read across the board by DPI changes resolution changes it will even work in things like iframes and pretender box's and emulators it all basically.
Now also as a rule of thumb your media queries should be at the bottom of your style sheet. We do this because style sheets are read from top to bottom so all overriding styles should go underneath original style rule's.
so you want this :
You were missing a . before text and also use float:none; when canceling a float.
I have also tidied up your html a little also with <img> tags always define the height and width withing the tag itself like so <img width="300" height="100" /> and then use css to override it. this is so the browser can render the image faster because it knows its proportion's & you should all ways have an alt attribute. finally images are not wrappers they do not need to end in </img> instead just finish it all off like this: <img width="300" height="100" alt="iam an image and if i wanted to be responsive i should have max-width:100%; height:auto; as my CSS rule." />
body {
background-image: url('website/resources/images/body.png')
}
.logo_container {
width: 700px;
height: auto;
margin-top: 60px;
margin-bottom: 40px;
}
#logo {
width: 100%;
height: auto;
}
.text {
font-size: 30px;
margin-bottom: 60px;
}
.gif {
float: right;
}
#media handheld,
screen and (max-width: 720px) {
#logo {
margin-top: 30px;
margin-bottom: 20px;
width: 100%;
}
.text {
border-bottom: 2px solid red;
}
.gif {
float:none;
}
}
<center>
<div class="logo_container">
<img id="logo" src="logo.png" alt="all images should have alts and use width and height" />
</div>
<div class="text">some text ...</div>
<div class="gif">
<img src="under_construction.gif" alt="all images should have alts and use width and height" />
</div>
</center>
on your desktop code you target logo as an id #logo and in your media query you target it as a class .logo
It works as expected but you have some problem in code inside your media query. You are referring it as class instead of id
#media (max-width: 720px) {
/*this is id but you just referred it with .logo which isn't present*/
#logo {
margin-top: 30px;
margin-bottom: 20px;
width: 100%;
}
/*only this piece of query works*/
.text {
border-bottom: 2px solid red;
}
.gif {
clear: right;
}
}
I had the same problem after css lint suggested to remove
* {
margin: 0;
padding: 0;
I replaced that and media queries worked again.
Most issues that I have come across with Media Queries not working as expected are due to the order they appear. Sometimes they can get out of order unexpectedly, especially when changing from min to max or vice versa.
When using max-width, check to make sure all queries appear largest to smallest width (1200px, then 992px, etc).
When using min-width, check to make sure all queries appear smallest width to largest (576px, then 768px etc).

Resize and Reposition DIVS possible using only CSS?

Okay, I've got it resizing nicely for devices using a media query. Now I need to reproduce this on a browser resize. Is it possible using only CSS? I'm trying to avoid multiple named divs for scalability (i.e. add another change the min-width etc and it'll still work)
Yes, this may well have been asked before (I really have hunted), but there's just so many ways of framing the question...please indulge me .
The media query with viewport turns the divs into columns of a specific size.
But how on earth do I do this during a browser resize?
If you view this result on device via Chrome inspect etc my point will be abundantly clear.
Thanks all!
#Page {
margin: 0 auto 20px;
width: 98%;
/*1000px*/
background-color: lightgray;
}
#content {
margin: 0 auto 10%;
width: 96%;
background-color: green;
max-width: 1100px;
}
.col_content {
float: left;
margin: auto 1%;
width: 30%;
background-color: pink;
min-width: 225px;
}
#media only screen and (max-device-width: 768px) {
#Page {
background-color: white;
}
#content {
max-width: 400px;
background-color: green;
}
.col_content {
float: none;
margin: 1%;
/*5px*/
width: 100%;
background-color: pink;
}
}
<div id="content">
<!--Content-->
<div class="col_content">
1
</div>
<!--end col_content-->
<div class="col_content">
2
</div>
<!--end col_content-->
<div class="col_content">
3
</div>
<!--end col_content-->
</div>
<!--end content-->
Try changing:
#media only screen and (max-device-width: 768px) {
to
#media screen and (max-width: 768px) {
I use the above on my website and it works on browser resizes and on devices.

Bootstrap div won't adapt to height based on content

Note: Yes I did make a research first trying to find a solution and tried to implement a number of options to fix the problem, nothing I could find worked though!
UPDATE
Because media queries are not an optimal solution for my problem, as I have to take into account multiple cases of width/height combinations in a responsive layout, I used some javascript at the end in order to calculate the difference in height of the banner div and the content div in order to readjust height accordingly. Here is the code I used
function resizeContainers()
{
var bannerContainer = $('#banner');
var contentContainer = $('#homeFormContainer');
var bannerContainerHeight = bannerContainer.height();
var bannerContainerBottom = $(window).height() - bannerContainerHeight;
var contentContainerBottom = $(window).height() - contentContainer.height();
var containersDiff = bannerContainerBottom - contentContainerBottom;
if (containersDiff > -200) {
var newBannerContainerHeight = bannerContainerHeight+contentContainerBottom+20;
bannerContainer.css('height', newBannerContainerHeight);
}
}
$(document).ready(function () {
// check and resize after page loads
resizeContainers();
// check and resize containers on window resize
$(window).resize(function() {
resizeContainers();
});
});
I am struggling with a div in bootstrap that won't adapt to the height of it's inner content. As far as I can tell, CSS looks OK (but it probably is not, so I could use some help).
You can see the fiddle here
http://jsfiddle.net/spairus/fbmssraw/6/
The basic HTML structure looks like this
<div id="banner" class="banner">
<div class="banner-image"></div>
<div class="banner-caption" style="display: table;">
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
(content here)
</div>
</div>
</div>
</div>
</div>
And the CSS
html,
body {
height: 100%;
}
img {
display: block;
max-width: 100%;
height: auto;
}
.banner {
width: 100%;
height:100%;
min-height: 100%;
position: relative;
color: #fff;
}
.banner-image {
vertical-align: middle;
min-height: 100%;
width: 100%;
}
.banner:after {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.55);
content: "";
}
.banner-caption {
position: absolute;
top: 22%;
width: 100%;
z-index: 2;
}
.subfooter {
background-color: #fafafa;
border-top: 1px solid #f3f3f3;
border-bottom: 1px solid #f3f3f3;
padding: 40px 0;
}
/* Backgrounds
----------------------------------------------------------------------------- */
.default-bg {
background-color: #222222;
color: #ffffff;
}
.space {
padding: 20px 0;
}
I need the .banner and .banner-caption to expand along with the content.
If you're going to have the following code:
html,
body {
height: 100%;
}
and then on your main container:
.banner {
width: 100%;
height:100%;
min-height: 100%;
position: relative;
color: #fff;
}
the container is not going to be responsive to the content inside it, because what you've said in the CSS is basically "hey container (banner), I want you to be 100% of the screen size of whatever device you show up on" .
Instead you need to change the css to said "hey container I want you to have a min-width or be responsive to whatever content you have inside you", so you could do the following:
The best solution I have for this problem (which I face quite often) is as follows.
#media only screen and (min-width : 992px) {
.banner {
width: 100%;
height:120%; // adjust this to whatever size you think your content will stretch to there is no golden rule saying it has to be 100% only.
min-height: 100%;
position: relative;
color: #fff;
}
}
Edit
Why does the above solution work? Notice how I have added a media query that says min width of 992px, now it's safe to say that the dimension of devices above 992px are predictable, and thus it's safe to use a height:120%; it's only when you go below 992px or 768px that the screen resolution (height & width) become unpredictable to counter this you add absolutely no styling to the height of the container.
How effective is this technique? Well, it is pretty effective, but on some screens (the screens above 992px) there might arise a problem of excessive white spacing or slight overlapping of content.
Now to counter this problem you can use multiple media queries : eg.
#media only screen and (min-width : 768px) {
height : 125 %;
}
/* Medium Devices, Desktops */
#media only screen and (min-width : 992px) {
height : 120 %
}
/* Large Devices, Wide Screens */
#media only screen and (min-width : 1200px) {
height : 100 %
}
but IMHO, this is not needed unless you need pin point accuracy (multiple media queries are a pain!). Worst case scenario, use two media queries.
Anyway, here's the technique in action. Try to reduce the screen size and voila! Still looks pretty, and nowhere have a added height:400% for smaller screens: it adapts by itself.