How to center "text-align: left" when wrapping text? - html

I am looking for a way in which my title text is always centered but letting it wrap on the left instead of the center, here's my markup so far:
.title-container {
height: 100vh;
width: 100vw;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
background-image: linear-gradient(#900, #990);
}
.title-container__text {
flex: 0 1 auto;
font-size: 80px;
color: #ddf;
}
<div class="title-container">
<h1 class="title-container__text">Gran Teatro Sara</h1>
</div>
This maintains the text centered up until the point where the screen size is too small and the text has to wrap, once it wraps the whole text aligns to the left and leaves a big space on the right... I would like to maintain the text wrapping like this, but to have equal space both on the left and the right.
I hope I made myself clear.
I'll explain myself with some visuals, the current behavior of the centering and wrapping goes like so:
The above example works just fine when viewing from big screen sizes
When the screen gets smaller the text wraps as I want, but the text stop being centered. My desired outcome goes like so:

One solution could be to add gutters on either side with flex: 1 and a max-width of your choosing – pixels or percentage, as in this snippet:
.title-container {
height: 100vh;
width: 100vw;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
flex-direction: row;
background-image: linear-gradient(#900, #990);
}
.title-container__text {
flex: 1;
font-size: 80px;
color: #ddf;
}
.gutter {
flex: 1;
max-width: 10%
}
<div class="title-container">
<div class="gutter"></div>
<div class="title-container__text">Gran Teatro Sara</div>
<div class="gutter"></div>
</div>

Add "text-align: center;" to your .title-container CSS.
.title-container {
height: 100vh;
width: 100vw;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
text-align: center;
background-image: linear-gradient(#900, #990);
}
.title-container__text {
flex: 0 1 auto;
font-size: 80px;
color: #ddf;
}
<div class="title-container">
<h1 class="title-container__text">Gran Teatro Sara</h1>
</div>

Related

How to make elements inside div same height and width even when contents are different?

I'm trying to create "cards" for my team. However, the look weird because the dimensions are different.
My set up looks like this:
<div class="card-wrapper">
<div class="card">Card1</div>
<div class="card">Card2</div>
</div>
My css looks like this now:
.card-wrapper
{
display: flex;
align-items: center;
align-content: center;
flex-direction: row;
}
.card
{
width: 30rem;
background: #000;
align-items: center;
justify-content: center;
}
I can't seem to find a way to make the height the same for all cards. I saw here on stackoverflow that I should:
.card-wrapper {
display:flex;
}
.card {
flex:1;
}
But that does not seem to work. Any suggestions?
Here is a picture of what it currently looks like:
CARDS NOW
i think is because you use
align-items: center
just remove it should have same height for these two card
https://stackblitz.com/edit/typescript-sevm5b
Setting a flex-basis would be helpful. Moreover you don't need the align-items and align-content for this.
* { box-sizing: border-box; }
.container {
display: flex;
flex-flow: row wrap;
justify-content: space-between;
height:20vh
}
.card {
box-shadow: 0 0 4px rgba(0,0,0,0.4);
flex: 0 1 33.33%;
padding: 15px; /* gutter width */
}
<div class="container">
<div class="card"><br></div>
<div class="card"><br></div>
<div class="card"><br><br><br><br><br></div>
</div>

Center a paragraph element when text wraps without centering the text using flex

Before this gets down-voted into oblivion, let me say I've searched through the numerous SO questions that looked similar to this one, but none that I found addressed my issue.
I'm using flexbox to center some <p> tags both horizontally and vertically within an element. I don't want to center the text within the <p> element, just center the <p> elements themselves.
.app-outer {
display: flex;
flex-direction: column;
}
.app {
display: flex;
flex-direction: column;
height: 100%;
justify-content: center;
align-items: center;
}
.app p {
display: inline-block;
}
.title-bar {
background-color: #202225;
color: #72767D;
font-weight: bold;
padding: 0 0 2px 8px;
display: flex;
flex-direction: row;
width: 100%;
}
<div class="app-outer">
<div class="title-bar">
<span class="draggable">Skipwars</span>
<span class="btns">
<button id="btn-minimize" tabindex="-1">-</button><!--
--><button id="btn-close" tabindex="-1">×</button>
</span>
</div>
<div class="app">
<p>Add a browser source pointed at <!--http://localhost:3333/--></p>
<p>
Optional parameter <code style="display:inline">threshold=n</code>. ex: http://localhost:3333/?threshold=4 (default 8)
</p>
</div>
</div>
My app is 300px wide (I'm using electron).
As you can see, if the paragraph doesn't have enough text for multiple lines, it works fine. If it does, the paragraph expands to the width of .app, and the text is left-justified.
This is what I'm looking for:
I thought that setting the paragraphs' display to inline-block would do the trick, but it doesn't.
It's working as expected, but since the <p> tags are the same width as the viewport, they are flush to the left when the vp is too small, and in this case the url is so long that it wraps to the next line and leaves a space on the first, making it appear that it is not centered. I added some horizontal padding to the <p> tags to better illustrate:
.app-outer {
display: flex;
flex-direction: column;
}
.app {
display: flex;
flex-direction: column;
height: 100%;
justify-content: center;
align-items: center;
}
.app p {
display: inline-block;
padding: 0 20px;
}
.title-bar {
background-color: #202225;
color: #72767D;
font-weight: bold;
padding: 0 0 2px 8px;
display: flex;
flex-direction: row;
width: 100%;
}
<div class="app-outer">
<div class="title-bar">
<span class="draggable">Skipwars</span>
<span class="btns">
<button id="btn-minimize" tabindex="-1">-</button><!--
--><button id="btn-close" tabindex="-1">×</button>
</span>
</div>
<div class="app">
<p>Add a browser source pointed at
<!--http://localhost:3333/--></p>
<p>
Optional parameter <code style="display:inline">threshold=n</code>. ex: http://localhost:3333/?threshold=4 (default 8)
</p>
</div>
</div>

How to align-items: center AND align-self: stretch?

I have an element I'd like to be (cross-axis) centered but also 'grow' to a nominal size even with too-little content, BUT ALSO 'shrink' when the width of the page becomes smaller than 350px wide.
HTML
<div class="parent">
<div class="child">
Some content
</div>
</div>
SCSS
.parent {
display: flex;
flex-direction: column;
align-items: center;
.child {
max-width: 350px;
align-self: stretch;
}
}
Adding align-self: stretch; to .child does the job of making it 350px wide, but it seems to negate the align-items: center; in .parent
Is there a way to do this in CSS that I'm missing? Please note that the element can't just be 350px wide all the time - it must also respond to horizontal page resizing as it does in the example fiddle.
Fiddle: https://jsfiddle.net/1uqpxn8L/1/
UPDATED
I think you should use justify-content to h-align child to center.
Please note, when you apply display: flex property to parent, you should apply flex property to child.
.parent {
background: yellow;
display: flex;
flex-direction: column;
align-items: center;
}
.parent .child {
background: black;
color: white;
text-align: center;
flex: 1 1 auto;
width: 100%;
max-width: 350px;
}
<div class="parent">
<div class="child">
I should be 350px wide
<br> and centered in the yellow
<br> unless the page gets smaller,
<br> in which case I should have
<br> 10px padding on either side.
</div>
</div>
Please see the result here, hope this is what you mean: https://jsfiddle.net/1uqpxn8L/11/
You can do something like this.
HTML
<div class="parent">
<div class="child">
Some content
</div>
</div>
SCSS
.parent {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 10px;
.child {
width: 350px;
#media(max-width: 350px) {
width: 100%;
}
}
}
.parent {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 10px;
background-color: red;
}
.child {
width: 350px;
background-color: yellow;
}
#media(max-width: 350px) {
.child { width: 100%; }
}
<div class="parent">
<div class="child">
Some content
</div>
</div>
So whats happening is I'm using a media query to change the width of the child depending on the width of the browser.
You just need to remove the flex-direction property. Then it's working as you expected. But there will be a problem if you want to display children elements as column manner. The shrinking problem occurs with the flex-direction property or flex-flow:column values as I checked.

Right Align an element INSIDE FLEXBOX DIV?

If we have this structure of HTML
<div id="main">
<div id="sub-1"><div>
<div id="sub-3"><div>
<div id="sub-3"><div>
</div>
And the SASS styles for that structure as:
#main
display: flex
flex-direction: column
justify-content: center
align-items: center
align-content: center
width: 1200px
#sub-1, #sub-2, #sub-3
width: 100px
height: 100px
With that structure we will have a column of 3 squares perfectly center-aligned. But what if we want to move #sub-1 to the left and #sub-3 to the right? Is there a way to do that?
Thank you very much!
You can use the property align-self like this:
#main {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
}
#sub-1, #sub-2, #sub-3 {
background:red;
width: 50px;
height: 50px
}
#sub-1 {
align-self:flex-start;
}
#sub-3 {
align-self:flex-end;
}
<div id="main">
<div id="sub-1"></div>
<div id="sub-2"></div>
<div id="sub-3"></div>
</div>

How to shrink div to fit word-wrapped text

I have a flex container with a picture and text beside it. This container is centered on the page. When I shrink the page causing the text to word-wrap, the container no longer looks centered because the text div doesn't shrink down to the size of the word wrapped text.
Is it possible to shrink the text div when word wrap occurs?
See JSFiddle here.
It might be tough to see what I'm talking about with the snippet because you might not be able to shrink the page enough to see the wrap happen. Better off looking at the jsfiddle.
.container,
.content {
display: flex;
justify-content: center;
}
.picture {
height: 130px;
width: 130px;
background-color: blue;
}
.text {
display: flex;
flex-direction: column;
justify-content: center;
margin-left: 10px;
}
.name {
font-size: 22px;
}
<div class="container">
<div class="content">
<div class="picture"></div>
<div class="text">
<div class="name">John Jacobjingleheimer</div>
</div>
</div>
</div>
I'm not sure this is the answer you're looking for but from reading what you wrote I think you're looking for
margin-left: auto;
margin-right: auto;
The above margin-left: and margin-right: with the auto value will cause the browser window to automatically calculate the left and right margin of the
.text {
display: flex;
margin-left: auto;
margin-right: auto;
flex-direction: column;
justify-content: center;
}