CSS Flex Not Working - html

I want to justify a few images in a div.
I added justify-content: center;, but the images align left. Why?
.item {
justify-content: center;
}
<div class="item">
<div>
<img src="http://placehold.it/75x75" />
</div>
<div>
<img src="http://placehold.it/75x75" />
</div>
<div>
<img src="http://placehold.it/75x75" />
</div>
</div>​

The justify-content property only aligns the content/s of a given container if the container is a flexible one.
Add this to your CSS:
display: flex;
The full CSS code in the head section should be:
<style type="text/css">
.flex-item {
display: flex;
justify-content: center;
}
</style>
Source: http://www.w3schools.com/cssref/css3_pr_justify-content.asp
Example: http://www.w3schools.com/cssref/playit.asp?filename=playcss_justify-content&preval=center
Fidde example: https://jsfiddle.net/L4tmgs18/1/

If I understand correctly and you're trying to center the contents of the .flex-item div (i.e. the icons), replace justify-content: center; with text-align: center;.
Your problem isn't with how you have applied the css, that is correct, but depending on what you are trying to achieve, your problem is with the actual css used.

As I can see, you have all the necessary code provided to you by "Hubert Siwkin", but it sits a tad off the left side of your page. Simple fix. You need to edit your "body" CSS style and add a "margin" value.
body {
margin: 0px;
}
This will fix the problem you are facing.
If you would like to know more about the "body" CSS style, use the following link.
http://www.w3schools.com/tags/tag_body.asp
UPDATE: I have provided an update to the jsFiddle provided by "Hubert Siwkin". Most of the code that you have is not necessary, so I have slim-lined it for you.
https://jsfiddle.net/hddbkcvv/

Related

How to center an image in vue.js?

I'm trying to do a front of an HTML website by using vue.js,
but I wasn't able to center an image using css.
I wrote all of my code in the App.vue file :
<template>
<div id="container3">
<img id="teamBackground" src="./assets/bourg_palette_rounded.png" alt="Bourg palette in background" width="360" height="170"/>
</div>
</template>
<style>
<!-- team -->
#container3 img{
display:block;
margin:0 auto;
}
</style>
I tried the text-align and the display-block + margin: 0 auto properties but it didn't change neither the placement of the image or the placement of other elements
Have you tried using display:flex; together with justify-content:center;?
You can also try out using position:absolute;
You can read more about image-centering methods here: https://www.freecodecamp.org/news/how-to-center-an-image-in-css/
#container3 {
display: flex;
flex-direction: column;
justify-content: center;
}
you can put those css codes to the parent div
#container3 {
display: flex;
width:100%;
height:500px;
justify-content: center;
align-items: center;
}
Flexbox is the most suitable solution for your problem.
The parent (#container3) should be a flex container. For horizontal centering we use justify-content: center & for vertical centering we use align-items: center.
Note: We must provide height to the parent for child to align in vertical direction. Also the above example is for default flex-direction ( row). For more details refer to the page https://www.digitalocean.com/community/tutorials/css-centering-using-flexbox

Why align-self do not work as text-align in CSS?

I will use extracts and simplify for explanation purposes.
I was looking for errors in a webpage I designed some months ago and I found the following problem in the logo section:
<div>
<a id="logo" href="index.html" title="Página Principal" class="logo center">
<img src="img/mainlogo.png" alt="Corporate-logo" title="Página Principal">
</a>
</div>
Consider that the parent container has: display: flex; and there are another elements (child) inside.
In the stylesheet CSS
.logo {
flex:1;
align-self: center;
}
.center {
text-align: center;
}
As soon as I remove the center class the logo goes to left (default value I suppose). I do not understand with alig-self do not place the logo in the center.
What am I missing or not understanding?, I do not want to use the center class to center the logo.
What you need to set is the display property.
display: flex; instead of flex: 1;
(flex: 1 is shorthand for setting flex-grow to 1)
Your alignment can then be defined using the justify-content and align-items properties.
align-self will work as you have it if you add display: flex to the parent. The snippet shows a more common method.
align-self allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.
SOURCE
.parent {
display: flex;
justify-content: center;
}
<div class="parent">
<a id="logo" href="index.html" title="Página Principal" class="logo center">
<img src="img/mainlogo.png" alt="Corporate-logo" title="Página Principal">
</a>
</div>

Why do my 2 divs inside a footer go in different corners?

while making a footer for a school task I encountered this problem: both texts should be on the same line, but one goes higher and one goes lower
I tried to put a link in the left side of the footer and some text in the right side. For some reason when I split the divs into their positions they end up like this. Link goes in left top corner and text goes in bottom right corner. I'm trying to get them both VERTICALLY centered but nothing I do gets them both there.
I've managed to get both in the center by themselves but never both at the same time. What is going on here?
Here's my html:
<footer>
<div class="link_class">
asd
</div>
<div class="copy_class">
<p>© dsa</p>
</div>
</footer>
and here's the css:
footer{
width: 20%;
height: 5%;
background-color: gray;
display: flex;
justify-content: space-between;
}
In your example, the cause is not flex behaviour.
Native browsers styles apply margin to 'p' tags (margin-block-start: 1em;margin-block-end: 1em;) but not to 'a' tags.
To solve it, you have some simple options:
Apply margin: 0 to your 'p'
Change the p tag with another that doesn´t have default margins.
Here is the reference of the default browsers styles:
https://www.w3schools.com/cssref/css_default_values.asp
if you want them to be on separate lines then
align-items: center;
flex-direction: column;
and leave out justify-content
Just add this, which aligns them next to each other using the magical powers of flexbox.
align-items: center;
footer{
width: 20%;
height: 5%;
background-color: gray;
display: flex;
align-items: center;
}
<footer>
<div class="link_class">
asd
</div>
<div class="copy_class">
<p>© dsa</p>
</div>
</footer>

How to make a div section responsive in a vertical website

I am creating a vertical website that has several different sections.
I want to make each section responsive to the content it has, but it seems like it's not responsive right now. Those two texts on the first row below the navbar is supposed to be in two different lines because it is written like:
<div id="firstRow">
<a id="about" class="smooth"></a>
<div class="intro">
<div>Welcome to my website</div>
<div>Scroll down to know more about us</div>
</div>
</div>
and I tried to use flex to make the first div responsive
div#firstRow {
padding: 100px;
display: flex;
}
How can I make this work?
I think you should put the display: flex property to your .intro div and also add a flex-direction of row to put it on the same line:
.intro {
display: flex;
flex-direction: row;
}
Example on jsFiddle.
do it something like this
.intro > div {
float:left;
clear: both;
display:block;
}

centering a everything in a div (imgs, text,.....)

(sorry for bad english)
I want to center everything in the div "center" (like imgs and text...) with CSS
<div id="123">
<div id="center">
<img>
<p>
...
</div>
</div>
You can do this:
CSS
#center{
display: flex;
justify-content: center;
align-items: center;
}
DEMO HERE
If you mean horizontal centering u can do it in 2 rules simple rules:
Inline Element - text-align-center (paragrafs,headings,images)
Block Element- margin:0px auto; (divs)