How html2pdf generate pdf without media query css? - css

I'm making a RWD table, and using html2pdf to generate pdf file.
And I using media query to change small screen style like:
#media screen and (max-width: 480px) { /*small screen style*/ }
Is there a way can I genetate pdf by small screen, but without media query css?
Please tell me if you have better solution. Thanks!

I thinl i figure it out.
use a specific class for media query than remove it!
https://codepen.io/junjunfan/pen/LYBobgd
function generatePDF() {
const rootNode = document.querySelector("#print_content")
const opt = {
// margin: 10,
filename: 'mypdf.pdf',
jsPDF: { format: 'b4', orientation: 'portrait' },
html2canvas: {
scale: 4,
useCORS: true,
height: 1200,
}
}
$(".container").removeClass("mqcss");
const element = rootNode;
html2pdf()
.set(opt)
.from(element)
.save();
}
document.getElementById('exportPDF').addEventListener('click', generatePDF);
$(".container").addClass("mqcss");
* {
margin: 0;
padding: 0;
}
body {
width: 100%;
height: 100%;
position: relative;
}
.container {
background-color: #eee;
position: relative;
width: 80%;
max-width: 800px;
height: 80vh;
margin: 0 auto;
}
.container p {
font-size: 50px;
text-align: center;
}
#media screen and (max-width: 480px) {
.mqcss {
background-color: red;
}
.mqcss p {
font-size: 10px;
color: #fff;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.2/html2pdf.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="print_content">
<div class="container mqcss">
<p>RWD Content</p>
</div>
</div>
<button id="exportPDF">generate PDF</button>

Related

How do I change the dimensions of images according to if they are landscape or portraits?

I am very new to css. I have a flow of pictures which I preload in js to determine if they are portraits or landscapes. Then I want to return a div according to that. My css looks like that
.image-wrapper-landscape {
width: 100%;
height: 100%;
display: inline-block;
position: relative;
margin: 5px 0;
&:hover {
.text-wrapper {
opacity: 1;
}
}
}
.image-wrapper-portrait {
width : 49.5%;
display: inline-block;
position: relative;
margin: 5px 0;
&:hover {
.text-wrapper {
opacity: 1;
}
}
}
Then my divs are
<div
className='image-wrapper-portrait'
key={i}
>
<img src={image.src} alt={altText} data-position={image.position} />
</div>
<div
className='image-wrapper-landscape'
key={i}
>
<img src={image.src} alt={altText} data-position={image.position} />
</div>
This doesn't seem to work though since the portrait pictures all look different. What I would like to do is resize all of them so that I end up with a flow of either landscapes or portraits, I also tried pixels instead of percentages but still nothing. I would appreciate nay help, css confuses me.
I think it's working for you, try making the img width 100%;
But while using scss try to make it useful.
.scss
.image-wrapper {
display: inline-block;
position: relative;
margin: 5px 0;
&.portrait {
width: 100%;
height: 100%;
}
&.landscape {
width : 49.5%;
}
&:hover {
.text-wrapper {
opacity: 1;
}
}
img {
width: 100%;
}
}
use class name image-wrapper portrait for portrait and use image-wrapper landscape.
And use object-fit if you wants to.

Is it possible to get child height from parent width dynamically on media query using by sass function? [duplicate]

This question already has answers here:
Responsively change div size keeping aspect ratio [duplicate]
(5 answers)
Closed 7 years ago.
I want to set child item height in various screen size as dynamically using sass function instead of writing manually. I know we can use css position: absolute; property or simply use jQuery code to get the result, but I want to achieve child item value using with sass function.
**We can give width as percentage value, but height can't result without giving position: absolute; when giving percentage value.
Check my pen : http://codepen.io/nikhil8krishnan/pen/adqbeR?editors=1000
Check below codes , Here I'm writing item value manually on each screen size.
Outputs
.container {
background-color: red;
margin: auto;
text-align: center;
color: #fff;
font-size: 0;
}
.child {
display: inline-block;
border-right: solid 1px #fff;
background-color: green;
}
/* container width and child width & height is set by manually on each screen size*/
/*max to 767px*/
.container {
width: 100%;
}
.child {
width: 25%;
height: 25%;
}
#media (min-width: 768px) {
.container {
width: 600px;
}
.child {
height: 150px;
width: 150px;
}
}
#media (min-width: 980px) {
.container {
width: 800px;
}
.child {
height: 200px;
width: 200px;
}
}
#media (min-width: 1200px) {
.container {
width: 1000px;
}
.child {
height: 250px;
width: 250px;
}
}
How can I write those codes with sass functions? Is it possible please share thoughts.
To automate the export of media queries you could use the following function
$viewports: ( 100%, 600px, 800px, 1000px);
#each $viewport in $viewports {
#media (min-width: #{$viewport}) {
.container {
width: $viewport;
}
.child {
height: $viewport/4;
width: $viewport/4;
}
}
}
An example: http://www.sassmeister.com/gist/e87c4121d584776355cd

