【问题标题】:How do I align two objects vertically that aren't in the same div如何垂直对齐不在同一个div中的两个对象
【发布时间】:2012-08-05 08:46:25
【问题描述】:

我有一个 wordpress 页面,在论坛内容上方有一个登录小部件 <div id='widget-sidebar' class='widgets_on_page'>。我想将它浮动到右侧并使论坛标题在左侧垂直对齐,如下所示:

我遇到的问题是论坛标题是动态生成的,所以我不能将它们放在同一个 div 中。

有什么想法吗?

这里是源代码:

        <div id="primary">
            <div id="content" role="main">


                    <div id='widget-sidebar' class='widgets_on_page'>
    <ul><li id="wp_sidebarlogin-2" class="widget widget_wp_sidebarlogin"><h2 class="widgettitle">Welcome Admin</h2><div class="avatar_container"><img src="http://localhost/wordpress/wp-content/plugins/user-avatar/user-avatar-pic.php?src=http://localhost/wordpress/wp-content/uploads/avatars/1/1344255249-bpfull.jpg&#038;w=38&#038;id=1&#038;random=1344255249" alt="" class=" avatar  avatar-38  photo user-1-avatar" width="38" height="38" /></div><ul class="pagenav"><li class="page_item"><a href="http://localhost/wordpress/wp-admin/">Dashboard</a></li><li class="page_item"><a href="http://localhost/wordpress/wp-admin/profile.php">Profile</a></li><li class="page_item"><a href=" http://localhost/wordpress/wp-login.php?action=logout&amp;redirect_to=http%3A%2F%2Flocalhost%2Fwordpress%2F%3Fforum%3Dna-forum&amp;_wpnonce=caf8874286">Logout</a></li></ul></li></ul>
  </div><!-- widgets_on_page -->

    <article id="post-2901" class="post-2901 forum type-forum status-publish hentry">
        <header class="entry-header">
                        <h1 class="entry-title"><a href="http://localhost/wordpress/?forum=na-forum" title="Permalink to NA Forum" rel="bookmark">NA Forum</a></h1>


                    </header><!-- .entry-header -->

这是我正在使用的论坛页面模板的一部分:

        <div id="primary">
            <div id="content" role="main">

            <?php widgets_on_template("widget-sidebar"); ?>

            <?php if ( have_posts() ) : ?>

                <?php twentyeleven_content_nav( 'nav-above' ); ?>

                <?php /* Start the Loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>

                    <?php get_template_part( 'content', get_post_format() ); ?>

                <?php endwhile; ?>

                <?php twentyeleven_content_nav( 'nav-below' ); ?>

【问题讨论】:

    标签: php css wordpress


    【解决方案1】:

    对小部件使用绝对定位应该可以。比如:

    div#content {
        position: relative;
    }
    
    div#widget-sidebar {
        position: absolute;
        top: 10px;
        right: 10px;
    }
    

    【讨论】:

      【解决方案2】:

      如果您不喜欢 IE7,您可以使用 display: table-cell; 垂直对齐容器。由于它被呈现为一个表格,它失去了浮动的能力。因此,您必须至少在其中一个元素上显式设置宽度。

      CSS

      div {
          display:        table-cell;
          vertical-align: middle;
      }
      
      div:first-child {
          width:      80%;
      }
      

      HTML

      <div>
          first line<br>
          second line<br>
          last line
      </div>
      
      <div>
          just one line<br>
      </div>
      

      小提琴

      http://jsfiddle.net/y96hb/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-15
        • 2018-05-02
        • 2013-09-23
        • 2016-02-21
        • 1970-01-01
        • 2015-12-14
        • 2013-11-30
        • 1970-01-01
        相关资源
        最近更新 更多