I have some vertically aligned text in a div that I want to remain vertically centered when the browser resizes. As the div shrinks with the browser, my text becomes offset towards the bottom of the div.
Any suggestions?
Here is a Fiddle of the issue I'm experiencing.
HTML:
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
CSS:
.outer-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat left top;
background-size: 100% auto;
height: 360px;
}
.inner-wrapper {
display: table;
width: 100%;
height: 360px;
}
.header-wrapper {
display: table-cell;
text-align: center;
vertical-align: middle;
}
h1 {
color: #fff;
}
Is this what you're looking for?
The text actually does center, though the background image doesn't, hence it looks like it doesn't, so here is 2 versions (fiddle use background-size: cover) where image centers too
Updated fiddle
html, body {
height: 100%;
}
.outer-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat center center;
background-size: 100% auto;
max-height: 360px;
height: 100%;
}
.inner-wrapper {
display: table;
width: 100%;
height: 100%;
}
.header-wrapper {
display: table-cell;
text-align: center;
vertical-align: middle;
}
h1 {
color: black;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
The problem is your background, the div is staying the same size and the text is staying centered, you background makes it seem not so. Like Gavin said use background-size cover.
Your problem isn't actually that your text isn't centered, because it is. Your actual issue is that your image doesn't have enough height at that size to fill the height of the container.
See the screenshot below, here I have used the element inspector to highlight the "outer-wrapper" which is the blue box you see. Notice the text is centered within it.
So to tackle the actual issue which is the image either needs to fill the div or needs to stay centered itself.
To stay centered add the following to the outer-wrapper: (Fiddle)
background-position: center;
To fill the div, remove the background-size from outer-wrapper and add this: (Fiddle)
background-position: center;
background-cover: cover;
Related
I am new to HTML and CSS and I am wondering how I can put an image directly to the left of a textbox. For this the image must have the same height as the textbox and the overall picture has to be centered. This is the code I already have written:
div {
margin: auto;
width: 20%;
border: 1px solid black;
padding: 25px 50px 500px;
background-color: lightblue;
}
<img src="https://climate.nasa.gov/system/feature_items/images/28_global_ice_viewer.jpg">
<div> Textbox </div>
How do I make an image the same size as the box next to it and how do i align/put this image next to the left-border of this rectangle. The overall block has to be in the center.
I would use the flex property of CSS and adjust the image as a background image to make in align center to the div. Heres the code :
.container {
display: flex;
}
.content {
flex: 1;
padding: 50px;
background-color: lightblue;
}
.image {
flex: 1;
background-image: url("https://climate.nasa.gov/system/feature_items/images/28_global_ice_viewer.jpg");
background-position: center;
background-size: cover;
}
<div class="container">
<div class="image">
</div>
<div class="content">
<p>How do I make an image the same size as the box next to it and how do i align/put this image next to the left-border of this rectangle. The overall block has to be in the center.</p>
</div>
</div>
I have two questions. Essentially, I want to display an image in its original size and place it in the middle of the screen(by middle I mean in the "horizontal" middle not the center of the screen), and then place a div with texts right below this image. This is the code I use:
<div class="figure">;
<img src="...">;
</div>';
<div class="text">
text here
</div>
This is the css:
.figure{
position:absolute;
margin:0 auto;
left:0;
right:0;
bottom:10px;
top:30px;
}
.figure img{
margin-left: auto;
margin-right: auto;
display: block;
height:100%;
width:100%;
}
I have two questions. The first one is when the image height is theoritcally longer than the screen height, there is no vertical scrollbar, the image just got resized to fit the screen. The second question is how can I place the text below the image without knowing the size of the image? I tried figcaption but it doesn't work.
To make it perfect center, you might need min-height: 100vh; and min-width: 100vw; then use display flex to center it. Otherwise, you might not center it vertically.
Also, move your text block inside one div with the img.
by default, the img will not resize unless you specify it.
By default div has display block so it will take the whole row, with text-align: center; it will just center your text.
.figure {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
min-width: 100vw;
}
.centered {
text-align: center;
}
html,
body {
padding: 0;
margin: 0;
}
<div class="figure">
<div>
<img src="http://placehold.it/950x450">
<div class="centered">
text here
</div>
</div>
</div>
First of all I I assume this does what you have in mind.
html
<img src="https://static.tumblr.com/07f1e3ffdfdd03631d00e7792ea3fa93/mja6mxp/AUUna8jev/tumblr_static_tumblr_static_88o50pnpc3k04gw0ck888o80o_640.jpg"/>
<div class="text">Isn't that pretty!</div>
CSS
body {
color: #eee;
background-image: url("chrome://global/skin/media/imagedoc-darknoise.png");
}
img {
margin: 0 auto;
display: block;
}
.text {
margin: 0 auto;
display: table;
}
Your first question about why the image is resized when it's bigger than the page. I have to point out that in your CSS you set the image to be the 100% the of the possible width and height and by default, the images will be stretched to fit the element.
To answer your second question, because your "figure" div containing the img position is absolute it ignores the position of other elements. Change position to another type such as "position: relative" and it will position its self with other elements in mind.
I'm not the most confident in html and css skills but I hope my two cents at least helps you press on forward.
I need to make a website and there is a button which is in the middle horizontally. I can use left 50% or margin-left but when I minimize the window - it isn't in the middle. How do I set the div to be exactly in the middle also when you minimize the window of the browser?
Edit: fixed it by doing as Trix said and adding
.center-div{
margin: 0 auto;
}
If your div has a width value, you may use:
CSS
.center-div{
width: 100px;
margin: 0 auto;
}
But, if not, you may add text-align: center to its parent and display: inline-block; to the centering div itself:
HTML
<div class="parent">
<div class="center-div">
</div>
</div>
CSS
.parent{
text-align: center;
}
.parent .center-div{
display: inline-block;
}
Novice question; trying to learn web design and i've come along way. However could somebody try and explain this to me.
This is how one particular section in my website looks:
I am trying to split the image 40% of the screen and the text in the remaining 60%. It's gone well, however I cannot figure out why the text appears at the bottom and not the top? I am guessing it is very simple, or have I just messed it up?
Here is my HTML:
<section>
<div class="about">
<div class="about_img"></div>
<div class="about_text">
<h1>Some Text</h1>
<p>Some supporting text</p>
</div>
</section>
And my CSS for the section:
.about{
width: 100%;
height: 300px;
background-color: #F0F0F0;
min-height: auto;
display: inline-block;
}
.about_img{
width: 40%;
height: 100%;
background-image: url("/assets/alex-image.JPG");
background-size: cover;
display: inline-block;
}
.about_text{
width: 20%;
display:inline-block;
position: relative;
margin-left: 50px;
}
Please let me know if you need any additional information. I have also uploaded my site to http://www.alexwiley.co.uk
Thanks,
ANSWER, thanks to CBroe.
Use vertical-align: top; to align the element to the top of the block.
You may add vertical-align: top to the <div class="about_text"> to align it vertically on top.
Possible values
According to this page, other possible values you have are:
vertical-align: middle
The element is placed in the middle of the parent element
vertical-align: bottom
The bottom of the element is aligned with the lowest element on the line
vertical-align: {length}
Raises or lower an element by the specified length. Negative values are allowed
vertical-align: {X}%
Raises or lower an element in a percent of the "line-height" property. Negative values are allowed
vertical-align: sub
Aligns the element as if it was subscript
vertical-align: super
Aligns the element as if it was superscript
vertical-align: text-top
The top of the element is aligned with the top of the parent element's font
vertical-align: text-bottom
The bottom of the element is aligned with the bottom of the parent element's font
Default
the default value is:
vertical-align: baseline
Align the baseline of the element with the baseline of the parent element.
Answer
.about{
width: 100%;
height: 300px;
background-color: #F0F0F0;
min-height: auto;
display: inline-block;
}
.about_img{
width: 40%;
height: 100%;
background-image: url("http://www.alexwiley.co.uk/assets/alex-image.JPG");
background-size: cover;
display: inline-block;
}
.about_text{
width: 20%;
display:inline-block;
vertical-align: top;
position: relative;
margin-left: 50px;
}
<div class="about">
<div class="about_img"></div>
<div class="about_text">
<h1>Some Text</h1>
<p>Some supporting text</p>
</div>
</div>
You should add this css to the text to have it in the top :
vertical-align: top
This is the header of my page:
--------------------------------
Logo | fixed | Title with
Image | margin | unknown width
--------------------------------
I want to horizontally center the whole header in my page. Currently I set the logo image as the background image of a container and wrap the title in the container.
HTML is:
<header role="banner">
<div class="logo">
<h1>Title</h1>
</div>
</header>
CSS is:
header[role="banner"] .logo{
height: 76px;
background: url(/images/logo.png) no-repeat;
}
.logo h1{
line-height: 76px;
text-indent: 96px;
}
Before you answer please notice that the hard parts of this problem are:
The width of title is unknown.
There is a logo image to the left of the title. Only centering the title is what I already know how to do but not what I want. Centering the image is just wrong — [logo + title] is to be centered, logo should not be centered in the header.
I'm not sure if your HTML is the best way of having a logo and page title. I'd personally go with an <img> for the logo.
You can use the display: table and display: table cell for centering without knowing the width.
Here's how I'd do it with the image for a logo:
HTML
<header role="banner">
<div class="extra-container">
<div class="logo">
<img src="http://placehold.it/100x76" alt="Logo Image" />
</div>
<h1>Title</h1>
</div>
</header>
CSS
header[role="banner"] {
display: table;
margin: 0 auto;
}
header[role="banner"] .extra-container {
display: table-cell;
}
header[role="banner"] .logo {
margin-right: 40px;
float: left;
}
header[role="banner"] .logo img {
display: block;
}
h1{
line-height: 76px;
display: inline-block;
}
http://jsbin.com/UhIqUXe/1/edit
You don't need the <div> to be honest. Make the background of the <header> your background image like this:
header[role="banner"] {
background: url(/images/logo.png) center top no-repeat;
width: 100%;
height: 76px;
}
And to center your title put text-align: center;
The header will need to have a width specified in order for the margin trick to work. If you don't know the exact width you could use a percentage for the header tag.
Have a look at this source CSS-Trick
The best way should be set margin..
margin:0px auto 0px auto;
this will set margin of top and bottom to 0px and right and left to auto,
this will trick the wrapperDiv in center( the container of image or text)