SOLVED - Why is this bootstrap card misaligned? - html

The image and text of this bootstrap card are supposed to be side by side, with the combo title + text centralized in relation to the image. But they are somewhat above the image.
Image of how it looks:
HTML:
<div class="card bg-transparent border-0 text-white mb-3" >
<div class="row g-0">
<div class="col-md-6">
<img src="../assets/images/cp/sobre-cid-1.jpg" class="img-fluid rounded-start img-esq" alt="cid-1">
</div>
<div class="col-md-6">
<div class="card-body">
<h3 class="card-title">TITLE</h3>
<p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
</div>
CSS:
.img-fluid.rounded-start {
height: calc(120px + 10vw);
width: 90vw;
object-fit: cover;
}
.img-esq {
border-radius: 0px 30px 30px 0px;
}
.img-dir {
border-radius: 30px 0px 0px 30px;
}
.card-title {
font-family: 'Martel';
font-size: 22px;
padding: 20px;
text-align: left;
font-weight: 600;
}
.card-title-right {
font-family: 'Martel';
font-size: 22px;
padding: 20px;
text-align: right;
font-weight: 600;
}
.card-text {
font-family: 'Martel';
font-size: 14px;
padding-left: 20px;
padding-right: 30px;
text-align: left;
}
.card-text-right {
font-family: 'Martel';
font-size: 14px;
padding-left: 30px;
padding-right: 20px;
text-align: right;
}
Additional info: The card is inside a div that gets the entire page, this is it's CSS:
.bg {
background-image: url(../images/cp/fundo.png);
background-position: center;
background-repeat: repeat-y;
background-size: cover;
margin-top: 0px;
text-align: center;
color: white;
display: flex;
flex-direction: column;
justify-content: center;
}
I mostly looked for margins and paddings. I tried changing padding-top and margin-top to zero, I even looked for this in the item above the bootstrap card, but nothing helped, there was only a br tag between the item above the card and the card itself. I also looked for a margin in the card and everything seems right.
EDIT 1:
I added to .row a display: flex and align-items:center, it is not perfectly centered but maybe I can use it, but there's still a big space in between each card:
EDIT 2:
I added to .row a margin-top: -80px and now the space between cards is good. Not the perfect solution I think but works for now.
To make the text vertically centered in relation to the image, I added margin-top: 60px to the cards' titles, and it pushed them down. Again, not perfect but works for now.
EDIT 3:
I fixed it. The problem was in a part of the CSS code I didn't know influenced the whole page. It was like this:
img {
width: 70%;
height: auto;
margin-top: 15%;
}
I thought it would only influence the class that was above it. Turns out, it influenced all the img tags.
I found out by deleting part by part of the CSS code in Codepen.

The problem may be caused by the calc() function in the height property for the img-fluid.rounded-start class. This function calculates the height of the image based on the size of the viewport, which could be causing the image to be larger or smaller than expected. Try to set fixed values to it.
Also, you can try and use align-items: center in the .bg class, this should align the elements at the center of the div.

Related

Rotate text 90 degrees and vertically center next to wrapped inline text

