Adding non-centered text shifts centered elements [duplicate]

Adding non-centered text shifts centered elements [duplicate]



This question already has an answer here:



After putting a centered header, I add a non-centered output with JS. After the output is produced, the header shifts a bit left. What can be done to tackle this problem?




let output = ;

function spit()
for (let i = 0; i < 100; i++)
output.push(i);

document.getElementById("output").innerHTML =
output.join("<br>");


.header
background-color: lightgray;
border: none;
color: black;
padding: 17px 25px;
display: block;
text-align: center;
font-size: 36px;
width: 100%;
margin-left: auto;
margin-right: auto;


<h2 id="dictName" class="header">
Testing Page
</h2>

<button style="font-size:20pt;height:35pt" onclick="spit()">
Press me!
</button>

<p id="output">
</p>



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





@RachelGallen Thanks for the editing! I haven't mastered snippets yet.
– Eugene Barsky
Sep 5 '18 at 20:55





It looks like it is still being centered. It just shifts to the left because the scroll bar is added after the button is pressed.
– MichaelvE
Sep 5 '18 at 20:56






@EugeneBarsky the snippet icon is to the right of the picture icon; you can enter your code here when you're composing your question - just for future reference :)
– Rachel Gallen
Sep 5 '18 at 20:58





@RachelGallen Thanks, I'll try to use it, it's very convenient.
– Eugene Barsky
Sep 5 '18 at 20:59





@MichaelvE Yes, I think that's the reason, but it looks ugly, so what can be done to avoid this behaviour?
– Eugene Barsky
Sep 5 '18 at 21:01




4 Answers
4



One crazy solution might be to set you body height to the view port height, that way you start off with a scroll, avoiding the shift when the button gets pressed.




let output = ;

function spit()
for (let i = 0; i < 100; i++)
output.push(i);

document.getElementById("output").innerHTML =
output.join("<br>");


body
min-height: 100vh;


.header
background-color: lightgray;
border: none;
color: black;
padding: 17px 25px;
display: block;
text-align: center;
font-size: 36px;
width: 100%;
margin-left: auto;
margin-right: auto;


<h2 id="dictName" class="header">
Testing Page
</h2>

<button style="font-size:20pt;height:35pt" onclick="spit()">
Press me!
</button>

<p id="output">
</p>



I added a universal margin:0; padding:0; to your css code. The code did seem to be centered but I think the margin of -50 (that's being created by the auto margin ) is throwing off the look.




let output = ;

function spit()
for (let i = 0; i < 100; i++)
output.push(i);

document.getElementById("output").innerHTML =
output.join("<br>");


*
margin: 0px; padding:0px;


button
/*margin-left:15px;*/
margin-top:7px;
font-size: 20pt;
height: 35pt;


.header
background-color: lightgray;
/* border: 15px solid white;*/ /*use the commented props if you still want the "indented" effect" */
color: black;
padding: 17px 25px;
display: block;
text-align: center;
font-size: 36px;
width: 100%;
margin-right:auto;
margin-left:auto;


<h2 id="dictName" class="header">
Testing Page
</h2>

<button onclick="spit()">
Press me!
</button>

<p id="output">
</p>





this does affect the padding on the header, but you could use border or outline as a solution to that e.g. border: 15px solid white;
– Rachel Gallen
Sep 5 '18 at 21:13




IF you don't want the h1 to shift due to the scrollbar, you would have to calculate, using css calc() (and maybe some other things too), 50vw - (widthOfH1/2). This works because the vw unit (viewport width) is not affected by the scrollbar.



One way for the scrollbar to not affect the centering of the h1 would be to use JQuery.


$(#dictName).style.marginLeft = 'calc(50vw -'+(this.width/2)+'px)';



I haven't tested this so I'm not 100% sure if it will work, but please tell me if it does or doesn't. You may need to rerun this code when the button is pressed.



After some googling it seems I found the easiest way to solve this problem here, on SO:



How to prevent scrollbar from repositioning web page?


html
overflow-y: scroll;



Probably, the question should be closed as duplicate, but I don't have enough reputation to do it.





how would that solve the problem? the initial look would still be off, and the user would have to scroll over?
– Rachel Gallen
Sep 5 '18 at 21:24





@RachelGallen I've added these 3 lines to my code, and the header doesn't shift anymore. I'm a total newbie to html/css, so I can't explain, how it works, but my problem is solved.
– Eugene Barsky
Sep 5 '18 at 21:27





It creates a scrollbar though (so it pushes it over without shifting)...So the position would be still off? Still, if it works for you.. all good...
– Rachel Gallen
Sep 5 '18 at 21:30





@RachelGallen So is the solution with * margin: 0px; padding:0px; more correct in your opinion?
– Eugene Barsky
Sep 5 '18 at 21:37


* margin: 0px; padding:0px;





That's what I would use, to prevent overflow. You can still get the indented effect on the header by adding a white border. But it's up to you.
– Rachel Gallen
Sep 5 '18 at 21:40


Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)