I have this code:
<pre><code>
<!-- some code -->
</code></pre>
and I'm center it with flexbox. But when the screen is small the code tag go outside the pre-tag. Here is my code:
pre {
width: 100%;
border: 1px solid black;
display: flex;
justify-content: center;
min-width: 100%;
box-sizing: border-box;
}
code {
display: inline-block;
}
https://codepen.io/jcubic/pen/zYEOovQ
How can I make the pre tag, to always be the same as code, not 100% max?
I've tried to use float: left
on the flexbox element but I think that float is ignored when using flexbox.
This is probably a common problem, but I was not able to find the question on SO.
CodePudding user response:
Add white-space: break-spaces;
to your <code>
rules:
pre {
border: 1px solid black;
float: left;
justify-content: center;
box-sizing: border-box;
}
code {
display: inline-block;
white-space: break-spaces;
}
<pre><code>
<!DOCTYPE html>
<html lang="pl-PL">
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="keywords" content="">
<title>Tytuł strony</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<p>To jest paragraf</p>
</body>
</html>
</code></pre>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
To shrink the <pre>
to match the code width, remove the width
and min-width
settings.
CodePudding user response:
You can work with css overflow:auto.
div.scrolli {
overflow: auto;
white-space: nowrap;
border: 1px solid black;
}
<div class="scrolli">
<pre>
<code>
<!DOCTYPE html><html lang="pl-PL"> <head> <meta charset="utf-8"> <meta name="description" content=""> <meta name="keywords" content=""> <title>Tytuł strony</title> <link rel="stylesheet" href="style.css"> </head> <body> <p>To jest paragraf</p> </body></html>
</code>
</pre>
</div>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>