I am trying to rotate text 90 degrees and vertically center it next to inline wrapped text. I need to keep the text inline so I can left-align the text itself, then center the whole thing when the text wraps.
It seems to work fine in Chrome when the text is on one line and when wrapped. In Firefox it seems to work fine when the text is on one line, but when wrapped, it does not vertically center the rotated text.
It seems like Firefox is not recognizing the height of the wrapped text.
Here is a fiddle showing what I'm working with. https://jsfiddle.net/hemmieweizen/7t5gh91z/12/ Notice on Firefox when you decrease the width of the output and the text wraps, the vertical text does not continue to center.
.title-container {
font-size: 70px;
position: relative;
}
.title-container .title {
position: relative;
}
.title-container .pre-title {
position: absolute;
bottom: 50%;
left: -55px;
transform: rotate(-90deg);
transform-origin: center bottom;
height: 22px;
border-bottom: 1px solid #000;
font-size: 16px;
text-transform: uppercase;
}
<h2 class="title-container m-b-0">
<span class="title">
<span class="pre-title h6 m-b-0">Your Agent</span>
Charles Smith
</span>
</h2>
Try to use a layout like flex; make both <span>Charles Smith</span> and <span>Your Agent</span> direct children of <h2> rather than nested children. Remove the hard-coded position rules from these elements, and let the flexbox manage them for you. You can still refine their positions with mere margin and padding.
#import url("https://d133rs42u5tbg.cloudfront.net/hero/assets/css/kv-custom-colors.css");
.title-container {
font-size: 70px;
position: relative;
display: flex; /* ADDED THIS NEW RULE */
flex-direction: row; /* ADDED THIS NEW RULE */
}
.title-container .pre-title { /* DID NOT CHANGE ANYTHING HERE */
position: absolute;
bottom: 50%;
left: -55px;
transform: rotate(-90deg);
transform-origin: center bottom;
height: 22px;
border-bottom: 1px solid #000;
font-size: 16px;
text-transform: uppercase;
}
.title-container .title {
/* REMOVED OLD POSITION RULE */
}
<section id="custom-about" class="position-relative p-y-3">
<div class="container p-y-3">
<div class="row">
<div class="col-xs-12 col-md-6">
<h2 class="title-container m-b-0">
<!-- CHANGED HIERARCHICAL STRUCTURE HERE -->
<span class="pre-title h6 font-sans m-b-0">Your Agent</span>
<span class="title">Charles Smith</span>
</h2>
</div>
<div class="col-xs-12 col-md-6 m-t-2">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<div class="col-xs-12 col-md-6 col-md-offset-6">
<div class="text-center">
<a class="btn btn-white" href="contact">Contact</a>
</div>
</div>
</div>
</div>
</section>

Wrapping and shrinking text if needed to fit button

