Centering table with more rows - html

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;
}
}

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

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;
}

CSS, two columns - left with dynamic width (input), right with fixed (button)

can somebody please point me to a solution for this?
HTML
<div class="container">
<input type="text" class="left" />
<button class="right">Some button</button>
</div>
CSS
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
}
.right { width: 100px; }
Here is code pen sample: http://codepen.io/be-codified/pen/qdRRBY
Input field should be stretchable, button should be fixed positioned on right.
Thank you in advance.
// edit
I can not use table tag because layout needs to be responsive.
I gave the input a width of calc(100% - 110px) and the button a float:right which resulted in the following. Is that what you need? The input type you want to use is, as far as I know, not stretchable by the user.
CSS
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
}
.right {
width: 100px;
float: right;
}
input.left {
width: calc(100% - 110px); //This makes sure the input area is as wide as possible, while also leaving space for the button. Play with the exact measurements to get what you need.
}
I suggest you to put the form elements into <div>, so don't change their default display properties, and then set the left input box to 100% width as needed.
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
}
.right {
width: 100px;
}
.left input {
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div class="left"><input type="text" /></div>
<div class="right"><button>Some button</button></div>
</div>
In fact, both left and right can have dynamic width, so right column always get the minimum width based on the button length.
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
white-space: nowrap;
}
.left {
width: 100%;
}
.left input {
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div class="left"><input type="text" /></div>
<div class="right"><button>Some button</button></div>
</div>
Here is full responsive solution.
HTML
<div class="container">
<div class="input-flied-box">
<form>
<input type="text" required="required" placeholder="Right Some thing" />
<button type="submit" class="submit-button">Send</button>
</form>
</div>
</div>
CSS
/* RESPONSIVE CSS */
.container{
width: 100%;
}
.input-flied-box{
width: 100%;
}
.input-flied-box input{
padding: 6px 12px 6px 12px;
}
.submit-button{
top: inherit;
right: inherit;
position: relative;
margin-bottom: 15px;
}
#media (min-width: 768px){
.container{
width: 750px;
}
.input-flied-box{
width: 600px;
}
.input-flied-box input{
padding: 6px 101px 6px 12px;
}
.submit-button{
top: 14px;
right: 14px;
position: absolute;
margin-bottom: 0;
}
}
#media (min-width: 992px){
.container{
width: 960px;
}
}
#media (min-width: 1200px){
.container{
width: 1170px;
}
}
/* RESPONSIVE CSS END */
*:after,
*:before{
box-sizing: border-box;
}
*{
box-sizing: border-box;
}
.container:after,
.container:before{
display: table;
content: " ";
clear: both;
}
.container{
margin-left: auto;
margin-right: auto;
}
.input-flied-box {
background-color: #666666;
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
position: relative;
}
.input-flied-box input {
background-color: #ffffff;
border: 1px solid #cccccc;
height: 40px;
margin-bottom: 15px;
margin-top: 15px;
width: 100%;
border-radius: 4px;
}
.submit-button {
background-color: #fc3850;
border: medium none;
border-radius: 4px;
color: #ffffff;
font-family: Arial;
line-height: 1;
padding: 13px 30px;
text-transform: uppercase;
}
https://jsfiddle.net/bL3wgrv9/

Attempting to resize img with CSS, not responding to any size attributes

