Trouble with positioning images evenly inside container - html

One of my questions about applying float to resizable images was answered by Gasim (thankyou) but I still have one more problem to fix.
I have two images which have this css code applied to them...
.image-container {
display: inline-block;
width: 100%;
background-size: cover;
background-position: center;
background-image: url(Images/Filler2.png);
height: 250px;
margin-right: auto;
margin-left: auto;
}
#media only screen and (min-width : 1200px) {
.image-container {
max-width: 500px;
}
}
.image-container2 {
display: inline-block;
width: 100%;
background-size: cover;
background-position: center;
background-image: url(Images/Filler3.png);
height: 250px;
margin-right: auto;
margin-left: auto;
}
#media only screen and (min-width : 1200px) {
.image-container2 {
max-width: 500px;
}
}
So now they're resizing correctly and aligned next to one another, I am unable to position them horizontally like my example below. I have tried the only way I know how to do this (margin-left: auto , margin-right:auto) but this didn't work. Also padding isn't working? Is there some part of this code which prevents these both from working and if so what do I need to fix it?
Any input if helpful thanks :)

Maybe this is not the best solution for responsive images, but I generally use media queries with a mobile first approach and it has been working out great. Basically, you set the boxes' widths to 100% by default and on larger screens you set the width that you want:
.image-container {
float: left;
width: 100%;
background-size: cover;
background-position: center center;
background-color: red; /* for testing */
height: 250px;
}
#media only screen and (min-width : 1224px) {
.image-container {
max-width: 500px;
}
}
Here is the result: http://codepen.io/gasim/pen/xbYQdd
Try to make the window size smaller and you will see the difference. It is much simpler to test, debug, and modify this code.
EDIT: Forgot to mention. If you can also use display: inline-block instead of floats.
EDIT 2: To add padding between containers you need to add margin to a specific value, not auto. For example:
.image-container {
/* rest of the values */
margin: 5px;
}
This will add 5px margin to left, right, top, and bottom of the image container. If you want to have center the image containers also, I would add them in another container:
<div class="container">
<div class="image-container" style="background-image: url(URL_HERE);"></div>
<div class="image-container" style="background-image: url(URL_HERE);"></div>
</div>
and add the styles:
#media only screen and (min-width: 1224px) {
.container {
width: 80%;
margin: 0 auto;
}
}
Also, please check out the way I added the background images, using styles. It is much cleaner than writing CSS for each background image. That way you can have one class image-container and just add as many background-images without relying on CSS.

Related

HTML/CSS Fit Div to Contents unless screen is small

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.

Responsive images in bootstrap carousel

I have different image sizes in a div in a bootstrap carousel. It is either landscape or portrait images and if I set the width to be 100% the landscape images become perfect but the portraits to high.
And if I set max-hight to it the lanscape images doesn't become full width.
Link to Wep-page
CSS:
.case-box {
margin-bottom: 1.5em;
display: flex;
width: 100%;
height: 420px;
align-items: center;
justify-content: start;
}
#media (min-width: 768px)
.case-box {
height: 428px;
}
#media (min-width: 1280px)
.case-box {
height: 540px;
}
.case-img {
width: auto;
max-height: 100%;
}
Why don't you use Bootstrap 4 class .img-fluid for making images responsive (.img-responsive for Bootstrap 3)?
Set your images with css:
max-width: 100%;
max-height: 100%;
It should adapt them correctly no matter the height of width.
Solved it by creating images within a white frame, so they all have the same width and height.. Simple but not the best.

HTML/CSS - Mobile friendly and resizing easily

