Football Fantasy lineup with image and divs - html

I am developing a little Fantasy Football web. For the lineup, I want to put some div's with the players data above an image of a football field, and want to make it responsive too.
My example code:
HTML:
<div class="container" style="position: relative;" >
<div class="tag">player</div>
<img class="img-fluid" id="image" src="<?php echo base_url() ?>assets/img/grass.jpg">
</div>
CSS:
<style type="text/css">
.tag {
float: left;
position: absolute;
left: 55px;
top: 100px;
z-index: 1000;
background-color: #92AD40;
padding: 5px;
color: #FFFFFF;
font-weight: bold;
}
The problem comes when the browser resizes or it's in mobile version, since the position of the divs goes elsewhere, even out of the image. There is way to establish the div's position relative to the image itself? So no matter if I access through a desktop or a cellphone it maintains its position and I can see the players in the position that I want. And in this case, it's better to have an image or maybe a div with a image background? Or maybe a bootstrap method/class can do something similar.
Desktop:
Mobile:
What I want to accomplish (edited with paint):

The problem is to get the players positioned correctly relative to the grass image whatever the device dimensions.
You can get a container div to take on the size of its contents by making it display: inline-block. Then you want the grass to fill as much as possible of the screen - but it must keep its aspect ratio. The method you have (using img rather than background-image) is probably the better one to use here because the sizing will occur automatically.
Once you have done that you can position your players in the x and y directions by giving their percentage positions. Also if you alter your HTML slightly so that the img comes first in the container it will always be behind the players you put afterwards. The players are positioned with absolute, as you have done, so that they do not effect each others' positions.
* {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: auto;
height: auto;
display: inline-block;
}
.tag {
position: absolute;
left: 55%;
top: 80%;
background-color: #92AD40;
padding: 5px;
color: #FFFFFF;
font-weight: bold;
font-size: 2vw;
}
#image {
width:100vw;
max-width: 100vh;
}
<div class="container" >
<img class="img-fluid" id="image" src="https://i.stack.imgur.com/3I0Ie.jpg">
<div class="tag">player</div>
</div>
I have rather arbitrarily set the font-size to be proportional to viewport width, so it shrinks proportionally, but you probably want to think about whether the smallest devices need a larger font size to be readable.

Related

CSS #media doing strange things

I have a HTML element:
<div id=... class="pie-chart-container"></div>
I'm trying to change the size of this div depending on whether media type is screen or print.
Originally, my CSS styles defined the size of the screen version, and then #media print to change the size of the printed version. This wasn't working as I expected, so I swapped around the styles to define the print version, and then use #media screen to control the size of the screen version, as follows:
.pie-chart-container {
width: 20cm;
height: 25cm;
margin: auto;
}
#media screen {
.pie-chart-container {
width: 80vw;
height: 100vh;
}
}
However, this also seems to be behaving strangely, as when I change the width and height inside the media query, the dimensions of the div are changed in the print version (and also the screen version). Changing the width and height in the first selector, outside the media query, also has an effect on the printed version, but not in the same way as changing the values inside the media query - changing the values in the selector outside the query seems to increase the space the div takes up, without changing the size of the elements inside the div very much, although the elements inside do move around a little.
If anyone has any knowledge on what/why this is happening, I would be very greatful if you shared it.
Note: I am using Chrome's print preview to view the effects of the styles on the printed version.
Update: This div contains a Google Chart, so there is a massive amount of code inside. The top few levels look like this:
<div id="donutchart2" class="pie-chart-container">
<div style="position: relative">
<div dir="ltr" style="position: relative; width: 1138px; height: 653px">
<div
aria-label="A chart."
style="
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
"
>
</div>
</div>
</div>
<div
aria-hidden="true"
style="
display: none;
position: absolute;
top: 663px;
left: 1148px;
white-space: nowrap;
font-family: Arial;
font-size: 15px;
font-weight: bold;
"
>
</div>
<div></div>
</div>
</div>

Text Stacking Over Itself

I am working on a website from a template, and for some reason the text in the lower left corner stacks on itself when I reduce the window size. I am by no means an expert in HTML/CSS but I can't seem to figure out why exactly it is doing this. I've tried messing with the div properties and the z-index. It looks like this
My HTML code:
<!-- Site Logo -->
<div id="logo">salem music</div>
CSS:
#logo {
font-family: 'Coustard', serif;
font-size: 49px;
bottom:40px;
height:auto;
left:40px;
position:absolute;
width:20%;
z-index:1000;
color: #fff;
}
I'd like it to look like
The width is set to 20%. If you decrease the window width, then eventually the #logo element will get too small in width and will cause the text to wrap.
Consider setting a larger width value.
.logo {
background-color: grey;
margin: 10px;
}
.logo1 {
width: 5%;
}
.logo2 {
width: 75%;
}
<div class="logo logo1">This width is too small and will wrap</div>
<div class="logo logo2">This width is better and won't wrap</div>
Or, if you want to keep it that width, and want to allow text to overflow out of the <div>, you can use white-space: nowrap. However, I wouldn't recommend this, as it would probably overlap the links next to it.
.logo {
background-color: grey;
margin: 10px;
}
.logo1 {
width: 5%;
}
.logo2 {
width: 5%;
white-space: nowrap;
}
<div class="logo logo1">This width is too small and will wrap</div>
<div class="logo logo2">This will not wrap even though it is too small</div>
#Bálint made a good point in the comments as well: when possible, use the em unit for measurements instead of percentages. ems are based on font size, rather than window size. Therefore, changing the window wouldn't affect it, but changing the font size would (in a desirable way).

