Home > Web Front-end > CSS Tutorial > How can I align two div blocks horizontally using CSS?

How can I align two div blocks horizontally using CSS?

Patricia Arquette
Release: 2024-11-03 16:40:03
Original
955 people have browsed it

How can I align two div blocks horizontally using CSS?

Positioning Two Div Blocks Horizontally

Align two div blocks on the same horizontal line by utilizing CSS and HTML techniques.

Solution:

CSS:

<code class="css">#block_container {
    text-align: center;
}
#bloc1, #bloc2 {
    display: inline;
}</code>
Copy after login

In this CSS, we:

  • Center the parent container (#block_container) using text-align: center to horizontally align both div blocks.
  • Set the display property of both div blocks (#bloc1 and #bloc2) to inline to make them appear on the same line.

HTML:

<code class="html"><div id="block_container">
    <div id="bloc1"><?php echo " version " . $version . " Copyright &copy; All Rights Reserved."; ?></div>  
    <div id="bloc2"><img src="..."></div>
</div></code>
Copy after login

Explanation:

  1. The #block_container div serves as the parent container for both div blocks.
  2. The text-align property of #block_container centers the content within it, including both #bloc1 and #bloc2.
  3. By setting the display property to inline for #bloc1 and #bloc2, they are displayed on the same horizontal line.

Additional Notes:

  • Use appropriate HTML tags for content placement instead of placing raw content in
    s. For text, use

    or , for example.

  • A demonstration of this solution can be found on jsFiddle.

The above is the detailed content of How can I align two div blocks horizontally using CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template