I don't have a web development background, but have a small business client who has me doing other things who got me working on their web site. I've designed a home page, more or less ready for me to turn it into a template in whatever CMS we decide to use.
It was going great until I started replacing blank divs with images. Now that I'm putting in the proxy images, everything is breaking down.
The primary issue is that the CSS attributes are not resizing the image, but the image at full size would be acceptable if the rest of the body actually flowed in such a way that the image wasn't covering it when rendered.
So far, I've tried adding height and with attributes to the img tag equal to both the actual height of the image and the height I'd like it to be. (310px for the desktop width.)
I've also tried nesting the image inside a div inside the section and applying widths there. I ended up with other problems, I assume from the shortcuts I took in the CSS, but even after changing how I was designating the description, it still was not resizing and the overflow was not being hidden.
Here's a JSFiddle.
The current HTML:
<body>
<header id="page-head" class="clearfix">
<div id="logo">Logo</div>
<h1>Academic Tours and Travel</h1>
<nav>
<ul class="clearfix">
<li><div>Travel With Us</div></li>
<li><div>Sign In</div></li>
</ul>
</nav>
</header>
<section id="feature">
<img src="https://i.imgur.com/CG8cppD.jpg">
<div>
<h1>Malta-Sicily Delight</h1>
<p>Our star tour. Visit Malta and Sicily with our hand crafted tours.</p>
</div>
</section>
<div id="content-wrap" class="clearfix">
<div id="right-wrap" class="clearfix">
<section id="about">
<header><h2>About</h2></header>
<p>Founded in 1984, Academic Tours and Travel is a leading specialist in travel to Malta and Sicily. They also are home to experts in many European destinations including Italy and Romania.</p>
</section>
<section id="pledge">
<header><h2>Our Pledge</h2></header>
<p><em>"From Concept to Completion"</em></p>
<p>Our agents will work with you from the first idea until you pick you unpack. If we ever recieve complaints from a local provider they are investigated and blacklisted for at least a year.</p>
</section>
<a href="">
<div id="call" class="clear">
<h1>Browse our Destinations</h1>
</div>
</a>
</div>
<section id="contact">
<header><h2>Contact an Agent</h2></header>
<form>
<label>Name: <input type="text" name="name" placeholder="Jane Doe" required="true" /></label><br />
<label>Email: <input type="text" name="email" placeholder="address#server.com" required="true" /></label><br />
<label>What's your dream vacation? <textarea name="destination" placeholder="Sample text" required></textarea></label><br />
<button type="submit">Send</button>
<button type="reset">Reset</button>
</form>
</section>
</div>
</body>
Current CSS:
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
/* border: 1px solid black; */
}
html {
height: 100%;
}
a {
text-decoration: none;
}
body {
font-size: 62.5%;
margin: auto;
height: 600px;
font-family: Geneva, Tahoma, Verdana, sans-serif;
}
button {
width: 47%;
margin-right: 5%;
display: inline-block;
}
button:last-child {
margin-right: 0;
}
h1 {
font-size: 2.1em;
text-align: center;
color: #c2d22d;
}
h2 {
font-size: 1.6em;
text-align: center;
color: #c2d22d;
}
header {
background-color: #134313;
}
input {
width: 100%;
}
img {
height: 100px;
}
label {
font-size: 1.2em;
}
nav ul {
min-width: 350px;
position: relative;
}
nav li {
display: inline;
list-style: none;
text-align: center;
padding-top: 12px;
width: 25%;
min-width: 60px;
background-color: #B3DFB3;
position: absolute;
height: 40px;
font-size: 1.3333em;
border-radius: 5px;
}
nav li:hover {
background-color: #A4B4C7;
}
nav li:first-child {
left: 20%;
}
nav li:last-child {
right: 20%;
}
nav li a {
text-decoration: none;
color: #010A01;
}
nav li a div {
height: 100%;
width: 100%;
}
p {
font-size: 1.2em;
}
section p {
padding: 0 10px;
}
textarea {
width: 100%;
}
#page-head {
width: 100%;
height: 120px;
}
#call {
min-height: 60px;
}
#call:hover {
background-color: #A4B4C7;
}
#call h1 {
color: #134313;
padding: 15px;
}
#content-wrap {
background-color:#E0FEE0;
}
#logo {
display: none;
}
#feature {
clear: both;
position: relative;
}
#feature div {
width: 77.5%;
margin: auto;
position: absolute;
left: 11.25%;
bottom: 3.8461%;
}
#media screen and (min-width: 425px) {
#about {
margin-right: 5%;
}
#about, #pledge {
width: 47.5%;
float: left;
}
#call {
font-size: 1.3333em;
}
}
#media screen and min-width: 600px) {
#page-head h1 {
max-width: 100%;
text-align: left;
float: left;
}
#page-head nav {
max-width: 60%;
float: right;
}
}
#media screen and (min-width: 740px) {
body {
max-width: 960px;
}
button {
width: 46%;
}
/* IDs for specific features. */
#contact {
width: 30%;
}
#content-wrap {
width: 100%;
min-height: 250px;
}
#feature {
min-height: 310px;
width: 83.3333%;
margin: auto;
position: relative;
}
#logo {
display: block;
}
#page-head {
height: 60px;
}
#page-head > * {
float: left;
}
#page-head > nav {
float: right;
}
#right-wrap {
width: 65%;
height: 100%;
float: right;
}
}
/* Utility classes. */
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clear {
clear: both;
}
Try setting
Img{ width:100%}
And then edit the height of the section #feature to the height you want it to be. The image will fill the the width of the section.

div overlaps on another div when screen size reduced

Below is a simple html web page that is responsive except for one div (goplay) that over lays other parts of the page when screen size is reduced, instead of dropping below the image.
Styling Sheet external
#wrapperlp {
width: 100%;
margin: auto;
}
#media screen and (max-width: 800px) {
#wrapperlp {
width: 90%;
min-width: 100px;
}
}
#headerlp {
font-size: 30px;
color: white;
text-align: center;
padding-top: 10px;
padding-bottom: 10px;
}
#para {
font-size: 20px;
color: white;
text-align: center;
}
#game_img {
height: 250px;
width: auto;
margin-bottom: -30px;
position: relative;
display: inline-block;
float: left;
margin-top:-30px;
padding-top: 5px;
max-width: 100%;
}
#goplay {
position: relative;
display: inline-block;
float: right;
margin-top:-250px;
margin-left:80px
}
#spacer {
height: 40px;
margin-left: auto;
margin-right: auto;
width: 100%;
max-width: 900px;
padding-top:20px;
}
Html which is set to call the above css
<div id="wrapperlp">
<div style="background-image: url(https://.jpg); height: 430px; width: 1000px; margin-left: auto; margin-right: auto; max-width: 100%;">
<div id="headerlp">Some Text</div>
<div id="para">More Text</div>
<div id="game_img"><a href="//www.youtube.com/" target="_blank"><br />
<img src="https://.png" height="auto"/></a></div>
</div>
</div>
<div id="goplay">----form----/div>
<div id="spacer">
<div style="position: relative; float: left">Text</div>
</div>
margin-top and left should in %. thats y its overlay becoz of px
First off, it looks like you're missing a couple of divs.
The goplay div doesn't have a closing tag, (well it's got one but not that works)
Also your bottom spacer looks like it's missing a closing tag as well. Not sure if it's supposed to wrap anything or what.
Perhaps you had some copy/paste errors?
Normally if you set a negative margin it will overwrite other divs. You should, for the most part, not have to use negative margins.