Can I create more "media min-width" cases for Bootstrap 3?

Boostrap 3 comes with this:
#media (min-width: 1200px)
.container {
width: 1170px;
}
#media (min-width: 992px)
.container {
width: 970px;
}
#media (min-width: 768px)
.container {
width: 750px;
}
Can I freely add more min-width cases? what considerations should I have?
I want to have more cases between 1200px and, say, 1920px... because in a 1920px I end up having a container of 1170px and that sucks because it could be, say, 1300px
It's not as simple as that. Bootstrap 3 comes with 4 classes:
xs (extra small) 0-768
sm (small) 768-992
md (medium) 992-1200
lg (large) >1200
And the container class is linked to this classes, so if you want to have other stepps between this width's you must start with adding a new class for example: xxs (extra extra small) 0-384 (witch is not implemented in bootstrap right now), you have to add this classes:
.col-xxs-1, .col-xxs-2, .col-xxs-3, .col-xxs-4,
.col-xxs-5, .col-xxs-6, .col-xxs-7, .col-xxs-8,
.col-xxs-9, .col-xxs-10, .col-xxs-11, .col-xxs-12 {
min-height: 1px;
padding-left: 15px;
padding-right: 15px;
position: relative;
}
#media (max-width: 384px) {
.col-xxs-1,
.col-xxs-2,
.col-xxs-3,
.col-xxs-4,
.col-xxs-5,
.col-xxs-6,
.col-xxs-7,
.col-xxs-8,
.col-xxs-9,
.col-xxs-10,
.col-xxs-11 {
float: left;
}
.col-xxs-1 {
width: 8.333333333333332%;
}
.col-xxs-2 {
width: 16.666666666666664%;
}
.col-xxs-3 {
width: 25%;
}
.col-xxs-4 {
width: 33.33333333333333%;
}
.col-xxs-5 {
width: 41.66666666666667%;
}
.col-xxs-6 {
width: 50%;
}
.col-xxs-7 {
width: 58.333333333333336%;
}
.col-xxs-8 {
width: 66.66666666666666%;
}
.col-xxs-9 {
width: 75%;
}
.col-xxs-10 {
width: 83.33333333333334%;
}
.col-xxs-11 {
width: 91.66666666666666%;
}
.col-xxs-12 {
width: 100%;
}
.col-xxs-push-1 {
left: 8.333333333333332%;
}
.col-xxs-push-2 {
left: 16.666666666666664%;
}
.col-xxs-push-3 {
left: 25%;
}
.col-xss-push-4 {
left: 33.33333333333333%;
}
.col-xxs-push-5 {
left: 41.66666666666667%;
}
.col-xxs-push-6 {
left: 50%;
}
.col-xxs-push-7 {
left: 58.333333333333336%;
}
.col-xxs-push-8 {
left: 66.66666666666666%;
}
.col-xxs-push-9 {
left: 75%;
}
.col-xxs-push-10 {
left: 83.33333333333334%;
}
.col-xxs-push-11 {
left: 91.66666666666666%;
}
.col-xxs-pull-1 {
right: 8.333333333333332%;
}
.col-xxs-pull-2 {
right: 16.666666666666664%;
}
.col-xxs-pull-3 {
right: 25%;
}
.col-xxs-pull-4 {
right: 33.33333333333333%;
}
.col-xxs-pull-5 {
right: 41.66666666666667%;
}
.col-xxs-pull-6 {
right: 50%;
}
.col-xxs-pull-7 {
right: 58.333333333333336%;
}
.col-xxs-pull-8 {
right: 66.66666666666666%;
}
.col-xxs-pull-9 {
right: 75%;
}
.col-xxs-pull-10 {
right: 83.33333333333334%;
}
.col-xxs-pull-11 {
right: 91.66666666666666%;
}
.col-xxs-offset-1 {
margin-left: 8.333333333333332%;
}
.col-xxs-offset-2 {
margin-left: 16.666666666666664%;
}
.col-xxs-offset-3 {
margin-left: 25%;
}
.col-xxs-offset-4 {
margin-left: 33.33333333333333%;
}
.col-xxs-offset-5 {
margin-left: 41.66666666666667%;
}
.col-xxs-offset-6 {
margin-left: 50%;
}
.col-xxs-offset-7 {
margin-left: 58.333333333333336%;
}
.col-xxs-offset-8 {
margin-left: 66.66666666666666%;
}
.col-xxs-offset-9 {
margin-left: 75%;
}
.col-xxs-offset-10 {
margin-left: 83.33333333333334%;
}
.col-xxs-offset-11 {
margin-left: 91.66666666666666%;
}
}
And also the container class you wrote:
#media (min-width: 384px)
.container {
width: 372px;
}
As you can see, it is very complicated, and it's recomanded not to use bootstrap if you want to customize it very very much.
when you need to use different break-points for container class, you may face 2 scenarios:
Scenario 1:
You know the breaking point. In this case, you can modify the SASS or LESS files, or simply create a customized version at http://getbootstrap.com/customize/#container-sizes
Scenario 2:
The breaking point is variable. In this case you use the class .container-fluid . This class can be (and is usually) used with container class in order to have some control of content inside, but the same way, you can create full screen layouts with a .semi-container class which is a somewhere between the .container class and the full width of the screen. Example:
<div class="container-fluid"><!-- full width -->
<div class="semi-container container-fluid"><!-- max-width -->
<div class="container"><!-- container -->
</div>
</div>
</div>
and then the CSS for .semi-container would be
.semi-container{max-width:1300px} /* or whatever you need */
Of course you can create more breakpoints etc. As long as you're not altering the bootstrap stylesheet it you won't affect the framework.
In reality you need to make cases for 320, 480, 600 etc... More than that.
In truth the bootstrap grid is only a starting point. I mean, come on - you can expect that everything under 768 deserves one rule.
As comprehensive as it is, bootstrap is still only supposed to be a starting point.
I've done tons of things like:
#media (max-width:480px) {
col-xs-4 {
width: 49.999%;
}
}
Anyone who tells you you can't needs a smack - just make sure to test what you've done.
Since your example only targets .container you're pretty safe. All the percentage based widths under it should work as expected.

