getting a tad lost in trying to vertically align text - css

Display flex works great as the variable height columns all equal one another. I have not really used CSS for many years. So I do apologise if I have missed something obvious. Any help would be massively appreciated.
.row {
display: flex;
}
I followed some S-O posts to vertically align text, however, I cannot seem to replicate the shared success of others.
.row {
display: flex;
justify-content: center;
align-items: center;
}
Works however not as I intended, the individual divs bgs no longer stretch, to equal heights.
I read some further posts and css-tricks, stumbled across align-self stretch.
[class*='col-'] {
float: left;
padding-right: 28px;
border: 1px solid red;
align-self: stretch;
}
.row {
display: flex;
justify-content: center;
align-items: center;
}
This unfortunately seems to go back to my first image.
I read another post, highlighting the use of flex:1.
The columns jump around, in a manner, I am not accustomed to.
.row {
display: flex;
justify-content: center;
align-items: center;
}
[class*='col-'] {
float: left;
padding-right: 28px;
border: 1px solid red;
flex: 1;
}
If i remove the justify-content and align-items. That does not really help me either.
Here is my markup:
<div class='row'>
<div class='col-m-1 col-t-1 col-d-1' style='background-color:blueviolet;'>
<a href="" id='navigation_link'>☰</a>
</div>
<div class='col-m-1 col-t-2 col-d-2' style='background-color: blue;'>
<h1>Header</h1>
</div>
<div class='col-m-4 col-t-5 col-d-9 t-end' style='background-color:blueviolet;'>
<form>
<input type='search' placeholder='Search Today' minlength='100'>
<input type='submit' value='Search'>
</form>
</div>
</div>
Here is snapshot of my css
#media only screen and (min-width: 768px) {
h1 {
font-size: 2.4rem;
/* =24px */
}
.col-d-1 {
width: 8.33%;
}
.col-d-2 {
width: 16.66%;
}
.col-d-3 {
width: 25%
}
.col-d-4 {
width: 33.33%
}
.col-d-5 {
width: 41.66%
}
.col-d-6 {
width: 50%;
}
.col-d-7 {
width: 58.33%;
}
.col-d-8 {
width: 66.66%;
}
.col-d-9 {
width: 75%;
}
.col-d-10 {
width: 83.33%;
}
.col-d-11 {
width: 91.66%
}
.col-d-12 {
width: 100%;
}
[class*='col-'] {
float: left;
padding-right: 28px;
border: 1px solid red;
}
}
[class*='col-']:first-of-type {
padding-left: 16px;
}
[class*='col-']:last-of-type {
padding-right: 16px;
}
.row {
display: flex;
}
.row::after {
content: "";
clear: both;
display: table;
}

For posterity, I managed to resolve this problem with the help of this S-O post.
You need to add display flex to the child items; then centre. Thus adding the below snippet to my original post remedies my issue.
.row [class*='col-'] {
display: flex;
align-items: center;
}

Related

Using css ::after to create horizontal dash after input without impacting other elements

using ::after to create a "-" between two inputs. The issue is that I need to move the dash central between the two inputs, I have been trying to do this by adding margin or padding but then that pushes the second input further out which can't happen.
How can I adjust my current code to allow the "-" to move to the left/right without pushing other elements around?
.column {
padding-right: 24px;
padding-left: 24px;
}
.styled-group, styled-group-two {
position: relative;
display: flex;
flex-wrap: wrap;
align-items: stretch;
}
.styled-group::after {
content: "-";
margin: 0 5px;
height: 1px;
}
.input-two {
width: 45px;
}
.container {
display: flex;
}
<div class="container">
<div class="column">
<div class="styled-group">
<input class="input-one" type="text">
</div>
</div>
<div class="column">
<div class="styled-group-two">
<input class="input-two" type="text">
</div>
</div>
</div>
Codepen: https://codepen.io/simoncunningham/pen/mdwMgyL
If you don't mind giving margin to inputs, this can be handled by absolute positioning.
.styled-group,
styled-group-two {
position: relative;
display: flex;
flex-wrap: wrap;
}
.styled-group input {
margin: 0rem 1rem;
}
.styled-group::after {
content: "-";
position: absolute;
left: 95%;
height: 1px;
}
.input-two {
width: 45px;
}
.container {
display: flex;
}

Can't center header image with CSS no matter what

