HTML/CSS Image box (popup) adjusting to variable images size - html

I am trying to create a box over a dark background to show a picture over the content (as many sites do). There should be a dark color filling the screen (OK), a box centered on the screen (OK) with a 1em space between the box border and the picture. The picture can be portrait or landscape, and should be resized so it doesn't exceed either 90% height and width (keeping ratio). I am trying to do that purely in CSS/HTML, without javascript (if possible) My goal
Here is the code I currently have:
HTML:
<div id="picture_viewer_container" class="picture_viewer_container">
<div id="picture_viewer_content" class="box">
<span id="picture_viewer_content_close">Fermer</span>
<div id="picture_viewer_content_picture">
<img src="data:image/jpeg;base64,xxx">
</div>
</div>
</div>
CSS
.box {
background-color:#F6E9F7;
border:1px solid #E298EA;
}
.picture_viewer_container {
position:fixed;
background: rgba(0, 0, 0, 0.8);
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index:1000;
align-items: center;
display: flex;
justify-content: center;
}
#picture_viewer_content {
padding:1em 1em 1em 1em;
position:relative;
}
#picture_viewer_content_close {
position:absolute;
right:0;
top:0;
}
#picture_viewer_content_picture {
display:inline;
}
#picture_viewer_content_picture img {
max-width:90%;
max-height:90%;
}
Current issues:
a landscape picture gets more than 1em of margin on the left and right side, but proper margin above and below
a portrait picture ignores the max-height and is bigger than the screen.

Use auto with your CSS img height.
#picture_viewer_content_picture img {
max-width:90%;
height:auto;
}
If you need other solutions, something like adaptive-images may be something to look into.

Related

How to create a responsive image with a button/link responding with width in the middle of image

I am creating a website and I want to have a link or a button in the middle of this image
PC Image
the link/button needs to be in the center of the PC screen and say "Start Learning" when the link/button is clicked it will link to another page. I have tried creating this on my own and am trying to make the website responsive but when I am at 100% browser width the link is perfectly centered and when i minimize the browser the PC Image stays at 100% width which is good and responsive but the "Start Learning" link wont stay centered on the image and minimize with it, the link just jumps around the page.
use vw(% of the viewport width) for your font size it will scale the size of your font and I set some css for your code that makes your button responsive and centered even if you resize your browser width.
.banner-inner
{
width:100%;
height:100%;
position:relative;
text-align: center;
}
.centered
{
position: absolute;
margin: 0 auto;
width:100%;
height:auto;
display: block;
text-align:center;
font-size:6vw;
left:0;
right:0;
top: 50%;
transform: translateY(-50%);
}
.centered a
{
margin: 0 auto;
color: #000;
text-decoration: none;
padding: 20px;
}
.img
{
width:100%;
height:auto;
display: block;
margin: 0 auto;
object-fit: cover;
}
<div class="banner-inner">
<img class="img" alt="" src="https://img00.deviantart.net/080b/i/2014/360/d/3/texture_13_by_sirius_sdz-d19qqe1.jpg">
<div class="centered">Start Learning</div>
</div>
I had to make a few changes to your style to make it responsive.
Your Centered class has a margin left and right set to auto to center it horizontally. We made margin top a % so it will decrease as the image does.
We also used display flex to center everything. and set a font which will decrease via media queries. I also added a width of 100% to the image and an auto height.
The CSS
#media only screen and (max-width:1000px){.centered{font-size:12pt!important;}}
#media only screen and (max-width:800px){.centered{font-size:11pt!important;}}
#media only screen and (max-width:600px){.centered{font-size:10pt!important;}}
#media only screen and (max-width:400px){.centered{font-size:9pt!important;}}
#media only screen and (max-width:200px){.centered{font-size:8pt!important;}}
.banner-inner{
width:100%;
height:100%;
position:absolute;
text-align: center;
color: white;
}
.centered{
position: absolute;
margin-left: auto;
margin-right: auto;
width:100%;
height:auto;
margin-top:20%;
display: flex;
justify-content: center;
align-items: center;
text-align:center;
font-size:12pt;
}
.img{
width:100%;
height:auto;
float:left;
}
The HTML
<div class="banner-inner">
<img class="img" alt="" src="img/Website PC.png">
<div class="centered">Start Learning</div>
</div>

Turning floated elements into stacked elements on small screens