For months this is the only problem I haven't been able to solve. I've hired multiple people on Freelancer to help and two gave up, and another said it was impossible.
I'm simply trying to get the text of my buttons to stay within the boundaries of the button. Right now it looks like this.
All I need to do is wrap the text if it's too long to fit the horizontal boundaries of the button, and shrink the text if it's too big to fit within the boundaries of the button. Like this:
I've tried:
Using Fitty, FitText, and other libraries which don't work at all. They'll sometimes make the text too big to fit within the boundaries of the button, and sometimes they'll make all of my text small unnecessarily.
Creating my own function by looking at clientWidth and clientHeight, and shrinking the font as necessary. When I do that, clientWidth of my elements stay the same regardless of the actual size, I've also used getComputedStyle which doesn't seem to calculate properly either.
Paying people. Again multiple people have given up, and I've spent months trying to solve this with no success.
I've created a codepen with a minimally reproducible example showing the problem.
https://codepen.io/TheNomadicAspie/pen/dyRLrej
And here is the code (I've removed all of the unnecessary code, but left in the parent divs of the buttons in case they are affecting whatever is keeping the libraries/my functions/other people from being able to do this).
<div id="screen" class="screen">
<div id="display" class="display">
<div id="bottom_bar" class="bottom-bar">
<div id="bottom_display" class="bottom-display">
<div id="answers_display" class="answers-display">
<div id="answer_container_1" class="answer-button-1">
<div id="answer_checkbox_1" class="checkbox">
</div>
<div id="answer_button_container_1" class="answer-button-container">
<button id="answer_button_1" class="button lower-button">
</button>
</div>
</div>
<div id="answer_container_2" class="answer-button-2">
<div id="answer_checkbox_2" class="checkbox">
</div>
<div id="answer_button_container_2" class="answer-button-container">
<button id="answer_button_2" class="button lower-button">
</button>
</div>
</div>
<div id="answer_container_3" class="answer-button-3">
<div id="answer_checkbox_3" class="checkbox">
</div>
<div id="answer_button_container_3" class="answer-button-container">
<button id="answer_button_3" class="button lower-button">
</button>
</div>
</div>
<div id="answer_container_4" class="answer-button-4">
<div id="answer_checkbox_4" class="checkbox">
</div>
<div id="answer_button_container_4" class="answer-button-container">
<button id="answer_button_4" class="button lower-button">
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<link href="styles.css" rel="stylesheet">
* {
outline: none;
opacity: 1;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html,
body {
position: fixed;
height: 100%;
background-color: #26004b;
font-size: 2vh;
margin: 0 auto;
font-family: open_sans;
}
.screen {
position: absolute;
height: 100%;
}
.title {
grid-column: 2/3;
position: relative;
color: #f5f5f5;
height: 100%;
width: 100%;
margin: 0 auto;
text-align: center;
justify-content: center;
align-items: center;
font-family: hack;
font-size: clamp(2vw, 8vw, 10vh);
display: flex;
top: 0%;
}
.display {
position: relative;
height: 86.286%;
width: 100vw;
}
.bottom-bar {
display: grid;
grid-template-columns: 38.2% 61.8%;
position: relative;
height: 38.2%;
width: 100vw;
bottom: 0%;
}
.character {
grid-column: 1/2;
position: relative;
background-size: contain;
background-repeat: no-repeat;
background-position-y: bottom;
background-position-x: center;
}
.bottom-display {
grid-column: 2/3;
position: relative;
height: 100%;
padding-right: 5vw;
padding-top: 1%;
padding-bottom: 3%;
}
.answers-display {
display: grid;
gap: 1%;
max-height: 99%;
grid-template-columns: 100%;
grid-template-rows: 25% 25% 25% 25%;
height: 100%;
}
.answer-button-1 {
position: relative;
grid-row: 1/2;
display: grid;
grid-template-columns: 20% 80%;
height: 98%;
}
.answer-button-2 {
position: relative;
grid-row: 2/3;
display: grid;
grid-template-columns: 20% 80%;
height: 98%;
}
.answer-button-3 {
position: relative;
grid-row: 3/4;
display: grid;
grid-template-columns: 20% 80%;
height: 99%;
}
.answer-button-4 {
position: relative;
grid-row: 4/5;
display: grid;
grid-template-columns: 20% 80%;
height: 99%;
width: 100%;
}
.checkbox {
grid-column: 1/2;
height: 100%;
width: 100%;
overflow: hidden;
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
object-fit: contain;
}
.answer-button-container {
grid-column: 2/3;
padding-left: 2%;
height: 100%;
}
.answer-button-container button {
width: 100%;
padding-left: 1%;
padding-right: 1%;
padding-top: 2%;
padding-bottom: 2%;
}
.button {
display: block;
position: relative;
height: 100%;
width: 100%;
background-color: black; /*Button Color*/
color: #f5f5f5;
font-family: open_sans;
font-size: 1.5rem;
border-radius: 20px;
text-decoration: none;
box-shadow: 0.1em 0.2em black;
cursor: pointer;
}
.lower-button {
white-space: nowrap;
}
const answer_button_1 = document.getElementById("answer_button_1");
const answer_button_2 = document.getElementById("answer_button_2");
const answer_button_3 = document.getElementById("answer_button_3");
const answer_button_4 = document.getElementById("answer_button_4");
answer_button_1.innerText = "This is a really really really really really really really really really really really really really really really really really really really really really long test answer";
answer_button_2.innerText = "This is a pretty long test answer but not as long as the other one";
answer_button_3.innerText = "This is a fairly short test answer";
answer_button_4.innerText = "Really short answer";
Edit: To clarify, I need the button to not get larger to fit the text, I need the text to get smaller to fit the button.
This here does the job by wrapping the text and making the box larger, it does however mess up the spacing between the boxes. Took me a few minutes, hope it helps :)
#answer_button_1 {
height: auto;
max-width:30wv;
hyphens: auto;
white-space: normal;
}
The Spacing between the buttons can be fixed by removing the following lines from .answers-display:
grid-template-rows: 25% 25% 25% 25%;
height: 100%;
Hmmmm. Yes, this is a tricky one. Using this article as inspiration, I was able to come up with the following solution. The technique used is to start with a really small font size (I have set minSize to 8 for this example) and test whether the text overflows its container; if the text does not overflow, increase the font size by a small amount (I've set step to 0.5) and re-test; then if the text overflows, revert to the previous font size.
Note, however, that it uses regular divs rather than buttons. The solution relies on a set of nested elements, which the button element does not support. Buttons also seem to have some built-in padding or sizing which is difficult to control. I suspect that Fitty and FitText don't work on buttons. I did try swapping the innermost divs with buttons in this snippet, and while it still works fairly well, it's more complicated and doesn't look as good. The only reason to prefer a button over a div is purely a semantic one, so I'd recommend sticking to using divs. Just add your click handler and off you go.
You can try different values for minSize, step and so on to see how that affects the result. Note that because I have used a minSize of 8, there comes a point where very long texts still overflow the button. Setting minSize to 0 avoids this -- the text fits on the button regardless of its length, but for some reason the text doesn't quite fill the button: the bottom padding appears larger. But your results may vary.
const isOverflown = ({ clientHeight, scrollHeight }) => scrollHeight > clientHeight
const resizeText = ({ element, elements, minSize = 10, maxSize = 512, step = 1, unit = 'px' }) => {
(elements || [element]).forEach(el => {
let i = minSize
let overflow = false
const parent = el.parentNode
while (!overflow && i < maxSize) {
el.style.fontSize = `${i}${unit}`
overflow = isOverflown(parent)
if (!overflow) i += step
}
// revert to last state where no overflow happened
el.style.fontSize = `${i - step}${unit}`
})
}
resizeText({
elements: document.querySelectorAll('.button>div>div'),
minSize: 8,
step: 0.5
})
body {
background: #33A;
font-family: sans-serif;
}
.button {
margin: 1em 0;
width: 300px;
height: 50px;
padding: 10px 10px 10px 15px;
color: #f5f5f5;
background-color: black;
border: 1px outset;
border-radius: 20px;
box-shadow: 0.1em 0.2em black;
cursor: pointer;
text-align: center;
}
.button>div {
width: 100%;
height: 100%;
}
<div class="button">
<div>
<div>
This text
</div>
</div>
</div>
<div class="button">
<div>
<div>
This Text is a bit longer
and should be wrapped correctly
</div>
</div>
</div>
<div class="button">
<div>
<div>
This text is the longest and should appear quite small.
This text is the longest and should appear quite small.
</div>
</div>
</div>
<div class="button">
<div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
</div>
</div>
<div class="button">
<div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
<div class="button">
<div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
It's a real pity that web standards do not currently provide a mechanism for automatically adjusting font size to fill a fixed size container. There are mechanisms to do it with images, so why not with text? I myself have had regular situations over the years where I have wished this were possible.
I am curious, though, as to why it is so important for you for the buttons to be a fixed size? Doesn't readability become a problem when there's a lot of text, and the font-size gets so small? Would it not be a better solution to simply allow the buttons to grow vertically to contain longer pieces of text? Or even truncate the text at a maximum number of characters or words, and add an ellipsis to indicate that truncation occurred?

Div with Two Columns Containing Vertically Centred Divs of Different Heights

Background
I have a div with two columns. In column A, there will be a div with an "About Me" section. This is height X. In column B will be another image of a map. This is height Y. X is not equal to Y.
I am making both columns (combined) = to 100vw. Each column is defined as 50%.
(I can calculate the height of the map in column B with the following math (which can be seen more clearly in the linked Jfiddle): Image is 350x600px. If 350px = 50%, then 600px = ~171%. 171 / 2 = 85.5% so the following code snippet should give the correct transform value:
margin-top: 85.5%;
transform: translateY(-50%);)
Objective
I would like the column height to adjust to the height of the tallest element. The tallest element will most likely always be in column B. I would like the div in column A to sit halfway down the page. However, if I should ever change the order and want to put something taller in column A than in column B, it would be really great to have a fallback so that the element in column B repositions itself to become vertically centred. (But I can live without that and do it manually should I need to.)
Problem
Column heights aren't behaving themselves. The height of column A (shown in dark green) is higher than that of column B, even though the element in A is shorter.
Jfiddle
http://jsfiddle.net/ubjo1s3y/28/
(With nice bright div colours)
Code
css:
.column {
margin: -5px 0px 30px 0px;
float: left;
width: 50%;
height: auto;
background-color: green;
}
.row:after {
background-color: pink;
content: "";
display: table;
clear: both;
height: 0;
}
#aboutmecontainer {
background-color: aqua;
width: 90%;
float: left;
padding: 0px 5% 0px 5%;
margin-top: 85.5%;
transform: translateY(-50%);
}
#facephoto {
background-color: red;
float: left;
width: 130px;
margin: 0px 0px 10px 50%;
transform: translateX(-50%);
}
#aboutmetext {
background-color: yellow;
float: left;
width: 100%;
height: auto;
}
#map {
background-color: yellowgreen;
float: left;
width: 90%;
padding: 0px 5% 0px 5%;
}
html:
<div class="row">
<div class="column">
<!-- container for round face photo -->
<div id="aboutmecontainer">
<div id="facephoto">
<!-- photo -->
<img src="http://via.placeholder.com/200x200" alt="Face" style="height: 100%; width: 100%; object-fit: contain;" />
</div>
<!-- container for text underneath face photo -->
<div id="aboutmetext"><h3 style="color: #000000;">About Me</h3><p></p><h5 style="color: #000000;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</h5>
</div>
</div>
</div>
<div class="column">
<!-- div for map to right of "about me" section -->
<div id="map">
<!-- map picture can be edited in photoshop to add new countries -->
<img src="http://via.placeholder.com/350x600" alt="Map" style="width: 100%; height: 100%; object-fit: contain;">
</div>
</div>
</div>
First of all, I would definitely recommend learning how to use flexbox.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox.
Additionally, using floats in this situation is going to ruin the flow of the dom. There are situations where using floats can work, but it removes the targeted element from the regular flow of the dom and will always cause you grief if you don't know how to use floats.
Here is a revised jsfiddle that does not use flex. You want to set .column's display to inline-block and put font-size: 0 on .row (removing white-space). And remove all of your floats.
JSFiddle
Have you looked into Flexbox? It drastically simplifies this old height problem.
See this fiddle :
http://jsfiddle.net/uy2pqbkL/
Everything else is the same except the new properties added to your .row class.
.row {
display:flex;
align-items:center;
}