Responsive Picture Gallery

I'm working on a responsive picture gallery and I'm looking to modify some code I found. I've made a jsFiddle to show you what I'm working with.
jsFiddle
I want to be able to have a couple buttons below each image in the gallery. As you notice, if there is an image underneath an image the image above has its text cut off. I've thought about adding a margin bottom to .box as such:
.box {
float: left;
position: relative;
width: 14.28%;
padding-bottom: 14.28%;
margin-bottom: 30px;
}
jsFiddle with margin-bottom
I'm wondering if there is a better way to approach this. It seems if you re-size the window too small the text overlaps the images.
You just need to make the bottom padding on .box-container 30px:
.box_container {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 10px 10px 30px 10px;
margin-bottom: -35px;
margin-left: 5px;
margin-right: 5px;
}
http://jsfiddle.net/jFwYU/3/
body {
margin: 0;
padding: 0;
background: #EEE;
font: 10px/13px'Lucida Sans', sans-serif;
}
.box {
float: left;
position: relative;
width: 14.28%;
padding-bottom: 10px;
}
.boxInner img {
width: 100%;
}
body.no-touch .boxInner:hover .titleBox, body.touch .boxInner.touchFocus .titleBox {
margin-bottom: 0;
}
#media only screen and (max-width : 480px) {
/* Smartphone view: 1 tile */
.box {
width: 100%;
padding-bottom: 10px;
}
}
#media only screen and (max-width : 650px) and (min-width : 481px) {
/* Tablet view: 2 tiles */
.box {
width: 50%;
padding-bottom: 10px;
}
}
#media only screen and (max-width : 1050px) and (min-width : 651px) {
/* Small desktop / ipad view: 3 tiles */
.box {
width: 33.3%;
padding-bottom: 10px;
}
}
#media only screen and (max-width : 1290px) and (min-width : 1051px) {
/* Medium desktop: 4 tiles */
.box {
width: 25%;
padding-bottom: 10px;
}
}
#media only screen and (max-width : 1590px) and (min-width : 1291px) {
/* Large desktop: 5 tiles */
.box {
width: 20%;
padding-bottom: 10px;
}
}
#media only screen and (max-width : 1920px) and (min-width : 1591px) {
/* Extra large desktop: 6 tiles */
.box {
width: 16.6%;
padding-bottom: 10px;
}
}
1) Removed position:absolute; for boxInner and box_container
2) Avoid using floats and position:absolute; together
3) Only horizontal dimensions should be defined in percentage, not the vertical ones(padding-bottom:33% is not good)
It seems that perhaps the simplest solution for this problem would be to modify the css for the box:
.box {
float: left;
position: relative;
width: 14.28%;
height: 14.28%;
margin-bottom: 40px;
}
height is a much more semantic way than padding-bottom to get what you are looking for, and to make sure the text displays properly (which probably shouldn't be absolutely positioned, but minor issue), add a margin-bottom.
You could also have the margin-bottom as a percent to have it be a percent of the size of the box, say, 20% or so, but px might be a good idea if you're not worried about teeny tiny boxes being an issue (whole gallery less than 50px)
Hope that helps!
I created something to help a college student get started. I adapted your images to the attached code. There are some features that the student needed that you did not ask for, but you can easily remove them.
I believe it meets the intent of preserving the space between images and allows text for each picture. The text is locked to its picture.
The code is not optimized and can certainly be improved, but it will hopefully put you a little closer to what you wanted,
Updated: Example Photo Gallery
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Photo Viewer</title>
<style>
html {
border: 0 none transparent;
}
body {
padding: 0;
margin: 0;
font-family: arial, tahoma, sans-serif;
font-weight: 400;
font-size: 15px;
background: #FEFADA;
color: #2C2218;
width: 100%;
height: 100%;
border: none;
text-align: center;
font-style: normal;
font-weight: normal;
font-variant: normal;
cursor: pointer;
}
.picture {
/*Add venfor specific property*/
-webkit-column-count: 4;
/*Set the default to 4 columns*/
column-count: 4;
/*Set text line height*/
line-height: 1.5;
/*Add venfor specific property*/
-webkit-column-gap: 15px;
column-gap: 15px;
/*Outer margin for picture container*/
margin: auto 10px;
}
/*Switch to three columns at this display width*/
#media (max-width: 1024px) {
.picture {
/*Add venfor specific property*/
-webkit-column-count: 3;
column-count: 3;
}
}
/*Switch to two columns at this display width*/
#media (max-width: 764px) {
.picture {
/*Add venfor specific property*/
-webkit-column-count: 2;
column-count: 2;
}
}
/*Switch to one columns at this display width*/
#media (max-width: 480px) {
.picture {
/*Add venfor specific property*/
-webkit-column-count: 1;
column-count: 1;
}
}
.hide {
display: none;
}
.img, .selected-img {
width: 100%;
margin: 8px auto;
white-space: nowrap;
}
.selected-img {
max-width: 1024px;
}
.selected {
position: fixed;
margin: 5px auto;
left: 0;
right: 0;
z-index: 10;
display: block;
}
.closeme {
-webkit-align-content: center;
left: 0;
right: 0;
width: auto;
margin: auto;
display: block;
}
.disable {
background: rgba(0, 0, 0, .3);
width: 100% !important;
height: 100% !important;
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: 5;
position: fixed;
}
.btn {
border: 1px;
border-color: transparent #D1C89D transparent #D1C89D;
background: #EBE6C1;
height: 40px;
margin: auto 0;
width: 100%;
}
.picture-text {
margin: auto;
padding: 10px 3px;
display: table-cell;
white-space: normal;
}
.inline-block {
display: inline-block;
white-space: nowrap;
}
</style>
</head>
<body>
<div>
<!-- Hide the selected image window until its needed with the hide class -->
<div id="selectedWindow" class="hide">
<button class="closeme btn" onclick="closeWindow()">Click this Button or on the Selected Image to close window.</button>
<!-- Load the selected image here -->
<img id="selectedImage" class="selected-img" onclick="closeWindow()" />
</div>
<button class="btn" onclick="loadImages()">Load Images</button>
<div id="imageContainer" class="picture" onclick="loadSelectedPicture(event)"></div>
<div id="disableMask"></div>
</div>
<script type="application/javascript">
function loadImages() {
// For DEBUG if you need it: alert("Made it to the function");
var element = document.getElementById("imageContainer");
var imageArray = ["http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/1.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/2.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/3.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/4.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/5.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/6.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/7.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/8.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/9.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/10.jpg", "http://www.dwuser.com/education/content/creating-responsive-tiled-layout-with-pure-css/images/demo/11.jpg"];
var picture = "";
for (var i = 0; i < imageArray.length; i++) {
// Create an id for each image and add its class.
picture += "<div><img id=\"i" + i + "\" class=\"img\" src=\"" + imageArray[i] + "\"><div class=\"inline-block\"><div class=\"picture-text\">Here is some text for each image. How long can this text be before we have some issues.</div></div></div>";
// For DEBUG if you need it: console.log(picture);
}
element.innerHTML = picture;
}
function loadSelectedPicture(event) {
var target = event.target || event.srcElement;
this.stopEventPropagation(event);
var selectedElement = document.getElementById(target.id);
var imageElement = document.getElementById("selectedImage");
if (!selectedElement.src)
return;
imageElement.src = selectedElement.src;
document.getElementById("selectedWindow").className = "selected";
document.getElementById("disableMask").className = "disable";
}
function stopEventPropagation(event) {
if (!event)
event = window.event;
//IE9 & Other Browsers
if (event.stopPropagation) {
event.stopPropagation();
}
//IE8 and Lower
else {
event.cancelBubble = true;
}
}
function closeWindow() {
document.getElementById("selectedWindow").className = "hide";
document.getElementById("disableMask").className = "";
}
</script>
</body>
</html>
I will try to update this post with a working jsFiddle when I have some free time.

