How to constrain the text overflowing within a container? - html

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.

Related

How do I make my social media links appear site wide on additional html pages?

The issue I'm experiencing is so simple it's confusing me...
I've implemented my social media logo as links. They work completely fine on the index page, just not elsewhere. I've essentially copied and pasted the exact code below to the remaining html pages, and nothing. The code isn't responsive. Please see examples below.
index.html
resume.html
Here is my code...
.socialBanner {
position: absolute;
}
.githubLogo {
position: absolute;
width: 40px;
height: auto;
top: -1260px;
left: 185px;
}
.linkedinLogo {
position: absolute;
width: 40px;
height: auto;
top: -1260px;
left: 265px;
}
<div class="socialBanner">
<img src="githubLogo.png" alt="githubLogo" class="githubLogo">
<img src="linkedinLogo.png" alt="linkedinLogo" class="linkedinLogo">
</div>
Thank you in advance!
As stated below, I've tried copy and pasting the code as well as reviewing it line by line, and I'm not sure what I'm missing exactly. Any guidance is appreciated!
It’s a positioning issue. Remove all your positioning code and you’re good to go.
img {width: 64px;}
<div class="socialBanner">
<img src="https://cdn-icons-png.flaticon.com/512/25/25231.png" alt="githubLogo" class="githubLogo">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/LinkedIn_icon_circle.svg/1200px-LinkedIn_icon_circle.svg.png" alt="linkedinLogo" class="linkedinLogo">
</div>
Looking at the code I cannot think of a reason to use top: -1260px for social icons other than some animation moving them in from 'above'.
I have somewhat replicated the example image showing three icons below the byline. As you can see in the snippet CSS, with careful planning of flexbox elements and proper alignment you can have a responsive 'banner' without media queries.
The left side of the banner .me is a vertical flexbox container with three rows of which .socialBanner is a flexbox row container.
The right side of the banner .menu is a horizontal flexbox container with four menu options.
Upon resizing the browser, the flexbox alignment and wrapping options make the banner neatly fit in viewports >= 320px wide.
Check out the little math in html { font-size: calc(0.625vmin + 0.75rem) } which calculates the root font-size relative to the minimum viewport size (vmin) resulting in:
font-size: 14px on 320px viewport minimum size devices
font-size: 20px on 1280px viewport minimum size devices
All other font sizes are calculated relative to the current viewport minimum size. Effectively: for each 160px change in viewport size, the font size changes 0.25px
Math: linear equation y = mx + b for points p1(320,14) and p2(1280,20) => y = 0.00625x + 12 converted to CSS using 100vmin for x and 0.75rem for b (which is 12 / 16 = 0.75rem).
BTW site wide will still require you to copy the code to each individual html file unless you use some framework that does the copying automatically...
Snippet
#import url('https://fonts.googleapis.com/css2?family=Poppins:wght#400;600&display=swap');
*, ::before, ::after { box-sizing: border-box }
/* Linear equation y=mx+b using points p1(320,14) p2(1280,20) */
html { font-size: calc(0.625vmin + 0.75rem) }
body {
margin: 0; /* remove default spacing */
display: grid; place-items: center; /* Easy centering demo banner */
width: 100%; min-height: 100vh; /* at least full screen */
font-size: 1rem; /* inherit responsive root font */
font-family: Poppins, sans-serif;
background-color: #222222; color: White;
cursor: default;
}
a { text-decoration: none; color: currentColor }
.main-banner {
width: 100%; min-height: 40vh;
padding: 1.5rem 2.5rem;
background-color: #c1e1c1;
}
/* horizontal orientation */
.main-banner, .me, .menu, .socialBanner {
/* Flexbox layout */
display: flex; flex-wrap: wrap;
/* Flexbox alignment */
align-items: center;
/* text alignment */
text-align: center;
}
/* vertical orientation */
.me { flex-direction: column }
/* Flexbox alignment specifics */
.socialBanner { justify-content: center }
.main-banner { justify-content: space-around }
.menu, .me { justify-content: space-between }
.me > * { width: 100%; text-align: center; font-weight: bold }
.name { font-size: 3.5em ; line-height: 1 }
.byline { font-size: 1.25em; line-height: 2 }
.socialBanner a { display: block; padding: 1rem 0.5rem 2rem 0.5rem }
.socialBanner img {
display: block; /* removes tiny whitespace below images */
border-radius: 50%
}
.menu { color: #b5b5b7; font-size: 1.25em; font-weight: bold }
.menu > * { border-bottom: 1px solid transparent; flex: 1; padding: 1rem; cursor: pointer }
.menu > :hover { border-bottom: 1px solid White; color: White }
<div class="main-banner">
<div class="me">
<div class="name" >jordan fichter</div>
<div class="byline">portfolio + ux design + web development</div>
<div class="socialBanner">
<a target="_blank" href="https://github.com/thesd5x"><img src="https://picsum.photos/40?random=1" alt="githubLogo" class="githubLogo"></a>
<a target="_blank" href="https://www.linkedin.com/in/jordan-fichter-a40762152/"><img src="https://picsum.photos/40?random=2" alt="linkedinLogo" class="linkedinLogo"></a>
<a target="_blank" href="https://google.com"><img src="https://picsum.photos/40?random=3" alt="googleLogo" class="googleLogo"></a>
</div>
</div>
<div class="menu">
<div>welcome</div>
<div>resume </div>
<div>about </div>
<div>contact</div>
</div>
</div>

How do I pin text to a particular part of an image on resize?

I have a splash/masthead/full page image whose center text needs to be pretty much always the same look/format as in this no matter what the resize. (see below imgur link)
[Glassball Background Image with Text]1
I tried a workaround where the background had the text already as a part of the image, but my client complained about the lack of "luminosity" in the white that was lost, compared to the "brightness" of the white in the menus.
The problem is that the glassball of the background image is slightly off center and throws everything else off.
I also have a button that needs to be to the far right of the center text on desktops and below it on mobile screens.
All of my code solutions have been changing the padding or margins, but I have to add endless media queries.
I'm working in Bootstrap 4... there must be a better solution than those I've tried.
Please any help or advice would help (and note that I"m terrible at photoshop and other workarounds).
Here's my Masthead CSS:
.masthead {
height: 100vh;
background-image: url('../images/glassball2.jpeg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.btn {
border-radius: 34.5px;
border: none;
color: white;
}
.btn.main-cta {
font-family: $header-black;
padding: 20px;
font-size: 20px;
}
.splash-header h1 {
text-transform: uppercase;
font-family: $body-font;
color: white;
text-shadow: 0px 5px 5px rgba(0, 0, 0, 0.4);
letter-spacing: .5px;
line-height: 1.3em;
}
And my HTML
<header class="masthead">
<div class="container h-100">
<div class="row h-100 align-items-center">
<div class="col-md-2">
<p></p>
</div>
<div class="splash-header col-md-7 text-center">
<h1>your industry link to trusted global marketplaces and technologies</h1>
</div>
<div class="col-md-3 text-center">
<a class="main-cta gradient btn" data-value="contact" href="#">Schedule your free <br> 30-min consultation</a>
</div>
</div>
</div>
</header>
The requirement is to place text over the globe wherever it appears in the viewport - i.e. whatever the aspect ratio of the image compared to the viewport. The main problem being that it is not centered either vertically or horizontally so normal text-centering techniques do not work.
If we measure the height and width of the image and the center position of the globe and its diameter (it doesn't matter in what units)
then we can get CSS to calculate various percentage positions etc. so we can always place a div element on top of the globe.
Here is the snippet which does that using CSS variables for the dimensions so you can change them should the image change (or if you measure them more accurately!). The gray circle is just to show where the div has been placed and of course can be removed. You will want to look at the class naming and also the font sizes for the final production version.
There are more accurate ways of making text stay inside a circle but it's hoped this snippet will at least help get the basic position right.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
/* the image width and height (in any unit) */
--w: 23.6;
--h: 14.9;
/* the globe center and diameter */
--cx: 12.4;
--cy: 11;/*10.6*/
--doriginal: 6.5;
/* we need the actual width of the text to be a little less for portrait phones */
--d: calc(var(--doriginal) - 0.1);
}
/* for when the width can be accommodated but we have to crop the top and bottom */
#headercontainer {
width: 100vw;
height: auto;
margin-left: 0;
margin-top: calc(calc(100vh - calc(100vw * calc(var(--h) / var(--w)))) / 2);
position: fixed;
overflow:hidden;
}
#headerimg {
position: relative;
width: 100vw;
height: auto;
overflow: hidden;
}
.center {
/* these 3 lines are here just to demonstrate where the text is going. Remove when have a real image */
background-color: gray;
border-style: solid;
border-radius: 50%;
position: absolute;
width: calc(calc(var(--d) / var(--w)) * 100%);
line-height: calc(calc(var(--d) / var(--w)) * 100vw);
height: calc(calc(var(--d) / var(--h)) * 100%);
text-align: center;
left: calc(calc(calc(var(--cx) - calc(var(--d) / 2)) /var(--w)) * 100%);
top: calc(calc(calc(calc(var(--cy) - calc(var(--d) / 1)) /var(--h)) * 100%));
}
.center h1 {
margin: auto;
/*font-size: 1.5em;*/
font-size: 4vmin;
font-weight: normal;
text-transform: uppercase;
/* font-family: $body-font; */
font-family: Arial, Helvetica, 'sans serif';
color: white;
text-shadow: 0px 5px 5px rgba(0, 0, 0, 0.4);
letter-spacing: .5px;
/* for vertical centering */
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
/* for when the height can be accommodated but we have to crop the sides */
#media screen and (max-aspect-ratio: 236/149) {/* rumour has it that you cant use variables in aspect ratios - is that true? */
#headercontainer, #headerimg {
height: 100vh;
width: auto;
top: 0;
}
#headercontainer {
margin-left: calc(calc(100vw - calc(100vh * calc(var(--w) / var(--h)))) / 2);
margin-top: 0;
}
.center {
padding: 0 20px 0 20px;/* to stop text flowing slightly out on the right on narrow portrait devices */
line-height: calc(calc(var(--d) / var(--h)) * 100vh);
}
}
<div style="overflow:hidden;">
<div id="headercontainer">
<img id="headerimg" src="https://i.stack.imgur.com/kcTtE.png" />
<div class="center"><h1>your industry link to trusted global market places and technologies</h1></div>
</div>
</div>
Here is the original answer to set the scene:
This is not a complete answer but it is too long for a sensible comment.
We need to ascertain exactly what is to happen on different device aspect ratios.
I've been through all the devices offered in Chrome dev tools and all can include the complete globe (just, in some cases) which is a good start, but the text is going to have to flow outside it in different ways. Could you look at these two images and say what you want the text to look like in each case.
Various things to be decided - what is the minimum font size that is acceptable, where the globe takes up most of the width is the text to have several more lines and flow out of the globe vertically, if so is it equally on both top and bottom, if the globe sits well within the viewport left and right what it the maximum width of the text box to be?