I have 3 boxes that are currently floated left of each other,
https://jsfiddle.net/2owu0k7s/
When viewing on a smartphone I want the width of the boxes to be near full screen, and the height of each box to be the same height has the viewport. Is this possible to do?
I have tried doing this within a media query,
.box {
float:none;
width:95%;
margin:0 auto 20px;
height:95%;
}
But on my iphone 6s I can still see more than 1 box and 5% of another.
When viewing on a smartphone I want the width of the boxes to be near full screen, and the height of each box to be the same height has the viewport. Is this possible to do?
With a media query and flexbox it is possible (and quite simple, in fact). Here's the smartphone view:
body {
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
box-sizing: border-box;
border: 3px solid red;
}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
Revised Fiddle
.box {
width:350px;
height:50vh;
border:1px solid red;
float:left;
margin-right:10px;
}
#media screen and (max-width: 740px) {
.box {
float:none;
width:95%;
margin:0 auto 20px;
height:96vh;
}
}
Write an media query and
Give the height measurements using 'vh' -vertical height.
100vh means full screen-height.

Div moving on screen size change

I have a static HTML website that works perfectly, apart from the footer. Essentially, the footer is an image in which has text overlap. When the screen size is changed, the elements on the page move with it to fit the screen. Except for the footer text. I cannot workout what i am doing wrong and fairly positive that this is something so simple i've missed.
Here is a diagram of what is happening and what i need it to do:
So, the text box (in white for demo purposes), is aligned with the left box/edge above. Even when the screen enlarges and shrinks, i need the text to remain in line with that 'marker'.
However, when i enlarge the screen, you can see that the white box above moves, yet the text does not go with it!
The code for the footer is as follows:
<div class="orangeFooter">
<img src="images/orange-footer.png">
<h2><span class="orangeText">Promoting a knowledge based NHS</span</h2>
</div>
And the CSS:
.orangeFooter{
margin-top:40px;
width:100%;
position:relative;
height:133px;
}
h2 .orangeText{
background:#fff;
padding:7px;
font-size: 40px;
}
.orangeFooter img{
width:100%;
height:100%;
}
.orangeFooter h2{
position:relative;
font-style:italic;
left: 210px;
bottom:80px;
font-size: 40px;
}
Use this CSS:
.orangeFooter h2 {
position: relative;
font-style: italic;
/* left: 210px; */
bottom: 80px;
font-size: 40px;
display: block;
margin: 0 auto;
width: 1016px;
}
It makes the h2 1016px wide (which is the width of your site). it also centers it in your site (with the margin: 0 auto;)

Divs being pushed down when windows is made smaller

Pretty new to html and css and i'm just having the problem described in the title. My nav is pushing down div with the id main.
nav{
width:120px;
float:left;
margin:0px 5px 0px 5px;
#main{
display:inline-block;
padding: 1em;
float:left;
position:relative;
min-width: 900px;
Any help is appreciated, cheers.
edit:
Hi guys, maybe i should explain it better. On the page there is a nav to the left and a div to the right of it. When the windows width is made smaller the div to the right is being pushed below the nav instead of stay where it is and it's content being displayed off the screen.
You have explicitly told your element to behave like that. When you set min-width and a width in pixels, you are telling your elements to stay the same size no matter what happens. Remove min-width and set width to a percentage value like 1% instead of 50px like this:
nav {
width: 50%;
background: red;
height: 50px;
float:left;
}
#main {
display: inline-block;
height:50px;
float:left;
position: relative;
width: 50%;
background: black;
}
body {
margin: 0;
}
<nav></nav>
<div id="main"></div>

Not getting desired effect with CSS

I'm trying to get this effect with CSS (that little grey side stripe thingy):
However, nothing of what I'm doing is working. My code looks like this:
<header class="entry-header">
<div class="entry-title">
<h1>Contact</h1>
</div>
<div class="entry-float"></div>
</header><!-- .entry-header -->
I've tried floating div.entry-title left and padding div.entry-float, changed div.entry-float to <span>, tried using backgrounds on one div only, all of it unsuccessfully.
This must be 100% flexible, as not all headings are the same width. I'd prefer avoiding Javascript/jQuery entirely.
Can anybody assist?
I would do this with a trick where you place the "line" as a CSS3 pseudo element aligned to the center of the container. Then give the title element a background color with padding and place it over the top of the line. This will allow for variable length titles.
body{
background:#FAFAFA;
}
.entry-title{
position:relative;
margin:0 110px;
}
.entry-title:before{
position:absolute;
content:"";
top:50%;
background:#f7f7f7; /* line color */
left:0;
right:0;
height:10px;
margin:-5px 0 0; /* = half the height value */
}
h1{
position: relative;
color: #d9d9d9;
padding:0 20px 0 0; /* increase this number to add more spacing to the right of your title before the line */
background:#fafafa; /* same as background color of container element */
display:inline-block;
}
JSFIDDLE DEMO
There are various ways to achieve this, one of them is to add pseudo element to header, absolutely position it as desired and hide extra with overflow: hidden:
<h1>Content</h1>
h1 {
text-transform: uppercase;
font-size: 1em;
color: grey;
position: relative;
overflow: hidden;
}
h1:after {
content: "";
display: inline-block;
height: 4px;
width: 100%;
background: grey;
position: absolute;
top: 8px;
margin-left: 10px;
}
JSBin.