Make responsive text relative to responsive image? - html

I'm trying to make an image responsive, and have the text also be responsive, but align in a relative manner so it matches up and doesn't ruin the output.
This is my html:
.checks {
position: relative;
display: inline-block;
height: auto;
max-width: 50%;
}
.competitive {
position: absolute;
display: inline-block;
bottom: 80%;
font-family: 'museo_slab500';
font-size: 150%;
color: #fff;
padding: 0 20px;
width: 50;
line-height: 150%;
}
<div class="checks">
<img src="http://jqlconsulting.com/wp-content/uploads/2015/06/forcheckmarks2-Converted.png" alt="" style="max- width:100%"/>
<h2 class="competitive"><span>3 Tiered Quality Review Process</h2></span>
</div>
What am I doing wrong?

You can make image responsive by using '%' like 100% or etc.
But you can't do same with text to make it responsive.
You need to use units like 'em' or 'rem' instead of '%' or 'px' to make text responsive.
And btw 16px = 1em or 1rem. For more info search further about css units 'rem' and 'em'.
So you should use:
font-size: 1rem;
//or
font-size = 1em;
.competitive {
position: absolute; // use relative instead of absolute to make class'competitive' relative to parent.
display: inline-block;
bottom: 80%;
font-family: 'museo_slab500';
font-size: 3em; // Or use 'rem' to make text responsive.
color: #fff;
padding: 0 20px;
width: 50;
line-height: 150%;
}

I made this jsfiddle example to demonstrate your problem as I understand it. Since your question is too vague to understand, this might not be the solution you want. Also, this is not a full answer, just something to get you started.
I think what you want is to keep the text 3 Tiered Quality Review Process to keep on the top maroon image even if the window size is changed.
Alert me if you dont want this solution. And please change your question too for other users to understand it properly.
The answer as your see in the jsfiddle is this javascript which resizes your competitive container to your checks div.
window.onresize = resizeText;
function resizeText()
{
var heightPercentage = 10; //Make the height 10% of parent
var widthPercentage = 100; //Make the width 100% of parent i.e. full width
var parentHeight = $(".checks").height();
var parentWidth = $(".checks").width();
$(".competitive").css("height", parentHeight * (heightPercentage / 100) + "px");
$(".competitive").css("width", parentWidth * (widthPercentage / 100) + "px");
}
Then I included a bunch of these lines #media all and (min-width: 50px) { body { font-size:5px; } } in your css to adjust the font-size based on the window size. You can play around with this and add more of these to cover all possible cases. Note this depends on how much is your window's max and min height/width.
Also check this answer which closely relates to your problem (IMO)
I also suggest strongly to use bootstrap for any responsive css.

.checks {
position: relative;
display: inline-block;
height: auto;
max-width: 50%;
}
.competitive {
position: absolute;
display: inline-block;
bottom: 80%;
font-family: 'museo_slab500';
font-size: 150%;
color: #fff;
padding: 0 20px;
width: 50;
line-height: 150%;
}
<div class="checks">
<img src="http://jqlconsulting.com/wp-content/uploads/2015/06/forcheckmarks2-Converted.png" alt="" style="max- width:100%"/>
<h2 class="competitive"><span>3 Tiered Quality Review Process</h2></span>
</div>

Related

how to make container responsive when device change?

I have created a div, and give it an initial size, css as below, I want it can responsive in different devices.
.main {
font-size: 14px;
line-height: 1.4;
font-family: museo_sans,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif;
width: 30em;
height: 40em;
background-color: tomato;
border: solid 1px black;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto
}
demo result
What I want is make it responsive, when view in different device.
Similar as a component in Airtasker website, in the browser, it has its initial size (width and heigth) enter image description here
but when viewed on different device, or just zoom out browser, it will auto-scale to fit the screen
enter image description here
I know the solution may be very simple, but I just don't understand how to do it.
You can achieve it in many ways, and two most common would probably be:
setting its width to some percentage value (like 80%) and some fixed max-width value. This way it will take 80% of the screen on smaller devices, but won't be bigger than the fixed value you specified on bigger screens;
.modal {
display: block;
width: 80%;
max-width: 50rem;
height: 30rem;
background-color: lightgray;
margin: 0 auto;
}
<div class="container">
<div class="modal"></div>
</div>
using media-queries, where you will have exact control over how elements behave under certain conditions (screen size most commonly).

Why doesn't the background image show up without specific width and height?

