As I'm not good at explaining, I drew a few pictures.
I currently have the following code
<header style="height: 280px;">
<div id="topmenu" style="height: 70px; width: auto; margin-top: 217px;">
<ul><li>home</li><li>page</li><li>contact</li><li>more</li><li>more</li></ul>
advert
</div>
</header>
<div id="leftnav" style="float: left; width:200px;">
<center>menu here</center>
</div>
<div id="rightnav" style="float: right; width:200px;">
menu here
</div>
<div id="content" style="margin-left: 276px; margin-right: 260px;">
content
</div>
Which looks like this:
But on a mobile device or smaller screen, or when I add more menu items, the advert is pushed out because it doesn't have enough space and overlaps the content div, it looks like this:
But what I want it to do, is push the content div down, so it doesn't overlap, like so:
How can I modify my html / css to correctly display the advert on smaller screens?
min-width is not an option.
If suppose you are using different agents, like mobile or tablet, then you have something different. you would observe that most of the website like, orkut, facebook, gmail ect... has a mobile version website and a touch version website. These websites are made to suit the user screen. Similarly, you have to code a website to suit the user agent.
Read this link and this link. Now yoy have to make a separate page for mobile and other such agents. And redirect the user to the mobile version website.
For different computer screen size, you can use your width and height in form % or em. That would work. The screen size is dynamic and % or em provides with mapping your objects on basis of dynamic screen size.
Try it, may that work.
Instead of
#main:
{
width:270px;
height:300px;
}
use this:
#main:
{
width:40%;
height:60%
}
You will find a better explanation here.
Read about cross browser compatibility. This is also a good link for cross browser compatibility.
Related
I'm writing an ebook in HTML and converting to MOBI with Kindlegen. I want to make sure the images never take up the whole page. However some images are doing just that.
I've tried multiple CSS styles but nothing seems to change. I'm testing on Kindle Previewer, iPhone X, kindle paper white (older device) and iPad. All these devices seem to react to CSS differently and the iPad seems to completely ignore my image styles. No matter what I set the iPAD images don't change. How can I make sure the images are never too large? I want the image to be small enough so that text is also on the same page. Ideal never larger than about 30% of the screen.
I've tried setting a percentage
width: auto;
height: 30%;
and setting em
width: auto;
height: 20em;
I get an error from Kindlegen if I use max-height
.image {
width: auto;
height: 30%;
}
.centerImg {
text-indent: 0;
margin: 1em 0 0 0;
padding: 0;
text-align: center;
}
<!-- Page 29 -->
<p class="centerImg">
<img class="image" alt="lock" src="images/page29.jpg" />
</p>
<p class="collector">
Text
</p>
<br />
<p class="description">
Text
</p>
<div class="pagebreak"></div>
What's the best way to do this?
CSS with ebooks on Amazon can be a bit daunting. I've even seen major bestsellers where the layout didn't work out as intended. Although I've never gotten an ebook to look exactly the same across all devices, I have been able to size my images satisfactorily. I use the free program Sigil for editing, then convert to .mobi with Calibre.
Because CSS can be so unreliable on ebooks, I sized the image in the HTML itself:
<div align="center"><img height="148" src="../Images/stars-300.jpg" width="200"/></div>
<br/>
<h1 class="cinz" id="sigil_toc_id_21">-21-</h1>
<br/>
<h1 class="toocinz sigil_not_in_toc">Between Worlds</h1>
Below is an image of the above code on Kindle Paperwhite. On the iPad, the image is a bit smaller, and some of the spacing is different, but it looks close enough. Another trick I've used to 'force' the ebooks to use your styling, is to use two CSS stylesheets. The first one simply refers to the second, "real" one. This can get around some of the default styles that override custom styles. I'm not sure how well it's worked, but it hasn't hurt:
Style0001.css has only this line:
#import url(../Styles/Style0002.css);
Style0002.css is where all my actual styling is. All my book pages link to the first stylesheet:
<link href="../Styles/Style0001.css" rel="stylesheet" type="text/css"/>.
How to set the width of popup in % ? I have tried giving width:80% of the popup div. It works fine in a desktop browser, but in mobile browser it squezees automatically.
here is the markup :
<div data-role="popup" style="width:80%;left:auto !important;right:0 !important;" id="help-dialog" data-overlay-theme="a" data-theme="a" data-dismissible="false" data-transition="none">
here is a screenshot :
Working example: http://jsfiddle.net/vds2U/61/
When changing jQuery Mobile CSS you need to be careful, specially when working with percentages. Only during pageshow event we can calculate correct page dimensions, ipso facto only at this point we can set percentage values to inner page content.
Problem with mobile devices is their sluggishness, it takes time for pageshow event to trigger.
So best course of action requires programatically solution:
HTML :
Some dummy popup:
<div data-role="popup"id="help-dialog" data-overlay-theme="a" data-theme="a" data-dismissible="false" data-transition="none" data-position-to="window">
<div data-role="header" data-theme="a" class="ui-corner-top">
<h1>Popup</h1>
</div>
<div data-role="content" data-theme="d" class="ui-corner-bottom ui-content">
<h3 class="ui-title">Focused Field</h3>
<p>
<input type="text" id="popup_input" />
</p> Close
</div>
</div>
JavaScript:
$(document).on('pageshow', '#index', function(){
var content_width = $.mobile.activePage.find("div[data-role='content']:visible:visible").outerWidth();
$('#help-dialog').css({'width':content_width*0.8});
});
For this solution to work, it MUST be executed during pageshow event. This solution also works on mobile devices.
One last thing, use data-position-to="window" attribute in your popup <div> if you want it centered.
There is no need to change the jQuery Mobile CSS, you can simply override it.
What's causing you problems is that jQuery mobile automatically puts your popup div in a container. If you want to change the width of your popup div you need to target the width of its container.
From jQuery Mobile docs:
The popup consists of two elements: the screen (...) and the container, which is the popup itself. If your original element had an id attribute, the screen and the container will each receive an id attribute based on it. The screen's id will be suffixed with "-screen", and the container's id will be suffixed with "-popup".
In your case, giving
#help-dialog-popup {
width: 80%;
left: auto;
right: 0;
}
Should do the trick.
I know this is an old post, but I run into the same problem right now. To create a popup that will look perfect on mobile but not too big for computer screen I use a class like dialog-medium below. This way you force the outside container to adjust size and to calculate the right position on screen. You can use 80vw to resolve the initial request or any percentage you want, but never use a higher percentage.
.dialog-medium {
width:90vw;
max-width:35em;
}
With browser inspection tool (ex. right-click -> Inspect element) you will find the class that popup uses.
data-role="popup" normally uses the .ui-popup-container.
So you add into style tag of your page something like the following.
.ui-popup-container {
height: 90vh;
width: 90vw;
}
In the above example, popup will be 90% of the viewport's size. Assuming that you ve written the tag.
If you want to have different size on desktop and mobile devices (you should), use the following:
#media screen and (max-width: 1000px) {
.ui-popup-container {
height: 90vh;
width: 90vw;
}
}
#media screen and (min-width: 1001px) {
.ui-popup-container {
height: 60vh;
width: 60vw;
}
}
In the above example, popup will be 90% of the viewport's size on mobile screens (less than 1001px width) and 60% of viewport's size on big screens (computer)
I just received help in another question I recently asked here in regards to sprites, but now that I have them working properly, I have run into another problem: How can I scale the image displayed?
My current code is as follows:
<div class="index1"></div>
And CSS:
img.index1 {
margin:auto;
width:258px;
height:300px;
background:url("../Images/index.png") 0px 0px;
}
This is the basic code that works as is, to display the appropriate image from the sprite at an absolute dimension of 258*300 pixels. Problem is, I also want to set the width to be 40%. Here is what I tried:
.index1 {
margin:auto;
width:40%;
max-width:258px;
height:300px;
background:url("../Images/index.png") 0px 0px;
}
If I were to use normal images (ie, not sprites), I'd simply set the width to 40%, and the image scales nicely to the containing div. This would be in a ratio, so the height decreases as well, without any distortion or image cutoff. Using the CSS above, the image is unscaled, and because the width is lowered, the sides are cut off.
The aim here is to have the width adjust accordingly, to different browser widths, and simply put, I want to replicate the image behaviour exactly the same way as it would be with a simple image with a percentage width set on it. I have thus far failed at it. And no, I cannot work around the scaling issue.
EDIT: I should also mention that adjusting the browser width merely adjusts how much of the image is cut off.
Solution:
First off, this only works if you have multiple images of the same size (perhaps of the same ratio, but I haven't tested). So I'll try and explain the solution as best I can, with the help of a hypothetical example:
Firstly, you need a placeholder image. This image MUST be the same size, or ratio, as the images you will be displaying. Doesn't matter what the image content is, but I went with a transparent image.
Now, let's say you want 5 images in your sprite, and they will be displayed on your homepage. Let's call these images index1.png, index 2.png etc. Here is the HTML to display it:
<div class="stretch15">
<img class="trans" src="Images/trans.png">
<img class="sprite sprite1" src="Images/index.png" />
</div>
<div class="stretch15">
<img class="trans" src="Images/trans.png">
<img class="sprite sprite2" src="Images/index.png" />
</div>
<div class="stretch15">
<img class="trans" src="Images/trans.png">
<img class="sprite sprite3" src="Images/index.png" />
</div>
<div class="stretch15">
<img class="trans" src="Images/trans.png">
<img class="sprite sprite4" src="Images/index.png" />
</div>
<div class="stretch15">
<img class="trans" src="Images/trans.png">
<img class="sprite sprite5" src="Images/index.png" />
</div>
You'll notice that there are two images inside a div. I'll get to the significance of the div class name in a moment. The trans class shows the transparent image, and the "sprite sprite5" classes show the actual image. I have two classes assigned for this image, just to keep the number of lines in my CSS to a minimum, and to make global image changes much simpler. You'll also notice that the difference between the contents of the five divs is simply the second class name for the second image changes from sprite1 to 5. I'll also explain this in a moment.
Now for the CSS:
.stretch15 {
width:15%;
max-width:258px;
overflow:hidden;
position:relative;
margin:0px auto;
display:block;
}
.trans {
width:100%;
height:auto;
display:block;
margin:0;
padding:0;
}
.sprite {
position:absolute;
top:0;
max-width:none;
max-height:100%;
display:block;
}
.sprite1 {
left:0;
}
.sprite2 {
left:-100%;
}
.sprite3 {
left:-200%;
}
.sprite4 {
left:-300%;
}
.sprite5 {
left:-400%
}
I used the class name 'stretch15' to show the width of the div. I use multiple div widths, so, I used some arbitrary name 'stretch', and then a number to let me know what the width is. So stretch15 tells me the div will be 15% wide, as seen in the CSS. By changing the width in the CSS for this div, you essentially replicate the function of (which I am aware isn't valid code, but it's the simplest way to show it).
As for sprite1 to 5, they all share the same styles in the sprite class, so all that was left was for me to identify which 'left' position to start displaying an image from. This is why the images have to all be the same size. If you move -100% to the left (or 100% to the right), you should land on the left side of the second image. -200% left gets you to the third image, and so on.
As for the rest of the stuff, I honestly wasn't too sure why they were there, other than the fact they they work. What I did was view the source code over here. Also, it really helps if you have something like Firebug (Firefox extension), where you can disable styles to see how things are affected. It will help you understand a lot.
If you'd want a simple fix without worrying about crossbrowser-issues, I'd suggest to use a normal image.
Next, one could use data URIs or use a combo of zoom for webkit/ie and -moz-transform:scale for Firefox or finally break your head on background-size calculations.
You might also want to read: How can I scale an image in a CSS sprite
Hope this helps!
not too sure if i totally get what you mean .
If you want to strectch(?) image on it's width,
you need to get the ratio of the part of your sprite you want to be seen
http://codepen.io/gcyrillus/pen/fjCdD here 'background-size is 100% 232%.
Resize windows width untill box reaches max-width:40%.
maybe a better exemple keeping ration of the sprite and container :
http://codepen.io/gcyrillus/pen/KuJDl
i have a problem with CSS position of one image on my page. I need it to keep that image in top right corner all time, so i choose to use this style for image:
HTML:
<div class="head">
<div class="content nm">
<div class="menu_inline">Home</div>
<div class="menu_inline">Help Center</div>
<div class="corner">
<img src="{$basePath}/images/corner.png" alt="GOOGLE PLAY" id="corner">
</div>
</div>
</div>
CSS:
.#corner { display:inline; position: absolute; top:0px; right: 0px;}
Everything works OK in desktop browsers, but problem is, when i load my page on any smartphone (tested Android and iPhone), when i loadd a page, page is zoomed and the picture cover my menu and other content of page. You can see the problem on picture below.
Do you have any advice, how to correct this picture. I understand that browser probably calculate a user width and on then place
link to picture with problem
Please Try to add this in your page inside head tag.
I have seen several sites where these social share buttons looks perfectly horizontal aligned. Take in account that many of these buttons are iframes.
Here is my current painful situation:
Change the margin-top on your iframes (or a div element above it) to negative values to have them line up. Use trial-and-error once you identify the correct elemtn until you get it right, for example using the following HTML:
<div id="twitter">
<iframe/>
</div>
<div id="facebook">
<iframe/>
</div>
<div id="digg">
<iframe/>
</div>
The CSS would look something like this:
div#facebook
{
margin-top:-5px;
}
div#digg
{
margin-top:-10px;
}