How to do a tumblr like grid in HTML/CSS

After spending all day long trying to found how to make a nice tumblr-like grid for my website, I'm posting here to find help.
here's the page: http://alexis.gargaloni.free.fr/main.html
In order to access to my project there's a grid of images displayed. At the moment it looks OK, but now that I need to add something new, it starts to look really bad. screenshot
As you can see, there's a white gap. I've tried many things and there's every time a gap (even when it's not supposed to be there).
Here's an example of what I want to achieve: http://alexgargaloni.tumblr.com
here's my HTML code (included filter):
<div id="myBtnContainer">
<button class="btn active" onclick="filterSelection('all')"> TOUT</button>
<button class="btn" onclick="filterSelection('imprime')"> IMPRIMÉ</button>
<button class="btn" onclick="filterSelection('digital')"> NUMERIQUE</button>
<button class="btn" onclick="filterSelection('picture')"> PHOTO</button>
</div>
<div class="pic"> <img class="filterDiv picture" src="images/ego5-4.jpg" alt="ego graphique" style="width:40% "> <img class="filterDiv imprime" src="images/ihp1_1.jpg" alt="affiches pour l'institut henri poincaré" style="width:25%"> <img class="filterDiv imprime" src="images/jpogat17.png" alt="portes ouvertes lycée du gué à tresmes 2017" style="width:25%"><img class="filterDiv imprime" src="images/detailbook1.jpg" style="width:35%"> <img class="expav filterDiv digital" src="images/expavstatic.png" alt="expérience de la durée" style="width:35%" ></div>
and CSS:
html {
position: relative;
min-height: 100%;
}
body {
background: white;
font-size: 20px;
font-family: Helvetica, Arial, sans-serif;
line-height: 25px;
margin: 0 0 900px; /* bottom = footer height */
padding: 25px;
}
.img{
margin: 20px;
}
/* FILTER */
.container {
overflow: hidden;
}
.filterDiv {
float:left;
/*color: #ffffff;*/
width: 100px;
line-height: 100px;
text-align: center;
margin: 13px;
display:none; /* Hidden by default */
}
.show {
display: block;
}
.pic
{
max-width: 100%;
}
I'm new to HTML and CSS, and I know that my code is not a 100% clean and it could be way more simplified, but I'm working on it.
Excuse my English, I'm French
Thank for your help!
EDIT #ben_fluleck
Thank you. There’s a problem with “height: 100%”, because it modifies the aspect ratio of my pic. If I change height and width with max-width and max-height, the white gap is back. I also need to keep “display: none” on. filterDiv to make the filter function work (something with javascript). And also I’m having a problem with filter now, it still works, but pics are not getting how they’re supposed to (before:picture after:picture, it's like elements filtered create a white space instead of disappearing). I’ve tried to do something with the tumblr html, but it didn’t seem to work. Simple things are super tricky to do… I really need something that trick the size itself like tumblr theme, because when I’ll ad new things on my website, I feel like it’s going to be a mess again.
Yes, my footer is not really well implemented, I’ve checked online a way to make it because it’s really tricky, and how I did was the only way I was able to make it work. Thank a lot for you help! We can see the footer later, for the moment I really need to focus on this grid
Sorry, the picture in my Css above should be pic since you have named them you can constrain the pics giving them a width property. I would remove the inline styling for width and use percentages in the Css.
I have constructed a fiddle you could expand upon but it should give you a better idea of the layout.
.container {
display: flex;
justify-content: center;
align-items: center;
}
.box {
flex: 1 1 auto;
color: white;
font-size: 50px;
text-align: center;
padding: 10px;
}
/* Colours for each box */
.box1 {
background: #1abc9c;
}
.box2 {
background: #3498db;
}
.box3 {
background: #9b59b6;
}
https://jsfiddle.net/sLjuLjoc/1/
Try these CSS Layouts they will get your pics in order
Flexbox or Grid
set a viewport height in the body and work of that
body{
background: white;
font-size: 20px;
font-family: Helvetica, Arial, sans-serif;
line-height: 25px;
/* margin: 0 0 900px; */
padding: 25px;
height: 100vh;
display: flex;
flex-direction: column;
}
.pic {
display: grid;
grid-template-columns: repeat(4,1fr);
grid-gap: 10px;
}
.filterDiv {
float: left;
width: 100%;
line-height: 100px;
text-align: center;
height: 100%; /*optional if you want them to be all the same same */
}
.show {
/* display: block; */
}

New To HTML/CSS Can't Get Divs In Header To Stay In Place And Keep Aspect Ratio With Window Resize

First of all, I apologise if most of this code is messed up. I've been trying to solve this problem and have made a number of adjustments without really knowing what I'm doing.
I'm totally new to html and CSS. I'm attempting to make a header, everything looks fine until I try to decrease the window size.
I wanted 2 images either side of the title. I made a div within the main container (.banner) div, and put the two separate images in there too.
As soon as I decrease the width the whole thing begins to squish down, the photos loose their width until they go distorted (they seem to keep their height), the main title text and the text below starts stacking vertically, so the 'Webpage' shoots below 'My First' etc and the righthand side image shoots down and left below everything.
How do I make it so everything keeps it's aspect ratio as the browser window decreases in size? I don't want them to be a fixed size with any size window, I want them to decrease in size with the window whilst holding their position and aspect ratio.
I hope that makes sense.
Thanks.
body {
margin: 0;
background-color: black;
}
.banner {
}
.banner-photo1 {
height: 250px;
width: 10%;
display: inline-block;
border-radius: 50%;
margin-top: 6%;
margin-left: 2%;
min-width: 10%;
}
.banner-photo2 {
height: 250px;
width: 10%;
display: inline-block;
border-radius: 50%;
margin-top: 6%;
margin-right: 2%;
min-width: 10%;
}
.banner-title {
height: 50%;
width: 75%;
color: #9649CB;
display: inline-block;
background-color: black;
margin-right: 0;
}
.banner h1 {
font-size: 150px;
padding: 0;
margin-top: 0;
text-align: center;
margin-bottom: 0;
}
.banner-title h2 {
font-size: 60px;
text-align: center;
margin-top: 0;
margin-bottom: 20px;
}
.banner-title p {
text-align: center;
margin-top: 0;
font-size: 40px
}
<!DOCTYPE html>
<html>
<head>
<title>Sample Website</title>
</head>
<body>
<div class="banner">
<img class="banner-photo1"
src="https://www.startuptalks.in/wp-content/uploads/2016/04/HTML.jpg">
<div class="banner-title">
<h1>My First Webpage</h1>
<h2>Made With HTML & CSS</h2>
<p>This Is A Sample Header</p>
</div>
<img class="banner-photo2"
src="http://icons.iconarchive.com/icons/graphics-
vibe/developer/256/css-icon.png">
</div>
</body>
</html>
That's because when you use % for an element, you are actually setting this element to cover a percentage of the container it is inside.
So to get that fixed you can use min-width: number px;
You would be doing yourself a favor if you learn Bootstrap as it saves time in my experience
https://www.w3schools.com/bootstrap/
Responsive web development
It is called responsive because of your website response differently to different kind of screen sizes.
To keep it dynamic within different window sizes, e.g. desktop, mobile, you can use #media.
You can read more of it from w3school. https://www.w3schools.com/css/css_rwd_mediaqueries.asp
Basically, we are setting up different styles for different windows. Take an example, for a mobile phone, we do not want it to be too massive, but simply readable and does not strain the eyes. Thus we set the condition:
"If the size is less than so and so, reduce the font size to X. Maybe add some other extra features while you're into it."
Here is a solution for you:
#media only screen and (max-width: 768px) {
/* For mobile phones: */
.banner-title {
width: 60% !important;
display: inline-block;
}
.banner-title h1{
font-size: 60px !important;
padding: 0;
margin-top: 0;
text-align: center;
margin-bottom: 0;
}
.banner-photo1 {
width: 20%;
height: auto !important;
display: inline-block;
margin: 0;
padding: 0;
}
.banner-photo2 {
width: 20%;
height: auto !important;
display: inline-block;
margin: 0;
padding: 0;
}
}
https://jsfiddle.net/8a8pLsfz/
To avoid stretching of the images, I suggest you set the condition of your image per one rule, e.g., only by width, or by height. Avoid both, unless your fixing it such as 40px by 40px;
Puting condition such as percentage % width and pixel px height will tear it apart because of different screen sizes.
Making use of grid
One good thing for you to know is how to measure the width of the screen using percentage:
[Image source from http://bootstrap-sass.happyfuncorp.com]
This concept is applied in Bootstrap which you could try, but it is good to see the code how they actually do it. Check out BOOTSTRAP CSS and CTRL + F col-md-3 and see how they do it.
Where to from here
You can check out how to develop a mobile responsive website at W3school
https://www.w3schools.com/css/css_rwd_mediaqueries.asp
And later, you will understand how it is being used in bootstrap, and why bootstrap increases your work speed with a lot of useful styles.

Adjusting image size for smaller sized screens

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).