I am using a theme to which I have added an image header (with our logo). I have called this image "https://www.londonim.co.il/wp-content/uploads/2019/04/logo.jpg". the website address is https://londonim.co.il - it is in Hebrew (regardless).
The div in which "logo.jpg" is situated is inheriting some display from the there (i think "block"), and I would very much like it to be centered.
In order to try and center it i have used margins of 25% but it would not keep its central alignment when displayed in bigger screens (unless in full screen mode).
when i temper with the wrapper's display property it generally disappears or loses the alignment altogether. Any thoughts please?
theme header:
<div class="cutewp-container" id="cutewp-header" itemscope="itemscope" itemtype="http://schema.org/WPHeader" role="banner">
<div class="cutewp-head-content clearfix" id="cutewp-head-content">
<div class="cutewp-outer-wrapper">
<div class="cutewp-header-inside clearfix">
<div id="cutewp-logo">
<div class="site-branding">
<a href="https://www.londonim.co.il/" rel="home" class="cutewp-logo-img-link">
<img src="https://www.londonim.co.il/wp-content/uploads/2019/04/logo.jpg" alt="" class="cutewp-logo-img">
</a>
</div>
my tweaks:
/* homepage tweaks */
.cutewp-main-wrapper {
position:relative!important;
margin-left:12.5%;
margin-right:12.5%;
}
#cutewp-logo {
margin-left:25%;
margin-right:25%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
#cutewp-primary-navigation {
text-align: center;
text-align: -webkit-center;
}
#cutewp-header {
text-align: center;
text-align: -webkit-center;
}
.menu-main-container {
margin-left: 25%;
}
Thanks in advance for the help!
The best solution is to:
#cutewp-logo {
margin-left:25%;//delete this
margin-right:25%;//delete this
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;//delete this
width: 100vw;//add this
}
The element isn't centering because it doesn't occupy the full width of the screen therefore has no reference on to be centered to
You need to clear out a few of those margin-left and margin-right
CHANGE
#cutewp-logo {
margin: 5px 0px 5px 0px;
float: left;
width: 41%;
}
.cutewp-main-wrapper {
position: relative!important;
margin-left: 12.5%;
margin-right: 12.5%;
}
#media only screen and (max-width: 890px) {
#cutewp-header {
margin-left: 25%;
background-color: white;
}
.cutewp-content-wrapper {
margin-left: 25%;
}
}
TO
#cutewp-logo {
margin: auto;
}
.cutewp-main-wrapper {
position: relative!important;
}
#media only screen and (max-width: 890px) {
#cutewp-header {
background-color: white;
}
.cutewp-content-wrapper {
margin: auto;
}
}
This will fix the issue on both your desktop and mobile view.

Trying to center one column with content to the left in CSS [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 5 years ago.
I have a design to implement that is supposed to have a centered column of shortcuts and then a text to the left of each.
It should look like this:
The column of the shortcuts should be centered below the title.
I can't get this to work with a responsive design.
The markup I have is ATM this:
<div class="ax-learn__shortcuts">
<h2>Learn shortcuts</h2>
<div class="ax-learn__shortcuts-row">
<div>Go to home:</div>
<div><kbd>g</kbd>then<kbd>h</kbd></div>
</div>
<div class="ax-learn__shortcuts-row">
<div>Go to search:</div>
<div><kbd>g</kbd>then<kbd>s</kbd></div>
</div>
<div class="ax-learn__shortcuts-row">
<div>Go to learn:</div>
<div><kbd>g</kbd>then<kbd>l</kbd></div>
</div>
And the current CSS, which doesn't center it perfectly at all, is this:
.ax-learn__shortcuts {
display: flex;
flex-direction: column;
}
.ax-learn__shortcuts h2 {
text-align: center;
}
.ax-learn__shortcuts-row {
display: flex;
justify-content: center;
align-items: center;
}
And here's an example on Codepen
By adding a pseudo as a ghost element, that matches the labels on the left, you can set that and the left labels to flex: 1, and they will take all available space and leave the kbd in the middle.
Then text-align: right for labels and margin: 0 10px for kbd, and it will look nice too :)
I added these rules, to achieve that.
.ax-learn__shortcuts-row::after {
content: '';
}
.ax-learn__shortcuts-row::after,
.ax-learn__shortcuts-row div:nth-child(1) {
flex: 1;
}
.ax-learn__shortcuts-row div:nth-child(1) {
text-align: right;
}
.ax-learn__shortcuts-row div:nth-child(2) {
margin: 0 10px;
}
Updated codepen
Stack snippet
.ax-learn__shortcuts-row {
display: flex;
justify-content: center;
align-items: center;
}
.ax-learn__shortcuts-row::after {
content: '';
}
.ax-learn__shortcuts-row::after,
.ax-learn__shortcuts-row div:nth-child(1) {
flex: 1;
}
.ax-learn__shortcuts-row div:nth-child(1) {
text-align: right;
}
.ax-learn__shortcuts-row div:nth-child(2) {
margin: 0 10px;
}
.ax-learn__shortcuts {
display: flex;
flex-direction: column;
}
.ax-learn__shortcuts kbd {
padding: 7px 15px;
border: 2px solid black;
border-radius: 5px;
background: white;
display: inline-block;
}
.ax-learn__shortcuts kbd:first-of-type {
margin-right: 15px;
}
.ax-learn__shortcuts kbd:last-of-type {
margin-left: 15px;
}
.ax-learn__shortcuts h2 {
text-align: center;
}
<div class="ax-learn__shortcuts">
<h2>Learn shortcuts</h2>
<div class="ax-learn__shortcuts-row">
<div>Go to home:</div>
<div><kbd>g</kbd>then<kbd>h</kbd></div>
</div>
<div class="ax-learn__shortcuts-row">
<div>Go to search:</div>
<div><kbd>g</kbd>then<kbd>s</kbd></div>
</div>
<div class="ax-learn__shortcuts-row">
<div>Go to learn:</div>
<div><kbd>g</kbd>then<kbd>l</kbd></div>
</div>
</div>
Use a table without borders for aligning and give it a relative width, just so it can scale to screen size.

