CSS Make an image and title align [duplicate] - html

This question already has answers here:
Vertically align text next to an image?
(26 answers)
Logo image and H1 heading on the same line
(13 answers)
Closed 3 years ago.
I'm working on trying to create a website, and I want an image to appear to the right side of the title I have picked, but I cant find any way to align them that properly works.
.title {
background-color: white;
color: black;
width: 680px;
}
.logo {
width: 100px;
height: 100px;
}
<div class="title">
<h1>
<font size="+10" /><u/>Computer Science A-Level Notes</h1>
<img class="logo" src="breh.png">
</div>
can anyone tell me what I can do?

There are a lot of ways to align two elements horizontally. The two modern way to achieve this is to rely on flexbox or css grid.
Here is a basic usage of flexbox:
.title {
background-color: white;
color: black;
width: 680px;
}
.logo {
width: 100px;
height: 100px;
}
div {
display: flex;
flex-direction: row;
}
<div class="title">
<h1>Computer Science A-Level Notes</h1>
<img class="logo" src="https://via.placeholder.com/100x100">
</div>
Related to Flexbox: center horizontally and vertically

Try this:
.title{
background-color: white;
color: black;
width: 680px;
display: inline-flex;
}
.logo{
width: 100px;
height: 100px;
float: right;
}
<div class="title">
<h1>Computer Science A-Level Notes</h1>
<img class="logo"src="images/1.jpeg">
</div>

Here is how you can do it with flexbox.
.title {
display: flex;
align-items: center;
}
<div class="title">
<h1>Computer Science A-Level Notes</h1>
<img class="logo"src="https://picsum.photos/200/200">
</div>

You need to make both things inside the .title class have a inline-block alignment, and for the best look you should set the vertical-align property on both.
.title {
background-color: white;
color: black;
width: 680px;
}
.title h1 {
display: inline-block;
vertical-align: middle;
}
.title .logo {
width: 100px;
height: 100px;
display: inline-block;
vertical-align: middle;
}
<div class="title">
<h1>Computer Science A-Level Notes</h1>
<img class="logo" src="https://placehold.it/100x100">
</div>

You can also do it without using flexbox
by simply applying this css
.title{
background-color: white;
color: black;
width: 680px;
}
.title *
{
float:left;
}
.logo{
width: 100px;
height: 100px;
}

Related

