Is there any way to make an image resize to the users screen size? (HTML/CSS)
I've tried Media queries, but they haven't proven very useful except for text I believe.
#media screen and (min-width:600px) {
#dick{
color: black;
z-index: 400;
position:absolute;
left: 58%;
top: 210px;
height: 50%;
font-family: Georgia, "Times New Roman", Times, serif;
font-size:100%;
}
}
Simply making the width span as much as it can works.
img {
width: 100%;
}
You can define a class like resize-img and add this class to your img tag :
<img class="resize-img">
and then in you stylesheet :
#media screen and (min-width:600px) {
.resize-img{
width: 100px;
height:200px;
// and whatever else :d
}
}
#media screen and (min-width:1000px) {
.resize-img{
width: 300px;
height:600px;
// and whatever else :d
}
}
For image elements id.ot provides an answer. For background images, you can use this:
html, body, .column {
padding: 0;
margin: 0;
}
body {
background: url('http://placehold.it/300x300') no-repeat 0px 0px/100vw;
font-size: 10vh;
}
h2 {
font-size: 2em;
}
<h1>This is the header</h1>
<p>and some paragraph text</p>
Try to resize the window when using "Full page" view mode and see how the image resizes. If you don't care about distortion, you can even use
background: url('http://placehold.it/300x300') no-repeat 0px 0px/100vw 100vh;
By the way, vw and vh units can also be used to resize text (regarding your comment on id.ot's answer).
Related
I'm working on a free-code-camp project and when I try to shrink this site the text
overflows from the container which width is set to "auto". The text is overflowing from the bottom and I have no idea why.
I have tried height: auto, height: 100%, etc., but nothing works.
Markup:
<section id="product-info">
<img class="background-image" src="https://i.postimg.cc/50G4sHcs/water-
2208931.jpg" alt="">
<div class="product-info-wrapper">
<h1 class="product-info-title">Why our bottles?</h1>
<p class="product-description">We offer bottled water cared for from
end to end with the idea of nothing ever becoming "waste". Our Better
Bottle and label are made from plants, non toxic and compostable. We
also offer collection for the bottles we sell, taking responsibility
for our product post-sale and making sure that every bottle returned,
ends up in the correct facility, not the environment. We have been
developing our plant-based cap as currently, only plastic options are
available. The way we see it, if we
are using plastic, it should be made from naturally renewable
materials and hold the ability to disappear after its useful life </p>
</div>
</section>
CSS:
#product-info {
width: 100%;
position: relative;
}
.background-image {
width: 100%;
height: 100%;
object-fit: cover;
max-height: 600px;
filter: blur(1px) grayscale(70%);
}
.product-info-wrapper {
position: absolute;
top: 0;
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
align-items: center;
/* background: white; */
height: 100%;
color: white;
}
.product-info-title,
.product-description {
font-family: "Open Sans", sans-serif;
}
.product-info-title {
font-family: "Kalam", cursive;
/* text-decoration: underline #271F30; */
text-transform: uppercase;
letter-spacing: 5px;
font-size: 35px;
}
.product-description {
width: 80%;
line-height: 1.8;
/* height: 150px; */
/* padding: 10px 20px 25px ; */
}
I want the text to be responsive when I resize the window and I don't want to hide it, I just want it to shrink responsively.
Could you please advise me?
Add word-wrap: break-word to your CSS
For text responsiveness you can use vw units https://www.w3schools.com/cssref/css_units.asp
Just dynamically adjust the font size to the available screen width.
Do your custom calculation as the default:
.product-description {
font-size: calc(10px + (100vw / 200));
}
Then, set a maximum width with a media query
#media screen and (min-width: 800px) {
.product-description {
font-size:16px;
}
}
The example above has a minimum font-size of 10px, increased by 1 for each available 200px screen width. For instance: screen width: 600px => font-size 13px
Still, you need to handle long text with care. You may reach a point, where a "read more" button/link is unavoidable.
PS: object-fit is not supported on IE. If you rely on that browser, rework your css to use:
background-image: url"(myImage.jpg"); background-size: cover;
*{
word-break: all;
}
you can use any selector you want :)
I hope it helps.
I am trying to code up a banner with a minimal amount of media queries, as in the past for header images and text, I can't figure out a way to use anything under 10.
My current issue:
I have a responsive background image that looks like this:
Header Image
However as the browser resizes, although the background image is being responsive, the container within the background div that houses the text is not resizing, therefore not keeping the text centered in the middle.
You can see something like this is happening:
Container overflowing
I've tried all kinds of things, heights on containers, removing heights. But all areas I end up getting to, I am having to do too many media queries to fix the text, or change the height of the image instead as the screen size goes down. I'd like to try and learn how to code a better header for this website that is more flexible and intuitive, that I can carry over too my future projects.
Here is the code HTML:
<section class="home_banner">
<?php
$featuredimage = get_the_post_thumbnail_url();
?>
<div class="banner_image" style="background-image: url('<?php echo $featuredimage; ?>'); ">
<div class="container">
<div class="banner_text_inner">
<p>Lorem Ipsum</p>
<div class="banner_excerpt">
<h1>Powerful engaging opening title</h1>
</div>
</div>
</div>
</div>
</section>
Here is the css:
.home_banner{
.banner_image {
background-size:100%;
height:100rem;
background-repeat: no-repeat;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
text-align: center;
.banner_text_inner {
max-width: 1000px;
width: 100%;
margin: auto;
padding: 0 10px;
box-sizing: border-box;
font-family: $autumnchant;
font-size: 40px;
color: $color-primary;
#media (max-width: 1000px){
font-size: 30px;
p {
margin-bottom: 0;
}
}
#media (max-width: 400px){
font-size: 22px;
}
}
.banner_excerpt {
#media (max-width: 1000px){
h1 {
font-size: 40px;
}
}
#media (max-width: 400px){
h1 {
font-size: 30px;
line-height: 1;
letter-spacing: 5px;
margin-bottom: 0;
}
}
}
}
}
Try to add "background-size: cover;" to your banner-image and also add min-height.
I'm really having a lot of trouble trying to recreate this Wix site with the goal of having the mobile & desktop website responsive with other content.
http://nl.wix.com/website-template/view/html/936?&viewMode=mobile
As soon as I use a different width the 30° moves to a different position,
which means the site isn't properly responsive. I have no idea how to fix this.
Here's my code:
HTML:
<body>
<div class="header">
<h1>30 °C</h1>
<div class="clearfix"><img src="./img/banner.png" alt="banner"> </div>
<p>Beginning application developer</p>
</div>
</body>
CSS:
.header {
background-color: #7A7CB1;
width:100%;
position:absolute;
top:0;
left:0;
}
.header img {
width: 62.5%;
margin-top: 2.5%;
margin-left: 6.75%;
float:left;
}
.header h1 {
position: absolute;
top: 30%;
left: 30%;
font-size: 200%;
}
.clearfix {
overflow: auto;
}
.header p {
margin-left: 6.75%;
font-family: Verdana, geneva, sans-serif;
font-size: 13px;
}
I want the 30° to be below the logo like it is in the photo, but the logo and the text should become smaller or bigger (responsive) if the width of the phone is smaller or bigger.
If you want the text to become larger/smaller on different device widths, you need to add Media Screens
Here is an example of a media Screen
#media only screen and (max-width:940px){ /* Tablets*/
h1{
font-size:12px;
}
}
What this does is when the max width of the device is 940px(Average tablet size). The h1's font size is set to a different size.
I'm trying to build a very basic site with an image centered in the middle of the page with three lines of text below it, also centered.
I have it how I want it to look on a larger screen, but when viewed on a smaller screen (iPhone) the image is too large. I need to have the image resize based on the screen resolution.
I've done some Google'ing and know this is possible, but have not been able to get it to work. HTML/CSS is not my strong suite. Any help would be much appreciated. Here is my code:
<html>
<style>
body {
font-family: 'Helvetica', 'Arial', sans-serif;
background: white }
section {
background: white;
color: black;
border-radius: 1em;
padding: 1em;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%) }
</style>
<section>
<IMG src="Logo.png" alt="Logo">
<br><br>
<h1><center><p>Email
<p><font color=B5B5B5>Phone Number
<font size=7> <p><i>Tagline</i></center></font>
</section>
</html>
You can use media queries. Try to add the following code in your CSS.
CSS:
#media screen and (max-width: 480px) {
img {
width: 400px;
}
}
Once the browser is at 480px, it will make the img width 400px. You can change these numbers to suit your preference.
You need to look into setting up fluid images, this will help you get started...
CSS Fluid Image Technics
Here is an example...
HTML
<section class="container">
<img src="http://placehold.it/750x250">
<div class="copy">
Email
<p>
<span class="phone-number">Phone Number</span><br />
<span class="tagline">Tagline</span>
</p>
</div>
</section>
CSS
body {
font-family: 'Helvetica', 'Arial', sans-serif;
background: white
}
.container {
bottom: 0;
left: 0;
height: 300px;
margin: auto;
position: absolute;
right: 0;
text-align: center;
top: 0;
width: 100%;
}
.container img {
max-width: 100%;
}
https://jsfiddle.net/kennethcss/71a6mngh/
The image is centered (using absolute centering), and when you drag the browser in the image automatically adjust it's size...this is how fluid images behave (no need for media queries per se). If you still need a media query you can do something like this...
https://stackoverflow.com/a/39760016/4413798
https://css-tricks.com/snippets/css/media-queries-for-standard-devices/
You need to add a max-width to the image:
img {
max-width: 100%;
}
just off topic: <h1><center><p>..</p></center></h1> is invalid. Just use <h1>..</h1> and style it.
<font> is also invalid and deprecated (just like center)
Try something as below, there were few error in your codes, you could style your HTML elements by adding style tag in your targeted HTML element or by adding external or internal CSS files. Well now to make it responsive use CSS media query as below, define breakpoints where you need your image to change.
#media screen and (max-width : 480px){
.......
.......
.......
}
#media screen and (max-width : 320px){
.......
.......
.......
}
body{
background:#fff;
}
#box{
width:70%;
height:300px;
margin:auto;
margin-top:20%;
}
#box > .bximg{
width:180px;
height:180px;
overflow:hidden;
margin:auto;
}
#box > .bximg > img{
width:100%;
min-height:100%;
}
#media screen and (max-width: 480px){
#box > .bximg{
width:120px;
height:120px;
}
}
<div id="box">
<div class="bximg">
<img src="https://source.unsplash.com/random">
</div>
<h1 style="text-align:center;margin:0px;">
Email</h1>
<p style="text-align:center;margin:10px 0px; "><font color=B5B5B5>Phone Number</font>
<p style="text-align:center;margin:10px 0px;"><i>Tagline</i></p>
</div>
You can use max-width for <img> element.
section img {
max-width: 100%;
}
You're going to want to take a look at media queries in the Mozilla docs.
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
There's a link to help you get a better understanding of it but basically the web content will resize based on the size of the screen.
I have a sidebar div that takes up 12% of the total screen width (set as a css property). I also have an <h1> block within this div, with a title. When I switch monitors to a smaller one, the sidebar ends up being skinnier, resulting in the title to extend OUT of the sidebar.
How can I format so that the text will always stay within the line? ("MY TI..." is fine for a result)
If the title text is known, you may be able to using viewport units vw for the font-size either in the original style or in the media queries.
You would also need to set the sidebar width to vw too, or a percentage value to make it all responsive.
html, body {
height: 100%;
margin: 0;
}
.sidebar {
border-right: solid;
height: 100%;
float: left;
width: 15vw;
}
.sidebar h1 {
font-size: 4vw;
text-align: center;
}
<div class="sidebar">
<h1>MyTitle</h1>
</div>
jsFiddle
Another solution would be using CSS ellipses, replace the overflow text with "...".
html, body {
height: 100%;
margin: 0;
}
.sidebar {
border-right: solid;
height: 100%;
width: 15%;
float: left;
}
.sidebar h1 {
white-space: nowrap;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="sidebar">
<h1>MyTitle MyTitle MyTitle</h1>
</div>
jsFiddle
There is no 100% sure way when it comes to CSS but the title should normally go onto two lines which would be better than what its doing in your screen shots. Post your code if you want someone to look at that.
What you should do though is use media queries to make the sidebar wider when its on a smaller screen:
.sidebar
{
width:12%;
}
#media screen and (max-width: 600px) {
.sidebar
{
width:30%;
}
}
Here is an example
http://codepen.io/nathanfelix/pen/KzZPGy
Also, here you can read more about media queries:
http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
Please try like this:
<div class="sidebar">
<h1>
MY TITLE
</h1>
</div>
.sidebar {
border-right: 1px solid black;
height: 600px;
width: 186px;
}