Here's an example code
I've been wondering why the background-image doesn't show up unless I specific the image's width and height in pixels. I tried to specific only the width in percentage but it didn't work. The w3cschools.com was able to show the background image without specifying the width and height, but it works only in body background. Any explanation or solution workaround?
HTML
<div class="pic"></div>
CSS
.pic {
background: url("http://i.imgur.com/HIt6f8r.png") no-repeat;
display: block;
position: relative;
width: 244px;
height: 230px;
background-size: contain;
}
Your <div> element don't have any content, so the <div> height is 0px.
The width of the <div> is still 100%.
If you add any content to the div it will have some height and it will show a portion of image.
<body> by default has the height of the window, so you can see the background-image.
I found a great alternative without specifying the height, thanks to http://blog.brianjohnsondesign.com/maintain-aspect-ratio-for-html-element-using-only-css-in-a-responsive-design/.
HTML
<div class="pic"></div>
CSS
.pic {
background: url("http://i.imgur.com/HIt6f8r.png") no-repeat;
display: block;
position: relative;
width: 20%;
height: 0;
padding-bottom: 20%;
background-size: 100%;
}
All you need to do, assuming that it's a square, to match the padding-bottom to the width in css.
Update:
I also heard about another solution that may be useful. http://www.mademyday.de/css-height-equals-width-with-pure-css.html
CSS
.pic {
position: relative;
width: 50%;
}
.pic:before {
content: "";
display: block;
padding-top: 100%;
}
although I haven't tested it out yet....
<div class="pic"></div>
Div is container, it expects to have inner elements, when it's empty you must explicitly define height.
Your background image will not show because the div element has no content, this means that its height is 0.
You could use this jQuery code to make your div take the size of the window.
$(function () {
'use strict';
$('.div').height($(window).height());
$(window).resize(function () {
$('.div').height($(window).height());
})
});
If you don't specify height, the size of your div is given by the size of its contents, i.e. it's 0x0, so you don't have much chance of seeing a background image. Add
border: 1px solid red;
to see how large your div is (or isn't).
I am struggling with similar, trying to put text on top of image using css, but as I dont set height, it doesnt show. Have tried code above as well
.module5 {
background: url(image.jpg);
display: block;
background-size: 100%;
width: 100%;
height: 0;
position: relative;
float: left;
border: 1px solid red;
}
.mid h2 {
font-family: 'Roboto', sans-serif;
font-weight: 900;
color: white;
text-transform: uppercase;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
font-size: 2rem;
transform: translate(-50%, -50%);
}
And where pointing it:
<div class="module5 mid" style="width: 100%;">
<h2>My Text</h2>
</div>
So unless I set a height of module, it just shows a red line(my border for testing)

HTML / CSS Display fixed ratio images based on the height of the page

There is probably a relentlessly simple solution to this but I've been chasing my tail for a while so I've come to ask those wiser and smarter than me.
I've got a website for a personal project I'm making which displays images within a lightbox. See image:
The header area (red) is fixed height.
I want the images (yellow) to sit within a light box (green) which also has a caption. Crucially the images displayed need to retain their aspect ratio, 5:4, and fill the remaining height left below the header (bar a small margin top and bottom).
There's probably a really simple, elegant solution out there but I've not found it.
Any help gratefully received.
EDIT ---
Here's a fiddle of what I'm trying to do: http://jsfiddle.net/qh2V8/
Even this isn't right as I've had to put a fixed width in to even try and get it to work.
CSS:
#header{
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 145px;
background-color: #F00;
}
#overlayBg {
position: fixed;
top: 155px;
bottom: 20px;
padding: 8px;
padding-bottom: 30px;
left: 0px;
right: 0px;
margin-left: auto;
margin-right: auto;
background: #FF0;
width: 400px;
}
#overlayContainer img {
position: relative;
height: 100%;
width: 100%;
}
#overlayBg p {
position: relative;
padding-top: 8px;
padding-bottom: 10px;
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
font-size: 14px;
}
HTML:
<div id="header"></div>
<div id="overlayBg">
<div id="overlayContainer">
<img src="http://i.imgur.com/u9VIg60.jpg" />
</div>
<p>Caption</p>
</div>
The image size need to be set through scripting, unless the images are a fixed constant size. The following link is of good help to your problem: Change image size with JavaScript
I'm pretty sure that you can get the original size of the image through yourImg.Style.Height and yourImg.Style.Width, and then make the calculations required to make it a 5:4 picture..
Here's where I got to.
There are fixed ratio solutions if you are basing the size of the element on width, using :before and padding-top. There's a good write up here.
There is a fixed ratio solution if you are basing the size of the element on height, however the height must be a % of the height of the screen. Written up here in another Stackoverflow question:
Resize a Div Based on Height but Retain Aspect Ratio (almost got it) Strange Reload bug
If you have a fixed pixel size header or footer and need an element to expand to fill the exact size remaining you can't do it with just HTML and CSS.
Here's a codepen.io of where I got to:
http://codepen.io/niazipan/pen/ydkGt
JS to set the image height, and CSS to style everything else around it. Code below:
HTML
<div id="overlayBg">
<div id="overlayContainer">
<img src="http://i.imgur.com/u9VIg60.jpg" id="yourImgId" />
</div>
<p>Caption</p>
</div>
CSS
html, body {
height: 100%;
text-align: center;
}
#header{
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 50px;
background-color: #F00;
}
#overlayBg {
position: fixed;
top: 55px;
padding: 8px;
margin-left: auto;
margin-right: auto;
background: #FF0;
position: relative;
text-align: left;
}
#overlayContainer {
height: 100% !important;
width: 100%;
}
#overlayBg p {
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
font-size: 14px;
margin-top: 5px;
margin-bottom: 5px;
}
JS
var size = window.innerHeight - 120;
document.getElementById('yourImgId').style.height = size + 'px';
document.getElementById('overlayBg').style.width = size * 1.25 +'px';