Vertically align image with dynamic text

So I have a div, whose height I don't know initially. It has two children - an <img> on the left, and a <div> contains some content on the right. The image size is fixed, say 50 by 50. However, the content can be of any size. It may be even one line, in which case it becomes smaller in size compared to the photo. Either way, the parent div has to find out which one is bigger and grow vertically to accommodate both, while ensuring that both are properly centered (taking into account a vertical padding of 5 px).
Also imagine that the content won't wrap below the image, i.e it is a standalone element on the right side (as if floating right).
That is, if the content is say just one line, then the parent div should be of height 50+5*2 = 60px, the img on the left being centred at 5px from top, the content on the right (assume at a margin of 5 px from the left), which here is just one line, centred vertically.
Or say the content is so big that it is 100px high, then the parent div will be 100+5*2 = 110px high. The img will be on the left, at 30px from the top.
Can anyone help me fix the issue?
This is what I came up with: https://jsfiddle.net/fj77eobe/
.elem-option {
text-align: left;
white-space: nowrap;
width: 300px;
background: pink;
}
/* The ghost, nudged to maintain perfect centering */
.elem-option:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
/* The element to be centered, can also be of any width and height */
.elem-photo {
display: inline-block;
vertical-align: middle;
width: 40px;
height: 40px;
margin-left: 10px;
}
.elem-content {
display: inline-block;
margin-left: 5px;
width: 100px;
word-wrap: break-word;
overflow: none;
}
<div class='elem-option'>
<img src="https://img.zmtcdn.com/data/reviews_photos/b22/28f633be81fd340785c3af7f7858cb22_1463913069.jpg" class="elem-photo" />
<div class='elem-content'>
Amader ekhane ekjon er heavy khar chhilo...take paedo bole khepano hoto..toh se ajkal ei defense deyAmader ekhane ekjon er heavy khar chhilo...take paedo bole khepano hoto..toh se ajkal ei defense deyAmader ekhane ekjon er heavy khar chhilo...take paedo
bole khepano hoto..toh se ajkal ei defense deyAmader ekhane ekjon er heavy khar chhilo...take paedo bole khepano hoto..toh se ajkal ei defense dey
</div>
</div>
Try setting the container to display: flex; + align-items: center;
.elem-option {
display: flex;
align-items: center;
padding: 5px;
background: pink;
}
.elem-photo {
margin-right: 5px;
flex: 0 0 50px;
height: 50px;
}
<div class='elem-option'>
<img src="//unsplash.it/200" class="elem-photo" />
<div class='elem-content'>
Lorem ipsum dolor sit amet.
</div>
</div>
<br>
<div class='elem-option'>
<img src="//unsplash.it/200" class="elem-photo" />
<div class='elem-content'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident.
</div>
</div>

