html
<div class="article">
<img src="http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2013/01/Cookies1-520x245.jpg"/>
<div class="dateAuthor">
<span class="date">Jan 4, 2014</span>
<span>— By </span>
<span class="author">Jonathan Lakin</span>
</div>
<h1>Cookies Tell You A Lot About Your Audience, But Most of it is Wrong</h1>
</div><div class="article">
<img src="http://cdn2.tnwcdn.com/wp-content/blogs.dir/1/files/2014/01/20140103_151801-520x245.jpg"/>
<div class="dateAuthor">
<span class="date">Jan 4, 2014</span>
<span>— By </span>
<span class="author">Jonathan Lakin</span>
</div>
<h1>Ten Website Trends To Expect</h1>
</div>
css
.article{
display: inline-block;
vertical-align: top;
margin: 0 1.6839378238341968911917098445596% 30px 1.6839378238341968911917098445596%; /* 13px / 772px */
}
.article img{
max-width: 360px;
width: 100%;
}
#media only screen
and (max-width : 770px) {
.article img{
max-width: 100%;
}
.article h1{
max-width: 97.222222222222222222222222222222%; /* 350px / 360px */
}
}
jsFiddle
The problem here is image only grows to the width of the parent if the heading below is long enough to take the width of the parent, else it stays up to the maximum width of the image which is 520px, What I want to do is always grow the image according the width of the parent as what happens to the first image in the fiddle. What styles should be added?
Note: The first image is working properly only because the heading
below is long enough.
You did your img sizes backwards and you need to give .article a size to be so things inside know what to be.
JSFiddle Sweetness
CSS
.article img {
max-width: 100%;
width: 520px;
}
#media only screen and (max-width : 770px) {
.article {
width: 95%;
}
.article img {
max-width: 100%;
width: 100%;
}
.article h1 {
max-width: 97.222222222222222222222222222222%;
/* 350px / 360px */
}
}
Related
How do I setup HTML/CSS to have my DIV follow the screen size for width, but stop expanding once it fits the contents (it should scroll left/right when the div cannot fully contain the contents).
Pseudo-Code:
HTML:
<div class="image-container">
<img width="1000">
</div>
CSS:
.image-container {
/* ??? */
display: inline-block; /* ??? */
overflow: auto;
}
EDIT: Per Evadore's answer, I was able to come up with the following CSS.
.image-container {
display: inline-block;
overflow: auto;
}
/* optimize these px dimensions, 900 worked for my application */
#media (max-width: 900px) {
.image-container {
max-width: 710px;
}
}
/* redundant, I plan to tweak this range later */
#media (min-width: 901px) and (max-width: 1575px) {
.image-container {
max-width: 710px;
}
}
#media (min-width: 1576px) {
.image-container {
max-width: 1385px;
}
}
The following reference also helped: w3schools
Use CSS Media queries to setup for various screen sizes.
view source code of this page to see how media queries were used.
for this set the parent div width to fit-content and max-width to 100%. now the parent div will remain between the width of the content and the with of the screen if the screen size is not enough. And lastly for scrolling inside the parent div on the small screen devices put overflow:scroll.
Here is the Codepen demo
.parent {
background-color: green;
width: fit-content;
max-width: 100%;
overflow: scroll;
}
.child {
padding: 30px;
width: 700px;
background-color: red;
color: #fff;
}
<div class="parent">
<div class="child">
test string
</div>
</div>
ps: I've added bg colors just for reference purposes, to show whether the parent component is expanding or not.
I want to make an 100% full page, within 4 images on each row. But if you resize the window you'll get 3 images than 2 images till there is 1 left. After research I become at #media to fill the full page without getting any blank spaces. Now i've made this jsfiddle but if you resize the results window you will see blank spaces who are not filled. What am I doing wrong?
#media only screen and (min-width : 354px) {
/* Smartphone view: 1 tile */
.image{
width: 100% }
https://jsfiddle.net/8hfLkb3k/
The image always must fill both width's, if the width is 50% for 2 both images must fill it in for 50%.
Thanks.
I recommend using max-widths for your media queries, but you don't have to do this, as long as what you do gives you the intended and correct result.
In my working example I've chosen to set the one-image container for 25% unless something changes like the viewport width.
At 1024, 767 and 600 I've chosen the 1/3 and 1/2 sizes and finally the 100% width.
I expect this is what you mean, but feel free to adapt this code as you see fit, especially to match the media query viewport attributes to what you desire.
* {
box-sizing: border-box;
}
.one-image {
width: 25%;
float: left;
border: 1px solid red;
}
.one-image img {
width: 100%;
border: 1px solid blue;
display: block;
}
#media screen and (max-width: 1068px) {
.one-image {
width: 33.33%;
}
}
#media screen and (max-width: 767px) {
.one-image {
width: 50%;
}
}
#media screen and (max-width: 600px) {
.one-image {
width: 100%;
}
}
<div class="one-image">
<img src="http://placehold.it/200x200">
</div>
<div class="one-image">
<img src="http://placehold.it/200x200">
</div>
<div class="one-image">
<img src="http://placehold.it/200x200">
</div>
<div class="one-image">
<img src="http://placehold.it/200x200">
</div>
Your images (<img> elements) are not expanding to the full width of the .image containers because you're missing this rule, which you can add to your CSS:
.image img {
width: 100%;
}
Here's your example code updated to show this working:
.image img {
width: 100%;
}
.f_left {
float: left;
}
#media only screen and (min-width: 354px) {
/* Smartphone view: 1 tile */
.image {
width: 100%;
}
}
#media only screen and (min-width: 708px) {
/* Smartphone view: 2 tile */
.image {
width: 50%;
}
}
#media only screen and (min-width: 1062px) {
/* Small desktop / ipad view: 3 tiles */
.image {
width: 33.3%;
}
}
#media only screen and (min-width: 1417px) {
/* Medium desktop: 4 tiles */
.image {
width: 25%;
}
}
<div class="image f_left">
<img src="http://www.devual.nl/project/gmsbase/img/cover.png" />
</div>
<div class="image f_left">
<img src="http://www.devual.nl/project/gmsbase/img/cover.png" />
</div>
<div class="image f_left">
<img src="http://www.devual.nl/project/gmsbase/img/cover.png" />
</div>
<div class="image f_left">
<img src="http://www.devual.nl/project/gmsbase/img/cover.png" />
</div>
Working on a news site and once I get to mobile, I'd like it where the image fits inside the entire width of the div....whatever that might be. The images are originally 240px in width so we are talking about bumping them up. This is what I'm currently using:
.thumb { float: left; clear: both; width: 100%; height: auto; margin: 0; }
.thumb img { max-width: 100%; max-height: 100%; }
So on my iPhone, the pic it a little bit too small since the div is 320px and the img is 240. Of course, this would change for say, landscape at 480px for example. So it needs to be flexible. Any suggestions?
You might try the following, set the image width to 100%.
.thumb {
float: left;
clear: both;
width: 100%;
height: auto;
margin: 0;
}
.thumb img {
width: 100%;
}
<div class="thumb">
<img src="http://placehold.it/200x100">
</div>
#media screen and (max-width: 480px) //place whatever the resolution you are working here{
/*enter your code here for this resolution*/
}
to learn more about
media queries
This is very simple as like this.
img {
width:100%;
}
div{
float: left; clear: both; width: 100%; height: auto; margin: 0;
}
<div>
<img src="http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg" />
</div>
I am trying style a div so that its width decreases after a certain point wrt to the width of the viewport.
Here is my code:
<div data-category="budget" class="hotels-block active ">
<img src="images/budget1.jpg">
<div class="info-block">
<h2>BUDGET HOTEL 1</h2>
<p>fist line, second line, USA, third line comes over here</p>
<hr/>
BOOK NOW
</div>
</div>
CSS :
.hotels-block {
overflow: hidden;
margin-bottom: 30px;
}
.hotels-block img {
float:left;
}
.info-block {
width: 320px;
float: left;
display: inline-block;
background-color: #62bcb1;
text-align: center;
color: #FFF;
margin-bottom: -9999px;
padding-bottom: 9999px;
}
#media screen and (min-width: 961px) and (max-width: 1050px)
{
.info-block
{
width: 30%;
}
}
I want the width of the .info-block div to reduce until the viewport width hits 961px.
Currently, .info-block shrinks until 977px and then falls to the next line.
How do I prevent .info-block from going to the next line? No JS/JQuery please.
you want to use min-width and max-width properties? usually as absolutes and then set the standard width to a percentage. Also set the width in the media query to !important to supersede the pre-set arrangement.
#media screen and (min-width: 961px) and (max-width: 1050px)
{
.info-block
{
min-width:200px;
width: 30% !important;
max-width:400px;
}
}
html
<div class="article">
<img src="http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2013/01/Cookies1-520x245.jpg"/>
<div class="dateAuthor">
<span class="date">Jan 4, 2014</span>
<span>— By </span>
<span class="author">Jonathan Lakin</span>
</div>
<h1>Cookies Tell You A Lot About Your Audience, But Most of it is Wrong</h1>
</div><div class="article">
<img src="http://cdn2.tnwcdn.com/wp-content/blogs.dir/1/files/2014/01/20140103_151801-520x245.jpg"/>
<div class="dateAuthor">
<span class="date">Jan 4, 2014</span>
<span>— By </span>
<span class="author">Jonathan Lakin</span>
</div>
<h1>Ten Website Trends To Expect</h1>
</div>
css
.article{
display: inline-block;
vertical-align: top;
margin: 0 1.6839378238341968911917098445596% 30px 1.6839378238341968911917098445596%; /* 13px / 772px */
}
.article img{
max-width: 360px;
width: 100%;
display: block;
}
.article h1{
max-width: 350px;
width: 100%; /* 350px / 800px */
margin-top: 10px;
font-size: 1.5em;
}
#media only screen
and (max-width : 1000px) {
#articlesWrapper{
margin-left: 1.0416666666666666666666666666667%; /* 10px / 960px */
margin-right: 1.0416666666666666666666666666667%; /* 10px / 960px */
}
}
#media only screen
and (max-width : 720px) {
.article img{
max-width: 100%;
}
.article h1{
max-width: 97.222222222222222222222222222222%; /* 350px / 360px */
}
}
jsFiddle
The problem here is when the width drops below around 850px the inline-block styles doesn't work and the elements drops below other elements, what I want to do is make those responsive. I mean, when the width drops below 850px I want the elements to stay as inline-blocks and instead reduce there size/width as the width drops. Something like what is happening below 720px but as inline-blocks. How can that be done?
http://jsfiddle.net/6wbfy/3/
This seems to work very well.
.article {
width: 45%;
}