Positioning flex items on top of each other in grid without wrapping them [duplicate]

This question already has answers here:
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Make a div span two rows in a grid
(2 answers)
Closed 5 years ago.
I have the following layout using flexbox:
I want to have the div containing 2 on the right hand side, and the Team and Scorers should make up the space to the left of it.
Required layout:
It's the same idea as the 2 div having a rowspan of 2, if using a table.
Is there a way to position Team and Scorers to the left of 2 without wrapping them in their own div? If so, is it worth the trouble?
Here is my CSS so far:
.container {
max-width: 600px;
}
.team {
background-color: chartreuse;
}
.score {
background-color: brown;
}
.scorers {
background-color: steelblue;
}
.cards-desktop {
background-color: goldenrod;
}
.carded-players {
background-color: darkorange;
}
.left-col {
display: flex;
flex-flow: row wrap;
}
.left-col > * {
flex: 1 100%;
}
.team {
order: 1;
}
.score {
order: 3;
}
.scorers {
order: 2;
}
.cards-desktop {
order: 4;
}
.carded-players {
order: 5;
}
.team {
flex: 1 auto;
}
.score {
flex: 0 150px;
font-size: 60px;
}
The layout will be different on other breakpoints, so I want to have one HTML block that doesn't get duplicated or mimicked for other breakpoints. That's why I don't want to wrap these two divs in a container, because it's unnecessary on other breakpoints' layouts.
Codepen Link
Here..
Wrap 1, 2 & 3 in their own div with display:flex / flex-direction:column / flex-wrap:wrap.
Then set widths on the various components to suit.
Unfortunately, I think Chrome this requires a fixed height on that wrapper to force the wrap (it's a bug I think)...and there you have.
* {
box-sizing: border-box;
}
.team {
background: chartreuse;
}
.score {
background: brown;
}
.scorers {
background: steelblue;
}
.cards-desktop {
background: goldenrod;
}
.carded-players {
background: darkorange;
}
.wrap {
width: 80%;
margin: auto;
display: flex;
flex-direction: column;
}
.top > div {
padding: 5px;
}
.bottom > div {
height: 25px;
}
.top {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 150px;
}
.team,
.scorers {
height: 50%;
width: 75%;
}
.score {
width: 25%;
flex: 1 0 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
}
<div class="wrap">
<div class="top">
<div class="team">Team</div>
<div class="scorers">Scorers</div>
<div class="score">
<h1>2</h1>
</div>
</div>
<div class="bottom">
<div class="cards-desktop">cards-desktop</div>
<div class="carded-players">carded-players</div>
</div>
</div>

Center vertically a unknown height text in a unknown height div

I know this is a common issue, and I've already asked a similar one.
But I could not find the solution this time.
I am trying to vertically center a text which can have different height in a DIV which can have different height.
And I am trying to solve this only with CSS, without touching the HTML.
Example : http://jsfiddle.net/F8TtE/
<div id="test">
<div id="sumup">
<h1 class="titre">Title</h1>
<div id="date">hello guys</div>
</div>
</div>
My goal is to have the text centered vertically and horizontally whatever its size.
Here it is :
#test {
text-align:center;
}
#test::before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
#sumup {
display: inline-block;
vertical-align: middle;
}
#test {
height : 180px;
text-align:center;
background: yellow;
}
#sumup {
background-color: #123456;
}
#test:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
#sumup {
display: inline-block;
vertical-align: middle;
}
<div id="test">
<div id="sumup">
<h1 class="titre">Title</h1>
<div id="date">hello guys</div>
</div>
</div>
EDIT: It's now 2015 and thankfully the web changes. Assuming you don't support obsolete browsers, it's usually simpler and cleaner to vertically center elements with the Flex model.
#test {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#test {
height : 180px;
background: yellow;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#sumup {
background-color: #123456;
}
<div id="test">
<div id="sumup">
<h1 class="titre">Title</h1>
<div id="date">hello guys</div>
</div>
</div>
Here's another way using display:table-cell since I don't quite understand how dystroy's answer works.
#test {
width:100%;
height : 400px;
display:table;
}
#sumup {
width : 100%;
height : 100%;
background-color: #123456;
display:table-cell;
vertical-align:middle;
}
http://jsfiddle.net/F8TtE/3/
CSS with display solution:
#test {
display:table;
}
#sumup {
display:table-cell;
}
The Demo http://jsfiddle.net/F8TtE/7/

Resources