Issue Scaling Font to Screen Size

I have a simple page with a navbar and a homepage. The navbar is fixed and the homepage takes up 100% of the screen. The viewer then scrolls down from the homepage to view the rest of the web content.
I'm having an issue with the font not scaling when viewing on a mobile device or devices with smaller screen sizes. I believe this is due to me changing the navbar to take up 100% width and for the homepage to be taking up 100% height. The text under section1 scales correctly (the font gets bigger when the screen is smaller).
How can I have the homepage and the navbar increase in font?
h1{
text-shadow: 0px 4px 3px rgba(0,0,0,0.4),
0px 8px 13px rgba(0,0,0,0.1),
0px 18px 23px rgba(0,0,0,0.1);
}
html {
height: 100%;
min-height: 100%;
}
body {
background: #1A3742;
font-family: 'Source Sans Pro', sans-serif;
color: white;
margin: auto 100px;
height: 100%;
}
#header {
background: gray;
padding: 28px 0 26px;
position: fixed;
top: 0;
left: 0;
z-index: 1000;
width: 100%;
}
#top{
text-align: center;
height: 100%;
}
#home-content{
position: relative;
top: 50%;
transform: translateY(-50%);
}
a[href="#top"] {
margin-left:100px;
margin-right:50px;
vertical-align: middle;
text-decoration: none;
font-weight: bold;
}
a img{
vertical-align:middle;
}
.content {
margin-left:75px;
margin-top:25px;
overflow: hidden;
}
.content p{
margin-top: 0px;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header id="header">
Name
<a href="">
<img src="" alt="img" height="24" width="24">
</a>
</header>
<div id="top">
<div id = "home-content">
<h1>Top</h1>
<h2>Sub title</h2>
<p>
This text does not scale at all.
</p>
</div>
</div>
<div id="section1">
<h1>Section 1</h1>
<div class = "content">
<p>
This scales with the screen.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</div>
</body>
</html>
Here is an example on mobile that shows the top text not scaling, but the section1 scaling correctly.
That is a Galaxy S5 in google Chrome. The text in the homepage/top portion and navbar should be scaling similar to the way the section1 text does.
How can I fix it so everything scales to the screen?
First, none of it is scaling. It's applying browser defaults as you've not set any font-size in the css provided. You can test it in the web inspector (remember to reload the page after activating it).
You can use vh (view height) or or vw (view width) as percentage messure for the font.
use media queries to adjust according to screen size
ex:
#media screen and (max-width:1000px){
#top{
font-size : 40px; /change it to whatever you need/
}
}
change the max-width accordingly to manage perfectly for every screen
for more information on media queries
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries