Vertically align content to the middle of a div? [duplicate] - css

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 6 years ago.
I have a div containing some content. It has a height of 31px and inside it, the button has smaller height.
<div class="headerAndButton">
<h3 class="h3">
Click the button to the right
<button>click me</button>
</h3>
</div>
I have another header that does not have a button in it.
<div class="headerAndButton">
<h3 class="h3">No button here, I only want to have this header have the same vertical alignment, height and margin as the other header</h3>
</div>
I want to vertically align the <h3> text within each div. I have tried to solve the problem with the following SCSS:
.headerAndButton {
margin-bottom: $space-300;
h3 {
min-height: 31px;
vertical-align: bottom;
button {
margin-left: $space
}
}
}
The vertical-align property has no visible effect. The text in the without the botton is top aligned. The text in the other is a bit lower. It also becomes top aligned if I remove the button.
How can I make this headers have the same vertical alignment? I am not good at CSS, so maybe there are completely different better ways to solve this.

You could use several methods to vertical align an element. For example you could use new display: flex methods:
display: flex;
justify-content: center;
flex-direction: column;
Example
If its no problem to position: absolute your element you could use
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
There is a quite good tutorial from CSS-Tricks, named Centering in CSS: A Complete Guide.

display: flex;
justify-content: center;
align-items: center;
just apply this CSS code on headerAndButton

You can use display:table-cell then vertical-align:middle to do that.
JS Fiddle
( I add a border so you can see the h3 is aligned )

Related

CSS adapt the size of a div between two fixed divs [duplicate]

This question already has answers here:
Make a div fill the height of the remaining screen space
(41 answers)
Closed 7 months ago.
I have 3 divs:
<div>
<div id="div1">Title</div>
<div id="div2">Some text</div>
<div id="div3">Footer</div>
</div>
Every div have a width: 100%.
The title div height depends on its content so it can evoluate a little bit, and it has a fixed position.
The Footer div has a fixed size (its content cannot change) and a fixed position.
The goal is to have the text div between this two divs, having its size exactly matches the remaining places between title and text div so I can apply a scroll on it.
Can somebody explain to me how to do that ?
Thanks
I assume you want something like this:
#div1 {
background: rgba(0,0,250,0.2)
}
#div2 {
flex-grow: 1;
background: rgba(0,250,0,0.2);
overflow: scroll;
}
#div3 {
height: 10vh;
background: rgba(250,0,0,0.2);
}
.container {
height: 100vh;
flex-direction: column;
display: flex;
}
<div class="container">
<div id="div1">Title</div>
<div id="div2">Some text</div>
<div id="div3">Footer</div>
</div>
Judging by the clarification in your comment what you're trying to achieve is a basic layout which should be done using the <header> and <footer> tags rather than reinventing the wheel with divs.
However if you're set on using divs you should use position: absolute; or position: fixed; on the #div1 and #div3 depending on what you need the to do. Using this method you should add apropriate margins to make sure div1 and 3 dont cover div2.

Make this div appear the middle of the body? [duplicate]

This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 3 years ago.
I am trying to make a div appear in the middle of the browser. However, I am unable to find any form of guide working with the CSS I already have.
CSS
#register-modal {
display: inline;
position: absolute;
z-index: 1;
margin: 0 auto;
background-color: aqua;
top: 50%;
left: 50%;
text-align: center;
}
With this CSS I expected the div to appear in the middle of the browser windows, but it appears in the bottom right corner.
The HTML affected by the CSS forms a "modal" and thus I can't have the position be anything other than absolute.
You can easily achieve this by using the flex layout. And by setting the justify-content and align-items to center
justify-content: align the items on the main axis.
align-items: align the items on cross axis.
You can read about it more on the MDN doc
.container{
display:flex;
justify-content: center;
align-items: center;
width:100%;
height:100%
}
body, html {
width:100%;
height: 100%;
}
<div class="container">
<div class="sub-container">Child</div>
</div>

Positioning text in the middle of a gallery so that it's responsive