Website does not scale down for mobile screens

I am making a web for desktop, tablet and mobile screens. For this I am using css media queries. When I test my website on desktop browser and when I scale my web bowser down then it does change its layout when it reaches to max-width 740px as mentioned in css. But the problem is when I test this on my mobile then it does not change its layout to fit mobile screens.
I am using 12 columns layout system and using less css. Please help me why it does not change its layout on mobile phones to fit mobile screens.
Here is my css or less css to be precise
html, body {
height: 100%;
//overflow: hidden;
}
body {
min-width: 360px;
background-color: #ffffff;
#page {
width: 100%;
height: 100%;
overflow: auto;
}
.right {
position: relative;
right: 0;
}
// Extend column system (from defaults/layout.css)
.row {
clear: both;
> .container {
max-width: 1024px;
margin: 0 auto;
padding: 0 2% 0 2%;
.container {
max-width: 100%;
padding-left: 0;
padding-right: 0;
&.one {
width: 8%;
}
&.two {
width: 16%;
}
&.three {
width: 25%;
}
&.four {
width: 33%;
}
&.five {
width: 41%;
}
&.six {
width: 50%;
}
&.seven {
width: 58%;
}
&.eight {
width: 66%;
}
&.nine {
width: 75%;
}
&.ten {
width: 83%;
}
&.eleven {
width: 91%;
}
&.twelve {
width: 100%;
}
}
}
}
}
/*Collapse columns*/
#media only screen and (max-width: 740px) {
.column, .column.one, .column.two, .column.three, .column.four, .column.five, .column.six, .column.eight, .column.nine, .column.ten, .column.eleven, .column.twelve,
.column.close-right, .column.one.close-right, .column.two.close-right, .column.three.close-right, .column.four.close-right, .column.five.close-right, .column.six.close-right, .column.eight.close-right, .column.nine.close-right, .column.ten.close-right, .column.eleven.close-right, .column.twelve.close-right {
//width: auto;
//float: none;
//clear: both;
margin-right: 0;
}
.column.third {
display: none;
}
.column.second {
width: 77%;
}
.column.first {
//min-width: 180px;
}
}
Try adding this to the top of your HTML page:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
That should let the mobile browser know that the page you be rendered at the size of your device/browser, instead of faking the screen size of a desktop computer (and thus confusing your media queries).
http://www.allenpike.com/2010/choosing-a-viewport-for-ipad-sites/

Resources