How do I position an image to the top left corner of a div, and keep it there if the div moves?

I apologize if this has been answered time and time again. I remember searching thoroughly for an answer a couple years ago when I first wrote up my website script, but I couldn't ever find one. The same for now.
Recently I reworked my website's script so I can host it onto Weebly. Here is one of the four pages of my site that I need help with. As you can see, the images that pop up when the thumbnail is hovered over are absolutely positioned. For most computer resolutions and/or browsers, this will have the image appear out of the designated box.
How could I position them to the inner top left corner of the div? Or better yet, horizontally and vertically centered within it?
<section id="Sizes" style="float: left">
<a href="#Space">
<img class="Small" src="/files/theme/SampleD_Fun_Icon.png" width="150" height="150" alt="Sample 1: Day of Fun" />
<img class="Large" src="/files/theme/SampleD_Fun.png" width="150" height="150" alt="Sample 1: Day of Fun" />
</a>
...
</section>
<a id="Space"></a>
<span class="Popup">Hover over thumbnail to display sample artwork.</span>
<br style="clear: left" />
a:hover img.Small
{
border: 5px solid #21568b;
margin: 10px;
text-decoration: none;
}
section#Sizes a img.Large
{
border-width: 0;
height: 0;
left: 438px;
position: absolute;
top: 326px;
width: 0;
}
section#Sizes a:hover img.Large
{
height: 526px;
left: 438px;
position: absolute;
top: 326px;
width: 520px;
}
.Popup
{
border: 3px solid;
float: left;
height: 272px;
margin: 8px 20px 0px 0px;
padding-top: 254px;
text-align: center;
width: 520px;
}
Thank you for your time. :)
Your whole design is a bit fragile, and I wouldn't recommend building this this way in the first place, but you're looking for practical answers, so here's the smallest change I can think of that fixes your problem:
1) Add this to your style sheet:
body { position: relative; }
2) On line 40 from your main_style.css, change top: 326px to top: 316px and left: 438px to left: 428px, so that it becomes like this:
section#Sizes a:hover img.Large {position: absolute; top: 316px; left: 428px; width: 520px; height: 526px;}
How does that work?
Your images are place using absolute positioning. By default, that works relative to the viewport (the window). But by turning the body into position relative, it becomes a containing block, and position absolute is relative to the nearest containing block ancestor.
So now, your images are fixed within the body element, instead of being fixed relative to the window. Since the margins of the body element is what's changing size when you resize the window, that makes the various pieces of your content fixed relative to each other. You then just need to remove 10px from the top and left side, since that's the size of the border of your body element, and we're now measuring from inside the border.
TLDR: You can't do this in pure CSS.
You can easily position the image inside the container div if you place the image element inside the div element, and then use absolute positioning like top: 0; left: 0; (or with a number of other methods). But then you'd need JavaScript to correlate the hovered thumbnail with the popup full-size image.
Alternatively, you can have the full-size image be nested in the thumbnail element (like you currently have), but then you'd need JavaScript to position the full-size popup image inside the container div.
Of the two alternatives, I recommend the first: put all the popup images inside the target container, and use JavaScript to show or hide them when a thumbnail is hovered. Correlating the thumbnail and the full size image via JavaScript is going to be easier then writing positioning code.
I see you're using jQuery already so why not do something like this?
$('.Small').on('mouseover', function(){
$('.Popup').empty().html($(yourtarget).attr('img' , 'src'));
});
$('.Small').on('mouseout', function(){
$('.Popup').empty().html('Hover over thumbnail to display sample artwork.');
});
Just because everyone was saying it can't be done with pure css, I wanted to demonstrate that it can, and it is even quite easy. Have a look at the folowing example:
http://jsfiddle.net/aafa2zp5/
<div id='images-wrapper'>
<ul>
<li>
<img class='small' src='http://placehold.it/50/ff0000'/>
<img class='big' src='http://placehold.it/300/ff0000'/>
</li>
<!-- and some more similar thumb / image groups -->
</ul>
<div class='preview-area'></div>
</div>
CSS (or the relevant part at least)
#images-wrapper {
position: relative;
}
.big {
display: block;
position: absolute;
top: 54px;
right: 54px;
opacity: 0;
transition: opacity .5s;
}
.preview-area {
width: 350px;
height: 350px;
border: 4px solid blue;
position: absolute;
top: 21px;
right: 21px;
}
li:hover .big {
opacity: 1;
}
The key is to set a position relative to the wrapper (and keep all of the descendants as their default static). Then you can use this to position the preview area and the big images against by setting them to postion absolute and carefully calculating the correct postion. I even added a cross fade, just because it is so easy, but you could just as well work with display block / none if you prefer.
For smaller screens you may want to alter the dimensions and positioning inside a media query, but it still should be doable (though depending on the hover state is perhaps not the best idea on a touch device)
I hope you get the idea and you can figure out how to apply this technique to your own site. Feel free to ask if you want me to explain further or when you get stuck.