My classmates and I are trying to figure how to make this code be completely mobile friendly. We tried using #media screen but it did not work. We want to make this happen with HTML and CSS.
HTML
<!--List Content Start-->
<div class="listcontent">
<div class="listnumber">1</div>
<div class="listtitle">This div tag emphasizes the title.</div>
<div class="listpic"></div>
</div>
<br><br>
<!-- List Content End-->
<div class="listcontent">
<div class="listnumber">2</div>
<div class="listtitle">This div tag emphasizes the title.</div>
<div class="listpic"></div>
</div>
</div>
CSS
.listcontainer {
width: 100%;
height: 100%;
}
.listcontent {
width: 500px;
height: 400px;
margin: auto;
background-color: #F5EFEF;
padding:5px;
}
.listnumber {
width: 50px;
height: 50px;
float: left;
background-color: #B33638;
padding: 5px;
color: white;
font-size: 45px;
text-align: center;
}
.listtitle {
width: 425px;
height: 50px;
padding: 5px;
float: right;
background-color: #fff;
}
.listpic {
width: 100%;
height: 335px;
margin-top: 65px;
}
/* Smartphones (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
}
/* iPads (portrait and landscape) ----------- */
#media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}
/* Desktops and laptops ----------- */
#media only screen
and (min-width : 1224px) {
/* Styles */
}
/* iPhone 4 ----------- */
#media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}
Here is the fix I created for you in terms of your coding snippet: JSFiddle
.listcontainer {
width: 100%;
height: 100%;
}
.listcontent:first-child {
margin-bottom: 40px;
}
.listcontent {
max-width: 500px;
min-width: 320px;
width: 100%;
height: 400px;
margin: auto;
background-color: #F5EFEF;
padding: 0;
}
.titlewrapper {
width: 100%;
}
.listnumber, .listtitle {
display: inline-block;
}
.listnumber {
width: 50px;
height: 50px;
background-color: #B33638;
padding: 5px;
color: white;
font-size: 45px;
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
float: left;
}
.listtitle {
height: 50px;
line-height: 50px;
padding: 5px;
background-color: #fff;
white-space: nowrap;
width: calc(100% - 70px);
max-width: 100%;
}
.listpic {
width: 100%;
height: 335px;
margin-top: 65px;
}
<div>
<!--List Content Start-->
<div class="listcontent">
<div class="titlewrapper">
<div class="listnumber">1</div>
<div class="listtitle">
This div tag emphasizes the title.
</div>
</div>
<div style="clear:both;" />
<div class="listpic"></div>
</div>
<!-- List Content End-->
<div class="listcontent">
<div class="titlewrapper">
<div class="listnumber">2</div>
<div class="listtitle">
This div tag emphasizes the title.
</div>
</div>
<div style="clear:both;" />
<div class="listpic"></div>
</div>
</div>
Ok so let's dive in, what are the reason for all these CSS and HTML changes?
To make something mobile responsive you need to consider the behavior it needs to have. When it comes to element widths, a general rule of thumb is the following.
CSS code example:
.some-wrapper-element {
width: 100%;
min-width: 320px;
max-width: 100%;
}
This makes a wrapping element, such as your .listcontent to become responsive with and without media queries being used. Note how I applied this throughout the CSS to give elements which needed to resize as the page resized, a dynamic width.
Your HTML layout needed a little more thought behind it. You are trying to horizontally align two elements and make them responsive. I will admit this is not a straight forward and easy to implement solution, but there are standard things to look at:
A wrapping element to ensure horizontal alignment occurs.
A CSS rule to keep the elements in line, such as display: inline-block or float: left, or a combination... the implementation depends on what works for you.
The elements to be horizontally aligned and made responsive, need to fit next to each other. This is important and it is the reason for all the added CSS code. See a very good reference here: How to place two divs side by side where one sized to fit and other takes up remaining space?
Media queries..., my rule of thumb is: does x element need to change responsively in a way which cannot be done with CSS styling first? Such as hiding/showing an image on certain screen widths, then your answer is yes please. Otherwise think of our layout first, how to make it responsive first and last how to use media queries for the things you cannot make responsive.
The <div style="clear:both;" /> code that was put there. That exists only to help separate your title section from your image section. It is another layout sugar I put there for you, because it will help keep things in place and separate content that does not need to be mixed. Awesome right!
line-height: 55px; This is simple: if you have text inside a small element (like the one you have) and you want it to look well, center it using line-height that is equal to the element's height. I did this just because I thought it looks nice, but change it if you think it is unnecessary.
Anyways, I hope this helps let me know if you have any questions.
The listcontainer should have the fixed width, while the listcontent fill them by 100%. All you have to do then is just fill the media querys like this:
#media only screen
and (min-device-width : 320px)
and (max-device-width : 1024px) {
.listcontainer {
width: 100%;
height: 100%;
}
}
This way the site will have a fixed width for desktop usage, once the browser is too small to display the entire page (in this case 1024px but that depends on the page - in your example probably 500px) it will go to 100% dynamically, which is the most common approach. I can't tell you all of the media querys, since it depends on the developer to decide what the bevahiour should look like.
If you want to have a really mobile friendly site I recommend you using a framework like bootstrap - it does most of the job for you and you'll learn exactly how media querys are working and how you are supposed to use them properly.