Image behind the link

So I have a menu and on it there is a button with text and I want behind the text to be an image that shows that you are on the page and this is the code:
HTML:
<div id="menu">
<div id="about">About Us</div>
</div>
CSS:
a {
text-decoration:none;
color: white;
background: url(images/hover.png);
width: 100%;
height: 38px;
}
#about {
background: url(images/button.png);
width: 168px;
height: 51px;
font-family: Anivers;
font-size: 20pt;
text-align: center;
color: white;
line-height: 50px;
vertical-align: middle;
margin-top: 1%;
}
So far, so good, except that the image will only show the height and width that coresponds to the size of the text. For instance if I make the text 24pt, the image behind it will grow larger, but if I make it smaller, the image will become smaller and I don't want that. So how do I stop it from happening. I already searched all over the place, sadly I couldn't find similar topic. I hope you can help me :).
If I understand your question correctly you need to add display: block to the <a> element and set height: auto; instead. As for the image it should not scale anymore and I centered an image for demo purposes.
DEMO
You can accomplish this by displaying your "a" element as a "block". This will allow you to specify the size of the element independent from the size of the font. You can then inherit the width and height of the "#about" css styling if that's the size of "hover.png", or specify your own size based on the actual size of "hover.png" if its different than that stated in "#about", it sounds like 38px for hover.png is what you want as opposed to the 51px height of the #about parent. Without setting "a" as a block, the font size of the text in "#about", the parent element, would rule the overall size of the a element and styling, and your background "images/hover.png" will only provide a background for that size.
Here's what your a element in css would look like with the 38px height, you could also say "inherit" for the height if desired. I tested this and it works:
a {
text-decoration:none;
color: white;
background: url(images/hover.png);
display: block;
width: inherit;
height: 38px;
}
I hope this helps.
<div id="menu">
<img src="images/4.png" />
About Us
</div>
#menu {
position: relative;
width: 168px;
height: 51px;
}
img {
width: 100%;
height: auto;
}
img:hover {
background: blue;
}
a {
position: absolute;
z-index: 100;
/* top: 0; PLACE LINK CORRESPOMNDING TO IMG
left: 0; PLACE LINK CORRESPOMNDING TO IMG */
background: red;
font-family: Anivers;
font-size: 23pt;
color: white;
line-height: 1.2;
}

How to make text relative to background using HTML/CSS?

I have made it so that my background image scales to fit the size of the viewport. Now, I want the text to scale relative to the background image. Note: I am using the <img> tag for the background image because this allows for scaling with smaller browser windows (i.e., mobile devices).
CSS
img.bg
{
min-height: 100%;
min-width; 781;
width: 100%;
height: auto;
top: 0;
left: 0;
position: absolute;
z-index: 1;
}
#media screen and (max-width: 781)
{
img.bg
{
left: 50%;
margin-left: -390.5;
}
}
#container
{
position: relative;
top: 0px;
width: 781;
height: 758;
border: 1px solid black
z-index: 2;
}
#left
{
position: relative;
left: 1.280409731113956%;
top: 14.51187335092348%;
padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
width: 50%;
height: 50%;
z-index: 2;
}
p
{
font: 14px Georgia;
color: #FFFFFF;
}
HTML
<img class="bg" src="background.jpg">
<div id="container">
<div id="left">
<p>Text</p>
</div>
</div>
You can't scale text dynamically in this way with css alone as far as I know.
you might be able to do it with jQuery if you make the selected font-size proportional to window.innerWidth using something like this.
function resizeText(event) {
//your text element
var $text = $('h2');
if(window.innerWidth <= 781){
//font scaling ratio
var fontSize = window.innerWidth/100;
$text.css('font-size', fontSize+'px');
} else {
$text.css('');
}
}
//run above on window load and resize events
window.onresize = resizeText;
window.onload = resizeText;
Are you doing all your text or just headings? You might want to look into "Fittex" or "Bigtext".
You can do it with strictly using CSS and Media Queries.
I also saw another method on here a few days ago with someone's method to do it strictly with CSS and without media queries.
Also check out this post:
Is it possible to dynamically scale text size based on browser width?