How do I center an anchor element in CSS? - html

I just want to have my anchor in the middle of the screen horizontally, how might I do this?
example

Add the text-align CSS property to its parent style attribute
Eg:
<div style="text-align:center">
example​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</div>​
Or using a class (recommended)
<div class="my-class">
example​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</div>​
.my-class {
text-align: center;
}
See below working example:
.my-class {
text-align: center;
background:green;
width:400px;
padding:15px;
}
.my-class a{text-decoration:none; color:#fff;}
<!--EXAMPLE-ONE-->
<div style="text-align:center; border:solid 1px #000; padding:15px;">
example​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</div>​
<!--EXAMPLE-TWO-->
<div class="my-class">
example​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</div>​
Q: Why doesn't the text-align style get applied to the a element instead of the div?
A: The text-align style describes how inline content is aligned within a block element. In this case, the div is a block element and it's inline content is the a. To further explore this consider how little sense it would make to apply the text-align style to the a element when it is accompanied by more text
<div>
Plain text is inline content.
example
<span>Spans are also inline content</span>
</div>
Even though there are line breaks here all the contents of div are inline content and therefore will produce something like:
Plain text is inline content. example Spans are also inline content
It doesn't make much sense as to how "example" in this case would be displayed if the text-align property were to be applied.

write like this:
<div class="parent">
example
</div>
CSS
.parent{
text-align:center
}

Two options, that have different uses:
HTML:
<a class="example" href="http://www.example.com">example</a>
CSS:
.example { text-align: center; }
Or:
.example { display:block; width:100px; margin:0 auto;}

Try:
margin: 0 auto;
display: table

try to wrap a div around and add these styles to the div:
width: 100%;
text-align: center;

<span style="text-align:center; display:block;">
Awaissoft
</span>

By default an anchor is rendered inline, so set text-align: center; on its nearest ancestor that renders as a block.

I think you can't do that with inline elements like anchor, span. But to make it work you have to set the display to block.
example

There are many ways.
<!-- Probably the most common: -->
<div style="text-align: center;">Link</div>
<!-- Getting crafty... -->
Link</div>
There are probably other ways too, but these three are probably the most common.

You can try this code:
/**code starts here**/
a.class_name { display : block;text-align : center; }

style="margin:0 auto; width:auto;" Try that.

css cannot be directly applied for the alignment of the anchor tag.
The css (text-align:center;) should be applied to the parent div/element for the alignment effect to take place on the anchor tag.

It is very Simple Anchor(a) is a inline element, it means width of inline element is equal to how much text width is there that much.so if you want horizontal center so you need to convert inline to block element, then add text align center.
<a style="display:block;text-align:center" href="http://www.example.com">example</a>

Just put it between center tags:
<center>><Your text here>></center>

Related

Inline p tags inside div

I am trying to display 3 inline p tags inside a div container but I have problem when I change the font-size of one p tag. Do I need to adjust the line-height of p tag or is there a universal command?
Also, is there any way except using margin-left or margin-right to put a space between p tags?
jsfiddle
.container{
width:200px;
padding:10px;
display:inline;
}
.one{
float:left;
font-size:25px;
}
.two{
float:left;
}
.three{
float:left;
}
<div class="container">
<p class="one">
sentence one
</p>
<p class="two">
sentence two
</p>
<p class="three">
sentence three
</p>
</div>
Do not use float to set inline elements. Use display:inline;
.one{
font-size:25px;
}
.container p {
display:inline;
}
Working DEMO.
BUT I suggest you to avoid p for inline elements, use span instead that are build for that.
The HTML <span> element is a generic inline container for phrasing content
Workind DEMO.
You can use display:inline-block , if you want to use p tag
Here is JSFiddle
but as paolo.basso99 said , to display inline elements, you should use span tag.
hope this helps.
This scenario can be achieved using CSS Flexbox. To make them equally spaced and vertically centered you need to apply these CSS properties.
Have a look at the code at jsFiddle.
Just add the following properties to parent element in your case its .container
.container {
display: flex;
justify-content: space-between;
align-items: center;
}

HTML Align DIV Children Horizontally

I am very new in HTML and CSS development. I wanted all children in my div parent align horizontally
My HTML :
<div class="parent">
<h1>Hello</h1>
<h1>World</h1>
</div>
I tried using inline-block on .parent like the others suggested but still the output is :
Hello
World
instead
Hello World
any ideas?
The property you're looking for is display: inline; this will make each tag render like it is "inline with each other".
.parent h1 {
display: inline;
}
You could also float the tags, but I would avoid doing that as it would break the flow of text if you were to add any other tags within the .parent container.
Example JSfiddle
Consider looking at float:left.
.parent h1 {
float:left;
}
<div class="parent">
<h1>Hello</h1>
<h1>World</h1>
</div>
Or even display:inline
.parent h1{
display:inline;
}
<div class="parent">
<h1>Hello</h1>
<h1>World</h1>
</div>
Keep in mind that using float is not recommended here because if you add a new element to the .parent div it will appear next to the h1 elements because those are floating left. +1 to #MathiasaurusRex for pointing that out.
You dont need to align the div, but need to align the h1's:
In your CSS code add:
h1 {
display: inline;
}
Fiddle here
Another approach is as follows:
.parent {
display: flex;
flex-direction: row;
}
<div class="parent">
<h1>Hello</h1>
<h1>World</h1>
</div>
One thing to note here is your choice of tags h1 for children is wrong, headers inserts an empty line before and after the header. As your motive is only to show some text header tag is not the right tag of choice.

How to position two elements side by side using CSS

I want to position a paragraph to the right of an <iframe> of a Google map.
I do not know how to show my code, so here is a screenshot of what I want:
Just use the float style. Put your google map iframe in a div class, and the paragraph in another div class, then apply the following CSS styles to those div classes(don't forget to clear the blocks after float effect, to not make the blocks trouble below them):
css
.google_map{
width:55%;
margin-right:2%;
float: left;
}
.google_map iframe{
width:100%;
}
.paragraph {
width:42%;
float: left;
}
.clearfix{
clear:both
}
html
<div class="google_map">
<iframe></iframe>
</div>
<div class="paragraph">
<p></p>
</div>
<div class="clearfix"></div>
You have two options, either float:left or display:inline-block.
Both methods have their caveats. It seems that display:inline-block is more common nowadays, as it avoids some of the issues of floating.
Read this article http://designshack.net/articles/css/whats-the-deal-with-display-inline-block/ or this one http://www.vanseodesign.com/css/inline-blocks/ for a more in detail discussion.
You can simply use a div to make a container and display: flex; to make the content appear side-by-side like this:
.splitscreen {
display: flex;
}
.splitscreen .left,
.splitscreen .right {
flex: 1;
}
<div class="splitscreen">
<div class="left">
Content
</div>
<div class="right">
Content
</div>
</div>
None of these solutions seem to work if you increase the amount of text so it is larger than the width of the parent container, the element to the right still gets moved below the one to the left instead of remaining next to it. To fix this, you can apply this style to the left element:
position: absolute;
width: 50px;
And apply this style to the right element:
margin-left: 50px;
Just make sure that the margin-left for the right element is greater than or equal to the width of the left element. No floating or other attributes are necessary. I would suggest wrapping these elements in a div with the style:
display: inline-block;
Applying this style may not be necessary depending on surrounding elements
Fiddle:
http://jsfiddle.net/2b0bqqse/
You can see the text to the right is taller than the element to the left outlined in black. If you remove the absolute positioning and margin and instead use float as others have suggested, the text to the right will drop down below the element to the left
Fiddle:
http://jsfiddle.net/qrx78u20/
For your iframe give an outer div with style display:inline-block, And for your paragraph div also give display:inline-block
HTML
<div class="side">
<iframe></iframe>
</div>
<div class="side">
<p></p>
</div>
CSS
.side {
display:inline-block;
}
Use either float or inline elements:
Example JSBIN
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div>float example</div>
<div><div style="float:left">Floating left content</div><div>Some content</div></div>
<div>inline block example</div>
<div><div style="display:inline-block">Some content</div><div style="display:inline-block">Next content</div></div>
</body>
</html>
Like this
.block {
display: inline-block;
vertical-align:top;
}
JSFiddle Demo
You can use float:left to align div in one line.
Fiddle
You can float the elements (the map wrapper, and the paragraph),
or use inline-block on both of them.
Wrap the iframe in a class, float that left.
The paragraph with then be forced up and to the right as long as there is room for it.
Then set your paragraph to display:inline-block, and add some left margin to tidy it up.
<div class="left">
<img src="http://lorempixel.com/300/300" /> <!--placeholder for iframe-->
</div>
<p>Lorem Paragraph Text</p>
.left { float: left; }
p { display: inline-block; margin-left: 30px; }
Here's a fiddle: http://jsfiddle.net/4DACH/
Put the iframe inside the <p> and make the iframe CSS
float:left;
display:inline-block;
give your boxes the class foo (or whatever) and add the css
.foo{
float: left;
}

H3 is taking more space than enclosing div

I have a web page like this.
<html>
<head></head>
<body>
<div style="background:blue">
<h3 style="background:green">Hello world.</h3>
</div>
</body>
</html>
When I analyze the output in chrome, it seems that the h3 tag is taking more space than the div tag. I want the div tag to completely include the h3 tag, and the background color of div to be shown in the entire area. Any idea how to do this?
The reason this is happening is that some elements have browser styling by default, that is why you should always use a css reset:
if you float the div it will wrap around the element, and set the margin of the h3 to 0.
<div style="background:blue;float:left;">
<h3 style="background:green;margin:0;">Hello world.</h3>
</div>
fiddle
For the div to take the entire screen's size remove the float.
<div style="background:blue;">
<h3 style="background:green;margin:0;">Hello world.</h3>
</div>
Like this
DEMO
CSS
.div1{
background-color:blue;
float:left;
}
h3{
margin:0;
padding:0;
background:green;
}
DEMO1
Set a font-size and line-height on the h3 like so:
h3 {
font-size: 16px;
line-height: 1em; }
Taken a css reset? This set all to default values.
http://meyerweb.com/eric/tools/css/reset/
Most simple solution I see, add overflow: hidden; to the enclosing div.
<h3 style="background:green;margin:0;">Hello world.</h3>
By default h3 has a margin associated with it. So you have to add a margin:0 to the h3 tag.
DEMO
set margin of h3 to 0.This will solve the problem.
I solved this issue by increasing the line height of h3.

How do I center all elements in the body of my page without applying the center format to text?

I want to center all elements in the body of my page, without applying the center format to paragraphs. For example, I want my paragraphs to be left aligned, but as a whole, centered in the window of the webpage.
I tried doing
<body>
<div align="center">
<p></p>
<img>
</div>
</body>
But while this centers all elements in the body on the page, it also forces center align within the paragraphs.
Add some CSS for the p elements.
p {
text-align: left;
}
You should be using margin: 0 auto to center block level elements. This also has the advantage of not changing inline centering.
If you know what width your main content will be the you can use margin:auto; similar to:
HTML
<body>
<div class="main">
<p>hello world</p>
<img>
</div>
</body>
CSS
body {
width: 200px;
margin:auto;
}
.main {
align: right;
background:#eee;
}
or play with this: http://jsfiddle.net/MEDBF/
i think this is what you need here:
http://jsfiddle.net/W3pae/1/
centered div, paragraph left, image left
Maybe this could help you.
http://andrewpeace.com/css-center-screen-div.html