Im writing some code using HTML and CSS only. And in header i have 1 image and some text after it. I need to place them in a one row to make it look like a normal header.
I was trying to use display: inline-block but it not worked for some reason
header{
display: inline-block;
width: auto;
}
<header>
<img src="https://picsum.photos/200" height="35" width="35" />
<h1>Learn about your game</h1>
</header>
Using flex
header {
display: flex;
align-items: center
}
.image-text {
display: flex;
align-items: center;
}
.image-text__image {
flex: 0 0 auto;
padding: 1em;
}
.image-text__image img {
display: block;
}
.image-text__text {
flex: 1 1 auto;
padding: 1em;
}
<header>
<div class="image-text">
<div class="image-text__image">
<img src="https://picsum.photos/200" height="35" width="35" />
</div>
<div class="image-text__text">
<h1>Learn about your game</h1>
</div>
</div>
</header>
Use Flexbox:-
header {
display: flex;
align-items: center;
justify-content:center;
}
header h1 {
padding-left:20px;
box-sizing:border-box;
}
header{
display: flex;
width: 90%;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 20px auto;
background-color: red;
text-align: center;
}
header img,header h1{
display: block;
width: 80%;
margin: 5px auto;
}
</style>
add this to your css
Related
This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 5 months ago.
header {
display: flex;
justify-content: space-between;
}
nav {
display: flex;
text-align: right;
align-items: center;
}
body {
font-size: 20px;
margin: 0px;
justify-content: center;
}
.logo {
text-align: left;
align-items: center;
}
.menu {
margin-left: 10px;
}
a {
text-decoration: none;
color: #000000;
}
.welcome {
background-color: #76a5d5;
width: 100%;
height: 300px;
display: flex;
align-items: center;
justify-content: center;
}
.welcome .p1 {
font-size: 40px;
}
main {
width: 1200px;
background-color: #76a5d5;
margin: 10px;
display: flex;
justify-content: center;
}
main>.item {
flex: none;
width: 580px;
height: 50px;
margin: 10px;
background-color: #000000;
}
<header>
<div class="logo">My Website</div>
<nav>
<div class="menu">
<a class="navitem_text" href="##">item1</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item2</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item3</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item4</a>
</div>
</nav>
</header>
<div class="welcome">
<div class="p1">Welcome to MyHome</div>
</div>
<main>
<div class="item">
</div>
</main>
hi I just started learning coding.I wanted to create a rwd page.Now I'm facing a problem is that I want to move my main area to the center of the page but I couldn't do it. I've tried to add display:flex to body. But everything would move. Should I add a div in the main? Can't figure it out. What should i do now? Here's the code.Thanks
You want to center the main element right?
main{
width: 1200px;
background-color: #76a5d5;
/*See here 10px will be used for top and bottom and for left and right
it will automatically divide equally both sides using auto.
*/
margin: 10px auto;
display: flex;
justify-content: center;
}
Try replacing this ruleset in your css.
Using auto for the left and right margin gives the desired result.
header {
display: flex;
justify-content: space-between;
}
nav {
display: flex;
text-align: right;
align-items: center;
}
body {
font-size: 20px;
margin: 0px;
justify-content: center;
}
.logo {
text-align: left;
align-items: center;
}
.menu {
margin-left: 10px;
}
a {
text-decoration: none;
color: #000000;
}
.welcome {
background-color: #76a5d5;
width: 100%;
height: 300px;
display: flex;
align-items: center;
justify-content: center;
}
.welcome .p1 {
font-size: 40px;
}
main {
width: 1200px;
background-color: #76a5d5;
margin: 10px auto;
display: flex;
justify-content: center;
}
main>.item {
flex: none;
width: 580px;
height: 50px;
margin: 10px;
background-color: #000000;
}
<header>
<div class="logo">My Website</div>
<nav>
<div class="menu">
<a class="navitem_text" href="##">item1</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item2</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item3</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item4</a>
</div>
</nav>
</header>
<div class="welcome">
<div class="p1">Welcome to MyHome</div>
</div>
<main>
<div class="item">
</div>
</main>
Answer
You can do this with CSS align-self property.
First, we need give the <body> display flex and set the direction to column (vertical).
body {
font-size: 20px;
margin: 0px;
display: flex;
flex-direction: column; /* this change the direction to vertical */
}
then change the main area to center
main {
width: 1200px;
background-color: #76a5d5;
margin: 10px;
align-self: center; /* this move the element to center */
}
Demo
header{
display: flex;
justify-content: space-between;
}
nav{
display: flex;
text-align: right;
align-items: center;
}
body{
font-size: 20px;
margin: 0px;
display: flex;
flex-direction: column;
}
.logo{
text-align: left;
align-items: center;
}
.menu{
margin-left: 10px;
}
a{
text-decoration: none;
color:#000000;
}
.welcome{
background-color: #76a5d5;
width: 100%;
height: 300px;
display: flex;
align-items: center;
justify-content: center;
}
.welcome .p1{
font-size: 40px;
}
main{
width: 1200px;
background-color: #76a5d5;
margin: 10px;
align-self: center;
}
main>.item{
flex: none;
width: 580px;
height: 50px;
margin: 10px;
background-color: #000000;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=divice-width, initial-scale=1">
<link rel="stylesheet" href="RWDstyle.css"/>
<title>RWD test</title>
</head>
<body>
<header>
<div class="logo">My Website</div>
<nav>
<div class="menu">
<a class="navitem_text" href="##">item1</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item2</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item3</a>
</div>
<div class="menu">
<a class="navitem_text" href="##">item4</a>
</div>
</nav>
</header>
<div class="welcome">
<div class="p1">Welcome to MyHome</div>
</div>
<main>
<div class="item">
</div>
</main>
</body>
</html>
Hi I have 6 images that I have in my website that are appearing vertically stacked but I want them to be horizontally stacked and center aligned. 4 of the images have captions too that I need to appear right below each image... can someone please help thanks so much
Here is my html:
<div class="container-fluid">
<div class="container-of-images">
<img src="chick2.png">
<figure>
<img id="img1" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<figure>
<img id="img2" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<figure>
<img id="img3" class="threeChoices" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<figure>
<img id="img4" class="fourChoices" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<img src="chick1.png">
</div>
</div>
And here is my CSS:
body {
background-color: #b6e6bd;
line-height: 1.6;
}
.container-fluid {
padding: 1% 20%;
margin: auto;
text-align: center;
}
img {
width: 100px;
line-height: 0;
margin: 0 1%;
display: flex;
justify-content: center;
}
figcaption {
text-align: center;
width: 100px;
}
Try this CSS code, the problem I found was that display: inline-block should have been used instead of flex.
body {
background-color: #b6e6bd;
line-height: 1.6;
}
.container-fluid {
margin: auto;
text-align: center;
width:100%;
}
img {
width: 100px;
line-height: 0;
margin: 0 1%;
display: inline-block;
justify-content: center;
vertical-align: middle;
}
figcaption {
text-align:
center;
width: 100px;
}
figure {
display: inline-block;vertical-align: middle;
}
Make your image container a flex element that lays out the content in a row:
.container-of-images {
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
}
You need to add dispay: inline; or dispay: inline-block; to your css.
body {
background-color: #b6e6bd;
line-height: 1.6;
}
.container-fluid {
padding: 1% 20%;
margin: auto;
text-align: center;
}
img {
width: 100px;
line-height: 0;
margin: 0 1%;
display: flex;
justify-content: center;
display: inline;
}
figcaption {
text-align: center;
width: 100px;
}
Did you try to use flexbox on the container? Try this:
.container-of-images {
display: flex;
}
Based on your if you add the CSS property display:flex to container-of-images class it will solve your problem. But you solve be aware of the responsive design for it.
.container-of-images {
display: flex;
flex-direction: row; /* extra line flex alone can solve your problem */
justify-content: space-around; /* extra line to space images around */
}
I have a project and i struggle with positioning elements in CSS. i want to create a header with a contact number and email to the right and in image at the centre.
The below is the code. suggestions to overcome this problem i'm facing are very welcome.
header {
display: block;
text-align: center;
}
#cw-logo-trans {
width: 200px;
display: inline-block;
align-items: right;
font-size: 24px;
}
#contacts {
margin-left: 5px;
float: left;
color: white;
font-size: 1.5em;
/* 40px/16=2.5em */
color: white;
}
.home {
width: 70px;
height: auto;
background-color: white;
padding: 10px;
margin: 10px;
float: left;
text-align: center;
}
<header>
<span id="cw-logo-trans" alt="cw-logo-trans">
<img src="images/cw_logo.png" alt="cw-logo-" > </span>
<span id="contacts">0800 111 111</br>email#emailyou.com</span>
<div class="home" alt="Home-button">Home</div>
</br>
</header>
Or you can use Flex as well.
.outer {
display: flex;
background-color: lightgray;
}
.outer div {
flex: 1;
align-items: center;
}
.logo {
display: flex;
justify-content: center;
}
<div class="outer">
<div></div>
<div class="logo">
<image src="https://facebookbrand.com/wp-content/uploads/2019/04/f_logo_RGB-Hex-Blue_512.png" height="40px" width="40px" />
</div>
<div>
<p>markzuckerberg#twitter.com</p>
<p>1234567890</p>
</div>
</div>
I'm trying to create a navigation bar but can't get rid of the extra space to the left of the logo container. How can I remove this space?
I've tried setting the margin and padding to 0 but neither worked.
Here's an image of what i'm talking about. I added red borders to make it easier to understand where the containers are:
The output should have the logo all the way to the left of the menu container. There should be no extra space.
.center-container{
max-width: 960px;
margin: 0 auto;
margin-top: 1rem;
}
menu{
background-color: cadetblue;
display: flex;
justify-content: space-between;
}
.logo{
height: 1.5rem;
margin-right: 1rem;
}
.logo-container{
display: flex;
align-items: center;
}
<header>
<menu class="center-container">
<div class="logo-container">
<img class="logo" src="https://s3.amazonaws.com/codecademy-content/courses/freelance-1/unit-5/tortoiseshell-optics/resources/images/logo-white.png">
<span>Tortoiseshell Optics</span>
</div>
</menu>
</header>
<menu> elements are usually styled like <ul> elements by the browser which have a left padding. Remove it and you get rid of the space:
.center-container {
max-width: 960px;
margin: 0 auto;
margin-top: 1rem;
}
menu {
background-color: cadetblue;
display: flex;
justify-content: space-between;
padding-left: 0;
}
.logo {
height: 1.5rem;
margin-right: 1rem;
}
.logo-container {
display: flex;
align-items: center;
}
<header>
<menu class="center-container">
<div class="logo-container">
<img class="logo" src="https://s3.amazonaws.com/codecademy-content/courses/freelance-1/unit-5/tortoiseshell-optics/resources/images/logo-white.png">
<span>Tortoiseshell Optics</span>
</div>
</menu>
</header>
Also note that support for <menu> is quite limited.
.center-container {
max-width: 960px;
margin: 0 auto;
margin-top: 1rem;
padding: 0;
}
menu {
background-color: cadetblue;
display: flex;
justify-content: space-between;
}
.logo {
height: 1.5rem;
margin-right: 1rem;
}
.logo-container {
display: flex;
align-items: center;
}
<header>
<menu class="center-container">
<div class="logo-container">
<img src="https://s3.amazonaws.com/codecademy-content/courses/freelance-1/unit-5/tortoiseshell-optics/resources/images/logo-white.png" class=logo>
<span>Tortoiseshell Optics</span>
</div>
</menu>
</header>
how can I make this container responsive so the text and the img automatically become block elements. I tried it out with flex direction but for someway it doesnt work. Can someone correct my code if necessary and suggest me a media query rule for the responsive design
<div class="top">
<h1>Welcome</h1>
<div class="portrait">
<img src="https://pixy.org/images/placeholder.png" alt="">
<h2>XXXXXXXXXX</h2>
</div>
</div>
.top h1{
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: black;
height: 20vw;
margin-top: 0;
font-size: 5vw;
color: white;
text-shadow: 5px 5px rgb(142, 135, 136);
}
.top img {
width: 20vw;
}
thanks in advance
I think this is what you are after. display: flex; is very powerful property and useful when it comes to take up rest of the space and centering.
Modification
here is a demo, I would not suggest this approach with using max-width as it's not "mobile-first". But if this is what you want for this project then ok.
.container {
display: flex;
flex-direction: row;
background-color: deepskyblue;
}
#img {
width: 140px;
height: 140px;
}
#text {
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: deeppink;
min-height: 100px;
}
#media screen and (max-width: 700px) {
.container {
flex-direction: column;
}
#img {
width: 100%;
height: auto;
}
}
.container {
display: flex;
background-color: deepskyblue;
}
#img {
width: 140px;
height: 140px;
}
#text {
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1;
background-color: deeppink;
}
<div class="container">
<img id="img" src="https://www.archlinux.org/static/vector_tux.864e6cdcc23e.png" />
<div id="text">text on the left, next to the img</div>
</div>
Ok, well so here it is if I understood well what you are trying to accomplish. Correct me or update your question if I am wrong!
#img{
width: 200px;
height: 150px;
float: left;
}
#text{
overflow: hidden;
}
<div class="container">
<img id="img" src="https://www.archlinux.org/static/vector_tux.864e6cdcc23e.png"/>
<div id="text">text on the left, next to the img</div>
</div>