How to prevent changing parent's width on resize

I got a question: I have an image in a div. the image is bigger that the div and it has height:100% to make it look ok. So when I do a resize image becomes bigger and it looks fine. but when I resize the browser to make it smaller image becomes smaller, but its parent saves the width of the original image. In fact it just takes the width of an image. I got a fiddle for you, just try to resize your browser or the output section to see the red background appear. I'm curious is there any chance to make the div dimenstions the same as the image's dynamically. I need the container dimensions cause I have some other elements besides the image and they use the coordinates of the div. thanks.
important! it works the way I saw it only in FireFox. Chrome's behaviour is different.
.img-wrapper {
display: inline-block;
height: 100%;
position: relative;
background-color: red;
}
.gallery-image {
bottom: 90px;
left: 0;
position: absolute;
right: 0;
text-align: center;
top: 25px;
background-color: grey;
}
img {
height: 100%;
}
<div class="gallery-image">
<div class="img-wrapper">
<img src="http://www.ibm.com/big-data/us/en/images/bigdata_homepage_maininfographic_345x194.jpg" alt=""/>
</div>
</div>
This is usually done with CSS using background-image:url("http://www.ibm.com/big-data/us/en/images/bigdata_homepage_maininfographic_345x194.jpg").. This way your image and div become one object. Then you just control the div and the background image size accordingly.
Side Note... It helps with performance as well.
You can set the minimum dimensions of an image so it won't become any smaller like this
img {
min-height: 200px;
min-width: 400px;
}

Width of website to always fit 100% of browser page width

I am a 3D artist by profession, however I have recently been trying to create a website for myself from scratch. My needs are very simple - a widescreen website which consists of a background image and thumbnails which once clicked load a overlay pop up showing further information on that particular content. The pop-up overlay is not the issue here.
My current problem is that I need my page to always be 100% of the browser width, so that means it must scale - along with all the content (thumbnails) in it. I created my first attempt on a screen which is 1920x1080 and the result was perfect, however - when I loaded it on my laptop which has a 1366 screen, it resulted in only showing me a slice of the full page, and gave me scroll bars to view the rest.
I am placing the thumbnails via px as I have got the values from Photoshop but I understand that my needs can only be accomplished via % - how can i overcome this?
Here is a visual of my setup http://i.imgur.com/ZdgTRYk.jpg
Grey is browser window
Red is background
Green is content
Everything should scale at the SAME rate.
Here is my HTML
<body>
<div id="background">
<img src="images/background.png">
<div id="box3thumb">
img src="images/box3thumb.png">
</div>
</div>
</body>
and my CSS
#background {
position:relative;
left:0px;
}
#box3thumb {
position:absolute;
left:514px;
top:117px;
width:92px;
height:200px;
}
I really appreciate any help I might recieve on this.
Thanksm
Elliott
ok, for your #background, you can use this css to scale the browser:
#background{
width: 100%;
background: red;
}
and for your thumbnails, I don't understand very well how you want them placed, but according to your image, you'll need to put them inline:
#thumbnails{
display: inline-block;
margin-left: 15%;
}
The % of the margin may vary depending on what you want.
If you set the body and html elements of your page to
CSS:
html, body
{
position: relative;
height: 100%;
width: 100%;
overflow: hidden;
}
This will prevent scrollbars from appearing, while maintaining the full width and height of the screen, regardless of resolution. As for your thumbnails, if you have a set number of thumbnails then you can set the widths of your thumbnails to say, 10% width and height with a margin: 1%;, this will allow you to fit roughly 64 thumbnails, but they will get small if the user has a shitty resolution.
.thumbnail
{
position: relative;
display: inline-block;
width: 10%;
height: 10%;
margin: 1%;
}
EDIT ------------------
With large thumbnails like that you could make it more like this:
.thumbnail
{
position: relative;
display: inline-block;
width: 20%;
height: 20%;
margin: 5%%;
}
Use in style.css
#background{
width: 90%;
background: red;
}
/*thumbnails*/
#thumbnails{
display: inline-block;
margin-left: 10%;
vertical-align:text-bottom;
}