Responsive CSS float table when resizing for mobile - html

I have a page with a simple 2 column CSS table inside a general container div, like so:
#container:after {
content: "";
display: table;
clear: both;
}
#col_1 {
float: left;
width: 50%;
padding-right: 20px;
}
#col_2 {
width: 50%;
}
It works correctly on computers but on mobile the two columns overlap/overlay and I would like col2 to go underneath col1. In other words float ONLY if there is room for both, otherwise become two rows.
How can I do that?
Thank you

Since I don't have your HTML code, I can only do a mock-up by myself. Add media query for breakpoint for the mobile device.
html,
body,
#container {
width: 100%;
margin: 0;
}
#container:after {
content: "";
display: table;
clear: both;
}
#col_1 {
float: left;
width: 50%;
height: 100px;
padding-right: 20px;
background: orange;
}
#col_2 {
float: left;
width: calc(50% - 20px); /* The 20px is the padding-right of the col_1 */
height: 100px;
background: lime;
}
#media screen and (max-width: 576px) {
#col_1 {
width: 100%;
}
#col_2 {
width: 100%;
}
}
<div id="container">
<div id="col_1"></div>
<div id="col_2"></div>
</div>

Related

How to fix responsive images not resolving to correct desktop size

I have placed responsive images next to text testimonials but they are not displaying at the correct maximum size on a desktop. They are displaying bigger, and I want them at their maximum size only, which is 451 px. The first one, for example, displays at 567 px, despite setting it (obviously incorrectly) in the code. You can see here: https://www.artisanbelle.com (scroll down to the light grey box of testimonials to see).
I have tried the following code (I've only reproduced the first testimonial block, the others are just repeats of it):
<div class="testimonial-container">
<div class="pic-testimonial">
<div class="testimonial-imga"><figure class="photo1"><img class="testimonial-photo" src="http://www.artisanbelle.com/images/stories/amandac.jpg" alt="Amanda C"></figure> </div>
<div class="testimonial-texta"><p>"I saw these and couldn't resist. They are even more beautiful on. The natural stones have a lovely depth of colour. The quality is amazing and great value for money. These would make a great present." - Amanda</p> </div>
</div>
</div>
```
```
.testimonial-container {
width: 100%;
border: none;
}
/* For mobile */
.testimonial-imga, .testimonial-imgb, .testimonial-texta, .testimonial-textb {
width: 100%;
}
.pic-testimonial {
padding:15px;
background-color: #DFE3E6;
}
.testimonial-imga, .testimonial-imgb {
float: left;
}
.testimonial-texta, .testimonial-textb {
float: left;
padding: 15px;
` background-color: #DDE1E4;
width: 50%
}
.pic-testimonial::after {
content: "";
clear: both;
display: table;
}
testimonial-photo {
max-width: 100%;
height: auto;
display: block;
margin-left: auto;
margin-right: auto;
}
#media only screen and (min-width: 768px) {
/* for desktop */
.testimonial-imga, .testimonial-texta, .testimonial-imgb, .testimonial-textb {
width: 50%;
}
.testimonial-imga {
float: left;
}
.testimonial-imgb {
float: right;
}
.testimonial-texta {
float: right;
padding: 15px;
` background-color: #DFE3E6;
width: 50%
}
.testimonial-textb {
float: left;
padding: 15px;
` background-color: #F3F3F3;
width: 50%
}
.photo1 {
display: block;
height: 451px;
overflow: hidden;
position: relative;
}
.testimonial-photo {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
Expected results - images to be max height 451px, with correct width (varies)
Actual result - images are all bigger. The first one is for example 567px
This solve your problem:
.testimonial-photo{
max-height:451px;
max-width:100%;
}
edited

Centering table with more rows

I have three numbers in a div element that is set to display table. I need to center numbers when they are divided in more rows for better experience on narrow devices.
Please run code snippet at full page and then reduce screen size (356px for example).
What I want:
case1: margin|div|div|div|margin
--------------------------------
case2: margin|div|div|margin
margin|div| |margin
--------------------------------
case3: margin|div|margin
margin|div|margin
margin|div|margin
Solved! ...added code + eddited snippet, feel free to try :)
Edit1: There´s aboutqi class which is set to display: table and margin: auto. On wide screen there is only one row and margin works perfect = it´s centered. But when there are 2 or 3 rows margin stop works and aboutqiitems are not centered.
.aboutqi {
margin: auto;
display: table;
}
.aboutqiitems {
float: left;
}
.aboutqiitem {
float: left;
margin-right: 0px;
margin-bottom: 30px;
width: 200px;
}
.aboutqiitem-inner {
width: 100%;
margin: auto;
}
.num {
margin: 0px;
margin-bottom: 20px;
padding-left: 13px;
font-size: 48px;
font-weight: bold;
color: #014493;
}
.num-center {
margin: auto;
display: table;
}
.num-center-last {
margin: auto;
display: table;
position: relative;
left: -7px;
}
/* My solution */
.aboutqiitems-solved {
float: left;
}
#media screen and (max-width: 616px) {
.aboutqiitems-solved {
max-width: 100%;
clear: both;
padding-left: calc((100% - 400px)/2);
}
}
#media screen and (max-width: 416px) {
.aboutqiitems-solved {
max-width: 100%;
clear: both;
padding-left: calc((100% - 200px)/2);
}
}
#media screen and (max-width: 216px) {
.aboutqiitems-solved {
max-width: 100%;
clear: both;
padding-left: 0px;
}
}
<div class="aboutqi">
<p>Hello, need to center numbers when there is more than 1 row.</p>
<div class="aboutqiitems">
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">1</p></div>
</div>
</div>
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">2</p></div>
</div>
</div>
<div class="aboutqiitem last">
<div class="aboutqiitem-inner">
<div class="num-center-last"><p class="num">3</p></div>
</div>
</div>
</div>
</div>
</br>
</br>
<!-- My Solution -->
<div class="aboutqi">
<p>How I solve it. Fell free to change page width :)</p>
<div class="aboutqiitems-solved">
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">1</p></div>
</div>
</div>
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">2</p></div>
</div>
</div>
<div class="aboutqiitem last">
<div class="aboutqiitem-inner">
<div class="num-center-last"><p class="num">3</p></div>
</div>
</div>
</div>
</div>
Example: http://www.dcit.sk/ ... almost at the bottom of the page
You should use media queries in your CSS sheet to help with this. It looks like your numbers are splitting onto 2 rows around 616px, so you want to do something along the following lines:
#media screen and (max-width: 616px) {
.aboutqiitems {
width: 100%;
}
.aboutqiitem {
display: block;
clear: both;
width: 100%;
}
}
Here is a jsfiddle that shows the extra code and how it works: https://jsfiddle.net/4be1k4jr/
You can read more about media queries and how to use them here:
http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
If you're looking to keep the boxes at 200px instead of 100% (in case you have images or something) you can use the following media query. It's a little less clean, but works if you absolutely must keep 200px.
#media screen and (max-width: 616px) {
.aboutqiitems {
width: 100%;
}
.aboutqiitem {
display: block;
clear: both;
width: 200px;
position: relative;
left: 50%;
margin-left: -100px;
}
}
From what I'm taking from this question, you want to change the css when the screen width gets to a certain width. The easiest way to do this is with an #media tag.
Here's my css solution to this problem.
<style>
.aboutqi {
margin: auto;
display: table;
}
.aboutqiitems {
float: left;
}
.aboutqiitem {
float: left;
margin-right: 0px;
margin-bottom: 30px;
width: 200px;
}
.aboutqiitem-inner {
width: 100%;
margin: auto;
}
.num {
margin: 0px;
margin-bottom: 20px;
padding-left: 13px;
font-size: 48px;
font-weight: bold;
color: #014493;
}
.num-center {
margin: auto;
display: table;
}
.num-center-last {
margin: auto;
display: table;
position: relative;
left: -7px;
}
#media only screen and (max-width: 615px){
.aboutqiitem{
width: 100%;
}
.aboutqiitems{
width: 100%;
}
.num{
padding: 0;
}
.num-center-last {
left: 0;
}
}
</style>
Keep in mind that you also need to set a viewport width with a meta tag in your head tag.
<meta content="width=device-width" name="viewport">
This would also work:
#media only screen and (max-width: 615px){
.aboutqiitems{
float: none;
}
.aboutqiitem{
float: none;
margin: auto;
}
.num-center-last{
position: inherit;
}
}

How center div with percentage width on responsive page?

I have a responsive page which can be resized to fit a desktop, tablet or phone. The phone and tablet css is ok but I have problem with the desktop. The difference is the main div which holds all the content.
For the phone and tablet, the main div width is 100% of the screen but for the desktop it should be fixed at 900px and also centered on the screen.
When I have it centered, the main div won't adjust its height depending on the content in it but it will for the other screen sizes. When I add a float: left; to the main div, it floats to the right and then the height follows the content in it
It's probably a really simple fix but I have tried everything I know and googled without finding the solution.
Thanks guys!
body {
background-color: #f1f1f1;
margin: 0;
}
#main {
background-color: #0000ff;
color: #222222;
float: left;
height: auto;
width: 100%;
}
/* Home Big
/*************************/
#home-big {
height: auto;
width: 100%;
}
#home-big h1 {
background-color: #1eaccc;
color: #ffffff;
margin: 0;
padding: 7px;
}
/* Home Big Content
/*************************/
.home-big-content {
float: left;
height: auto;
margin-top: 10px;
width: 100%;
}
/* Home Big Left
/*************************/
.home-big-left {
background-color: #ffff00;
float: left;
height: auto;
width: 50%;
}
.home-big-left img {
height: auto;
width: 100%;
}
/* Home Big Right
/*************************/
.home-big-right {
float: left;
height: auto;
margin-left: 0;
width: 50%;
}
/* TABLET SIZE
/*****************************************************************************/
#media all and (min-width: 600px) {
#main {
background-color: #00ff00;
float: left;
height: initial;
padding: 10px 0;
}
#home-big {
margin: 0 10px;
width: initial;
}
.home-big-left {
background-color: #ffff00;
}
}
/* DESKTOP SIZE
/*****************************************************************************/
#media all and (min-width: 900px) {
#main {
background-color: #ff0000;
float: initial;
margin: 0 auto;
width: 900px;
}
#home-big {
margin: 0;
width: initial;
}
.home-big-left {
background-color: #ffff00;
}
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="stylesheet.css" type="text/css">
</head>
<body>
<section id="main">
<article id="home-big">
<h1>Headline</h1>
<div class="home-big-content">
<div class="home-big-left">
Left
</div>
<div class="home-big-right">
Right
</div>
</div>
</article>
</section>
</body>
</html>
When you float stuff, you have to clear it or use overflow: hidden
Try:
.clearfix:after {
content: "";
display: table;
clear: both;
}
Apply clearfix class to all containers that contain floated elements, or use overflow: hidden;
You need to clear the floats that are with your #main element. I use the micro clearfix.
Use as follows:
<section id="main" class="cf"></section>
.cf:before,
.cf:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.cf:after {
clear: both;
}

Stretch DIV to full page width but also make it auto resize according to browsers borders

How can I make this layout to be fully responsive?
Basically I need both boxes:
chatEntries and #chatUsers to be sctretched across whole page.
They must share page width in ratio of: 85% [chatEntries] - 15% [chatUsers].
So how would I do this?
Accounting for the borders in your illustration I would recommend something like this:
jsFiddle
HTML
<div class="clearfix container">
<div class="chatEntries"></div>
<div class="chatUsers">
<h4>Online Users</h4>
</div>
</div>
CSS
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block; }
.container {
background: #000;
padding-top: 30px;
}
h4 {
color: #05E9FF;
text-align: right;
margin: 0;
font-size: 16px;
font-family: arial, sans-serf;
position: absolute;
top: -20px;
left: 0;
z-index: 1;
}
.chatEntries,
.chatUsers {
min-height: 500px;
}
.chatEntries {
width: 84.8%;
background: #ccc;
float: left;
}
.chatUsers {
position: relative;
width: 14.8%;
background: #999;
float: right;
margin-right: 0.2%;
}
Something like this?
JSFiddle
#chatEntries {
display:inline-block;
width:85%;
float:left; }
#chatUsers {
display:inline-block;
width:15%;
float:left; }

responsive columns not working?

Having trouble with my responsive layout. My twelve columns blocks are not on one row the last two float under it but should be all in one line.
Each row will be having its own column blocks.
You can get full code view of what I mean at my codepen snippet.
http://codepen.io/riwakawebsitedesigns/pen/zbmLE
.container {
display: block;
width: 94%;
max-width: 1280px;
margin: 2% auto 0 auto;
}
.row {
display: block;
}
.row:after {
content: "";
display: table;
clear: both;
}
.column {
background: #e5e5e5;
min-height: 40px;
text-align: center;
float: left;
margin-right: 20px;
}
.block-1 {
width: 8.33333333%;
}
.block-2 {
width: 16.66666667%;
}
.block-3 {
width: 25%;
}
.block-4 {
width: 33.33333333%;
}
.block-5 {
width: 41.66666667%;
}
.block-6 {
width: 50%;
}
.block-7 {
width: 58.33333333%;
}
.block-8 {
width: 66.66666667%;
}
.block-9 {
width: 75%;
}
.block-10 {
width: 83.33333333%;
}
.block-11 {
width: 91.66666667%;
}
.block-12 {
width: 100%;
}
.last, .omega, .end{
margin: 0;
}
Remove the margin on the columns:
.column {
background: #e5e5e5;
min-height: 40px;
text-align: center;
float: left;
margin-right: 20px; <!-- remove -->
}
Instead of the reaching 100% width the margin is causing less width for them due to the 20px x 12. So your getting 100% + 240px, this is causing the last 2 to move below.
DEMO HERE
Ruddy is right, you are not considering the gutter between columns. Do his way or...
SIMPLE SOLUTION:
Try to subtract 2% from each block class, worked here.
FANCY SOLUTION:
If you use LESS you can work with variables, so you could do something like this:
/*LESS FILE*/
#gutter: "20px";
.block1 {
width: (8.333% - #gutter);
}
Hope it helps.