Responsive elements - stretching and then stacking

I'm fairly new to responsive web design, so don't beat me up too badly.
I have a currently fixed-width gallery page that is 1000px wide. The 1000px outer div has 30px padding and 30px between each pair of images. So I've got 910px of space available for each pair. The page might look like this:
(30px spacing)(500px img)(30px spacing)(410px image)(30px spacing)
(30px spacing)(480px img)(30px spacing)(430px image)(30px spacing)
(30px spacing)(450px img)(30px spacing)(440px image)(30px spacing)
...etc.
I'd like to convert it to a responsive page so that the images scale down as the browser window shrinks and ultimately stack on top of each other once the browser window drops below 640px.
The only way I know to make this 640px change is inside a stylesheet. Is this the only way I can do this? Am I going to have to define styles within the stylesheet for every image?
For example, for a 480px wide image:
img.img480 {
width: 48%;
float: left;
}
#media screen and (max-width: 640px) {
img.img480 {
width: 100%;
max-width: 480px;
float: none;
}
}
Here is an example of the effect you're asking for with responsive design.
A couple things to note:
margins are bad, use padding and wrappers instead
floats are bad, use inline-block instead
(Demo)
html, body {
margin: 0;
}
.wrapper {
padding: 0 15px;
font-size: 0;
}
.img-wrp {
width: 50%;
padding: 0 15px;
display: inline-block;
box-sizing: border-box;
}
.img-wrp img {
width: 100%;
}
#media screen and (max-width: 640px) {
.img-wrp {
width: 100%;
}
}
<div class="wrapper">
<i class="img-wrp"><img src="//lorempixel.com/640/480" /></i>
<i class="img-wrp"><img src="//lorempixel.com/640/480" /></i>
</div>

How to size images in #media screen property?

I want to know how to size my images in #media screen and max-width:640.
My style.css:
#header-wrapper
{
position: relative;
padding: 7em 0 0;
background:url(images/fox-illustration.jpg) no-repeat center;
background-position:center;
width:auto;
height:768px;
}
My mobile.css:
#media screen and (max-width: 640px) {
header-wrapper {
width: 600px;
}
So the width is only applied to to the header-wrapper..
I also tried to give the header-wrapper in mobile.css another image
(which I sized in photoshop to mobile size).
But this didn't work either. I think because the image of the header-wrapper overwrite it in style.css?
background:url(images/fox-illustration.jpg);
background-size:600px 400px;
background-repeat:no-repeat;
Do this
First make sure your mobile.css is active.
If the second part of your code is at it is set now, you are probably missing an id tag in the css.
Try changing your
header-wrapper {
width: 600px;
}
to
#header-wrapper {
width: 600px;
}
Other than using fixed background size, you can use cover and follow the width of the #header-wrapper
#header-wrapper {
position: relative;
padding: 7em 0 0;
background:url(images/fox-illustration.jpg) no-repeat center;
background-size:cover;
width:auto;
height:768px;
}
#media screen and (max-width: 640px) {
#header-wrapper {
width: 600px;
}
}