how do I position a title on top of a gallery , like in the picture below.
I'm using a shortcode for the gallery, which comes with my theme.
I was able to do it using position:absolute; and top and bottom values, then center it with display:block; and margin:0 auto;. However, when I change the size of the screen it get's all out of whack.
the html is
<div class="gallery-column">
[shortcode goes here]
<h2>Title goes here</h2>
</div>
The easiest way to keep an element centered both horizontally and vertically is with flexbox.
All you need on the parent element is:
display: flex to treat the element as a flexbox
align-items: center for vertical alignment
justify-content: center for horizontal alignment
a fixed height (to create the padding) - 100vh for a full screen
This can be seen in the following:
body {
margin: 0;
}
.gallery-column {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
<div class="gallery-column">
<div class="centered">
<h2>Centered Element</h2>
</div>
</div>
The element in question will remain at the exact center of the page, regardless of screen resizes.
In your case you'll also want a z-index to ensure that the element stays on top.z-index: 1 should suffice.

Horizontally center text in row with floats on both sides, with flexbox?

Here's a CSS puzzle for you all.
I'm using flexbox in my layout. I have a header with a few buttons on the left side, some text in the center, and another button on the right. Here's an ascii drawing:
[btn][btn2][btn3][ text ][btn4]
Unfortunately, this looks weird because the text isn't centered in the header. What I really want is this:
[btn][btn2][btn3][ text ][btn4]
Ideally, I'd like to continue using flexbox to achieve this because it makes most of the horizontal layout really easy, but I'm willing to fall back to floats and/or positioning if need be.
One problem with positioning the text element absolutely is that long text will under/overlap the buttons on the side. I currently use text-overflow: ellipsis and as a bonus, I would love to continue to if possible:
[btn][btn2][btn3][ long text causes elli... ][btn4]
I'm also okay with adding extra container elements if that helps. Perhaps there's a way to solve this by adding left buttons and right buttons in containers and then ensuring those containers are always the same width?
Edit: I think I took a step in the right direction with this CodePen. It properly centers the text. The only downside is that the h1 needs a fixed or percentage width, and if that width is wider than the space available, it seems to just overlap the neighboring elements.
You came very close to a working sample. I forked your CodePen with a solution that don't require widths of any kind. It's using the power of flex to position elements.
The H1 will always be in the middle, with a width of the same size as the surrounding left-btnsand right-btns, using flex: 1;
You can, of course, specify your H1 to a fixed width as you did, or make it for example flex: 2; to have it take up 50% space instead of 33%.
Here's the fork on CodePen. I've removed unnecessary code.
And the code:
HTML
<div class="wrapper">
<div class="left-btns">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<h1>center me! center me! center me! test woah asdf veasdf veasdf veasdf veasdf ve</h1>
<div class="right-btns">
<div class="box"></div>
</div>
</div>
<h1>center me!</h1>
CSS
.wrapper {
background: green;
display: flex;
margin: 5px;
}
h1 {
flex: 1;
text-align: center;
margin: 5px;
background: yellow;
overflow: hidden;
text-overflow: ellipsis;
white-space: noWrap;
}
.box {
width: 50px;
height: 50px;
margin: 1px;
background: red;
}
.left-btns,
.right-btns {
margin: 5px;
display: flex;
flex: 1;
background: blue;
}
.right-btns {
justify-content: flex-end;
}

Horizontal alignment of an image [duplicate]

This question already has answers here:
How to make an image center (vertically & horizontally) inside a bigger div [duplicate]
(36 answers)
Centering image horizontally and vertically [duplicate]
(5 answers)
Closed 9 years ago.
I am trying to horizontally center a large image. Because I am using HTML5, I can't use <center>. I could use left:400px, but that wouldn't work for different screen sizes.
Wrap the image inside an element and use text-align: center;...
Demo
<div class="center">
<img src="http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif" alt="Google" />
</div>
.center {
text-align: center;
}
Alternatively if you are aware, that what's the image width, you can also use margin: auto; with display: block; as img tag is inline element by default and of course, the width property
img {
width: 276px;
display: block;
margin: auto;
}
Demo 2
Try this css to horizontally center
display: block;
margin: 0 auto;
= the top and bottom margin 0, and the left and right margin auto
Use CSS text-align: center;. And don't forget to set width on the div or it will look left-aligned.
<div style="text-align: center; width: 100%; border: 1px solid black;">Centered</div>
Depending on your specific situation, this has worked for me on several projects:
<style>
.outer{float: left; position: relative; left: 50%;}
.inner{float: left; position: relative; left: -50%;}
</style>
<div class="outer">
<div class="inner">content you want to center, image, text, whatevs</div>
</div>
The IMG element is inline, by default. So, as the others have pointed, you have two options:
1) Keep it inline, and use text-align: center;.
2) Make it a block element with display: block;, and then use margin: auto;, which works only on block elements. I think this solution is better. Setting the width is just another way to force it to be a block element, but it's less obvious for someone that may read the code later. So explicitly setting the display type to block is better for readability.
If your element has the width property , then give it margin:auto;.

Resources