Is there a way I can alling some "a" tags that are inside some boxes to a logo (vertical-align: middle) [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 months ago.
I am building my own personal web page, and while doing the "header" I wasn't able to align my "a" tags to the logo... I've been trying everything, but there are no solutions.
This is my code. If I am wrong in some part, please explain me to not commit the same mistake.
HTML
`
<div class="mainBox">
<!--Logo-->
<div class="navBox">
<img src="Images/Logo3Final.png" alt="logo" id="logo">
</div>
<div class="navBox">
/*About Me*/
</div>
<div class="navBox">
/*Contact*/
</div>
<div class="navBox">
/*Expertise*/
</div>
<div class="navBox">
/*Projects*/
</div>
</div>
`
CSS
`#logo{
max-width: 200px;
max-height: 220px;
right: -100px;
}
/*MENU*/
.mainBox{
position: relative;
display: flex;
width: 95%;
height: 25vh;
justify-content: space-evenly;
align-items: center;
}
.navBox{
padding-top: 25px;
padding-left: 10px;
padding-right: 10px;
width: 20%;
height: 20vh;
text-align: center;
top: 50%;
background: transparent;
}
.navBox a:hover{
padding-top: 50%;
background: transparent;
color: var(--text-color);
text-decoration: none;
}
a:visited, a:active, a:link{
text-decoration: none;
color: var(--text-color);
}
.navBox a{
vertical-align: middle;
color: var(--text-color);
padding-top: 0.5rem;
text-align: initial;
}
I try changing position values, I tried giving padding-top, top, margin, nesting into antoher div. But I can not achieve what I will like to be.
Here is a ScreenShot of my page.
You can align .navBox to the center, like this:
.navBox{
/* ... */
display: flex;
align-items: center;
}
or you can remove the height you've specified for .navBox which I think is redundant. It will look like this:
#logo{
max-width: 200px;
max-height: 220px;
}
.mainBox{
position: relative;
display: flex;
width: 95%;
height: 25vh;
justify-content: space-evenly;
align-items: center;
}
.navBox{
padding-top: 25px;
padding-left: 10px;
padding-right: 10px;
width: 20%;
text-align: center;
}
<div class="mainBox">
<!--Logo-->
<div class="navBox">
<img src="https://placekitten.com/96/140" alt="logo" id="logo">
</div>
<div class="navBox">
/*About Me*/
</div>
<div class="navBox">
/*Contact*/
</div>
<div class="navBox">
/*Expertise*/
</div>
<div class="navBox">
/*Projects*/
</div>
</div>
The issue is that your logo is overflowing its allocated size, as defined by the .navBox class.
This is what your header looks like as is:
This is what it looks like when applying overflow: hidden to the navBox class. As you can see, the logo is oversized for its container. You need to limit its height and width OR remove the limitation from the container.
Removing the height attribute from navBox makes it look like this:
Looks centered to me.

How can I center multiple items inside a div vertically using CSS without using flexbox? [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Central align of elements CSS without flexbox
(3 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 1 year ago.
I've been looking all over the internet and I've stumbled upon very similar problems but the solutions I've found vary so slightly that they don't work. I'm trying to create a banner for a webpage that has all items vertically aligned to the center without using a flexbox.
I need to align the logo and the name to the left and some buttons to the right. All I need to do now is align them vertically. I created a minimal snippet just to show my problem.
.banner {
background-color: gray;
width: 100%;
height: 80px;
}
.banner img {
float: left;
width: 40px;
height: 40px;
}
.banner h1 {
float: left;
}
.banner button {
float: right;
width: 80px;
height: 40px;
}
<div class="banner">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico"/>
<h1>Title</h1>
<button>Button 2</button>
<button>Button 1</button>
</div>
Hope, this solutions solve your problem but it's not actual solutions, it's better you move flex box, or also you can solve it using pading top and bottom.
.banner {
background-color: gray;
width: 100%;
height: 80px;
display: table;
}
.left-col {
width: 75%;
}
.right-col { min-width: 200px; }
.banner-col {
display: table-cell;
vertical-align: middle;
overflow: hidden;
}
.banner img {
display: inline-block;
width: 40px;
height: auto;
}
.banner h1 {
display: inline-block;
}
.banner button {
width: 80px;
height: 40px;
float: right;
}
<div class="banner">
<div class="banner-col left-col">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico"/>
<h1>Title</h1>
</div>
<div class="banner-col right-col">
<button>Button 2</button>
<button>Button 1</button>
</div>
</div>
This can be achieved using a table, which is very supported by any browser. If we don't want to use flexbox
Working example here.
Html -
<section class="banner">
<table>
<tr>
<td>
<div>
<img class="logo" src="https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico"/>
<h1>Title</h1>
</div>
</td>
<td>
<nav>
<button>Button 2</button>
<button>Button 1</button>
</nav>
</td>
</tr>
</table>
</section>
Css -
.banner {
background-color: gray;
width: 100%;
height: 80px;
}
.banner table{
height: 100%;
width: 100%;
}
table tr td:last-of-type {
text-align: right;
}
table h1{
margin: 0;
display: inline-block;
vertical-align: middle;
}
table img.logo {
vertical-align: middle;
}
A way of positioning vertically in its container is to move the whole banner down its container 50% and then back up by half its own height.
body {
height: 100vh;
}
.banner {
background-color: gray;
width: 100%;
height: 80px;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.banner img {
float: left;
width: 40px;
height: 40px;
}
.banner h1 {
float: left;
}
.banner button {
float: right;
width: 80px;
height: 40px;
}
<section class="banner">
<img src="img.png" />
<h1>Title</h1>
<nav>
<button>Button 2</button>
<button>Button 1</button>
</nav>
</section>
Note that this method relies on the banner's parent (or at least the first ancestor which has a position set or implied) having some height set. In this case the body has been given the full viewport height. Depending on your use case you may want to put an additional container with 100vh height.
To center multiple items just try this out, I have given 2 solutions.Try both
display: flex;
align-items: center;
justify-content: center;
.Aligner {
display: flex;
align-items: center;
justify-content: center;
}
.Aligner-item {
max-width: 50%;
}
.Aligner-item--top {
align-self: flex-start;
}
.Aligner-item--bottom {
align-self: flex-end;
}

Centering text between two images on center of screen

I'm having problems trying to take a piece of text, center it on the page, and have an image on the left and on the right of it.
Keep in mind, I'm only allowed to change CSS code for positioning. The HTML is completely right.
Here html code:
<div id="container">
<div>
<img src="../logo.png" id="header">
</div>
<div>
<img src="../barbecue01.jpg" id="pic_1">
<div id="aboutus">
<h1>About Us</h1>
<p>
Our restaurant has the best barbecue that you can find at Philadelphia.
We have an amazing team just to serve you, your family, and your friends.
</p>
<h1>Try It Now!</h1>
</div>
<img src="../barbecue02.jpg" id="pic_2">
</div>
</div>
And here is my CSS
#container {
width: 75%;
margin: 15px auto 15px auto;
}
* {
background-color: tan;
}
#pic_1 {
position: absolute;
display: inline-block;
float: left;
}
#pic_2 {
position: absolute;
display: inline-block;
float: right;
}
#aboutus {
position: relative;
text-align: center;
height: 275px;
width: 200px;
color: white;
display: inline-block;
left: 275px;
}
div {
border: solid 2px black;
}
The problem I am running into is that the first image is in the right spot, I'm just trying to get the 2nd image to go on the right side. For some reason, it's just not having it. The text is supposed to be centered.
Any help would be greatly appreciate it
I recommend you use flex instead of float, since float is really not meant for layout.
Stack snippet
#container {
width: 75%;
margin: 15px auto;
}
* {
background-color: tan;
}
#container > div:nth-child(2) {
display: flex;
}
#pic_1 {
flex: 1;
}
#pic_2 {
flex: 1;
}
#aboutus {
flex: 1 1 200px;
text-align: center;
height: 275px;
color: white;
}
div {
border: solid 2px black;
}
<div id="container">
<!-- ADD NEW CODE HERE... -->
<div>
<img src="../logo.png" id="header">
</div>
<div>
<img src="../barbecue01.jpg" id="pic_1">
<div id="aboutus">
<h1>About Us</h1>
<p>Our restaurant has the best barbecue that you can find at Philadelphia. We have an amazing team just to serve you, your family, and your friends. </p>
<h1>Try It Now!</h1>
</div>
<img src="../barbecue02.jpg" id="pic_2">
</div>
</div>
Make them display block and float left
#pic_1 {
display: block;
float: left;
width: 33%;
}
#pic_2 {
display: block;
float: left;
width: 33%;
}
#aboutus {
text-align: center;
display: block;
float: left;
width: 33%;
}

How can my title be centered without "padding-cheat"?

How can I center my h1 tag into the middle of my banner without setting a padding?
HTML
<div class="banner">
<div class="bannerContainer">
<h1>Group Title</h1>
</div>
</div>
CSS
.banner {
height: 500px;
width: 100%;
background-color: #000;
}
.bannerContainer {
}
Can you do something with vertical-align: middle; and display: table-cell; etc?
There's several options, I recommend looking through this - https://css-tricks.com/centering-css-complete-guide/
One option would be: http://jsfiddle.net/dtq7fed3/ which uses a line-height on the container that is the same of the height of the banner.
.banner {
height: 500px;
width: 100%;
background-color: #000;
}
.bannerContainer {
text-align: center;
line-height: 500px;
color: #fff;
}
This only works if the banner height is going to remain stagnant
Using transform, you can position in centrally like so: http://jsfiddle.net/otghf6zo/1/
adding this code will position the title in the exact middle of the containing div regardless of size.
h1 {
color: #fff;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
You can use CSS table like this
.banner {
height: 500px;
width: 100%;
background-color: #000;
display: table;
text-align: center;
color: white;
}
.bannerContainer {
display: table-cell;
vertical-align: middle;
}
<div class="banner">
<div class="bannerContainer">
<h1>Group Title</h1>
</div>
</div>
Or Flexbox like this
.banner {
height: 500px;
width: 100%;
background-color: #000;
display: flex;
color: white;
align-items: center;
justify-content: center;
}
<div class="banner">
<div class="bannerContainer">
<h1>Group Title</h1>
</div>
</div>

How to center a div vertically in HTML [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 7 years ago.
Consider there are two divs like showing bellow.
<style>
div.main{
width: 1000px;
height: 300px;
background-color: black;
}
div.sub{
width: 500px;
height: 100px;
background-color: red;
}
</style>
<div class="main">
<div class="sub">
</div>
</div>
I want the sub div to be vertically centered not to the whole page but to the main div.
Unlike what the convention has been for the last couple of years using tables, you can do this much more intuitively with flexbox.
div.main{
width: 1000px;
height: 300px;
background-color: black;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: center;
-webkit-align-items: center;
-webkit-box-align: center;
align-items: center;
}
(Your browser support mileage may vary.)
Position your divs correctlly . Please search before requesting an answer.
<style>
div.main{
width: 1000px;
height: 300px;
position:relative;
background-color: black;
}
div.sub{
width: 500px;
height: 100px;
position:absolute;
top:0;
bottom:0;
margin-top:auto;
margin-bottom:auto;
background-color: red;
}
</style>
<div class="main">
<div class="sub">
</div>
</div>
Use display:table and table-cell
.container {
display: table;
width: 100%;
height: 100%;
text-align: center;
vertical-align: middle;
border: 1px solid #000;
}
.content {
display: table-cell;
border: 1px solid #000;
display: inline-block;
text-align: left;
}
</style>
<div class="container">
<div class="content">
Test 1
<br/>
Test 2
<br/>
Test 3
</div>
</div>
http://jsfiddle.net/vxdkkeso/1/
I have made my css inside the tag and it looks good and satisfy your requirement.
<div style="width:1000px;position:fixed;z-index:210;background-color:black;">
<div style="position:relative; margin: 0 auto;width:500px;border:1px solid #000;background-color:red;height:26px;padding:5px;">
<div>
</div>
I hope my answer may helpful for you :)